use v6;
use Test;
plan 117;
my $pair = 'foo' => 'bar';
isa_ok($pair, Pair);
is(key($pair:), 'foo', 'got the right key($pair:)');
is(value($pair:), 'bar', 'got the right value($pair:)');
is($pair.key(), 'foo', 'got the right $pair.key()');
is($pair.value(), 'bar', 'got the right $pair.value()');
is($pair.key, 'foo', 'got the right $pair.key');
is($pair.value, 'bar', 'got the right $pair.value');
my @pair1a = kv($pair);
is(+@pair1a, 2, 'got the right number of elements in the list');
is(@pair1a[0], 'foo', 'got the right key');
is(@pair1a[1], 'bar', 'got the right value');
my @pair1b = kv $pair;
is(+@pair1b, 2, 'got the right number of elements in the list');
is(@pair1b[0], 'foo', 'got the right key');
is(@pair1b[1], 'bar', 'got the right value');
my @pair1c = $pair.kv;
is(+@pair1c, 2, 'got the right number of elements in the list');
is(@pair1c[0], 'foo', 'got the right key');
is(@pair1c[1], 'bar', 'got the right value');
my @pair1d = $pair.kv();
is(+@pair1d, 2, 'got the right number of elements in the list');
is(@pair1d[0], 'foo', 'got the right key');
is(@pair1d[1], 'bar', 'got the right value');
my $pair2 = 'foo' => 2;
isa_ok($pair2, Pair);
is($pair2.value, 2, 'got the right value');
my $pair3 = "foo" => ("bar" => "baz");
isa_ok($pair3, Pair);
my $pair3a = $pair3.value;
isa_ok($pair3a, Pair);
is($pair3a.key, 'bar', 'got right nested pair key');
is($pair3a.value, 'baz', 'got right nested pair key');
is($pair3.value.key, 'bar', 'got right nested pair key (method chaining)');
is($pair3.value.value, 'baz', 'got right nested pair key (method chaining)');
my $pair4 = ("foo" => "bar") => "baz";
isa_ok($pair4, Pair);
is($pair4.value, 'baz', 'got the right value');
isa_ok($pair4.key, Pair);
is($pair4.key.key, 'foo', 'got right nested key');
is($pair4.key.value, 'bar', 'got right nested value');
my $quux = (quux => "xyzzy");
is($quux.key, 'quux', "lhs quotes" );
{
my $pair = :when<now>;
is ~(%($pair)), "when\tnow\n", 'hash stringification';
ok (%($pair) ~~ Hash), '%() makes creates a real Hash';
}
{
is(:foo.key, 'foo', 'got the right key :foo.key');
isa_ok(:foo.value, Bool, ':foo.value isa Bool');
ok( (:foo), ':foo is True');
ok( :foo.value, ':foo.value is True');
is(:!foo.key, 'foo', 'got the right key :!foo.key');
isa_ok(:!foo.value, Bool, ':!foo.value isa Bool');
nok( :!foo.value, ':!foo.value is False');
}
{
my $var = 'foo' => 'bar';
sub test1 (Pair $pair) {
isa_ok($pair,Pair);
my $testpair = $pair;
isa_ok($testpair,Pair);
my $boundpair := $pair;
isa_ok($boundpair,Pair);
is($pair.key, 'foo', 'in sub test1 got the right $pair.key');
is($pair.value, 'bar', 'in sub test1 got the right $pair.value');
}
test1 $var;
}
my %hash = ('foo' => 'bar');
{
for %hash.pairs -> $pair {
isa_ok($pair,Pair) ;
my $testpair = $pair;
isa_ok($testpair, Pair);
my $boundpair := $pair;
isa_ok($boundpair,Pair);
is($pair.key, 'foo', 'in for loop got the right $pair.key');
is($pair.value, 'bar', 'in for loop got the right $pair.value');
}
}
sub test2 (%h){
for %h.pairs -> $pair {
isa_ok($pair,Pair) ;
is($pair.key, 'foo', 'in sub test2 got the right $pair.key');
is($pair.value, 'bar', 'in sub test2 got the right $pair.value');
}
}
test2 %hash;
sub test3 (%h){
for %h.pairs -> $pair {
isa_ok($pair,Pair);
isa_ok($pair[0], Pair, 'sub test3: $pair[0] is $pair');
ok $pair[1] ~~ Failure, 'sub test3: $pair[1] is failure';
}
}
test3 %hash;
my $should_be_a_pair = (a => 25/1);
isa_ok $should_be_a_pair, Pair, "=> has correct precedence";
{
my %x = ( "Zaphod" => (0 ?? 1 !! 2), "Ford" => 42 );
is %x{"Zaphod"}, 2, "Zaphod is 2";
is %x{"Ford"}, 42, "Ford is 42";
my %z = ( "Zaphod" => 0 ?? 1 !! 2, "Ford" => 42 );
is %z{"Zaphod"}, 2, "Zaphod is still 2";
is %z{"Ford"}, 42, "Ford is still 42";
}
{
my $pair = (a => 1);
is ~$pair, "a\t1", "pairs stringify correctly (1)";
is "$pair", "a\t1", "pairs stringify correctly (2)";
}
{
my $pair = (a => [1,2,3]);
is ~$pair, "a\t1 2 3", "pairs with arrayrefs as values stringify correctly (1)";
is "$pair", "a\t1 2 3", "pairs with arrayrefs as values stringify correctly (2)";
}
{
my $arrayref = [< a b c >];
my $hashref = { :d(1), :e(2) };
my $pair = ($arrayref => $hashref);
is ~$pair.key, ~$arrayref, "=> should not stringify the key (1)";
is ~$pair.value, ~$hashref, "=> should not stringify the key (2)";
push $pair.key, "d";
$pair.value<f> = 3;
is ~$pair.key, ~$arrayref, "=> should not stringify the key (3)";
is ~$pair.value, ~$hashref, "=> should not stringify the key (4)";
is +$pair.key, 4, "=> should not stringify the key (5)";
is +$pair.value, 3, "=> should not stringify the key (6)";
}
{
my $arrayref = [< a b c >];
my $hashref = { :d(1), :e(2) };
my $pair = ($arrayref => $hashref);
sub pair_key (Pair $pair) { $pair.key }
is ~pair_key($pair), ~$arrayref,
"the keys of pairs should not get auto-stringified when passed to a sub (1)";
push $pair.key, "d";
is ~pair_key($pair), ~$arrayref,
"the keys of pairs should not get auto-stringified when passed to a sub (2)";
is +pair_key($pair), 4,
"the keys of pairs should not get auto-stringified when passed to a sub (3)";
}
{
my ($key, $val) = <key val>;
my $pair = ($key => $val);
lives_ok { $pair.key = "KEY" }, "setting .key does not die";
is $pair.key, "KEY", "setting .key actually changes the key";
is $key, "key", "setting .key does not change the original var";
lives_ok { $pair.value = "VAL" }, "setting .value does not die";
is $pair.value, "VAL", "setting .value actually changes the value";
is $val, "val", "setting .value does not change the original var";
}
{
my ($key, $val) = <key val>;
my $pair = ($key => $val);
lives_ok { $pair.key := "KEY" }, "binding .key does not die";
is $pair.key, "KEY", "binding .key actually changes the key";
is $key, "key", "binding .key does not change the original var";
dies_ok { $pair.key = 42 }, "the .key was really bound";
lives_ok { $pair.value := "VAL" }, "binding .value does not die";
is $pair.value, "VAL", "binding .value actually changes the value";
is $val, "val", "binding .value does not change the original var";
dies_ok { $pair.value = 42 }, "the .value was really bound";
}
{
my ($key, $val) = <key val>;
my $pair = (abc => "def");
lives_ok { $pair.key := $key }, "binding .key does not die";
is $pair.key, "key", "binding .key actually changes the key";
$key = "KEY";
is $key, "KEY", "binding .key to a var works (1)";
is $pair.key, "KEY", "binding .key to a var works (2)";
$pair.key = "new";
is $key, "new", "binding .key to a var works (3)";
is $pair.key, "new", "binding .key to a var works (4)";
lives_ok { $pair.value := $val }, "binding .value does not die";
is $pair.value, "val", "binding .value actually changes the value";
$val = "VAL";
is $val, "VAL", "binding .value to a var works (1)";
is $pair.value, "VAL", "binding .value to a var works (2)";
$pair.value = "new";
is $val, "new", "binding .value to a var works (3)";
is $pair.value, "new", "binding .value to a var works (4)";
}
{
my $item = 'bar';
my $pair = (:$item);
ok($pair eqv (item => $item), ':$foo syntax works');
my @arr = <a b c d e f>;
$pair = (:@arr);
ok($pair eqv (arr => @arr), ':@foo syntax works');
my %hash = foo => 'bar', baz => 'qux';
$pair = (:%hash);
ok($pair eqv (hash => %hash), ':%foo syntax works');
}
{
my sub code {return 42}
$pair = (:&code);
ok($pair eqv (code => &code), ':&foo syntax works');
}
{
eval_lives_ok ':a()', 'can parse ":a()"';
lives_ok {; :a() }, 'can execute ":a()"';
eval_lives_ok ':a[]', 'can parse ":a[]"';
lives_ok {; :a[] }, 'can execute ":a[]"';
eval_lives_ok '(a => ())', 'can parse "(a => ())"';
lives_ok { (a => ()) }, 'can execute "(a => ())"';
eval_lives_ok '(a => [])', 'can parse "(a => [])"';
lives_ok { (a => []) }, 'can execute "(a => [])"';
}
{
is (a => 3).invert.key, 3, 'Pair.invert.key';
isa_ok (a => 3).invert.key, Int, 'Pair.invert.key type';
is (a => 3).invert.value, 'a', 'Pair.invert.value';
}