Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 200 lines (165 sloc) 5.588 kb
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
use v6;

use Test;

=begin pod

C<===> and C<eqv> are 2 distinct operators, where C<===> tests value
equivalence for immutable types and reference equivalence for
mutable types, and C<eqv> tests value equivalence for snapshots of mutable
types. So C<(1,2) === (1,2)> returns true but C<[1,2] === [1,2]> returns
false, and C<[1,2] eqv [1,2]> returns true.

=end pod

# L<S03/"Chaining binary precedence" /Value identity>

plan 75;

# === on values
{
  ok (1 === 1), "=== on values (1)";
  ok (0 === 0), "=== on values (2)";
  ok (1 + 1 === 2), '=== on non-literal values';
  ok !(0 === 1), "=== on values (3)";
  isa_ok (1 === 1), Bool, "=== on values (4)";
  ok ("abc" === "abc"), "=== on values(abc)";
  ok !("abc" === "ABC"), "=== on values(abc === ABC)";
  isa_ok ("abc" === "abc"), Bool, "=== on values (abc)";
  ok !(1 === 1.0), "=== on values (1 === 1.0)";
  ok !(1 === "1"), '=== on values (1 === "1")';
}

# more value tests
{
  ok 1/2 === 1/2, "=== on Rats";
  ok 1/2 !=== 3/2, "!=== on Rats";
  isa_ok 1/2 === 1/2, Bool, "=== on Rats yields Bool";
  isa_ok 1/2 !=== 3/2, Bool, "!=== on Rats yields Bool";
  ok 0.5e0 === 0.5e0, "=== on Nums";
  ok 0.5e0 !=== 1.5e0, "!=== on Nums";
  isa_ok 0.5e0 === 0.5e0, Bool, "=== on Nums yields Bool";
  isa_ok 0.5e0 !=== 1.5e0, Bool, "!=== on Nums yields Bool";
}

# Value types
{
  my $a = 1;
  my $b = 1;

  ok $a === $a, "=== on value types (1-1)";
  ok $b === $b, "=== on value types (1-2)";
  ok $a === $b, "=== on value types (1-3)";
  isa_ok $a === $b, Bool, "=== on value types (1-4)";
}

{
  my $a = 1;
  my $b = 2;

  ok ($a === $a), "=== on value types (2-1)";
  ok ($b === $b), "=== on value types (2-2)";
  ok !($a === $b), "=== on value types (2-3)";
  isa_ok ($a === $a), Bool, "=== on value types (2-4)";
}

# Reference types
{
  my @a = (1,2,3);
  my @b = (1,2,3);

  #?niecza todo
  ok (\@a === \@a), "=== on array references (1)";
  #?niecza todo
  ok (\@b === \@b), "=== on array references (2)";
  #?rakudo todo '\@a seems to flatten when it should not'
  ok !(\@a === \@b), "=== on array references (3)";
  isa_ok (\@a === \@a), Bool, "=== on array references (4)";
}

{
  my $a = \3;
  my $b = \3;

  ok ($a === $a), "=== on scalar references (1-1)";
  ok ($b === $b), "=== on scalar references (1-2)";
  #?pugs todo
  ok $a === $b, "=== on scalar references (1-3)";
  isa_ok ($a === $a), Bool, "=== on scalar references (1-4)";
}

{
  my $a = { 3 };
  my $b = { 3 };

  ok ($a === $a), "=== on sub references (1-1)";
  ok ($b === $b), "=== on sub references (1-2)";
  ok !($a === $b), "=== on sub references (1-3)";
  isa_ok ($a === $a), Bool, "=== on sub references (1-4)";
}

{
  ok (&say === &say), "=== on sub references (2-1)";
  ok (&map === &map), "=== on sub references (2-2)";
  ok !(&say === &map), "=== on sub references (2-3)";
  isa_ok (&say === &say), Bool, "=== on sub references (2-4)";
}

{
  my $num = 3;
  my $a = \$num;
  my $b = \$num;

  ok ($a === $a), "=== on scalar references (2-1)";
  ok ($b === $b), "=== on scalar references (2-2)";
  #?niecza todo
  ok ($a === $b), "=== on scalar references (2-3)";
  isa_ok ($a === $a), Bool, "=== on scalar references (2-4)";
}

{
  ok !([1,2,3] === [4,5,6]), "=== on anonymous array references (1)";
  ok !([1,2,3] === [1,2,3]), "=== on anonymous array references (2)";
  ok !([] === []), "=== on anonymous array references (3)";
  isa_ok ([1,2,3] === [4,5,6]), Bool, "=== on anonymous array references (4)";
}

{
  ok !({a => 1} === {a => 2}), "=== on anonymous hash references (1)";
  ok !({a => 1} === {a => 1}), "=== on anonymous hash references (2)";
  isa_ok ({a => 1} === {a => 2}), Bool, "=== on anonymous hash references (3)";
}

{
  ok !(\3 === \4), "=== on anonymous scalar references (1)";
  #?pugs todo
  ok \3 === \3, "=== on anonymous scalar references (2)";
  isa_ok (\3 === \4), Bool, "=== on anonymous scalar references (4)";
}

# Chained === (not specced, but obvious)
{
  ok (3 === 3 === 3), "chained === (1)";
  ok !(3 === 3 === 4), "chained === (2)";
}

# Subparam binding doesn't affect === test
{
  my $foo;
  my $test = -> $arg { $foo === $arg };

  $foo = 3;
  ok $test($foo), "subparam binding doesn't affect === (1)";
  ok $test(3), "subparam binding doesn't affect === (2)";

  ok !$test(4), "subparam binding doesn't affect === (3)";
  my $bar = 4;
  ok !$test($bar), "subparam binding doesn't affect === (4)";
}

{
    my $a = 1;
    my $b = 2;
    is($a === $a, Bool::True, '=== returns Bool::True when true');
    is($a === $b, Bool::False, '=== returns Bool::False when false');
}

# L<S03/"Chaining binary precedence" /Negated relational operators>
{
  ok !(1 !=== 1), "!=== on values (1)";
  ok !(0 !=== 0), "!=== on values (2)";
  ok (1 !=== 0), "!=== on values (3)";
  isa_ok (1 !=== 1), Bool, "!=== on values (4)";
  ok !("abc" !=== "abc"), "!=== on values(abc)";
  ok ("abc" !=== "ABC"), "!=== on values(abc !=== ABC)";
  ok (1 !=== 1.0), "!=== on values (1 !=== 1.0)";
  ok (1 !=== "1"), '!=== on values (1 !=== "1")';
}

#?pugs skip "this test alters # of run tests?"
ok 1|2 === 1, '=== does autothread (1)';
isa_ok 1|2 === 1, Junction, '=== does autothread (2)';

#?pugs skip 'roles'
{
    my $a = do {
        my role A { };
        1 but A;
    };
    my $b = do {
        my role A { };
        1 but A;
    };
    #?rakudo todo '.WHICH based on type names'
    nok $a === $b, 'two lexical roles come out as different when compared with ===';
}

done;

# vim: ft=perl6
Something went wrong with that request. Please try again.