Skip to content

Commit 64d2f35

Browse files
authored
Merge pull request #2812 from taboege/examples-compilation-memory
examples-compilation.t memory and code quality
2 parents 14c6081 + e9fdd1f commit 64d2f35

File tree

3 files changed

+78
-72
lines changed

3 files changed

+78
-72
lines changed

doc/Language/about.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ You can add emphasis with bold (B<V< B<> >>) or italicized (B<V< I<> >>),
136136
with or without code formatting (B<V< C<> >>). Due to current parser limitations,
137137
special steps have to be taken to use B<V< X<> >> with other formatting codes; for example:
138138
139-
=begin code
139+
=begin code :lang<pod6>
140140
=item X<B<foo>|foo> a fancy subroutine
141141
=end code
142142

doc/Language/pod.pod6

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Every Pod document has to begin with C<=begin pod> and end with C<=end pod>.
1212
Everything between these two delimiters will be processed and used to generate
1313
documentation.
1414
15-
=begin code
15+
=begin code :lang<pod6>
1616
=begin pod
1717
1818
A very simple Perl 6 Pod document
@@ -35,7 +35,7 @@ C<typename> of the block. Typenames that are entirely lowercase (for
3535
example: C<=begin head1>) or entirely uppercase (for example: C<=begin
3636
SYNOPSIS>) are reserved.
3737
38-
=begin code
38+
=begin code :lang<pod6>
3939
=begin head1
4040
Top Level Heading
4141
=end head1
@@ -92,7 +92,7 @@ The C<=for> marker is followed by the C<typename> of the block
9292
plus, optionally, any configuration data as in the delimited
9393
blocks described above.
9494
95-
=begin code
95+
=begin code :lang<pod6>
9696
=for head1
9797
Top Level Heading
9898
=end code
@@ -104,7 +104,7 @@ C<typename> of the block. All following data are part of the contents of the
104104
block, thus configuration data B<cannot> be specified for an I<abbreviated>
105105
block. The block ends at the next Pod directive or the first blank line.
106106
107-
=begin code
107+
=begin code :lang<pod6>
108108
=head1 Top level heading
109109
=end code
110110
@@ -178,7 +178,7 @@ Pod offers a wide range of standard block types.
178178
Headings can be defined using C<=headN>,
179179
where N is greater than zero (e.g., C<=head1>, C<=head2>, …).
180180
181-
=begin code
181+
=begin code :lang<pod6>
182182
=head1 A top level heading
183183
184184
=head2 A second level heading
@@ -222,7 +222,7 @@ Ordinary paragraphs do not require an explicit marker or delimiters.
222222
223223
Alternatively, there is also an explicit C<=para> marker that can be used to explicitly mark a paragraph.
224224
225-
=begin code
225+
=begin code :lang<pod6>
226226
=para
227227
This is an ordinary paragraph.
228228
Its text will be squeezed and
@@ -233,7 +233,7 @@ In addition, the longer C<=begin para> and C<=end para> form can be used.
233233
234234
For example:
235235
236-
=begin code
236+
=begin code :lang<pod6>
237237
238238
=begin para
239239
This is an ordinary paragraph.
@@ -269,7 +269,7 @@ This ordinary paragraph introduces a code block:
269269
270270
Code blocks can also be explicitly defined by enclosing them in C<=begin code> and C<=end code>
271271
272-
=begin code
272+
=begin code :lang<pod6>
273273
=begin code
274274
my $name = 'John Doe';
275275
say $name;
@@ -313,7 +313,7 @@ The three suspects are:
313313
314314
Lists that define terms or commands use C<=defn>, equivalent to the C<DL> lists in HTML
315315
316-
=begin code
316+
=begin code :lang<pod6>
317317
=defn Happy
318318
When you're not blue.
319319
@@ -337,7 +337,7 @@ Note that C<=item> is just an abbreviation for C<=item1>.
337337
338338
For example:
339339
340-
=begin code
340+
=begin code :lang<pod6>
341341
=item1 Animal
342342
=item2 Vertebrate
343343
=item2 Invertebrate
@@ -415,13 +415,13 @@ Pod comments are comments that Pod renderers ignore.
415415
416416
Comments are useful for meta-documentation (documenting the documentation). Single-line comments use the C<comment> keyword:
417417
418-
=begin code
418+
=begin code :lang<pod6>
419419
=comment Add more here about the algorithm
420420
=end code
421421
422422
For multi-line comments use a delimited C<comment> block:
423423
424-
=begin code
424+
=begin code :lang<pod6>
425425
=begin comment
426426
This comment is
427427
multi-line.
@@ -433,7 +433,7 @@ multi-line.
433433
All uppercase block typenames are reserved for specifying standard documentation,
434434
publishing, source components, or meta-information.
435435
436-
=begin code
436+
=begin code :lang<pod6>
437437
=NAME
438438
=AUTHOR
439439
=VERSION
@@ -536,7 +536,7 @@ code itself.
536536
C<P<>> codes are handy for breaking out standard elements of
537537
your documentation set into reusable components that can then be
538538
incorporated directly into multiple documents. For example:
539-
=begin code
539+
=begin code :lang<pod6>
540540
=COPYRIGHT
541541
P<file:/shared/docs/std_copyright.pod>
542542

