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
|
use strict;
use warnings;
use Test2::V0;
use TOML::Tiny;
require Math::BigInt;
require Math::BigFloat;
subtest 'integers' => sub{
subtest 'decimal' => sub{
is from_toml('x=42'), {x => 42}, 'positive';
is from_toml('x=-42'), {x => -42}, 'negative';
is from_toml('x=42_42'), {x => 4242}, 'underscores';
is from_toml('x=9223372036854775807'), hash{
field x => validator(sub{
my %params = @_;
Math::BigInt->new('9223372036854775807')->beq($params{got});
});
end;
}, 'positive bignum';
is from_toml('x=-9223372036854775808'), hash{
field x => validator(sub{
my %params = @_;
Math::BigInt->new('-9223372036854775808')->beq($params{got});
});
end;
}, 'negative bignum';
};
subtest 'hexadecimal' => sub{
is from_toml('x=0xDEADBEEF'), {x => 0xDEADBEEF}, 'all caps';
is from_toml('x=0xdeadbeef'), {x => 0xDEADBEEF}, 'all lower';
is from_toml('x=0xDeAdBeEf'), {x => 0xDEADBEEF}, 'mixed caps';
is from_toml('x=0xDEAD_BEEF'), {x => 0xDEADBEEF}, 'underscores';
is from_toml('x=0xDEADBEEF '), {x => 0xDEADBEEF}, 'trailing space';
is from_toml('x=0x7fffffffffffffff'), hash{
field x => validator(sub{
my %params = @_;
Math::BigInt->new('0x7fffffffffffffff')->beq($params{got});
});
end;
}, 'bignum';
};
subtest 'binary' => sub{
is from_toml('x=0b1010'), {x => 0b1010}, 'binary';
is from_toml('x=0b10_10'), {x => 0b1010}, 'underscores';
is from_toml('x=0o777777777777777777777'), hash{
field x => validator(sub{
my %params = @_;
Math::BigInt->from_oct('0777777777777777777777')->beq($params{got});
});
end;
}, 'bignum';
};
subtest 'octal' => sub{
is from_toml('x=0o755'), {x => 0755}, 'octal';
is from_toml('x=0o7_55'), {x => 0755}, 'underscores';
is from_toml('x=0b111111111111111111111111111111111111111111111111111111111111111'), hash{
field x => validator(sub{
my %params = @_;
Math::BigInt->new('0b111111111111111111111111111111111111111111111111111111111111111')->beq($params{got});
});
end;
}, 'bignum';
};
};
subtest 'floats' => sub{
is from_toml('x=4.2'), {x => 4.2}, '4.2';
is from_toml('x=+4.2'), {x => 4.2}, '+4.2';
is from_toml('x=-4.2'), {x => -4.2}, '-4.2';
is from_toml('x=0.42'), {x => 0.42}, '0.42';
is from_toml('x=+0.42'), {x => 0.42}, '+0.42';
is from_toml('x=-0.42'), {x => -0.42}, '-0.42';
subtest 'exponent w/ lowercase e' => sub{
is from_toml('x=4.2e3'), {x => 4.2e3}, '4.2e3';
is from_toml('x=-4.2e3'), {x => -4.2e3}, '-4.2e3';
is from_toml('x=4.2e-3'), {x => 4.2e-3}, '4.2e-3';
is from_toml('x=-4.2e-3'), {x => -4.2e-3}, '-4.2e-3';
};
subtest 'exponent w/ uppercase e' => sub{
is from_toml('x=4.2E3'), {x => 4.2e3}, '4.2E3';
is from_toml('x=-4.2E3'), {x => -4.2e3}, '-4.2E3';
is from_toml('x=4.2E-3'), {x => 4.2e-3}, '4.2E-3';
is from_toml('x=-4.2E-3'), {x => -4.2e-3}, '-4.2E-3';
};
is from_toml('x=inf'), {x => 'inf'}, 'inf';
is from_toml('x=+inf'), {x => 'inf'}, '+inf';
is from_toml('x=-inf'), {x => -'inf'}, '-inf';
for (qw(nan +nan -nan)) {
is from_toml("x=$_"), hash{
field x => 'NaN';
end;
}, $_;
}
is from_toml('x=nan'), {x => 'NaN'}, 'nan';
is from_toml('x=+nan'), {x => 'NaN'}, '+nan';
is from_toml('x=-nan'), {x => 'NaN'}, '-nan';
is from_toml('x=42_42.42_42'), {x => 4242.4242}, 'underscores';
};
#-------------------------------------------------------------------------------
# Ensure that numbers survive the trip through to_toml(from_toml(...)) and are
# not coerced into strings by perl.
#-------------------------------------------------------------------------------
subtest 'round trip preserves numerical values' => sub{
is to_toml(scalar from_toml('port=1234')), 'port=1234', 'integers';
is to_toml(scalar from_toml('pi=3.14')), 'pi=3.14', 'floats';
is to_toml(scalar from_toml('nan=nan')), 'nan=nan', 'nan';
is to_toml(scalar from_toml('pos=inf')), 'pos=inf', 'inf';
is to_toml(scalar from_toml('neg=-inf')), 'neg=-inf', '-inf';
};
done_testing;
|