@@ -33,52 +33,16 @@ Perl 6 level one.
33
33
34
34
= end SYNOPSIS
35
35
36
- my @ files = Test-Files. pods;
36
+ plan + my @ files = Test-Files. pods;
37
37
38
- sub walk ($ arg ) {
39
- given $ arg {
40
- when Pod ::FormattingCode { walk $ arg . contents }
41
- when Str { $ arg }
42
- when Array { $ arg . map ({walk $ _ }). join }
43
- }
44
- }
45
-
46
- # Extract all the examples from the given files
47
38
for @ files -> $ file {
48
- my $ counts = 0 ;
49
- my @ chunks = extract-pod($ file . IO ). contents;
50
- while @ chunks {
51
- my $ chunk = @ chunks . pop ;
52
- if $ chunk ~~ Pod ::Block::Code {
53
- if $ chunk . config<lang > && $ chunk . config<lang > ne ' perl6' {
54
- next ; # Only testing Perl 6 snippets.
55
- }
56
- my $ todo = False ;
57
- if $ chunk . config<skip-test > {
58
- % * ENV <P6_DOC_TEST_FUDGE > ?? ($ todo = True ) !! next ;
59
- }
60
- check-chunk( % (
61
- ' contents' , $ chunk . contents. map ({walk $ _ }). join ,
62
- ' file' , $ file ,
63
- ' count' , ++ $ counts ,
64
- ' todo' , $ todo ,
65
- ' ok-test' , $ chunk . config<ok-test > // " " ,
66
- ' preamble' , $ chunk . config<preamble > // " " ,
67
- ' method' , $ chunk . config<method > // " " ,
68
- ' solo' , $ chunk . config<solo > // " " ,
69
- ) );
70
- } else {
71
- if $ chunk .^ can (' contents' ) {
72
- @ chunks . push (| $ chunk . contents)
73
- }
74
- }
39
+ subtest $ file => {
40
+ plan + my @ examples = code-blocks($ file );
41
+ test-example $ _ for @ examples ;
75
42
}
76
43
}
77
44
78
- done-testing ;
79
-
80
- sub check-chunk ( $ eg ) {
81
-
45
+ sub test-example ($ eg ) {
82
46
# #1355 - don't like .WHAT in examples
83
47
if ! $ eg <ok-test >. contains (' WHAT' ) && $ eg <contents >. contains (' .WHAT' ) {
84
48
flunk " $ eg <file> chunk starting with «" ~ starts-with ($ eg <contents >) ~ ' » uses .WHAT: try .^name instead' ;
@@ -89,17 +53,17 @@ sub check-chunk( $eg ) {
89
53
next ;
90
54
}
91
55
92
- # Wrap each snippet in a block so it compiles but isn't run on EVAL
93
- # Further wrap in an anonymous class (so bare method works)
94
- # Add in empty routine bodies if needed
56
+ # Wrap snippets in an anonymous class (so bare method works)
57
+ # and add in empty blocks if needed.
95
58
96
59
my $ code ;
97
60
if $ eg <solo > {
98
- $ code = $ eg <preamble > ~ " ;\n " if $ eg <preamble >;
61
+ $ code = $ eg <preamble > ~ " ;\n " if $ eg <preamble >;
99
62
$ code ~ = $ eg <contents >;
100
- } else {
101
- $ code = ' no worries; ' ;
102
- $ code ~ = " if False \{\n class :: \{\n " ;
63
+ }
64
+ else {
65
+ $ code = ' no worries; ' ;
66
+ $ code ~ = " class :: \{\n " ;
103
67
$ code ~ = $ eg <preamble > ~ " ;\n " ;
104
68
105
69
for $ eg <contents >. lines -> $ line {
@@ -110,18 +74,18 @@ sub check-chunk( $eg ) {
110
74
# to make it compilable. Be careful with multiline signatures:
111
75
# '(' and ',' at the end of a line indicate a continued sig.
112
76
# We wait until none of them is encountered before adding '{}'.
77
+ # Cases that are not covered by the heuristic and which contain
78
+ # nothing but a method declaration can use :method instead.
113
79
$ in-signature ?| = $ line . trim . starts-with (any (<multi method proto only sub >))
114
- && $ eg <method > eq " " ;
80
+ && not $ eg <method >;
115
81
if $ in-signature && ! $ line . trim . ends-with (any (« } , ( »)) {
116
82
$ code ~ = " \{ }" ;
117
83
$ in-signature = False ;
118
84
}
119
- if $ eg <method > eq " " || $ eg <method > eq " False" {
120
- $ code ~ = " \n " ;
121
- }
85
+ NEXT $ code ~ = " \n " ;
86
+ LAST $ code ~ = " \{ }\n " if $ eg <method >;
122
87
}
123
- $ code ~ = " \{ }\n " if $ eg <method > eq " True" ;
124
- $ code ~ = " \n }}" ;
88
+ $ code ~ = " }" ;
125
89
}
126
90
127
91
my $ msg = " $ eg <file> chunk $ eg <count> starts with “" ~ starts-with ($ eg <contents >) ~ " ” compiles" ;
@@ -136,7 +100,8 @@ sub check-chunk( $eg ) {
136
100
$ proc . stdout. tap : {;};
137
101
$ proc . stderr. tap : {;};
138
102
$ has-error = ! await $ proc . start;
139
- } else {
103
+ }
104
+ else {
140
105
temp $ * OUT = open : w, $ * SPEC . devnull;
141
106
temp $ * ERR = open : w, $ * SPEC . devnull;
142
107
use nqp ;
@@ -153,11 +118,52 @@ sub check-chunk( $eg ) {
153
118
diag $ eg <contents >;
154
119
diag $ has-error ;
155
120
flunk $ msg ;
156
- } else {
121
+ }
122
+ else {
157
123
pass $ msg ;
158
124
}
159
125
}
160
126
161
- sub starts-with ( Str $ chunk ) {
127
+ sub code-blocks (IO () $ file ) {
128
+ my $ count ;
129
+ my @ chunks = extract-pod($ file ). contents;
130
+ gather while @ chunks {
131
+ my $ chunk = @ chunks . pop ;
132
+ if $ chunk ~~ Pod ::Block::Code {
133
+ # Only testing Perl 6 snippets.
134
+ next unless $ chunk . config<lang >: v eq ' ' | ' perl6' ;
135
+
136
+ my $ todo = False ;
137
+ if $ chunk . config<skip-test > {
138
+ % * ENV <P6_DOC_TEST_FUDGE > ?? ($ todo = True ) !! next ;
139
+ }
140
+ take % (
141
+ ' contents' , $ chunk . contents. map ({walk $ _ }). join ,
142
+ ' file' , $ file ,
143
+ ' count' , ++ $ count ,
144
+ ' todo' , $ todo ,
145
+ ' ok-test' , $ chunk . config<ok-test > // " " ,
146
+ ' preamble' , $ chunk . config<preamble > // " " ,
147
+ ' method' , $ chunk . config<method >: v,
148
+ ' solo' , $ chunk . config<solo >: v,
149
+ );
150
+ }
151
+ else {
152
+ if $ chunk .^ can (' contents' ) {
153
+ @ chunks . push (| $ chunk . contents)
154
+ }
155
+ }
156
+ }
157
+ }
158
+
159
+ sub walk ($ arg ) {
160
+ given $ arg {
161
+ when Pod ::FormattingCode { walk $ arg . contents }
162
+ when Str { $ arg }
163
+ when Array { $ arg . map ({walk $ _ }). join }
164
+ }
165
+ }
166
+
167
+ sub starts-with (Str $ chunk ) {
162
168
($ chunk . lines )[0 ]. substr (0 ,10 ). trim
163
169
}
0 commit comments