aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/if_assignment_spec.yue
blob: 1ce028ebeea4995c5da60849715ac97a8f917d37 (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
describe "if assignment", ->
	it "should assign and check truthy value", ->
		obj = find_user: (name) -> name == "valid" and {name: name} or nil
		if user := obj\find_user "valid"
			assert.same user.name, "valid"

	it "should not enter block when nil", ->
		obj = find_user: -> nil
		if user := obj\find_user!
			assert.is_true false -- should not reach
		else
			assert.is_true true

	it "should work with elseif", ->
		get_value = (key) ->
			switch key
				when "a" then 1
				when "b" then 2
				else nil

		result = nil
		if val := get_value "c"
			result = "c: #{val}"
		elseif val := get_value "b"
			result = "b: #{val}"
		else
			result = "no match"
		assert.same result, "b: 2"

	it "should scope variable to if block", ->
		if x := 10
			assert.same x, 10
		-- x should not be accessible here
		assert.is_true true

	it "should work with multiple return values", ->
		fn = -> true, "success"
		if success, result := fn!
			assert.is_true success
			assert.same result, "success"

	it "should work with table destructuring", ->
		get_point = -> {x: 10, y: 20}
		if {:x, :y} := get_point!
			assert.same x, 10
			assert.same y, 20

	it "should work with array destructuring", ->
		get_coords = -> [1, 2, 3]
		if [a, b, c] := get_coords!
			assert.same a, 1
			assert.same b, 2
			assert.same c, 3

	it "should chain multiple assignments", ->
		if a := 1
			if b := a + 1
				assert.same b, 2

	it "should work in expression context", ->
		get_value = (x) -> if x > 0 then x else nil
		result = if val := get_value 5
			val * 2
		else
			0
		assert.same result, 10

	it "should work with os.getenv", ->
		-- test with environment variable
		if path := os.getenv "PATH"
			assert.is_true type(path) == "string"
		else
			assert.is_true true

	it "should support table access", ->
		tb = {key: "value"}
		if val := tb.key
			assert.same val, "value"

	it "should work with function call results", ->
		fn = -> "result"
		if s := fn!
			assert.same s, "result"

	it "should handle false values", ->
		if val := false
			assert.is_true false -- should not enter
		else
			assert.is_true true