summaryrefslogtreecommitdiff
path: root/t/op/or.t
blob: 9e7de4c2bd155b887a7fceca8f4c1f46ffca93ec (plain)
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
#!./perl

# Test || in weird situations.

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
}


package Countdown;

sub TIESCALAR {
  my $class = shift;
  my $instance = shift || undef;
  return bless \$instance => $class;
}

sub FETCH {
  print "# FETCH!  ${$_[0]}\n";
  return ${$_[0]}--;
}


package main;
require './test.pl';

plan( tests => 14 );


my ($a, $b, $c);

$! = 1;
$a = $!;
my $a_str = sprintf "%s", $a;
my $a_num = sprintf "%d", $a;

$c = $a || $b;

is($c, $a_str, "comparison of string equality");
is($c+0, $a_num, "comparison of numeric equality");   # force numeric context.

$a =~ /./g or die "Match failed for some reason"; # Make $a magic

$c = $a || $b;

is($c, $a_str, "comparison of string equality");
is($c+0, $a_num, "comparison of numeric equality");   # force numeric context.

my $val = 3;

$c = $val || $b;
is($c, 3, "|| short-circuited as expected");

tie $a, 'Countdown', $val;

$c = $a;
is($c, 3,       'Single FETCH on tied scalar');

$c = $a;
is($c, 2,       '   $tied = $var');

$c = $a || $b;

{
    local $TODO = 'Double FETCH';
    is($c, 1,   '   $tied || $var');
}

$y = " ";
for (pos $x || pos $y) {
    eval { $_++ };
}
is(pos($y) || $@, 1, "|| propagates lvaluish context to its rhs");

$x = "  ";
pos $x = 1;
for (pos $x || pos $y) {
    eval { $_++ };
}
is(pos($x) || $@, 2, "|| propagates lvaluish context to its lhs");

for ($h{k} || $h{l}) {}
ok(!exists $h{k},
  "|| does not propagate lvaluish cx to a subscript on its lhs");
ok(!exists $h{l},
  "|| does not propagate lvaluish cx to a subscript on its rhs");

my $aa, $bb, $cc;
$bb = 1;

my $res = 0;
# Well, really testing OP_DOR I guess
unless ($aa || $bb // $cc) {
	$res = 1;
}
is($res, 0, "res is 0 after mixed OR/DOR");

$res = 0;
unless ($aa // $bb || $cc) {
	$res = 1;
}
is($res, 0, "res is 0 after mixed DOR/OR");