Commit | Line | Data |
91152fc1 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't'; |
5 | @INC = '../lib'; |
6 | require './test.pl'; |
7 | } |
8 | |
9 | # XXX remove this later -- dagolden, 2010-01-13 |
10 | # local *STDERR = *STDOUT; |
11 | |
12 | my @syntax_cases = ( |
13 | 'package Foo', |
14 | 'package Bar 1.23', |
15 | 'package Baz v1.2.3', |
16 | ); |
17 | |
18 | my @version_cases = <DATA>; |
19 | |
20 | plan tests => 5 * @syntax_cases + 5 * grep { $_ !~ /^#/ } @version_cases; |
21 | |
22 | use warnings qw/syntax/; |
23 | use version; |
24 | |
25 | for my $string ( @syntax_cases ) { |
26 | eval "$string"; |
27 | is( $@, '', qq/eval "$string"/ ); |
28 | eval "$string;"; |
29 | is( $@, '', qq/eval "$string;"/ ); |
30 | eval "$string ;"; |
31 | is( $@, '', qq/eval "$string ;"/ ); |
32 | eval "{$string}"; |
33 | is( $@, '', qq/eval "{$string}"/ ); |
34 | eval "{ $string }"; |
35 | is( $@, '', qq/eval "{ $string }"/ ); |
36 | } |
37 | |
38 | LINE: |
39 | for my $line (@version_cases) { |
40 | chomp $line; |
41 | # comments in data section are just diagnostics |
42 | if ($line =~ /^#/) { |
43 | diag $line; |
44 | next LINE; |
45 | } |
46 | |
47 | my ($v, $package, $quoted, $bare, $match) = split /\t+/, $line; |
48 | my $warning = ""; |
49 | local $SIG{__WARN__} = sub { $warning .= $_[0] . "\n" }; |
50 | $match = defined $match ? $match : ""; |
51 | $match =~ s/\s*\z//; # kill trailing spaces |
52 | |
53 | # First handle the 'package NAME VERSION' case |
54 | $withversion::VERSION = undef; |
55 | if ($package eq 'fail') { |
56 | eval "package withversion $v"; |
57 | like($@, qr/$match/, "package withversion $v -> syntax error ($match)"); |
58 | ok(! version::is_strict($v), qq{... and "$v" should also fail STRICT regex}); |
59 | } |
60 | else { |
61 | my $ok = eval "package withversion $v; $v eq \$withversion::VERSION"; |
62 | ok($ok, "package withversion $v") |
63 | or diag( $@ ? $@ : "and \$VERSION = $withversion::VERSION"); |
64 | ok( version::is_strict($v), qq{... and "$v" should pass STRICT regex}); |
65 | } |
66 | |
67 | |
68 | # Now check the version->new("V") case |
69 | my $ver = undef; |
70 | eval qq/\$ver = version->new("$v")/; |
71 | if ($quoted eq 'fail') { |
72 | like($@, qr/$match/, qq{version->new("$v") -> invalid format ($match)}) |
73 | or diag( $@ ? $@ : "and \$ver = $ver" ); |
74 | ok( ! version::is_lax($v), qq{... and "$v" should fail LAX regex}); |
75 | } |
76 | else { |
77 | is($@, "", qq{version->new("$v")}); |
78 | ok( version::is_lax($v), qq{... and "$v" should pass LAX regex}); |
79 | } |
80 | |
81 | # Now check the version->new(V) case, unless we're skipping it |
82 | if ( $bare eq 'na' ) { |
83 | pass( "... skipping version->new($v)" ); |
84 | next LINE; |
85 | } |
86 | $ver = undef; |
87 | eval qq/\$ver = version->new($v)/; |
88 | if ($bare eq 'fail') { |
89 | like($@, qr/$match/m, qq{... and unquoted version->new($v) has same error}) |
90 | or diag( $@ ? $@ : "and \$ver = $ver" ); |
91 | } |
92 | else { |
93 | is($@, "", qq{... and version->new($v) is ok}); |
94 | } |
95 | } |
96 | |
97 | |
98 | # The data is organized in tab delimited format with these columns: |
99 | # |
100 | # value package version->new version->new regex |
101 | # quoted unquoted |
102 | # |
103 | # For each value, it is tested using eval in the following expressions |
104 | # |
105 | # package foo $value; # column 2 |
106 | # and |
107 | # my $ver = version->new("$value"); # column 3 |
108 | # and |
109 | # my $ver = version->new($value); # column 4 |
110 | # |
111 | # The second through fourth columns can contain 'pass' or 'fail'. |
112 | # |
113 | # For any column with 'pass', the tests makes sure that no warning/error |
114 | # was thrown. For any column with 'fail', the tests make sure that the |
115 | # error thrown matches the regex in the last column. The unquoted column |
116 | # may also have 'na' indicating that it's pointless to test as behavior |
117 | # is subject to the perl parser before a stringifiable value is available |
118 | # to version->new |
119 | # |
120 | # If all columns are marked 'pass', the regex column is left empty. |
121 | # |
122 | # there are multiple ways that underscores can fail depending on strict |
123 | # vs lax format so these test do not distinguish between them |
124 | # |
125 | # If the DATA line begins with a # mark, it is used as a diag comment |
126 | __DATA__ |
127 | 1.00 pass pass pass |
128 | 1.00001 pass pass pass |
129 | 0.123 pass pass pass |
130 | 12.345 pass pass pass |
131 | 42 pass pass pass |
132 | 0 pass pass pass |
133 | 0.0 pass pass pass |
134 | v1.2.3 pass pass pass |
135 | v1.2.3.4 pass pass pass |
136 | v0.1.2 pass pass pass |
137 | v0.0.0 pass pass pass |
138 | 01 fail pass pass no leading zeros |
139 | 01.0203 fail pass pass no leading zeros |
140 | v01 fail pass pass no leading zeros |
141 | v01.02.03 fail pass pass no leading zeros |
142 | .1 fail pass pass 0 before decimal required |
143 | .1.2 fail pass pass 0 before decimal required |
144 | 1. fail pass pass fractional part required |
145 | 1.a fail fail na fractional part required |
146 | 1._ fail fail na fractional part required |
147 | 1.02_03 fail pass pass underscore |
148 | v1.2_3 fail pass pass underscore |
149 | v1.02_03 fail pass pass underscore |
150 | v1.2_3_4 fail fail fail underscore |
151 | v1.2_3.4 fail fail fail underscore |
152 | 1.2_3.4 fail fail fail underscore |
153 | 0_ fail fail na underscore |
154 | 1_ fail fail na underscore |
155 | 1_. fail fail na underscore |
156 | 1.1_ fail fail na underscore |
157 | 1.02_03_04 fail fail na underscore |
158 | 1.2.3 fail pass pass dotted-decimal versions must begin with 'v' |
159 | v1.2 fail pass pass dotted-decimal versions require at least three parts |
160 | v0 fail pass pass dotted-decimal versions require at least three parts |
161 | v1 fail pass pass dotted-decimal versions require at least three parts |
162 | v.1.2.3 fail fail na dotted-decimal versions require at least three parts |
163 | v fail fail na dotted-decimal versions require at least three parts |
164 | v1.2345.6 fail pass pass maximum 3 digits between decimals |
165 | undef fail pass pass non-numeric data |
166 | 1a fail fail na non-numeric data |
167 | 1.2a3 fail fail na non-numeric data |
168 | bar fail fail na non-numeric data |
169 | _ fail fail na non-numeric data |