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
|
use t::TestYAML tests => 134;
ok(YAML::Syck->VERSION);
is(Dump(42), "--- 42\n");
is(Load("--- 42\n"), 42);
is(Dump(\42), "--- !!perl/ref \n=: 42\n");
is(${Load("--- !!perl/ref \n=: 42\n")}, 42);
my $x;
$x = \$x;
is(Dump($x), "--- &1 !!perl/ref \n=: *1\n");
is(Dump(scalar Load(Dump($x))), "--- &1 !!perl/ref \n=: *1\n");
$YAML::Syck::DumpCode = 0;
is(Dump(sub{ 42 }), "--- !!perl/code: '{ \"DUMMY\" }'\n");
$YAML::Syck::DumpCode = 1;
TODO: {
local $TODO = "5.6 can't do code references in Syck right now" if($] < 5.007);
Test::More::like( Dump(sub{ 42 }), qr#--- !!perl/code.*?{.*?42.*?}$#s);
}
$YAML::Syck::LoadCode = 0;
{
my $not_sub = Load("--- !!perl/code:Some::Class '{ \"foo\" . shift }'\n");
is( ref $not_sub, "Some::Class" );
is( $not_sub->("bar"), undef );
}
{
my $sub = Load("--- !!perl/code '{ \"foo\" . shift }'\n");
is( ref $sub, "CODE" );
is( $sub->("bar"), undef );
}
my $like_yaml_pm = 0;
$YAML::Syck::LoadCode = 0;
ok( my $not_sub = Load("--- !!perl/Class '{ \"foo\" . shift }'\n") );
if ( $like_yaml_pm ) {
is( ref($not_sub), "code" );
is( eval { $$not_sub }, '{ "foo" . shift }' );
} else {
is ( $not_sub, '{ "foo" . shift }' );
ok(1); # stick with the plan
}
$YAML::Syck::LoadCode = 1;
my $sub = Load("--- !!perl/code: '{ \"foo\" . \$_[0] }'\n");
ok( defined $sub );
is( ref($sub), "CODE" );
is( eval { $sub->("bar") }, "foobar" );
is( $@, "", "no error" );
$YAML::Syck::LoadCode = $YAML::Syck::DumpCode = 0;
$YAML::Syck::UseCode = $YAML::Syck::UseCode = 1;
TODO: {
local $TODO;
$TODO = "5.6 can't do code references in Syck right now" if($] < 5.007);
is( eval { Load(Dump(sub { "foo" . shift }))->("bar") }, "foobar" );
$TODO = '';
is( $@, "", "no error" );
$TODO = "5.6 can't do code references in Syck right now" if($] < 5.007);
is( eval { Load(Dump(sub { shift() ** 3 }))->(3) }, 27 );
}
is(Dump(undef), "--- ~\n");
is(Dump('~'), "--- \'~\'\n");
is(Dump('a:'), "--- \"a:\"\n");
is(Dump('a: '), "--- \"a: \"\n");
is(Dump('a '), "--- \"a \"\n");
is(Dump('a: b'), "--- \"a: b\"\n");
is(Dump('a:b'), "--- a:b\n");
is(Load("--- ~\n"), undef);
is(Load("---\n"), undef);
is(Load("--- ''\n"), '');
my $h = {bar => [qw<baz troz>]};
$h->{foo} = $h->{bar};
is(Dump($h), << '.');
---
bar: &1
- baz
- troz
foo: *1
.
my $r; $r = \$r;
is(Dump($r), << '.');
--- &1 !!perl/ref
=: *1
.
is(Dump(scalar Load(Dump($r))), << '.');
--- &1 !!perl/ref
=: *1
.
# RT #17223
my $y = YAML::Syck::Load("SID:\n type: fixed\n default: ~\n");
eval { $y->{SID}{default} = 'abc' };
is($y->{SID}{default}, 'abc');
is(Load("--- true\n"), "true");
is(Load("--- false\n"), "false");
$YAML::Syck::ImplicitTyping = $YAML::Syck::ImplicitTyping = 1;
is(Load("--- true\n"), 1);
is(Load("--- false\n"), '');
# Various edge cases at grok_number boundary
is(Load("--- 42949672\n"), 42949672);
is(Load("--- -42949672\n"), -42949672);
is(Load("--- 429496729\n"), 429496729);
is(Load("--- -429496729\n"), -429496729);
is(Load("--- 4294967296\n"), 4294967296);
is(Load("--- -4294967296\n"), -4294967296);
# RT #18752
my $recurse1 = << '.';
--- &1
Foo:
parent: *1
Troz:
parent: *1
.
is(Dump(scalar Load($recurse1)), $recurse1, 'recurse 1');
my $recurse2 = << '.';
--- &1
Bar:
parent: *1
Baz:
parent: *1
Foo:
parent: *1
Troz:
parent: *1
Zort: &2
Poit:
parent: *2
parent: *1
.
is(Dump(scalar Load($recurse2)), $recurse2, 'recurse 2');
is(Dump(1, 2, 3), "--- 1\n--- 2\n--- 3\n");
is("@{[Load(Dump(1, 2, 3))]}", "1 2 3");
$YAML::Syck::ImplicitBinary = $YAML::Syck::ImplicitBinary = 1;
is(Dump("\xff\xff"), "--- !binary //8=\n");
is(Load("--- !binary //8=\n"), "\xff\xff");
is(Dump("ascii"), "--- ascii\n");
is(Dump("This is Perl 6 User's Golfing System\n"), q[--- "This is Perl 6 User's Golfing System\n"] . "\n" );
$YAML::Syck::SingleQuote = $YAML::Syck::SingleQuote = 1;
is(Dump("This is Perl 6 User's Golfing System\n"), qq[--- 'This is Perl 6 User''s Golfing System\n\n'\n]);
is(Dump('042'), "--- '042'\n");
roundtrip('042');
roundtrip("This\nis\na\ntest");
roundtrip("Newline\n");
roundtrip(" ");
roundtrip("\n");
roundtrip("S p a c e");
roundtrip("Space \n Around");
roundtrip("042");
roundtrip("0x42");
roundtrip("0.42");
roundtrip(".42");
roundtrip("1,000,000");
roundtrip("1e+6");
roundtrip("10e+6");
# If implicit typing is on, quote strings corresponding to implicit boolean and null values
$YAML::Syck::SingleQuote = 0;
is(Dump('N'), "--- 'N'\n");
is(Dump('NO'), "--- 'NO'\n");
is(Dump('No'), "--- 'No'\n");
is(Dump('no'), "--- 'no'\n");
is(Dump('y'), "--- 'y'\n");
is(Dump('YES'), "--- 'YES'\n");
is(Dump('Yes'), "--- 'Yes'\n");
is(Dump('yes'), "--- 'yes'\n");
is(Dump('TRUE'), "--- 'TRUE'\n");
is(Dump('false'), "--- 'false'\n");
is(Dump('off'), "--- 'off'\n");
is(Dump('null'), "--- 'null'\n");
is(Dump('Null'), "--- 'Null'\n");
is(Dump('NULL'), "--- 'NULL'\n");
is(Dump('oN'), "--- oN\n"); # invalid case
is(Dump('oFF'), "--- oFF\n"); # invalid case
is(Dump('nULL'), "--- nULL\n"); # invalid case
# RT 52432 - '... X'
my $bad_hash = {'... X' => ''};
my $bad_hash_should = "--- \n... X: ''\n";
TODO: {
local $TODO;
$TODO = "roundtrip is breaking for this right now: '$bad_hash_should'";
roundtrip($bad_hash);
}
is(Dump({ foo => "`bar" }), qq{--- \nfoo: "`bar"\n}, 'RT 47944 - back quote is a reserved character');
# quoted number corner cases:
foreach (qw/1 2 3 1.0 1.0000 1.00004 2.2 3.7 42.0 0.123 0.0042 0...02 98765432109123 987654321091234 -98765432109123/) {
roundtrip($_);
}
# Un-quoted number corner cases:
foreach (1, 2, 3, 1.0, 1.0000, 1.00004, 2.2, 3.7, 42.0, 0.123, 0.0042, 0, 1, 98765432109123, 987654321091234 -98765432109123) {
roundtrip($_);
}
# Simple integers dump without quotes
foreach (1, 2, 3, 0, -1, -2, -3) {
is(Dump($_), "--- $_\n", "Dumped version of file is unquoted");
}
is(Dump('0x10'), "--- 0x10\n", "hex Dump preserves as string");
is(Load("--- '0x10'\n"), "0x10", "hex Load preserves as string");
is(Dump('080'), "--- '080'\n", "oct Dump preserves by quoting");
is(Load("--- '080'\n"), "080", "oct Load preserves by quoting");
is(Dump('00'), "--- '00'\n", "00 Dump preserves by quoting");
is(Load("--- '00'\n"), "00", "00 Load preserves by quoting");
# RT 54780 - double quoted loading style
TODO: {
my $input = q{--- "<tag>content\
\ string</tag>\n\
<anothertag>other\
\ content</anothertag>\n\
\ \n<i>new</i>\n"};
my $expected = q{<tag>content string</tag>
<anothertag>other content</anothertag>
<i>new</i>
};
local $TODO = "not handling double quoted style right";
is(Load($input), $expected, "RT 54780 - Wrong loading of YAML with double quoted style");
}
|