xt/examples-compilation.t

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -33,52 +33,16 @@ Perl 6 level one.
3333
3434
=end SYNOPSIS
3535

36-
my @files = Test-Files.pods;
36+
plan +my @files = Test-Files.pods;
3737

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
4738
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;
7542
}
7643
}
7744

78-
done-testing;
79-
80-
sub check-chunk( $eg ) {
81-
45+
sub test-example ($eg) {
8246
# #1355 - don't like .WHAT in examples
8347
if ! $eg<ok-test>.contains('WHAT') && $eg<contents>.contains('.WHAT') {
8448
flunk "$eg<file> chunk starting with «" ~ starts-with($eg<contents>) ~ '» uses .WHAT: try .^name instead';
@@ -89,17 +53,17 @@ sub check-chunk( $eg ) {
8953
next;
9054
}
9155

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.
9558

9659
my $code;
9760
if $eg<solo> {
98-
$code = $eg<preamble> ~ ";\n" if $eg<preamble>;
61+
$code = $eg<preamble> ~ ";\n" if $eg<preamble>;
9962
$code ~= $eg<contents>;
100-
} else {
101-
$code = 'no worries; ';
102-
$code ~= "if False \{\nclass :: \{\n";
63+
}
64+
else {
65+
$code = 'no worries; ';
66+
$code ~= "class :: \{\n";
10367
$code ~= $eg<preamble> ~ ";\n";
10468

10569
for $eg<contents>.lines -> $line {
@@ -110,18 +74,18 @@ sub check-chunk( $eg ) {
11074
# to make it compilable. Be careful with multiline signatures:
11175
# '(' and ',' at the end of a line indicate a continued sig.
11276
# 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.
11379
$in-signature ?|= $line.trim.starts-with(any(<multi method proto only sub>))
114-
&& $eg<method> eq "";
80+
&& not $eg<method>;
11581
if $in-signature && !$line.trim.ends-with(any(« } , ( »)) {
11682
$code ~= " \{}";
11783
$in-signature = False;
11884
}
119-
if $eg<method> eq "" || $eg<method> eq "False" {
120-
$code ~= "\n";
121-
}
85+
NEXT $code ~= "\n";
86+
LAST $code ~= "\{}\n" if $eg<method>;
12287
}
123-
$code ~= "\{}\n" if $eg<method> eq "True";
124-
$code ~= "\n}}";
88+
$code ~= "}";
12589
}
12690

12791
my $msg = "$eg<file> chunk $eg<count> starts with “" ~ starts-with($eg<contents>) ~ "” compiles";
@@ -136,7 +100,8 @@ sub check-chunk( $eg ) {
136100
$proc.stdout.tap: {;};
137101
$proc.stderr.tap: {;};
138102
$has-error = ! await $proc.start;
139-
} else {
103+
}
104+
else {
140105
temp $*OUT = open :w, $*SPEC.devnull;
141106
temp $*ERR = open :w, $*SPEC.devnull;
142107
use nqp;
@@ -153,11 +118,52 @@ sub check-chunk( $eg ) {
153118
diag $eg<contents>;
154119
diag $has-error;
155120
flunk $msg;
156-
} else {
121+
}
122+
else {
157123
pass $msg;
158124
}
159125
}
160126

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) {
162168
($chunk.lines)[0].substr(0,10).trim
163169
}

0 commit comments

Comments
 (0)