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
|
#========================================================================
#
# t/codec/tt.t
#
# Test the Badger::Codec::TT module.
#
# Written by Andy Wardley <abw@wardley.org>
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#========================================================================
use lib qw( ./lib ../lib ../../lib );
use Badger::Test
debug => 'Badger::Codec::TT',
tests => 32,
args => \@ARGV;
use Badger::Codec::TT;
use constant Codec => 'Badger::Codec::TT';
#-----------------------------------------------------------------------
# basic encode/decode throughput tests
#-----------------------------------------------------------------------
my $data = {
pi => 3.14,
e => 2.718,
karma => -99,
hash => {
things => [ qw( foo bar baz ) ],
}
};
my $encoded = Codec->encode($data);
ok( $encoded, 'encoded data' );
my $decoded = Codec->decode($encoded);
ok( $decoded, 'decoded data' );
is( $decoded->{ pi }, $data->{ pi }, 'pi remains constant' );
is( $decoded->{ e }, $data->{ e }, 'e remains constant' );
is( $decoded->{ karma }, $data->{ karma }, 'karma is unchaged' );
is( $decoded->{ hash }->{ things }->[0], 'foo', 'foo is unchanged' );
#-----------------------------------------------------------------------
# define some more data and a comparison sub for different syntax tests
#-----------------------------------------------------------------------
$data = {
message => 'Hello World, this is some text',
things => ['a list', 'of some things'],
stuff => {
pi => 3.14,
foo => [ { nested => 'hash' }, ['nested', 'list' ] ],
},
};
sub compare_decoded_data {
my ($name, $decoded, $data) = @_;
is( $decoded->{message}, $data->{ message }, "$name message" );
is( $decoded->{things}->[0], $data->{things}->[0], "$name things 0" );
is( $decoded->{things}->[1], $data->{things}->[1], "$name things 1" );
is( $decoded->{stuff}->{pi}, $data->{stuff}->{ pi }, "$name pi" );
is( $decoded->{stuff}->{foo}->[0]->{nested}, $data->{stuff}->{foo}->[0]->{nested}, "$name foo.nested $data->{stuff}->{foo}->[0]->{nested}" );
is( $decoded->{stuff}->{foo}->[1]->[1], $data->{stuff}->{foo}->[1]->[1], "$name foo.nested $data->{stuff}->{foo}->[1]->[1]" );
}
#-----------------------------------------------------------------------
# Try it first with Perlish data syntax...
#-----------------------------------------------------------------------
$decoded = Codec->decode(<<EOF);
{
message => 'Hello World, this is some text',
things => ['a list', 'of some things'],
stuff => {
pi => 3.14,
foo => [ { nested => 'hash' }, ['nested', 'list' ] ],
},
}
EOF
ok( $decoded, 'decoded Perlish data' );
compare_decoded_data( Perlish => $decoded, $data );
#-----------------------------------------------------------------------
# ...then with reduced TT syntax...
#-----------------------------------------------------------------------
$decoded = Codec->decode(<<EOF);
{
message = 'Hello World, this is some text'
things = ['a list' 'of some things']
stuff = {
pi = 3.14
foo = [ { nested = 'hash' } ['nested' 'list' ] ]
}
}
EOF
ok( $decoded, 'decoded TTish data' );
compare_decoded_data( TTish => $decoded, $data );
#-----------------------------------------------------------------------
# ...and again with JSON syntax
#-----------------------------------------------------------------------
$decoded = Codec->decode(<<EOF);
{
message: 'Hello World, this is some text',
things: ['a list' 'of some things'],
stuff: {
pi: 3.14,
foo: [ { nested: 'hash' }, ['nested', 'list' ] ]
}
}
EOF
ok( $decoded, 'decoded JSONish data' );
compare_decoded_data( JSONish => $decoded, $data );
#-----------------------------------------------------------------------
# test different output formats
#-----------------------------------------------------------------------
$data = {
pi => 3.14,
e => 2.718,
foo => ['bar', 'baz'],
};
my $codec = Codec->new( assign => '=>', comma => ',' );
is( $codec->encode($data), "{e=>2.718,foo=>['bar','baz'],pi=>3.14}", 'encoded Perlishly' );
$codec = Codec->new( assign => ':' );
is( $codec->encode($data), "{e:2.718 foo:['bar' 'baz'] pi:3.14}", 'encoded JSONishly' );
#-----------------------------------------------------------------------
# check we can load it via Badger::Codecs
#-----------------------------------------------------------------------
package test1;
use Badger::Codecs codec => 'TT';
use Badger::Test;
is( encode({ msg => 'Hello' }), "{msg='Hello'}", 'encoded via TT codec' );
package test2;
use Badger::Codecs codec => 'tt';
use Badger::Test;
is( encode({ msg => 'Hello' }), "{msg='Hello'}", 'encoded via tt codec' );
package test3;
use Badger::Codecs;
use Badger::Test;
$codec = Badger::Codecs->codec( tt => { assign => ':=' } );
is( $codec->encode({ msg => 'Hello' }), "{msg:='Hello'}", 'encoded via custom tt codec' );
__END__
# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
|