aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test
diff options
context:
space:
mode:
Diffstat (limited to 'spec/inputs/test')
-rw-r--r--spec/inputs/test/anonymous_class_spec.yue27
-rw-r--r--spec/inputs/test/class_expression_spec.yue27
-rw-r--r--spec/inputs/test/constructor_promotion_spec.yue26
-rw-r--r--spec/inputs/test/continue_spec.yue37
-rw-r--r--spec/inputs/test/export_import_interactions_spec.yue19
-rw-r--r--spec/inputs/test/format_spec.yue7
-rw-r--r--spec/inputs/test/string_interpolation_spec.yue34
-rw-r--r--spec/inputs/test/using_spec.yue47
8 files changed, 224 insertions, 0 deletions
diff --git a/spec/inputs/test/anonymous_class_spec.yue b/spec/inputs/test/anonymous_class_spec.yue
new file mode 100644
index 0000000..72854ee
--- /dev/null
+++ b/spec/inputs/test/anonymous_class_spec.yue
@@ -0,0 +1,27 @@
1describe "anonymous class", ->
2 it "should create anonymous class", ->
3 AnonymousClass = class
4 value: 100
5 getValue: => @value
6
7 instance = AnonymousClass!
8 assert.same instance\getValue!, 100
9
10 it "should use assigned name", ->
11 MyClass = class
12 value: 50
13
14 instance = MyClass!
15 assert.is_true MyClass.__name == "MyClass"
16 assert.same instance.value, 50
17
18 it "should support anonymous subclass", ->
19 Base = class
20 baseMethod: => "base"
21
22 Sub = class extends Base
23 subMethod: => "sub"
24
25 instance = Sub!
26 assert.same instance\baseMethod!, "base"
27 assert.same instance\subMethod!, "sub"
diff --git a/spec/inputs/test/class_expression_spec.yue b/spec/inputs/test/class_expression_spec.yue
new file mode 100644
index 0000000..13c1d23
--- /dev/null
+++ b/spec/inputs/test/class_expression_spec.yue
@@ -0,0 +1,27 @@
1describe "class expression", ->
2 it "should support class expression assignment", ->
3 MyClass = class
4 value: 100
5
6 assert.same MyClass.value, 100
7
8 it "should support class expression in table", ->
9 classes = {
10 Alpha: class
11 new: => @value = 1
12 Beta: class
13 new: => @value = 2
14 }
15
16 a = classes.Alpha!
17 b = classes.Beta!
18 assert.same a.value, 1
19 assert.same b.value, 2
20
21 it "should work with return", ->
22 fn = ->
23 return class
24 value: 50
25
26 Instance = fn!
27 assert.same Instance!.value, 50
diff --git a/spec/inputs/test/constructor_promotion_spec.yue b/spec/inputs/test/constructor_promotion_spec.yue
new file mode 100644
index 0000000..83c9d15
--- /dev/null
+++ b/spec/inputs/test/constructor_promotion_spec.yue
@@ -0,0 +1,26 @@
1describe "constructor promotion", ->
2 it "should promote simple arguments to assignment", ->
3 class Thing
4 new: (@name, @age) =>
5
6 instance = Thing "Alice", 30
7 assert.same instance.name, "Alice"
8 assert.same instance.age, 30
9
10 it "should promote multiple arguments", ->
11 class Point
12 new: (@x, @y, @z) =>
13
14 p = Point 1, 2, 3
15 assert.same p.x, 1
16 assert.same p.y, 2
17 assert.same p.z, 3
18
19 it "should work with multiple parameters", ->
20 class Container
21 new: (@a, @b, @c) =>
22
23 c = Container!
24 assert.same c.a, nil
25 assert.same c.b, nil
26 assert.same c.c, nil
diff --git a/spec/inputs/test/continue_spec.yue b/spec/inputs/test/continue_spec.yue
new file mode 100644
index 0000000..bf4e839
--- /dev/null
+++ b/spec/inputs/test/continue_spec.yue
@@ -0,0 +1,37 @@
1describe "continue statement", ->
2 it "should skip odd numbers in for loop", ->
3 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
4 even = for n in *numbers
5 continue if n % 2 == 1
6 n
7
8 assert.same even, {2, 4, 6, 8, 10}
9
10 it "should filter values in while loop", ->
11 i = 0
12 result = []
13
14 while i < 10
15 i += 1
16 continue if i % 3 == 0
17 table.insert result, i
18
19 assert.same result, {1, 2, 4, 5, 7, 8, 10}
20
21 it "should skip with condition in loop expression", ->
22 items = [1, 2, 3, 4, 5]
23 odds = for item in *items
24 continue if item % 2 == 0
25 item
26
27 assert.same odds, {1, 3, 5}
28
29 it "should work with nested loops", ->
30 result = []
31
32 for i = 1, 5
33 for j = 1, 5
34 continue if i == j
35 table.insert result, {i, j}
36
37 assert.same #result, 20
diff --git a/spec/inputs/test/export_import_interactions_spec.yue b/spec/inputs/test/export_import_interactions_spec.yue
new file mode 100644
index 0000000..8281246
--- /dev/null
+++ b/spec/inputs/test/export_import_interactions_spec.yue
@@ -0,0 +1,19 @@
1describe "export import interactions", ->
2 it "should import with alias and destructuring", ->
3 source = {
4 origin: {x: 10, y: 20}
5 target: "result"
6 point: {x: 5, y: 15}
7 }
8 import x, y from source.origin
9 import target from source
10 px, py = do
11 local x, y
12 import x, y from source.point
13 x, y
14
15 assert.same x, 10
16 assert.same y, 20
17 assert.same target, "result"
18 assert.same px, 5
19 assert.same py, 15
diff --git a/spec/inputs/test/format_spec.yue b/spec/inputs/test/format_spec.yue
index 4da3f7e..2069795 100644
--- a/spec/inputs/test/format_spec.yue
+++ b/spec/inputs/test/format_spec.yue
@@ -55,11 +55,13 @@ files = [
55 "spec/inputs/syntax.yue" 55 "spec/inputs/syntax.yue"
56 "spec/inputs/global.yue" 56 "spec/inputs/global.yue"
57 "spec/inputs/plus.yue" 57 "spec/inputs/plus.yue"
58 "spec/inputs/test/string_interpolation_spec.yue"
58 "spec/inputs/test/with_spec.yue" 59 "spec/inputs/test/with_spec.yue"
59 "spec/inputs/test/try_catch_spec.yue" 60 "spec/inputs/test/try_catch_spec.yue"
60 "spec/inputs/test/operator_advanced_spec.yue" 61 "spec/inputs/test/operator_advanced_spec.yue"
61 "spec/inputs/test/with_statement_spec.yue" 62 "spec/inputs/test/with_statement_spec.yue"
62 "spec/inputs/test/literals_spec.yue" 63 "spec/inputs/test/literals_spec.yue"
64 "spec/inputs/test/continue_spec.yue"
63 "spec/inputs/test/varargs_assignment_spec.yue" 65 "spec/inputs/test/varargs_assignment_spec.yue"
64 "spec/inputs/test/advanced_macro_spec.yue" 66 "spec/inputs/test/advanced_macro_spec.yue"
65 "spec/inputs/test/pipe_spec.yue" 67 "spec/inputs/test/pipe_spec.yue"
@@ -77,11 +79,13 @@ files = [
77 "spec/inputs/test/operators_spec.yue" 79 "spec/inputs/test/operators_spec.yue"
78 "spec/inputs/test/comprehension_spec.yue" 80 "spec/inputs/test/comprehension_spec.yue"
79 "spec/inputs/test/attrib_spec.yue" 81 "spec/inputs/test/attrib_spec.yue"
82 "spec/inputs/test/using_spec.yue"
80 "spec/inputs/test/nil_coalescing_spec.yue" 83 "spec/inputs/test/nil_coalescing_spec.yue"
81 "spec/inputs/test/table_comprehension_spec.yue" 84 "spec/inputs/test/table_comprehension_spec.yue"
82 "spec/inputs/test/slicing_spec.yue" 85 "spec/inputs/test/slicing_spec.yue"
83 "spec/inputs/test/close_attribute_spec.yue" 86 "spec/inputs/test/close_attribute_spec.yue"
84 "spec/inputs/test/named_varargs_spec.yue" 87 "spec/inputs/test/named_varargs_spec.yue"
88 "spec/inputs/test/export_import_interactions_spec.yue"
85 "spec/inputs/test/table_spreading_spec.yue" 89 "spec/inputs/test/table_spreading_spec.yue"
86 "spec/inputs/test/macro_spec.yue" 90 "spec/inputs/test/macro_spec.yue"
87 "spec/inputs/test/chaining_comparison_spec.yue" 91 "spec/inputs/test/chaining_comparison_spec.yue"
@@ -89,10 +93,13 @@ files = [
89 "spec/inputs/test/destructure_spec.yue" 93 "spec/inputs/test/destructure_spec.yue"
90 "spec/inputs/test/vararg_spec.yue" 94 "spec/inputs/test/vararg_spec.yue"
91 "spec/inputs/test/string_spec.yue" 95 "spec/inputs/test/string_spec.yue"
96 "spec/inputs/test/anonymous_class_spec.yue"
92 "spec/inputs/test/implicit_object_spec.yue" 97 "spec/inputs/test/implicit_object_spec.yue"
98 "spec/inputs/test/constructor_promotion_spec.yue"
93 "spec/inputs/test/backcall_spec.yue" 99 "spec/inputs/test/backcall_spec.yue"
94 "spec/inputs/test/while_assignment_spec.yue" 100 "spec/inputs/test/while_assignment_spec.yue"
95 "spec/inputs/test/switch_spec.yue" 101 "spec/inputs/test/switch_spec.yue"
102 "spec/inputs/test/class_expression_spec.yue"
96 "spec/inputs/test/functions_advanced_spec.yue" 103 "spec/inputs/test/functions_advanced_spec.yue"
97 "spec/inputs/test/config_spec.yue" 104 "spec/inputs/test/config_spec.yue"
98 "spec/inputs/test/yaml_string_spec.yue" 105 "spec/inputs/test/yaml_string_spec.yue"
diff --git a/spec/inputs/test/string_interpolation_spec.yue b/spec/inputs/test/string_interpolation_spec.yue
new file mode 100644
index 0000000..02b1606
--- /dev/null
+++ b/spec/inputs/test/string_interpolation_spec.yue
@@ -0,0 +1,34 @@
1describe "string interpolation", ->
2 it "should interpolate in double quotes", ->
3 name = "World"
4 result = "Hello #{name}!"
5 assert.same result, "Hello World!"
6
7 it "should interpolate numbers", ->
8 a, b = 10, 20
9 result = "#{a} + #{b} = #{a + b}"
10 assert.same result, "10 + 20 = 30"
11
12 it "should interpolate expressions", ->
13 x = 5
14 result = "x * 2 = #{x * 2}"
15 assert.same result, "x * 2 = 10"
16
17 it "should interpolate function calls", ->
18 result = "result: #{math.floor 5.5}"
19 assert.same result, "result: 5"
20
21 it "should interpolate in string literals", ->
22 x = 100
23 result = "Value: #{x}"
24 assert.same result, "Value: 100"
25
26 it "should work with nested interpolation", ->
27 inner = "inner"
28 result = "Outer: #{inner}"
29 assert.same result, "Outer: inner"
30
31 it "should not interpolate in single quotes", ->
32 name = "World"
33 result = 'Hello #{name}!'
34 assert.same result, 'Hello #{name}!'
diff --git a/spec/inputs/test/using_spec.yue b/spec/inputs/test/using_spec.yue
new file mode 100644
index 0000000..e9ad749
--- /dev/null
+++ b/spec/inputs/test/using_spec.yue
@@ -0,0 +1,47 @@
1describe "using", ->
2 it "should prevent variable shadowing in assignment", ->
3 tmp = 100
4 i, k = 100, 50
5
6 process = (add using k, i) ->
7 tmp = tmp + add
8 i += tmp
9 k += tmp
10
11 process 22
12 assert.same i, 222
13 assert.same k, 172
14 assert.same tmp, 100
15
16 it "should handle multiple variable names", ->
17 a, b, c = 1, 2, 3
18
19 process = (sum using a, b) ->
20 a += 1
21 b += 2
22 c = sum + 100
23
24 process 10
25 assert.same a, 2
26 assert.same b, 4
27 assert.same c, 3
28
29 it "should work with nil value", ->
30 local x = 1
31
32 fn = (val using x) ->
33 if val ~= nil
34 x = val
35
36 fn 100
37 assert.same x, 100
38 assert.is_true x ~= 1
39
40 it "should work with function calls", ->
41 local count = 0
42
43 fn = (n using count) ->
44 count += n
45
46 fn 5
47 assert.same count, 5