2 # Copyright (C) 2006-2008, The Perl Foundation.
7 t/tools/smartlinks.t - test the smartlink generator
11 % prove t/tools/smartlinks.t
15 Tests L<SmartLink> and the F<tools/util/smartlinks.pl> utility
16 by exercising different options, processing example test files
17 and spec documents, and examining the output.
19 We never actually check the I<full> output of the utility.
20 We simply check several smaller components to avoid a test file
21 that is far too unwieldy.
27 use lib qw( . lib ../lib ../../lib );
29 use File::Temp qw( tempdir tempfile );
34 eval { require Moose };
36 ? (skip_all => 'Moose not installed')
41 use_ok 'SmartLink' or die;
44 sub vdiag(@) { &diag if $ENV{TEST_VERBOSE} }
47 vdiag 'SmartLink: multiple keyphrases';
48 my $link = q{L<S05/bar/baz quux>};
50 eval { my $l = SmartLink->new; };
51 like( $@, '/^Attribute \(.*? is required/', '->new requires one or more attributes' );
53 my $l = SmartLink->new( link => $link );
55 isa_ok( $l, 'SmartLink' );
56 is( $l->link, $link, '->link returns full link text' );
57 is( $l->section, 'bar', '->section returns document section' );
58 ok( $l->has_keyphrases, '->has_keyphrases returns true' );
60 vdiag 'SmartLink->Keyphrase';
61 my $k = $l->keyphrases;
62 ok( $l->has_keyphrases, '->has_keyphrases returns true' );
63 isa_ok( $k, 'Keyphrase' );
64 is( $k->string, 'baz quux', '->string returns keyphrase string' );
65 is_deeply( $k->list, [qw/baz quux/], '->list returns arrayref of keyphrases' );
66 is( $k->regex, '\bbaz\b.+?\bquux\b', '->regex returns regex' );
68 vdiag 'SmartLink->Doc';
71 is( $d->id, 'S05', '->id returns document identifier' );
72 is( $d->prefix, 'S', '->prefix returns document prefix' );
73 is( $d->num, '05', '->num returns document number' );
75 vdiag 'SmartLink: invalid format';
76 $link = q{L<S05/bar/>};
77 eval { my $l = SmartLink->new( link => $link ); };
80 '/^Attribute \(link\) does not pass the type constraint(?: because: Validation failed for)? [(\']PodLink[\')]/',
81 '->new fails with malformed smartlink'
84 vdiag 'SmartLink: complex keyphrases';
85 $link = q{L<S05/bar/a b 'c d e' f g "h'i j" k>};
86 $l = SmartLink->new( link => $link );
88 isa_ok( $l, 'SmartLink' );
89 is( $l->link, $link, '->link returns full link text' );
90 is( $l->section, 'bar', '->section returns document section' );
91 ok( $l->has_keyphrases, '->has_keyphrases returns true' );
93 vdiag 'SmartLink->Keyphrase';
95 ok( $l->has_keyphrases, '->has_keyphrases returns true' );
96 isa_ok( $k, 'Keyphrase' );
97 is( $k->string, q{a b 'c d e' f g "h'i j" k}, '->string returns keyphrase string' );
100 [ 'a', 'b', 'c d e', 'f', 'g', "h'i j", 'k' ],
101 '->list returns arrayref of keyphrases'
105 q{\ba\b.+?\bb\b.+?\bc\ d\ e\b.+?\bf\b.+?\bg\b.+?\bh\'i\ j\b.+?\bk\b},
106 '->regex returns regex'
109 vdiag 'SmartLink->Doc';
112 is( $d->id, 'S05', '->doc returns document identifier' );
113 is( $d->prefix, 'S', '->docprefix returns document prefix' );
114 is( $d->num, '05', '->docnum returns document number' );
116 vdiag 'SmartLink: no keyphrases';
117 $link = q{L<S05/bar>};
118 $l = SmartLink->new( link => $link );
120 isa_ok( $l, 'SmartLink' );
121 is( $l->link, $link, '->link returns full link text' );
122 is( $l->section, 'bar', '->section returns document section' );
123 ok( !$l->has_keyphrases, '->has_keyphrases returns false' );
124 is( $l->keyphrases, undef, '->keyphrases returns undef' );
129 my ( $fh, $fn ) = tempfile( UNLINK => 1 );
130 print $fh 'i am a file' and close $fh;
131 my $f = File->new( filename => $fn );
132 isa_ok( $f, 'File' );
134 $fh = $f->open( '<' );
135 is( $fh, $f->filehandle, 'open returns a filehandle' );
136 is( $f->mode, '<', 'open mode' );
137 is( scalar <$fh>, 'i am a file', 'read' );
140 local $TODO = 'filehandle after close';
141 is( $f->filehandle, undef, 'filehandle after close' );
144 my $no_such = File->new( filename => 'no_such.txt' );
145 ok( $no_such, 'ok to instantiate a non-existent file' );
146 eval { $no_such->open( '<' ); };
147 like( $@, '/can\'t open/i', 'fail to open non-existent file' );
153 my $fn = 'docs/pdds/pdd03_calling_conventions.pod';
155 eval { my $p = PodFile->new; };
156 like( $@, '/^Attribute \(.*?\) is required/', '->new requires one or more attributes' );
158 my $p = PodFile->new( filename => $fn );
160 isa_ok( $p, 'PodFile' );
161 is( $p->filename, $fn, '->filename returns given filename' );
162 is( $p->name, 'pdd03_calling_conventions', '->name returns file basename' );
163 is( $p->path, 'docs/pdds/', '->path returns file path' );
164 is( $p->extension, '.pod', '->extension returns C<.pod>' );
166 my $tree = $p->parse;
167 ok( $tree, 'parsed' );
175 my $fn = 'docs/pdds/pdd03_calling_conventions.pod';
178 eval { my $s = SpecFile->new; };
179 like( $@, '/^Attribute \(.*?\) is required/', '->new requires one or more attributes' );
181 my $s = SpecFile->new( filename => $fn, prefix => $pre );
183 isa_ok( $s, 'SpecFile' );
184 is( $s->name, 'pdd03_calling_conventions', '->name returns file basename' );
185 is( $s->path, 'docs/pdds/', '->path returns file path' );
186 is( $s->extension, '.pod', '->extension returns C<.pod>' );
187 is( $s->num, '03', '->num returns spec number' );
189 $s = SpecFile->new( filename => '3.pod', prefix => '' );
190 is( $s->name, '3', '->name returns "3"' );
191 is( $s->prefix, '', '->prefix returns empty string' );
192 is( $s->num, '3', '->num returns spec number' );
194 $s = SpecFile->new( filename => 'S.pod', prefix => 'S' );
195 is( $s->name, 'S', '->name returns "S"' );
196 is( $s->prefix, 'S', '->prefix returns "S"' );
197 is( $s->num, '', '->num returns empty string' );
203 my $root = 'docs/pdds/';
206 eval { my $s = SpecFiles->new; };
207 like( $@, '/^Attribute \(.*?\) is required/', '->new requires one or more attributes' );
209 my $s = SpecFiles->new( prefix => $pre, root => $root );
211 isa_ok( $s, 'SpecFiles' );
212 is( $s->root, $root, '->root returns spec file directory' );
213 is( $s->extension, '.pod', '->extension returns file extension' );
214 is( $s->prefix, $pre, '->prefix returns spec file prefix' );
215 is( ref $s->files, 'ARRAY', '->files is an array reference' );
217 ok( ( grep { $_->name eq 'pdd07_codingstd' } @{ $s->files } ),
218 '->files contains a known spec file' );
220 # RT #46789: many more ->files tests
226 my $fn = 't/util/smartlinks.t';
228 eval { my $t = TestFile->new; };
229 like( $@, '/^Attribute \(.*?\) is required/', '->new requires one or more attributes' );
231 my $t = TestFile->new( filename => $fn );
233 isa_ok( $t, 'TestFile' );
234 is( $t->filename, $fn, '->filename returns given filename' );
235 is( $t->name, 'smartlinks', '->name returns file basename' );
236 is( $t->path, 't/util/', '->path returns file path' );
237 is( $t->extension, '.t', '->extension returns C<.pod>' );
239 # RT #46791: ->tests, ->smartlinks
243 # RT #46795: TestInfo
244 # RT #46797: SmartLinkServer
246 # RT #46799: end-to-end testing
250 # cperl-indent-level: 4
253 # vim: expandtab shiftwidth=4: