1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
######################### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)
BEGIN { $| = 1; print "1..151\n"; }
END { print "not ok 1\n" unless $loaded; }
use Scalar::Properties;
$loaded = 1;
print "ok 1\n";
######################### End of black magic.
# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):
our $testcount = 1; # compensate for 'ok' above
sub true {
my $ok = shift;
our $testcount;
$testcount++;
print 'not ' unless $ok;
print "ok $testcount " . ($_[0] ? $_[0] : '') . "\n";
}
sub false { true(!$_[0]) }
my $pkg = 'Scalar::Properties';
{ # test added by DCANTRELL to tickle the binary-op operand re-ordering bug
true(time - 300 > 0, "binary-op operand re-ordering bug");
}
{ # test added by DCANTRELL to test for rt.cpan.org bug 4312
my $test = 0;
true(
$test
. "\$test"
. "\\$test"
. "\\\$test"
. "\\\\$test" eq '0$test\0\$test\\\\0',
"variable interpolation bug"
);
}
# die;
false(0);
true(1);
false(0->is_true);
true(0->is_false);
false(1->is_false);
true(1->is_true);
{
my $foo = 0->true;
true($foo->value == 0->{_value});
true($foo);
$foo += 7;
true(ref $foo eq $pkg);
true($foo);
true($foo == 7);
true($foo->{_value} == 7);
}
{
my $bar = 42->times(3);
true($bar == 126);
true($bar);
$bar->false;
false($bar);
true($bar == 126);
true($bar->times(4) == 504);
# set a property; note that the '1' itself becomes overloaded.
$bar->approximate(1);
true($bar->approximate);
true($bar->is_approximate);
true($bar->has_approximate);
$bar->approximate(0);
false($bar->approximate);
false($bar->is_approximate);
false($bar->has_approximate);
}
{
my $val = 0->true;
true($val && $val == 0);
}
{
my $quux = 37->prime(1);
true($quux);
true($quux == 37);
true($quux->is_prime);
}
{
my $baza = 42->value;
my $bazb = 42;
true(ref $baza eq '');
true($baza == $bazb);
}
{
my $h = 'hello world';
true($h);
true($h eq 'hello world');
$h->greeting(1);
true($h->is_greeting);
my @blah;
push @blah => $h;
push @blah => 'forget it';
push @blah => 'hi there'->greeting(1);
my @greets = grep { $_->is_greeting } @blah;
true(@greets == 2);
true($greets[0] eq 'hello world');
true($greets[1] eq 'hi there');
}
{
false('');
true(''->true);
false(''->false);
true('x');
true('x'->true);
false('x'->false);
}
{
my $len = 'hello world'->length;
true(ref $len eq $pkg);
true($len == 11);
my $rev = 'hello world'->reverse;
true(ref $rev eq $pkg);
true($rev eq 'dlrow olleh');
true(1234->length == 4);
true(1234->size == 1234->length);
true(1234->reverse == 4321);
}
{
my $t = 'hello cruel world';
my @s;
@s = $t->split;
true(@s == 3);
true($s[0] eq 'hello');
true($s[1] eq 'cruel');
true($s[2] eq 'world');
@s = $t->split(qr/ll/);
true(@s == 2);
true($s[0] eq 'he');
true($s[1] eq 'o cruel world');
@s = $t->split(qr/\s+/, 2);
true(@s == 2);
true($s[0] eq 'hello');
true($s[1] eq 'cruel world');
# There was a bug with split(), so we try it again to be
# sure it works
@s = $t->split(qr/ll/);
true(@s == 2);
true($s[0] eq 'he');
true($s[1] eq 'o cruel world');
}
{
true('hello world'->uc eq 'HELLO WORLD');
true('hello world'->ucfirst eq 'Hello world');
true('HELLO WORLD'->lc eq 'hello world');
true('HELLO WORLD'->lcfirst eq 'hELLO WORLD');
my $s = 'hello world';
true(ref $s->uc eq $pkg);
true(ref $s->ucfirst eq $pkg);
true(ref $s->lc eq $pkg);
true(ref $s->lcfirst eq $pkg);
}
{
true('0xAf'->hex == 175);
true('aF'->hex == 175);
true(123->hex == 291);
true(777->oct == 511);
true(123->oct == 83);
my $h = '0xffffff';
true(ref $h->hex eq $pkg);
true(ref $h->oct eq $pkg);
}
{
true('hello'->concat(' world') eq 'hello world');
true(ref 'hello'->concat(' world') eq $pkg);
true(ref 'hello'->append(' world') eq $pkg);
}
{
my $s = 'Hello World';
true($s->swapcase, 'hELLO wORLD');
}
{
my $s1 = 'aaa';
my $s2 = 'bbb';
true($s1 eq $s1);
true($s1->eq($s1));
true($s1 ne $s2);
true($s1->ne($s2));
true($s1 lt $s2);
true($s1->lt($s2));
true($s2 gt $s1);
true($s2->gt($s1));
true($s1 le $s1);
true($s1 le $s2);
true($s1->le($s1));
true($s1->le($s2));
true($s2 ge $s1);
true($s2 ge $s2);
true($s2->ge($s1));
true($s2->ge($s2));
true(ref $s1->eq($s1) eq $pkg);
true(ref $s1->ne($s1) eq $pkg);
true(ref $s1->lt($s1) eq $pkg);
true(ref $s1->gt($s1) eq $pkg);
true(ref $s1->le($s1) eq $pkg);
true(ref $s1->ge($s1) eq $pkg);
}
{
my $s1 = 'aaa';
my $s2 = 'BBB';
true($s1->eqi($s1));
true($s1->nei($s2));
true($s1->lti($s2));
true($s2->gti($s1));
true($s1->lei($s1));
true($s1->lei($s2));
true($s2->gei($s1));
true($s2->gei($s2));
true(ref $s1->eqi($s1) eq $pkg);
true(ref $s1->nei($s1) eq $pkg);
true(ref $s1->lti($s1) eq $pkg);
true(ref $s1->gti($s1) eq $pkg);
true(ref $s1->lei($s1) eq $pkg);
true(ref $s1->gei($s1) eq $pkg);
}
{
my $out;
3->times_do(sub { $out .= 'Hello' });
true($out eq 'HelloHelloHello');
$out = '';
5->times_do(sub { $out .= shift });
true($out == 12345);
my $sub = sub { $out .= "$_[0].. " };
$out = '';
1->do_upto(5 => $sub);
true($out eq '1.. 2.. 3.. 4.. 5.. ');
$out = '';
1->do_upto_step(5, 2, $sub);
true($out eq '1.. 3.. 5.. ');
$out = '';
5->do_upto(1 => $sub);
true($out eq '');
$out = '';
5->do_downto(3 => $sub);
true($out eq '5.. 4.. 3.. ');
$out = '';
5->do_downto_step(2, 2, $sub);
true($out eq '5.. 3.. ');
$out = '';
3->do_downto(5 => $sub);
true($out eq '');
}
{
true((-1942)->abs() == 1942);
true(0->abs == 0);
true(773->abs == 773);
true(0->zero);
false(1->zero);
my $foo = 27;
false($foo->zero);
$foo -= 27;
true($foo->zero);
}
{
pass_on('approximate');
true(get_pass_on == 1);
true(passed_on('approximate'));
}
{
my $foo = 1;
$foo->history(1);
my $pi = 3->approximate(1);
my $bar = $foo + $pi;
true($bar->is_approximate);
false($bar->has_history);
}
{
my $h1 = 'hi world'->approximate(1);
my $h2 = $h1->uc;
my @h3 = $h1->split;
true($_->approximate) for $h1, $h2, @h3;
}
{
my $ship = 1701->class('galaxy');
$ship->quadrant('alpha');
$ship->crew(1017);
my @props = $ship->get_props;
true(@props == 3);
true(grep /^$_$/ => @props) for qw/class quadrant crew/;
$ship->crew(0);
true($ship->get_props == 3);
$ship->del_prop('crew');
@props = $ship->get_props;
true(@props == 2);
true(grep /^$_$/ => @props) for qw/class quadrant/;
$ship->del_all_props;
@props = $ship->get_props;
true(@props == 0);
}
|