Skip to content

Commit 58ad620

Browse files
authored
Merge pull request #2978 from rintaro/gyb-doctest
[gyb] Get doctest to pass
2 parents c4a0c00 + 4bae3d4 commit 58ad620

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

utils/gyb.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def tokenize_template(template_text):
208208
... %% first line
209209
... %% second line
210210
... '''):
211-
... print (kind, text.strip().split('\n',1)[0])
211+
... print((kind, text.strip().split('\n',1)[0]))
212212
('literal', 'This is $some$ literal stuff containing a')
213213
('substitutionOpen', '${')
214214
('literal', 'followed by a %{...} block:')
@@ -361,7 +361,7 @@ def code_starts_with_dedent_keyword(source_lines):
361361
>>> code_starts_with_dedent_keyword(split_lines('except ifSomethingElse:'))
362362
True
363363
>>> code_starts_with_dedent_keyword(
364-
split_lines('\n# comment\nelse: # yes'))
364+
... split_lines('\n# comment\nelse: # yes'))
365365
True
366366
"""
367367
token_text = None
@@ -417,7 +417,7 @@ def token_generator(self, base_tokens):
417417
... literally
418418
... ''')
419419
>>> while ctx.token_kind:
420-
... print (ctx.token_kind, ctx.code_text or ctx.token_text)
420+
... print((ctx.token_kind, ctx.code_text or ctx.token_text))
421421
... ignored = ctx.next_token()
422422
('literal', '\n')
423423
('gybLinesOpen', 'for x in y:\n')
@@ -435,7 +435,7 @@ def token_generator(self, base_tokens):
435435
... THIS SHOULD NOT APPEAR IN THE OUTPUT
436436
... ''')
437437
>>> while ctx.token_kind:
438-
... print (ctx.token_kind, ctx.code_text or ctx.token_text)
438+
... print((ctx.token_kind, ctx.code_text or ctx.token_text))
439439
... ignored = ctx.next_token()
440440
('literal', 'Nothing\n')
441441
('gybLinesOpen', 'if x:\n')
@@ -450,20 +450,20 @@ def token_generator(self, base_tokens):
450450
... '''% for x in [1, 2, 3]:
451451
... % if x == 1:
452452
... literal1
453-
... % elif x > 1: # add an output line after this line to fix bug
453+
... % elif x > 1: # add output line here to fix bug
454454
... % if x == 2:
455455
... literal2
456456
... % end
457457
... % end
458458
... % end
459459
... ''')
460460
>>> while ctx.token_kind:
461-
... print (ctx.token_kind, ctx.code_text or ctx.token_text)
461+
... print((ctx.token_kind, ctx.code_text or ctx.token_text))
462462
... ignored = ctx.next_token()
463463
('gybLinesOpen', 'for x in [1, 2, 3]:\n')
464464
('gybLinesOpen', ' if x == 1:\n')
465465
('literal', 'literal1\n')
466-
('gybLinesOpen', 'elif x > 1: # add output line here to fix bug\n')
466+
('gybLinesOpen', 'elif x > 1: # add output line here to fix bug\n')
467467
('gybLinesOpen', ' if x == 2:\n')
468468
('literal', 'literal2\n')
469469
('gybLinesClose', '% end')
@@ -730,17 +730,17 @@ def parse_template(filename, text=None):
730730
If text is supplied, it is assumed to be the contents of the file,
731731
as a string.
732732
733-
>>> print parse_template('dummy.file', text=
733+
>>> print(parse_template('dummy.file', text=
734734
... '''% for x in [1, 2, 3]:
735735
... % if x == 1:
736736
... literal1
737-
... % elif x > 1: # add an output line after this line to fix the bug
737+
... % elif x > 1: # add output line after this line to fix bug
738738
... % if x == 2:
739739
... literal2
740740
... % end
741741
... % end
742742
... % end
743-
... ''')
743+
... '''))
744744
Block:
745745
[
746746
Code:
@@ -755,7 +755,7 @@ def parse_template(filename, text=None):
755755
{
756756
if x == 1:
757757
__children__[0].execute(__context__)
758-
elif x > 1: # add output line after this line to fix bug
758+
elif x > 1: # add output line after this line to fix bug
759759
__children__[1].execute(__context__)
760760
}
761761
[
@@ -784,8 +784,9 @@ def parse_template(filename, text=None):
784784
]
785785
]
786786
787-
>>> print parse_template(
788-
>>> 'dummy.file', text='%for x in range(10):\n% print x\n%end\njuicebox')
787+
>>> print(parse_template(
788+
... 'dummy.file',
789+
... text='%for x in range(10):\n% print(x)\n%end\njuicebox'))
789790
Block:
790791
[
791792
Code:
@@ -796,22 +797,22 @@ def parse_template(filename, text=None):
796797
[
797798
Block:
798799
[
799-
Code: {print x} []
800+
Code: {print(x)} []
800801
]
801802
]
802803
Literal:
803804
juicebox
804805
]
805806
806-
>>> print parse_template('/dummy.file', text=
807+
>>> print(parse_template('/dummy.file', text=
807808
... '''Nothing
808809
... % if x:
809810
... % for i in range(3):
810811
... ${i}
811812
... % end
812813
... % else:
813814
... THIS SHOULD NOT APPEAR IN THE OUTPUT
814-
... ''')
815+
... '''))
815816
Block:
816817
[
817818
Literal:
@@ -848,10 +849,10 @@ def parse_template(filename, text=None):
848849
]
849850
]
850851
851-
>>> print parse_template('dummy.file', text='''%
852+
>>> print(parse_template('dummy.file', text='''%
852853
... %for x in y:
853-
... % print y
854-
... ''')
854+
... % print(y)
855+
... '''))
855856
Block:
856857
[
857858
Code:
@@ -862,18 +863,18 @@ def parse_template(filename, text=None):
862863
[
863864
Block:
864865
[
865-
Code: {print y} []
866+
Code: {print(y)} []
866867
]
867868
]
868869
]
869870
870-
>>> print parse_template('dummy.file', text='''%
871+
>>> print(parse_template('dummy.file', text='''%
871872
... %if x:
872-
... % print y
873+
... % print(y)
873874
... AAAA
874875
... %else:
875876
... BBBB
876-
... ''')
877+
... '''))
877878
Block:
878879
[
879880
Code:
@@ -886,7 +887,7 @@ def parse_template(filename, text=None):
886887
[
887888
Block:
888889
[
889-
Code: {print y} []
890+
Code: {print(y)} []
890891
Literal:
891892
AAAA
892893
]
@@ -898,14 +899,14 @@ def parse_template(filename, text=None):
898899
]
899900
]
900901
901-
>>> print parse_template('dummy.file', text='''%
902+
>>> print(parse_template('dummy.file', text='''%
902903
... %if x:
903-
... % print y
904+
... % print(y)
904905
... AAAA
905906
... %# This is a comment
906907
... %else:
907908
... BBBB
908-
... ''')
909+
... '''))
909910
Block:
910911
[
911912
Code:
@@ -919,7 +920,7 @@ def parse_template(filename, text=None):
919920
[
920921
Block:
921922
[
922-
Code: {print y} []
923+
Code: {print(y)} []
923924
Literal:
924925
AAAA
925926
]
@@ -931,14 +932,14 @@ def parse_template(filename, text=None):
931932
]
932933
]
933934
934-
>>> print parse_template('dummy.file', text='''\
935+
>>> print(parse_template('dummy.file', text='''\
935936
... %for x in y:
936937
... AAAA
937938
... %if x:
938939
... BBBB
939940
... %end
940941
... CCCC
941-
... ''')
942+
... '''))
942943
Block:
943944
[
944945
Code:
@@ -986,7 +987,8 @@ def execute_template(ast, line_directive='', **local_bindings):
986987
... % else:
987988
... THIS SHOULD NOT APPEAR IN THE OUTPUT
988989
... ''')
989-
>>> print execute_template(ast, line_directive='//#sourceLocation', x=1),
990+
>>> out = execute_template(ast, line_directive='//#sourceLocation', x=1)
991+
>>> print(out, end="")
990992
//#sourceLocation(file: "/dummy.file", line: 1)
991993
Nothing
992994
//#sourceLocation(file: "/dummy.file", line: 4)
@@ -1004,7 +1006,8 @@ def execute_template(ast, line_directive='', **local_bindings):
10041006
... % end
10051007
... ${a}
10061008
... ''')
1007-
>>> print execute_template(ast, line_directive='//#sourceLocation', x=1),
1009+
>>> out = execute_template(ast, line_directive='//#sourceLocation', x=1)
1010+
>>> print(out, end="")
10081011
//#sourceLocation(file: "/dummy.file", line: 1)
10091012
Nothing
10101013
//#sourceLocation(file: "/dummy.file", line: 6)
@@ -1112,7 +1115,8 @@ def succ(a):
11121115

11131116
if args.test or args.verbose_test:
11141117
import doctest
1115-
if doctest.testmod(verbose=args.verbose_test).failed:
1118+
selfmod = sys.modules[__name__]
1119+
if doctest.testmod(selfmod, verbose=args.verbose_test).failed:
11161120
sys.exit(1)
11171121

11181122
bindings = dict(x.split('=', 1) for x in args.defines)

0 commit comments

Comments
 (0)