| 1 |
use Test2::Bundle::Extended -target => 'Test2::Compare::Number'; |
| 2 |
|
| 3 |
my $num = $CLASS->new(input => '22.0'); |
| 4 |
my $untrue = $CLASS->new(input => 0); |
| 5 |
|
| 6 |
isa_ok($num, $CLASS, 'Test2::Compare::Base'); |
| 7 |
isa_ok($untrue, $CLASS, 'Test2::Compare::Base'); |
| 8 |
|
| 9 |
subtest name => sub { |
| 10 |
is($num->name, '22.0', "got expected name for number"); |
| 11 |
is($untrue->name, '0', "got expected name for 0"); |
| 12 |
}; |
| 13 |
|
| 14 |
subtest operator => sub { |
| 15 |
is($num->operator(), '', "no operator for number + nothing"); |
| 16 |
is($num->operator(undef), '', "no operator for number + undef"); |
| 17 |
is($num->operator(1), '==', "== operator for number + number"); |
| 18 |
|
| 19 |
is($untrue->operator(), '', "no operator for 0 + nothing"); |
| 20 |
is($untrue->operator(undef), '', "no operator for 0 + undef"); |
| 21 |
is($untrue->operator(1), '==', "== operator for 0 + number"); |
| 22 |
}; |
| 23 |
|
| 24 |
subtest verify => sub { |
| 25 |
ok(!$num->verify(exists => 0, got => undef), 'does not verify against DNE'); |
| 26 |
ok(!$num->verify(exists => 1, got => {}), 'ref will not verify'); |
| 27 |
ok(!$num->verify(exists => 1, got => undef), 'looking for a number, not undef'); |
| 28 |
ok(!$num->verify(exists => 1, got => 'x'), 'not looking for a string'); |
| 29 |
ok(!$num->verify(exists => 1, got => 1), 'wrong number'); |
| 30 |
ok($num->verify(exists => 1, got => 22), '22.0 == 22'); |
| 31 |
ok($num->verify(exists => 1, got => '22.0'), 'exact match with decimal'); |
| 32 |
|
| 33 |
ok(!$untrue->verify(exists => 0, got => undef), 'does not verify against DNE'); |
| 34 |
ok(!$untrue->verify(exists => 1, got => {}), 'ref will not verify'); |
| 35 |
ok(!$untrue->verify(exists => 1, got => undef), 'undef is not 0 for this test'); |
| 36 |
ok(!$untrue->verify(exists => 1, got => 'x'), 'x is not 0'); |
| 37 |
ok(!$untrue->verify(exists => 1, got => 1), '1 is not 0'); |
| 38 |
ok(!$untrue->verify(exists => 1, got => ''), '"" is not 0'); |
| 39 |
ok(!$untrue->verify(exists => 1, got => ' '), '" " is not 0'); |
| 40 |
ok($untrue->verify(exists => 1, got => 0), 'got 0'); |
| 41 |
ok($untrue->verify(exists => 1, got => '0.0'), '0.0 == 0'); |
| 42 |
ok($untrue->verify(exists => 1, got => '-0.0'), '-0.0 == 0'); |
| 43 |
}; |
| 44 |
|
| 45 |
subtest rounding => sub { |
| 46 |
my $round = $CLASS->new(input => '60.48'); |
| 47 |
ok($round->verify(exists => 1, got => 60.48), '60.48 == 60.48'); |
| 48 |
|
| 49 |
{ |
| 50 |
my $todo = todo "floating point comparison representation error"; |
| 51 |
ok($round->verify(exists => 1, got => 125 - 64.52), '60.48 == 125 - 64.52'); |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
like( |
| 56 |
dies { $CLASS->new() }, |
| 57 |
qr/input must be defined for 'Number' check/, |
| 58 |
"Cannot use undef as a number" |
| 59 |
); |
| 60 |
|
| 61 |
like( |
| 62 |
dies { $CLASS->new(input => '') }, |
| 63 |
qr/input must be a number for 'Number' check/, |
| 64 |
"Cannot use empty string as a number" |
| 65 |
); |
| 66 |
|
| 67 |
like( |
| 68 |
dies { $CLASS->new(input => ' ') }, |
| 69 |
qr/input must be a number for 'Number' check/, |
| 70 |
"Cannot use whitespace string as a number" |
| 71 |
); |
| 72 |
|
| 73 |
done_testing; |