diff options
Diffstat (limited to 'spec/inputs/switch.yue')
-rw-r--r-- | spec/inputs/switch.yue | 86 |
1 files changed, 85 insertions, 1 deletions
diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue index ac3dbea..36f9be6 100644 --- a/spec/inputs/switch.yue +++ b/spec/inputs/switch.yue | |||
@@ -58,7 +58,91 @@ switch hi | |||
58 | 58 | ||
59 | switch hi | 59 | switch hi |
60 | when 3+1, hello!, (-> 4)! | 60 | when 3+1, hello!, (-> 4)! |
61 | yello | 61 | _ = yello |
62 | else | 62 | else |
63 | print "cool" | 63 | print "cool" |
64 | 64 | ||
65 | do | ||
66 | dict = { | ||
67 | {} | ||
68 | {1, 2, 3} | ||
69 | a: b: c: 1 | ||
70 | x: y: z: 1 | ||
71 | } | ||
72 | |||
73 | switch dict | ||
74 | when { | ||
75 | first | ||
76 | {one, two, three} | ||
77 | a: b: :c | ||
78 | x: y: :z | ||
79 | } | ||
80 | print first, one, two, three, c, z | ||
81 | |||
82 | do | ||
83 | items = | ||
84 | * x: 100 | ||
85 | y: 200 | ||
86 | * width: 300 | ||
87 | height: 400 | ||
88 | * false | ||
89 | |||
90 | for item in *items | ||
91 | switch item | ||
92 | when :x, :y | ||
93 | print "Vec2 #{x}, #{y}" | ||
94 | when :width, :height | ||
95 | print "Size #{width}, #{height}" | ||
96 | when false | ||
97 | print "None" | ||
98 | when __class: cls | ||
99 | switch cls | ||
100 | when ClassA | ||
101 | print "Object A" | ||
102 | when ClassB | ||
103 | print "Object B" | ||
104 | when #: mt | ||
105 | print "A table with metatable" | ||
106 | else | ||
107 | print "item not accepted!" | ||
108 | |||
109 | do | ||
110 | tb = {} | ||
111 | switch tb | ||
112 | when {:a = 1, :b = 2} | ||
113 | print a, b | ||
114 | |||
115 | do | ||
116 | tb = x: "abc" | ||
117 | switch tb | ||
118 | when :x, :y | ||
119 | print "x: #{x} with y: #{y}" | ||
120 | when :x | ||
121 | print "x: #{x} only" | ||
122 | |||
123 | do | ||
124 | matched = switch tb | ||
125 | when 1 | ||
126 | "1" | ||
127 | when :x | ||
128 | x | ||
129 | when false | ||
130 | "false" | ||
131 | else | ||
132 | nil | ||
133 | |||
134 | do | ||
135 | return switch tb | ||
136 | when nil | ||
137 | "invalid" | ||
138 | when :a, :b | ||
139 | "#{a + b}" | ||
140 | when 1, 2, 3, 4, 5 | ||
141 | "number 1 - 5" | ||
142 | when {:alwaysMatch = "fallback"} | ||
143 | alwaysMatch | ||
144 | else | ||
145 | "should not reach here" | ||
146 | |||
147 | nil | ||
148 | |||