about summary refs log tree commit diff stats
path: root/js/scripting-lang/tests/17_table_enhancements.txt
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/tests/17_table_enhancements.txt')
-rw-r--r--js/scripting-lang/tests/17_table_enhancements.txt234
1 files changed, 234 insertions, 0 deletions
diff --git a/js/scripting-lang/tests/17_table_enhancements.txt b/js/scripting-lang/tests/17_table_enhancements.txt
new file mode 100644
index 0000000..d935153
--- /dev/null
+++ b/js/scripting-lang/tests/17_table_enhancements.txt
@@ -0,0 +1,234 @@
+/* Unit Test: Table Enhancements */
+/* Tests: Enhanced combinators, t namespace, each combinator, embedded functions */
+
+/* ===== ENHANCED COMBINATORS ===== */
+
+/* Enhanced map with tables */
+numbers : {1, 2, 3, 4, 5};
+double : x -> x * 2;
+
+/* Test map with single table */
+doubled : map @double numbers;
+/* Note: Using dot notation for array-like tables */
+first : doubled[1];
+second : doubled[2];
+third : doubled[3];
+fourth : doubled[4];
+fifth : doubled[5];
+..assert first = 2;
+..assert second = 4;
+..assert third = 6;
+..assert fourth = 8;
+..assert fifth = 10;
+
+/* Test map with key-value table */
+person : {name: "Alice", age: 30, active: true};
+add_ten : x -> x + 10;
+
+mapped_person : map @add_ten person;
+/* Note: This will add 10 to all values, including strings */
+name_result : mapped_person.name;
+age_result : mapped_person.age;
+active_result : mapped_person.active;
+..assert name_result = "Alice10";
+..assert age_result = 40;
+..assert active_result = 11;
+
+/* Enhanced filter with tables */
+is_even : x -> x % 2 = 0;
+evens : filter @is_even numbers;
+even_2 : evens[2];
+even_4 : evens[4];
+/* Note: Keys 1, 3, 5 don't exist in filtered result */
+..assert even_2 = 2;
+..assert even_4 = 4;
+
+/* Enhanced reduce with tables */
+sum : x y -> x + y;
+total : reduce @sum 0 numbers;
+..assert total = 15;
+
+/* ===== T NAMESPACE OPERATIONS ===== */
+
+/* t.map */
+t_doubled : t.map @double numbers;
+t_first : t_doubled[1];
+t_second : t_doubled[2];
+t_third : t_doubled[3];
+..assert t_first = 2;
+..assert t_second = 4;
+..assert t_third = 6;
+
+/* t.filter */
+t_evens : t.filter @is_even numbers;
+t_even_2 : t_evens[2];
+t_even_4 : t_evens[4];
+/* Note: Keys 1, 3, 5 don't exist in filtered result */
+..assert t_even_2 = 2;
+..assert t_even_4 = 4;
+
+/* t.reduce */
+t_total : t.reduce @sum 0 numbers;
+..assert t_total = 15;
+
+/* t.set - immutable update */
+updated_person : t.set person "age" 31;
+..assert updated_person.age = 31;
+..assert person.age = 30; /* Original unchanged */
+
+/* t.delete - immutable deletion */
+person_without_age : t.delete person "age";
+..assert person_without_age.name = "Alice";
+..assert person_without_age.active = true;
+/* Note: age key doesn't exist in person_without_age */
+..assert person.age = 30; /* Original unchanged */
+
+/* t.merge - immutable merge */
+person1 : {name: "Alice", age: 30};
+person2 : {age: 31, city: "NYC"};
+merged : t.merge person1 person2;
+..assert merged.name = "Alice";
+..assert merged.age = 31;
+..assert merged.city = "NYC";
+
+/* t.length */
+length : t.length person;
+..assert length = 3;
+
+/* t.has */
+has_name : t.has person "name";
+has_email : t.has person "email";
+..assert has_name = true;
+..assert has_email = false;
+
+/* t.get */
+name_value : t.get person "name" "unknown";
+email_value : t.get person "email" "unknown";
+..assert name_value = "Alice";
+..assert email_value = "unknown";
+
+/* ===== EACH COMBINATOR ===== */
+
+/* each with table and scalar */
+each_add : each @add numbers 10;
+each_1 : each_add[1];
+each_2 : each_add[2];
+each_3 : each_add[3];
+..assert each_1 = 11;
+..assert each_2 = 12;
+..assert each_3 = 13;
+
+/* each with two tables */
+table1 : {a: 1, b: 2, c: 3};
+table2 : {a: 10, b: 20, c: 30};
+each_sum : each @add table1 table2;
+..assert each_sum.a = 11;
+..assert each_sum.b = 22;
+..assert each_sum.c = 33;
+
+/* each with scalar and table */
+each_add_scalar : each @add 10 numbers;
+scalar_1 : each_add_scalar[1];
+scalar_2 : each_add_scalar[2];
+scalar_3 : each_add_scalar[3];
+..assert scalar_1 = 11;
+..assert scalar_2 = 12;
+..assert scalar_3 = 13;
+
+/* each with partial application */
+add_to_ten : each @add 10;
+partial_result : add_to_ten numbers;
+partial_1 : partial_result[1];
+partial_2 : partial_result[2];
+partial_3 : partial_result[3];
+..assert partial_1 = 11;
+..assert partial_2 = 12;
+..assert partial_3 = 13;
+
+/* each with different operations */
+each_multiply : each @multiply numbers 2;
+mult_1 : each_multiply[1];
+mult_2 : each_multiply[2];
+mult_3 : each_multiply[3];
+..assert mult_1 = 2;
+..assert mult_2 = 4;
+..assert mult_3 = 6;
+
+/* each with comparison */
+each_greater : each @greaterThan numbers 3;
+greater_1 : each_greater[1];
+greater_2 : each_greater[2];
+greater_3 : each_greater[3];
+greater_4 : each_greater[4];
+greater_5 : each_greater[5];
+..assert greater_1 = false;
+..assert greater_2 = false;
+..assert greater_3 = false;
+..assert greater_4 = true;
+..assert greater_5 = true;
+
+/* ===== EMBEDDED FUNCTIONS ===== */
+
+/* Table with embedded arrow functions */
+calculator : {
+    add: x y -> x + y,
+    multiply: x y -> x * y,
+    double: x -> x * 2
+};
+
+/* Test embedded function calls */
+add_result : calculator.add 5 3;
+multiply_result : calculator.multiply 4 6;
+double_result : calculator.double 7;
+..assert add_result = 8;
+..assert multiply_result = 24;
+..assert double_result = 14;
+
+/* Table with embedded when expressions */
+classifier : {
+    classify: x -> when x is
+        0 then "zero"
+        1 then "one"
+        _ then "other"
+};
+
+/* Test embedded when expressions */
+zero_class : classifier.classify 0;
+one_class : classifier.classify 1;
+other_class : classifier.classify 42;
+..assert zero_class = "zero";
+..assert one_class = "one";
+..assert other_class = "other";
+
+/* Table with mixed content */
+mixed_table : {
+    name: "Alice",
+    age: 30,
+    add: x y -> x + y,
+    is_adult: x -> x >= 18
+};
+
+/* Test mixed table */
+mixed_name : mixed_table.name;
+mixed_age : mixed_table.age;
+mixed_sum : mixed_table.add 5 3;
+mixed_adult_check : mixed_table.is_adult 25;
+..assert mixed_name = "Alice";
+..assert mixed_age = 30;
+..assert mixed_sum = 8;
+..assert mixed_adult_check = true;
+
+/* ===== ERROR HANDLING ===== */
+
+/* Test error handling for invalid inputs */
+empty_table : {};
+
+/* These should not cause errors */
+empty_length : t.length empty_table;
+..assert empty_length = 0;
+
+/* Test safe operations */
+safe_get : t.get empty_table "nonexistent" "default";
+..assert safe_get = "default";
+
+..out "Table enhancements test completed successfully"; 
\ No newline at end of file