Permalink
Browse files

[t] moved hash binding tests to spec/

git-svn-id: http://svn.pugscode.org/pugs@22112 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information...
moritz
moritz committed Sep 2, 2008
1 parent c2b31f8 commit 73f142849253b01bd40335c0c7134f78da48010e
Showing with 181 additions and 0 deletions.
  1. +181 −0 S03-operators/binding-hashes.t
@@ -0,0 +1,181 @@
+use v6;
+
+use Test;
+
+# L<S03/Item assignment precedence>
+
+plan 37;
+
+# Binding of hash elements.
+# See thread "Binding of array elements" on p6l started by Ingo Blechschmidt:
+# L<"http://www.nntp.perl.org/group/perl.perl6.language/22915">
+{
+ my %hash = (:a<x>, :b<y>, :c<z>);
+ my $var = "d";
+
+ try { %hash<b> := $var };
+ is %hash<b>, "d", "basic binding of a hash element (1)";
+ unless %hash<b> eq "d" {
+ skip_rest "Skipping binding of hash elements tests (not yet implemented in the normal runcore)";
+ exit;
+ }
+
+ $var = "e";
+ is %hash<b>, "e", "basic binding of a hash element (2)";
+
+ %hash<b> = "f";
+ is $var, "f", "basic binding of a hash element (3)";
+}
+
+{
+ my %hash = (:a<x>, :b<y>, :c<z>);
+ my $var = "d";
+
+ %hash<b> := $var;
+ $var = "e";
+ is %hash<b>, "e", "binding of hash elements works with .delete (1)";
+
+ %hash.delete("b");
+ # $var unchanged, but assigning to $var doesn't modify @hash any
+ # longer; similarily, changing @hash[1] doesn't modify $var now
+ is $var, "e", "binding of hash elements works with .delete (2)";
+ is ~%hash.values.sort, "x z", "binding of hash elements works with .delete (3)";
+
+ $var = "f";
+ %hash<b> = "g";
+ is $var, "f", "binding of hash elements works with .delete (4)";
+ is %hash<b>, "g", "binding of hash elements works with .delete (5)";
+}
+
+{
+ my %hash = (:a<x>, :b<y>, :c<z>);
+ my $var = "d";
+
+ %hash<b> := $var;
+ $var = "e";
+ is %hash<b>, "e", "binding of hash elements works with resetting the hash (1)";
+
+ %hash = ();
+ # $var unchanged, but assigning to $var doesn't modify @hash any
+ # longer; similarily, changing @hash[1] doesn't modify $var now
+ is $var, "e", "binding of hash elements works with resetting the hash (2)";
+ is ~%hash, "", "binding of hash elements works with resetting the hash (3)";
+
+ $var = "f";
+ %hash<b> = "g";
+ is $var, "f", "binding of hash elements works with resetting the hash (4)";
+ is %hash<b>, "g", "binding of hash elements works with resetting the hash (5)";
+}
+
+{
+ my %hash = (:a<x>, :b<y>, :c<z>);
+ my $var = "d";
+
+ %hash<b> := $var;
+ $var = "e";
+ is %hash<b>, "e", "binding of hash elements works with rebinding the hash (1)";
+
+ my %other_hash = (:p<q>, :r<s>, :t<u>);
+ %hash := %other_hash;
+ # $var unchanged, but assigning to $var doesn't modify @hash any
+ # longer; similarily, changing @hash[1] doesn't modify $var now
+ is $var, "e", "binding of hash elements works with rebinding the hash (2)";
+ is ~%hash.values.sort, "q s u",
+ "binding of hash elements works with rebinding the hash (3)";
+
+ $var = "f";
+ %hash<b> = "g";
+ is $var, "f", "binding of hash elements works with rebinding the hash (4)";
+ is %hash<b>, "g", "binding of hash elements works with rebinding the hash (5)";
+}
+
+{
+ my sub foo (%h) { %h<b> = "new_value" }
+
+ my %hash = (:a<x>, :b<y>, :c<z>);
+ my $var = "d";
+ %hash<b> := $var;
+
+ foo %hash;
+ is $var, "new_value", "passing a hash to a sub expecting a hash behaves correctly (1)";
+ is ~%hash.values.sort, "new_value x z",
+ "passing a hash to a sub expecting a hash behaves correctly (2)";
+}
+
+{
+ my sub foo (Hash $h) { $h<b> = "new_value" }
+
+ my %hash = (:a<x>, :b<y>, :c<z>);
+ my $var = "d";
+ %hash<b> := $var;
+
+ foo %hash;
+ is $var, "new_value",
+ "passing a hash to a sub expecting a hashref behaves correctly (1)";
+ is ~%hash.values.sort, "new_value x z",
+ "passing a hash to a sub expecting a hashref behaves correctly (2)";
+}
+
+# Binding of not yet existing elements should autovivify
+{
+ my %hash;
+ my $var = "d";
+
+ lives_ok { %hash<b> := $var },
+ "binding of not yet existing elements should autovivify (1)";
+ is %hash<b>, "d", "binding of not yet existing elements should autovivify (2)";
+
+ $var = "e";
+ is %hash<b>, "e", "binding of not yet existing elements should autovivify (3)";
+ is $var, "e", "binding of not yet existing elements should autovivify (4)";
+}
+
+# Assignment (not binding) creates new containers
+{
+ my %hash = (:a<x>, :b<y>, :c<z>);
+ my $var = "d";
+
+ %hash<b> := $var;
+ $var = "e";
+ is %hash<b>, "e", "hash assignment creates new containers (1)";
+
+ my %new_hash = %hash;
+ $var = "f";
+ # %hash<b> and $var are now "f", but %new_hash is unchanged.
+ is $var, "f", "hash assignment creates new containers (2)";
+ is ~%hash\ .values.sort, "f x z", "hash assignment creates new containers (3)";
+ is ~%new_hash.values.sort, "e x z", "hash assignment creates new containers (4)";
+}
+
+# Binding does not create new containers
+{
+ my %hash = (:a<x>, :b<y>, :c<z>);
+ my $var = "d";
+
+ %hash<b> := $var;
+ $var = "e";
+ is %hash<b>, "e", "hash binding does not create new containers (1)";
+
+ my %new_hash := %hash;
+ $var = "f";
+ # %hash<b> and $var are now "f", but %new_hash is unchanged.
+ is $var, "f", "hash binding does not create new containers (2)";
+ is ~%hash\ .values.sort, "f x z", "hash binding does not create new containers (3)";
+ is ~%new_hash.values.sort, "f x z", "hash binding does not create new containers (4)";
+}
+
+# Binding %hash := $hashref.
+# See
+# http://colabti.de/irclogger/irclogger_log/perl6?date=2005-11-06,Sun&sel=388#l564
+# and consider the magic behind parameter binding (which is really normal
+# binding).
+{
+ my $hashref = { a => "a", b => "b" };
+ my %hash := $hashref;
+
+ is +%hash, 2, 'binding %hash := $hashref works (1)';
+
+ %hash<b> = "c";
+ is ~$hashref.values.sort, "a c", 'binding %hash := $hashref works (2)';
+ is ~%hash\ .values.sort, "a c", 'binding %hash := $hashref works (3)';
+}

0 comments on commit 73f1428

Please sign in to comment.