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
|
The logical AND and logical OR assignment operators set the left hand side
variable or value to the right hand side expression result depending on
whether the lhs value is truish.
1. The `&&=` operator overwrites the lhs variable or field with the rhs
expression result if the lhs is truish.
-- Expect stdout --
[
null,
false,
"is truish",
null,
false,
"is truish"
]
-- End --
-- Testcase --
{%
x = null;
y = false;
z = true;
o = {
a: null,
b: false,
c: true
};
res = [];
push(res, x &&= "is truish");
push(res, y &&= "is truish");
push(res, z &&= "is truish");
push(res, o.a &&= "is truish");
push(res, o.b &&= "is truish");
push(res, o.c &&= "is truish");
printf("%.J\n", res);
%}
-- End --
2. The `||=` operator overwrites the lhs variable or field with the rhs
expression result if the lhs is falsy.
-- Expect stdout --
[
"is falsy",
"is falsy",
true,
"is falsy",
"is falsy",
true
]
-- End --
-- Testcase --
{%
x = null;
y = false;
z = true;
o = {
a: null,
b: false,
c: true
};
res = [];
push(res, x ||= "is falsy");
push(res, y ||= "is falsy");
push(res, z ||= "is falsy");
push(res, o.a ||= "is falsy");
push(res, o.b ||= "is falsy");
push(res, o.c ||= "is falsy");
printf("%.J\n", res);
%}
-- End --
3. Ensure that the assignment value expression is not evaluated if the
assignment condition is false.
-- Expect stdout --
[
false,
false,
true,
false,
false,
true,
0,
0,
0,
0,
0,
0
]
-- End --
-- Testcase --
{%
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
o = {
a: false,
b: false,
c: true
};
x = false;
y = false;
z = true;
res = [];
push(res, x ??= a++);
push(res, y &&= b++);
push(res, z ||= c++);
push(res, o.a ??= d++);
push(res, o.b &&= e++);
push(res, o.c ||= f++);
printf("%.J\n", [ ...res, a, b, c, d, e, f ]);
%}
-- End --
|