diff options
Diffstat (limited to 'js/scripting-lang/tests/19_embedded_functions.txt')
-rw-r--r-- | js/scripting-lang/tests/19_embedded_functions.txt | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/js/scripting-lang/tests/19_embedded_functions.txt b/js/scripting-lang/tests/19_embedded_functions.txt new file mode 100644 index 0000000..a0e16aa --- /dev/null +++ b/js/scripting-lang/tests/19_embedded_functions.txt @@ -0,0 +1,101 @@ +/* Simple Unit Test: Embedded Functions in Tables */ + +/* ===== EMBEDDED ARROW FUNCTIONS ===== */ + +/* Table with simple arrow functions */ +calculator : { + add: x y -> x + y, + multiply: x y -> x * y, + double: x -> x * 2, + square: x -> x * x +}; + +/* Test embedded arrow function calls */ +add_result : calculator.add 5 3; +multiply_result : calculator.multiply 4 6; +double_result : calculator.double 7; +square_result : calculator.square 5; +..assert add_result = 8; +..assert multiply_result = 24; +..assert double_result = 14; +..assert square_result = 25; + +/* Table with more complex arrow functions */ +math_ops : { + increment: x -> x + 1, + decrement: x -> x - 1, + negate: x -> -x, + double: x -> x * 2 +}; + +/* Test complex arrow functions */ +inc_result : math_ops.increment 10; +dec_result : math_ops.decrement 10; +neg_result : math_ops.negate 5; +math_double : math_ops.double 7; +..assert inc_result = 11; +..assert dec_result = 9; +..assert neg_result = -5; +..assert math_double = 14; + +/* ===== EMBEDDED WHEN EXPRESSIONS ===== */ + +/* Table with embedded when expressions */ +classifier : { + classify: x -> when x is + 0 then "zero" + 1 then "one" + 2 then "two" + _ then "other" +}; + +/* Test embedded when expressions */ +zero_class : classifier.classify 0; +one_class : classifier.classify 1; +two_class : classifier.classify 2; +other_class : classifier.classify 42; +..assert zero_class = "zero"; +..assert one_class = "one"; +..assert two_class = "two"; +..assert other_class = "other"; + +/* ===== MIXED CONTENT TABLES ===== */ + +/* Table with mixed data and functions */ +person : { + name: "Alice", + age: 30, + city: "NYC", + greet: name -> "Hello, " + name +}; + +/* Test mixed table access */ +name : person.name; +age : person.age; +greeting : person.greet "Bob"; +..assert name = "Alice"; +..assert age = 30; +..assert greeting = "Hello, Bob"; + +/* ===== EDGE CASES ===== */ + +/* Table with empty function */ +empty_func : { + noop: x -> x +}; + +/* Test empty function */ +noop_result : empty_func.noop 42; +..assert noop_result = 42; + +/* Table with function that returns table */ +table_returner : { + create_person: name age -> {name: name, age: age} +}; + +/* Test function that returns table */ +new_person : table_returner.create_person "Bob" 25; +..assert new_person.name = "Bob"; +..assert new_person.age = 25; + +..out "Simple embedded functions test completed successfully"; \ No newline at end of file |