Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

59 additional tests ported over from set.t.

  • Loading branch information...
commit 9da8a25e98e885f1fd73a2296f34b0582c36ebea 1 parent a35fbd5
@colomon colomon authored
Showing with 151 additions and 2 deletions.
  1. +151 −2 S02-types/keyset.t
View
153 S02-types/keyset.t
@@ -1,7 +1,7 @@
use v6;
use Test;
-plan 49;
+plan 108;
# L<S02/Mutable types/"KeyHash of Bool">
@@ -17,10 +17,21 @@ sub showset($s) { $s.keys.sort.join(' ') }
is showset($s), 'a b foo', '...with the right elements';
is $s<a>, True, 'Single-key subscript (existing element)';
+ isa_ok $s<a>, Bool, 'Single-key subscript has correct type (existing element)';
is $s<santa>, False, 'Single-key subscript (nonexistent element)';
+ isa_ok $s<santa>, Bool, 'Single-key subscript has correct type (nonexistent element)';
is $s.exists('a'), True, '.exists with existing element';
is $s.exists('santa'), False, '.exists with nonexistent element';
+ ok ?$s, "Bool returns True if there is something in the KeySet";
+ nok ?Set.new(), "Bool returns False if there is nothing in the KeySet";
+
+ my $hash;
+ lives_ok { $hash = $s.hash }, ".hash doesn't die";
+ isa_ok $hash, Hash, "...and it returned a Hash";
+ is showset($hash), 'a b foo', '...with the right elements';
+ is $hash.values.grep({ ($_ ~~ Bool) && $_ }).elems, 3, "...and values";
+
dies_ok { $s.keys = <c d> }, "Can't assign to .keys";
dies_ok { $s.values = <True False> }, "Can't assign to .values";
@@ -56,7 +67,6 @@ sub showset($s) { $s.keys.sort.join(' ') }
is showset($s), 'a foo', '... but only if they were there to start with';
}
-
{
my %h := KeySet.new(<a c>);
is +%h.elems, 2, 'Inititalization worked';
@@ -79,6 +89,145 @@ sub showset($s) { $s.keys.sort.join(' ') }
# is %h.keys.sort.join, 'PQR', '... works as expected';
}
+{
+ my $s = KeySet.new(<foo bar foo bar baz foo>);
+ is showset($s), 'bar baz foo', 'KeySet.new discards duplicates';
+}
+
+{
+ my $b = KeySet.new([ foo => 10, bar => 17, baz => 42 ]);
+ isa_ok $b, KeySet, 'KeySet.new given an array of pairs produces a KeySet';
+ is showset($b), 'bar baz foo', '... with the right elements';
+}
+
+{
+ my $b = KeySet.new({ foo => 10, bar => 17, baz => 42 }.hash);
+ isa_ok $b, KeySet, 'KeySet.new given a Hash produces a KeySet';
+ is showset($b), 'bar baz foo', '... with the right elements';
+}
+
+{
+ my $b = KeySet.new({ foo => 10, bar => 17, baz => 42 });
+ isa_ok $b, KeySet, 'KeySet.new given a Hash produces a KeySet';
+ is showset($b), 'bar baz foo', '... with the right elements';
+}
+
+{
+ my $b = KeySet.new(set <foo bar foo bar baz foo>);
+ isa_ok $b, KeySet, 'KeySet.new given a Set produces a KeySet';
+ is showset($b), 'bar baz foo', '... with the right elements';
+}
+
+{
+ my $b = KeySet.new(KeySet.new(<foo bar foo bar baz foo>));
+ isa_ok $b, KeySet, 'KeySet.new given a KeySet produces a KeySet';
+ is showset($b), 'bar baz foo', '... with the right elements';
+}
+
+{
+ my $b = KeySet.new(KeyBag.new(<foo bar foo bar baz foo>));
+ isa_ok $b, KeySet, 'KeySet.new given a KeyBag produces a KeySet';
+ is showset($b), 'bar baz foo', '... with the right elements';
+}
+
+{
+ my $b = KeySet.new(bag <foo bar foo bar baz foo>);
+ isa_ok $b, KeySet, 'KeySet given a Bag produces a KeySet';
+ is showset($b), 'bar baz foo', '... with the right elements';
+}
+
+{
+ my $s = KeySet.new(<foo bar baz>);
+ isa_ok $s.list.elems, 3, ".list returns 3 things";
+ is $s.list.grep(Str).elems, 3, "... all of which are Str";
+ is $s.iterator.grep(Str).elems, 3, ".iterator yields three Strs";
+}
+
+{
+ my $s = KeySet.new(<foo bar baz>);
+ my $str;
+ my $c;
+ lives_ok { $str = $s.perl }, ".perl lives";
+ isa_ok $str, Str, "... and produces a string";
+ lives_ok { $c = eval $str }, ".perl.eval lives";
+ isa_ok $c, KeySet, "... and produces a KeySet";
+ is showset($c), showset($s), "... and it has the correct values";
+}
+
+{
+ my $s = KeySet.new(<foo bar baz>);
+ my $s;
+ lives_ok { $s = $s.Str }, ".Str lives";
+ isa_ok $s, Str, "... and produces a string";
+ ok $s ~~ /foo/, "... which mentions foo";
+ ok $s ~~ /bar/, "... which mentions bar";
+ ok $s ~~ /baz/, "... which mentions baz";
+}
+
+{
+ my $s = KeySet.new(<foo bar baz>);
+ my $s;
+ lives_ok { $s = $s.gist }, ".gist lives";
+ isa_ok $s, Str, "... and produces a string";
+ ok $s ~~ /foo/, "... which mentions foo";
+ ok $s ~~ /bar/, "... which mentions bar";
+ ok $s ~~ /baz/, "... which mentions baz";
+}
+
+# L<S02/Names and Variables/'C<%x> may be bound to'>
+
+{
+ my %s := KeySet.new(<a b c b>);
+ isa_ok %s, KeySet, 'A KeySet bound to a %var is a KeySet';
+ is showset(%s), 'a b c', '...with the right elements';
+
+ is %s<a>, True, 'Single-key subscript (existing element)';
+ is %s<santa>, False, 'Single-key subscript (nonexistent element)';
+
+ lives_ok { %s<a> = True }, "Can assign to an element (KeySets are immutable)";
+}
+
+# L<S32::Containers/KeySet/roll>
+
+{
+ my $s = KeySet.new(<a b c>);
+
+ my $a = $s.roll;
+ ok $a eq "a" || $a eq "b" || $a eq "c", "We got one of the three choices";
+
+ my @a = $s.roll(2);
+ is +@a, 2, '.roll(2) returns the right number of items';
+ is @a.grep(* eq 'a' | 'b' | 'c').elems, 2, '.roll(2) returned "a"s, "b"s, and "c"s';
+
+ my @a = $s.roll: 100;
+ is +@a, 100, '.roll(100) returns 100 items';
+ is @a.grep(* eq 'a' | 'b' | 'c').elems, 100, '.roll(100) returned "a"s, "b"s, and "c"s';
+}
+
+# L<S32::Containers/KeySet/pick>
+
+{
+ my $s = KeySet.new(<a b c d e f g h>);
+ my @a = $s.pick: *;
+ is @a.sort.join, 'abcdefgh', 'KeySet.pick(*) gets all elements';
+ isnt @a.join, 'abcdefgh', 'KeySet.pick(*) returns elements in a random order';
+ # There's only a 1/40_320 chance of that test failing by chance alone.
+}
+
+{
+ my $s = KeySet.new(<a b c>);
+
+ my $a = $s.pick;
+ ok $a eq "a" || $a eq "b" || $a eq "c", "We got one of the three choices";
+
+ my @a = $s.pick(2);
+ is +@a, 2, '.pick(2) returns the right number of items';
+ is @a.grep(* eq 'a' | 'b' | 'c').elems, 2, '.pick(2) returned "a"s, "b"s, and "c"s';
+ ok @a.grep(* eq 'a').elems <= 1, '.pick(2) returned at most one "a"';
+ ok @a.grep(* eq 'b').elems <= 1, '.pick(2) returned at most one "b"';
+ ok @a.grep(* eq 'c').elems <= 1, '.pick(2) returned at most one "c"';
+}
+
#?niecza skip "is KeySet doesn't work yet"
{
my %h is KeySet = a => True, b => False, c => True;
Please sign in to comment.
Something went wrong with that request. Please try again.