summaryrefslogtreecommitdiff
path: root/t/op/given.t
blob: 1187171fc34b4ad192b735545dd058356b62eb44 (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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!./perl

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
}

use strict;
use warnings;
no warnings 'experimental::smartmatch';

plan tests => 55;

CORE::given(3) {
    pass "CORE::given without feature flag";
}

use feature 'switch';

CORE::given(3) {
    pass "CORE::given with feature flag";
}

given(3) {
    pass "given with feature flag";
}

{
    my $x = "foo";
    is($x, "foo", "given lexical scope not started yet");
    given(my $x = "bar") {
	is($x, "bar", "given lexical scope starts");
    }
    is($x, "foo", "given lexical scope ends");
}

sub topic_is ($@) { is $_, shift, @_ }
{
    local $_ = "foo";
    is $_, "foo", "given dynamic scope not started yet";
    topic_is "foo", "given dynamic scope not started yet";
    given("bar") {
	is $_, "bar", "given dynamic scope starts";
	topic_is "bar", "given dynamic scope starts";
    }
    is $_, "foo", "given dynamic scope ends";
    topic_is "foo", "given dynamic scope ends";
}

given(undef) {
    is $_, undef, "folded undef topic value";
    is \$_, \undef, "folded undef topic identity";
}
given(1 < 3) {
    is $_, !!1, "folded true topic value";
    is \$_, \!!1, "folded true topic identity";
}
given(1 > 3) {
    is $_, !!0, "folded false topic value";
    is \$_, \!!0, "folded false topic identity";
}
my $one = 1;
given($one && undef) {
    is $_, undef, "computed undef topic value";
    is \$_, \undef, "computed undef topic identity";
}
given($one < 3) {
    is $_, !!1, "computed true topic value";
    is \$_, \!!1, "computed true topic identity";
}
given($one > 3) {
    is $_, !!0, "computed false topic value";
    is \$_, \!!0, "computed false topic identity";
}

sub which_context {
    return wantarray ? "list" : defined(wantarray) ? "scalar" : "void";
}
given(which_context) {
    is $_, "scalar", "topic sub called without parens";
}
given(which_context()) {
    is $_, "scalar", "topic sub called with parens";
}

my $ps = "foo";
given($ps) {
    is $_, "foo", "padsv topic value";
    is \$_, \$ps, "padsv topic identity";
}
our $gs = "bar";
given($gs) {
    is $_, "bar", "gvsv topic value";
    is \$_, \$gs, "gvsv topic identity";
}
my @pa = qw(a b c d e);
given(@pa) {
    is $_, 5, "padav topic";
}
our @ga = qw(x y z);
given(@ga) {
    is $_, 3, "gvav topic";
}
my %ph = qw(a b c d e f g h i j);
given(%ph) {
    is $_, 5, "padhv topic";
}
our %gh = qw(u v w x y z);
given(%gh) {
    is $_, 3, "gvhv topic";
}

given($one + 3) {
    is $_, 4, "general computed topic";
}

is join(",", 111, 222,
    do {
	no warnings "void";
	given($one, 22, $one, 33) {
	    is $_, 33, "list topic";
	    (1111, 2222);
	}
    },
    333, 444,
), "111,222,1111,2222,333,444", "stack discipline";

given(()) {
    is $_, undef, "stub topic value";
    is \$_, \undef, "stub topic identity";
}

foreach my $test (
    [ "no", "[aA][bB][cB][dA]" ],
    [ "last", "[aA][bB][dA]" ],
    [ "next", "[aA][bB][dA]" ],
    [ "redo", "[aA][bB][bB][cB][dA]" ],
) {
    my($loopex, $expect_act) = @$test;
    my $act = "";
    my $i = 0;
    {
	local $_ = "A";
	$act .= "[a$_]";
	given("B") {
	    $act .= "[b$_]";
	    $i++;
	    if($i < 2) {
		if($loopex eq "last") {
		    last;
		} elsif($loopex eq "next") {
		    next;
		} elsif($loopex eq "redo") {
		    redo;
		}
	    }
	    $act .= "[c$_]";
	}
	$act .= "[d$_]";
    }
    is $act, $expect_act, "given unlabelled $loopex loop exit";
    $act = "";
    $i = 0;
    {
	local $_ = "A";
	$act .= "[a$_]";
	G: given("B") {
	    $act .= "[b$_]";
	    {
		$i++;
		if($i < 2) {
		    if($loopex eq "last") {
			last G;
		    } elsif($loopex eq "next") {
			next G;
		    } elsif($loopex eq "redo") {
			redo G;
		    }
		}
	    }
	    $act .= "[c$_]";
	}
	$act .= "[d$_]";
    }
    is $act, $expect_act, "given labelled $loopex loop exit";
    $act = "";
    $i = 0;
    {
	local $_ = "A";
	$act .= "[a$_]";
	given("B") {
	    $act .= "[b$_]";
	    {
		$i++;
		if($i < 2) {
		    if($loopex eq "last") {
			last;
		    } elsif($loopex eq "next") {
			next;
		    } elsif($loopex eq "redo") {
			redo;
		    }
		}
	    }
	    $act .= "[c$_]";
	}
	$act .= "[d$_]";
    }
    is $act, "[aA][bB][cB][dA]", "interior $loopex loop exit";
    $act = "";
    $i = 0;
    {
	local $_ = "A";
	$act .= "[a$_]";
	B: {
	    local $_ = "B";
	    $act .= "[b$_]";
	    given("C") {
		$i++;
		if($i < 2) {
		    if($loopex eq "last") {
			last B;
		    } elsif($loopex eq "next") {
			next B;
		    } elsif($loopex eq "redo") {
			redo B;
		    }
		}
	    }
	    $act .= "[c$_]";
	}
	$act .= "[d$_]";
    }
    is $act, $expect_act, "exterior $loopex loop exit";
}

1;