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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
switch 10
case 5 then ok 0
case 'a'
true
false
ok 0
case 10 then ok 1
#! Mid-switch comment with whitespace
#! and multi line
case 11 then ok 0
default ok 0
func = (num) ->
switch num
case 2, 4, 6
Boolean true
case [1, 3, 5]
Boolean false
default
eq func(2), true
eq func(6), true
eq func(3), false
eq func(8), void
# One-liner
eq void, switch case 1 then break
eq 1 , switch case 0 then break default 1
eq 2 , switch case 1 then (while 0 then continue); 2
eq 3 , do -> switch 0 case 1 then -> 2 default 3
eq 4 , if 1 then switch 2 case 3 then default 4
compileThrows 'inconvertible statement' 3 '''
for ever
!switch
continue
'''
ok switch \words
case (<[ nonbare words ]>) then false
case <[ bare words ]>
switch Function::toString
case ok<[valueOf toString]> then true
, '`case` expands bare arrays'
# Sans-topic
eq ok, switch
case null then 0
case !1 then 1
case '' not of {''} then 2
case [] not instanceof Array then 3
case true is false then 4
case 'x' < 'y' > 'z' then 5
case 'a' in <[ b c ]> then 6
case 'd' in (<[ e f ]>) then 7
default ok
eq '''
var that;
switch (false) {
case !1:
return;
case !2:
throw me;
case !3:
break;
case !4:
// fallthrough
case !(that = 5):
that;
break;
case !(6 || 7 || 8):
break;
case !void 8:
break;
default:
9;
}
''', LiveScript.compile '''
switch
case 1 then return
case 2 then throw me
case 3 then break
case 4 then fallthrough
case 5 then that
case 6 [7 8] then
case[] then
default 9
''', {+bare,-header}
# `that`
eq 1, switch 1 case 1 then that
while 1
eq 3, switch 3 case 3 then that
break
switch case [0, 2, 4,] then eq 2, that
# `that` with default - #508
switch 10 | _ => that
# Sans-condition
switch
ok 1 'caseless switch is allowed'
break if true
ok 0 'for early breaking'
# case |
switch
| false then ok 0 | false then ok 0
| false
ok 0
| true
ok 1
| true then ok 0
# then =>
switch
| false => ok 0
| false =>
ok 0
| true => ok 1
| true => ok 0
# otherwise, _
eq otherwise?, false
switch
| false => ok 0
| otherwise => ok 1
switch 2 + 3
case 6 then ok 0
case _ then ok 1
switch
| false => ok 0
| _ => ok 1
switch 2 + 3
case 6 then ok 0
case otherwise then ok 1
# implicit switches
boom1 = ->
case false => 1
case otherwise => 2
3
eq 3 boom1!
do ->
| false => ok 0
| true => ok 1
do ~>
| false => ok 0
| true => ok 1
boom2 = ->
| false => 1
| otherwise => 2
3
eq 3 boom2!
# when
switch
when false then ok 0
when true then ok 1
# else
switch
| false => ok 0
else ok 1
#### match
x = 2
match x
| (== 3) => ok 0
| (== 2) => ok 1
| _ => ok 0
match ++x
| (== 4) => ok 0
| (== 3) or (==8) => ok 1
| _ => ok 0
false-func = -> false
true-func = -> true
# no subject
match
| false-func => ok 0
| true-func => ok 1
| otherwise => ok 0
# multiple topics
even = --> it % 2 == 0
odd = (not) . even
x = 1
y = 2
match x, y
| odd, odd => ok 0
| even, even => ok 0
| odd, even => ok 1
| otherwise => ok 0
# literals
x = 5
y = \moo
z = true
match x, y, z
| 5, \moo, false => ok 0
| 4, \moo, true => ok 0
| 5, \moo, true => ok 1
| otherwise => ok 0
x = [1 2 3]
y = 'haha'
z = {+foo, moo: 2, g: {hi: \?}}
match x
| [2 4 6] => ok 0
| [1 2 3 4] => ok 0
| [1 2 _] => ok 1
| otherwise => ok 0
match z
| {-foo, goo: 23, g: {hi: \?}} => ok 0
| {+foo, moo: 2, g: _} => ok 1
| otherwise => ok 0
match x, y, z
| [1 2 3], /^ha/g, {foo: true, moo: 2, g: {hi: \!}} => ok 0
| [1 2 3], /^ha/g, {foo: true, moo: 2, g: {hi: \?}} => ok 1
| otherwise => ok 0
match 2
| even and 2 => ok 1
| otherwise => ok 0
match 3, \haha
| _, 'muhaha' => ok 0
| even, _ => ok 0
| _, 'haha' => ok 1
| _ => ok 0
take = (n, [x, ...xs]:list) ->
match n, list
| (<= 0), _ => []
| _ , [] => []
| otherwise => [x] ++ take n - 1, xs
eq '1,2,3' "#{ take 3, [1 to 10] }"
x = -2
match x
| -2 => ok 1
| _ => ok 0
match 1, 3, 3
| 1, 1, 2 or 3 => ok 0
| 1, 2 or 3, 3 => ok 1
| _ => ok 0
# [LiveScript#1025](https://github.com/gkz/LiveScript/issues/1025)
# Expressions starting from `_` were treated as placeholders in `match` cases
_ = -> -> false
_.isString = -> typeof it == \string
match 1
| _.isString => ok 0
| (_.isString) => ok 0
| _! => ok 0
match \foo
| _.isString => ok 1
# other examples of Chains with tails
match \foo
| 'barbaz'~starts-with => ok 0
| 'foobar'~starts-with => ok 1
| otherwise => ok 0
# [LiveScript#926](https://github.com/gkz/LiveScript/issues/926)
# `match` wasn't binding `this` correctly in expression position
o =
bar: 42
run: ->
foo:
match @bar
| (== 42) => true
ok o.run!foo
# [gkz/LiveScript#931](https://github.com/gkz/LiveScript/issues/931)
# Let's not produce unreachable `break` statements
test-cases =
'''
foo = switch bar
| \\a =>
if baz then 1 else 2
| \\b => 3
'''
'''
foo = switch bar
| \\a =>
switch baz
| \\1 => 1
| otherwise => 2
| \\b => 3
'''
'''
switch foo
| \\a =>
if bar
return 1
else throw new Error
| \\b => baz!
'''
for test-cases
compiled = LiveScript.compile .., {+bare,-header}
eq -1 compiled.index-of(\break), "no break in:\n#compiled"
|