aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/cond_spec.yue
blob: 9c7cac7e56959b2df35b6e0d9c2a04c7e39de6c8 (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
describe "cond", ->
	it "should execute if branch when condition is true", ->
		result = nil
		if true
			result = "yes"
		assert.same result, "yes"

	it "should execute else branch when condition is false", ->
		result = nil
		if false
			result = "yes"
		else
			result = "no"
		assert.same result, "no"

	it "should support elseif chain", ->
		value = 2
		result = switch value
			when 1 then "one"
			when 2 then "two"
			else "other"
		assert.same result, "two"

	it "should handle nested conditions", ->
		result = nil
		if true
			if true
				result = "nested"
		assert.same result, "nested"

	it "should work as expression", ->
		value = if true then "yes" else "no"
		assert.same value, "yes"

	it "should work in string interpolation", ->
		flag = true
		result = "value is #{if flag then 1 else 0}"
		assert.same result, "value is 1"

	it "should support chained comparisons", ->
		assert.is_true 1 < 2 <= 2 < 3

	it "should short-circuit and expression", ->
		count = 0
		inc = ->
			count += 1
			false
		result = inc! and inc!
		assert.same count, 1

	it "should short-circuit or expression", ->
		count = 0
		inc = ->
			count += 1
			true
		result = inc! or inc!
		assert.same count, 1

	it "should support unless keyword", ->
		result = nil
		unless false
			result = "executed"
		assert.same result, "executed"

	it "should support unless with else", ->
		result = nil
		unless true
			result = "no"
		else
			result = "yes"
		assert.same result, "yes"

	it "should handle postfix if", ->
		result = nil
		result = "yes" if true
		assert.same result, "yes"

	it "should handle postfix unless", ->
		result = nil
		result = "yes" unless false
		assert.same result, "yes"

	it "should evaluate truthiness correctly", ->
		-- nil and false are falsy
		assert.is_false if nil then true else false
		assert.is_false if false then true else false

		-- Everything else is truthy
		assert.is_true if 0 then true else false
		assert.is_true if "" then true else false
		assert.is_true if {} then true else false
		assert.is_true if 1 then true else false

	it "should support and/or operators", ->
		assert.same true and false, false
		assert.same false or true, true
		assert.same nil or "default", "default"
		assert.same "value" or "default", "value"

	it "should handle complex boolean expressions", ->
		a, b, c = true, false, true
		result = a and b or c
		assert.same result, c

	it "should support not operator", ->
		assert.is_true not false
		assert.is_true not nil
		assert.is_false not true
		assert.is_false not 1

	it "should work with table as condition", ->
		result = nil
		if {}
			result = "truthy"
		assert.same result, "truthy"

	it "should work with string as condition", ->
		result = nil
		if ""
			result = "truthy"
		assert.same result, "truthy"

	it "should work with zero as condition", ->
		result = nil
		if 0
			result = "truthy"
		assert.same result, "truthy"

	it "should support multiple elseif branches", ->
		value = 3
		result = if value == 1
			"one"
		elseif value == 2
			"two"
		elseif value == 3
			"three"
		else
			"other"
		assert.same result, "three"

	it "should handle then keyword syntax", ->
		result = if true then "yes" else "no"
		assert.same result, "yes"

	it "should work with function call in condition", ->
		return_true = -> true
		result = if return_true! then "yes" else "no"
		assert.same result, "yes"