diff options
Diffstat (limited to 'spec/inputs/test/if_assignment_spec.yue')
| -rw-r--r-- | spec/inputs/test/if_assignment_spec.yue | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/spec/inputs/test/if_assignment_spec.yue b/spec/inputs/test/if_assignment_spec.yue new file mode 100644 index 0000000..1ce028e --- /dev/null +++ b/spec/inputs/test/if_assignment_spec.yue | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | describe "if assignment", -> | ||
| 2 | it "should assign and check truthy value", -> | ||
| 3 | obj = find_user: (name) -> name == "valid" and {name: name} or nil | ||
| 4 | if user := obj\find_user "valid" | ||
| 5 | assert.same user.name, "valid" | ||
| 6 | |||
| 7 | it "should not enter block when nil", -> | ||
| 8 | obj = find_user: -> nil | ||
| 9 | if user := obj\find_user! | ||
| 10 | assert.is_true false -- should not reach | ||
| 11 | else | ||
| 12 | assert.is_true true | ||
| 13 | |||
| 14 | it "should work with elseif", -> | ||
| 15 | get_value = (key) -> | ||
| 16 | switch key | ||
| 17 | when "a" then 1 | ||
| 18 | when "b" then 2 | ||
| 19 | else nil | ||
| 20 | |||
| 21 | result = nil | ||
| 22 | if val := get_value "c" | ||
| 23 | result = "c: #{val}" | ||
| 24 | elseif val := get_value "b" | ||
| 25 | result = "b: #{val}" | ||
| 26 | else | ||
| 27 | result = "no match" | ||
| 28 | assert.same result, "b: 2" | ||
| 29 | |||
| 30 | it "should scope variable to if block", -> | ||
| 31 | if x := 10 | ||
| 32 | assert.same x, 10 | ||
| 33 | -- x should not be accessible here | ||
| 34 | assert.is_true true | ||
| 35 | |||
| 36 | it "should work with multiple return values", -> | ||
| 37 | fn = -> true, "success" | ||
| 38 | if success, result := fn! | ||
| 39 | assert.is_true success | ||
| 40 | assert.same result, "success" | ||
| 41 | |||
| 42 | it "should work with table destructuring", -> | ||
| 43 | get_point = -> {x: 10, y: 20} | ||
| 44 | if {:x, :y} := get_point! | ||
| 45 | assert.same x, 10 | ||
| 46 | assert.same y, 20 | ||
| 47 | |||
| 48 | it "should work with array destructuring", -> | ||
| 49 | get_coords = -> [1, 2, 3] | ||
| 50 | if [a, b, c] := get_coords! | ||
| 51 | assert.same a, 1 | ||
| 52 | assert.same b, 2 | ||
| 53 | assert.same c, 3 | ||
| 54 | |||
| 55 | it "should chain multiple assignments", -> | ||
| 56 | if a := 1 | ||
| 57 | if b := a + 1 | ||
| 58 | assert.same b, 2 | ||
| 59 | |||
| 60 | it "should work in expression context", -> | ||
| 61 | get_value = (x) -> if x > 0 then x else nil | ||
| 62 | result = if val := get_value 5 | ||
| 63 | val * 2 | ||
| 64 | else | ||
| 65 | 0 | ||
| 66 | assert.same result, 10 | ||
| 67 | |||
| 68 | it "should work with os.getenv", -> | ||
| 69 | -- test with environment variable | ||
| 70 | if path := os.getenv "PATH" | ||
| 71 | assert.is_true type(path) == "string" | ||
| 72 | else | ||
| 73 | assert.is_true true | ||
| 74 | |||
| 75 | it "should support table access", -> | ||
| 76 | tb = {key: "value"} | ||
| 77 | if val := tb.key | ||
| 78 | assert.same val, "value" | ||
| 79 | |||
| 80 | it "should work with function call results", -> | ||
| 81 | fn = -> "result" | ||
| 82 | if s := fn! | ||
| 83 | assert.same s, "result" | ||
| 84 | |||
| 85 | it "should handle false values", -> | ||
| 86 | if val := false | ||
| 87 | assert.is_true false -- should not enter | ||
| 88 | else | ||
| 89 | assert.is_true true | ||
