about summary refs log tree commit diff stats
path: root/js/scripting-lang/tests
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/tests')
-rw-r--r--js/scripting-lang/tests/05_io_operations.txt37
-rw-r--r--js/scripting-lang/tests/18_each_combinator_basic.txt4
-rw-r--r--js/scripting-lang/tests/18_each_combinator_minimal.txt3
-rw-r--r--js/scripting-lang/tests/21_enhanced_case_statements.txt98
-rw-r--r--js/scripting-lang/tests/21_enhanced_case_statements_fixed.txt98
-rw-r--r--js/scripting-lang/tests/22_parser_limitations.txt115
-rw-r--r--js/scripting-lang/tests/23_minus_operator_spacing.txt51
-rw-r--r--js/scripting-lang/tests/repl_demo.txt180
8 files changed, 583 insertions, 3 deletions
diff --git a/js/scripting-lang/tests/05_io_operations.txt b/js/scripting-lang/tests/05_io_operations.txt
index a16bf94..6d05dfe 100644
--- a/js/scripting-lang/tests/05_io_operations.txt
+++ b/js/scripting-lang/tests/05_io_operations.txt
@@ -1,5 +1,5 @@
 /* Unit Test: IO Operations */
-/* Tests: ..out, ..assert operations */
+/* Tests: ..out, ..assert, ..listen, ..emit operations */
 
 /* Test basic output */
 ..out "Testing IO operations";
@@ -25,4 +25,39 @@ sum : x + y;
 ..assert (x * y) = 15;
 ..assert (x > y) = true;
 
+/* Test ..listen functionality */
+state : ..listen;
+..assert state.status = "placeholder";
+..assert state.message = "State not available in standalone mode";
+
+/* Test ..listen in when expression */
+result : when ..listen is
+    { status: "placeholder" } then "Placeholder detected"
+    { status: "active" } then "Active state detected"
+    _ then "Unknown state";
+..assert result = "Placeholder detected";
+
+/* Test ..emit with different data types */
+..emit "String value";
+..emit 42;
+..emit true;
+..emit { key: "value", number: 123 };
+
+/* Test ..emit with computed expressions */
+computed_table : { a: 10, b: 20 };
+computed_sum : computed_table.a + computed_table.b;
+..emit computed_sum;
+
+/* Test ..emit with conditional logic */
+condition : 10 > 5;
+message : when condition is
+    true then "Condition is true"
+    false then "Condition is false";
+..emit message;
+
+/* Test that ..emit doesn't interfere with ..out */
+..out "This should appear via ..out";
+..emit "This should appear via ..emit";
+..out "Another ..out message";
+
 ..out "IO operations test completed"; 
\ No newline at end of file
diff --git a/js/scripting-lang/tests/18_each_combinator_basic.txt b/js/scripting-lang/tests/18_each_combinator_basic.txt
index 95e9803..d926013 100644
--- a/js/scripting-lang/tests/18_each_combinator_basic.txt
+++ b/js/scripting-lang/tests/18_each_combinator_basic.txt
@@ -23,6 +23,8 @@ each_sum : each @add table1 table2;
 /* each with empty table */
 empty_table : {};
 empty_result : each @add empty_table 10;
-..assert empty_result = {};
+/* Check that empty_result is an empty object by checking its length */
+empty_length : t.length empty_result;
+..assert empty_length = 0;
 
 ..out "Basic each combinator test completed successfully"; 
\ No newline at end of file
diff --git a/js/scripting-lang/tests/18_each_combinator_minimal.txt b/js/scripting-lang/tests/18_each_combinator_minimal.txt
index 0da9320..1cd6516 100644
--- a/js/scripting-lang/tests/18_each_combinator_minimal.txt
+++ b/js/scripting-lang/tests/18_each_combinator_minimal.txt
@@ -51,7 +51,8 @@ mult_3 : each_multiply[3];
 /* each with empty table */
 empty_table : {};
 empty_result : each @add empty_table 10;
-..assert empty_result = {};
+empty_length : t.length empty_result;
+..assert empty_length = 0;
 
 /* each with single element table */
 single_table : {key: 5};
diff --git a/js/scripting-lang/tests/21_enhanced_case_statements.txt b/js/scripting-lang/tests/21_enhanced_case_statements.txt
new file mode 100644
index 0000000..79adb69
--- /dev/null
+++ b/js/scripting-lang/tests/21_enhanced_case_statements.txt
@@ -0,0 +1,98 @@
+/* Unit Test: Enhanced Case Statements - Fixed Version */
+/* Tests: FizzBuzz and advanced pattern matching with new capabilities */
+
+/* ===== FIZZBUZZ IMPLEMENTATION ===== */
+
+/* Classic FizzBuzz using multi-value patterns with expressions */
+fizzbuzz : n ->
+  when (n % 3) (n % 5) is
+    0 0 then "FizzBuzz"
+    0 _ then "Fizz"
+    _ 0 then "Buzz"
+    _ _ then n;
+
+/* Test FizzBuzz implementation */
+fizzbuzz_15 : fizzbuzz 15;
+fizzbuzz_3 : fizzbuzz 3;
+fizzbuzz_5 : fizzbuzz 5;
+fizzbuzz_7 : fizzbuzz 7;
+
+/* ===== TABLE ACCESS IN WHEN EXPRESSIONS ===== */
+
+/* User data for testing */
+admin_user : {role: "admin", level: 5, name: "Alice"};
+user_user : {role: "user", level: 2, name: "Bob"};
+guest_user : {role: "guest", level: 0, name: "Charlie"};
+
+/* Access control using table access in patterns */
+access_level : user ->
+  when user.role is
+    "admin" then "full access"
+    "user" then "limited access"
+    _ then "no access";
+
+/* Test access control */
+admin_access : access_level admin_user;
+user_access : access_level user_user;
+guest_access : access_level guest_user;
+
+/* ===== FUNCTION CALLS IN WHEN EXPRESSIONS ===== */
+
+/* Helper functions for testing */
+is_even : n -> n % 2 = 0;
+
+/* Number classification using function calls in patterns */
+classify_number : n ->
+  when (is_even n) is
+    true then "even number"
+    false then "odd number";
+
+/* Test number classification */
+even_class : classify_number 4;
+odd_class : classify_number 7;
+
+/* ===== SIMPLIFIED MULTI-VALUE VALIDATION ===== */
+
+/* Simplified validation - avoid complex and expressions */
+validate_name : name -> name != "";
+validate_age : age -> age >= 0;
+
+validate_user : name age ->
+  when (validate_name name) (validate_age age) is
+    true true then "valid user"
+    true false then "invalid age"
+    false true then "invalid name"
+    false false then "invalid user";
+
+/* Test user validation */
+valid_user : validate_user "Alice" 30;
+invalid_age : validate_user "Bob" -5;
+invalid_name : validate_user "" 25;
+
+/* ===== OUTPUT RESULTS ===== */
+
+/* Output FizzBuzz results */
+..out "FizzBuzz Results:";
+..out fizzbuzz_15;
+..out fizzbuzz_3;
+..out fizzbuzz_5;
+..out fizzbuzz_7;
+
+/* Output access control results */
+..out "Access Control Results:";
+..out admin_access;
+..out user_access;
+..out guest_access;
+
+/* Output number classification results */
+..out "Number Classification Results:";
+..out even_class;
+..out odd_class;
+
+/* Output user validation results */
+..out "User Validation Results:";
+..out valid_user;
+..out invalid_age;
+..out invalid_name;
+
+..out "Enhanced case statements test completed successfully"; 
\ No newline at end of file
diff --git a/js/scripting-lang/tests/21_enhanced_case_statements_fixed.txt b/js/scripting-lang/tests/21_enhanced_case_statements_fixed.txt
new file mode 100644
index 0000000..79adb69
--- /dev/null
+++ b/js/scripting-lang/tests/21_enhanced_case_statements_fixed.txt
@@ -0,0 +1,98 @@
+/* Unit Test: Enhanced Case Statements - Fixed Version */
+/* Tests: FizzBuzz and advanced pattern matching with new capabilities */
+
+/* ===== FIZZBUZZ IMPLEMENTATION ===== */
+
+/* Classic FizzBuzz using multi-value patterns with expressions */
+fizzbuzz : n ->
+  when (n % 3) (n % 5) is
+    0 0 then "FizzBuzz"
+    0 _ then "Fizz"
+    _ 0 then "Buzz"
+    _ _ then n;
+
+/* Test FizzBuzz implementation */
+fizzbuzz_15 : fizzbuzz 15;
+fizzbuzz_3 : fizzbuzz 3;
+fizzbuzz_5 : fizzbuzz 5;
+fizzbuzz_7 : fizzbuzz 7;
+
+/* ===== TABLE ACCESS IN WHEN EXPRESSIONS ===== */
+
+/* User data for testing */
+admin_user : {role: "admin", level: 5, name: "Alice"};
+user_user : {role: "user", level: 2, name: "Bob"};
+guest_user : {role: "guest", level: 0, name: "Charlie"};
+
+/* Access control using table access in patterns */
+access_level : user ->
+  when user.role is
+    "admin" then "full access"
+    "user" then "limited access"
+    _ then "no access";
+
+/* Test access control */
+admin_access : access_level admin_user;
+user_access : access_level user_user;
+guest_access : access_level guest_user;
+
+/* ===== FUNCTION CALLS IN WHEN EXPRESSIONS ===== */
+
+/* Helper functions for testing */
+is_even : n -> n % 2 = 0;
+
+/* Number classification using function calls in patterns */
+classify_number : n ->
+  when (is_even n) is
+    true then "even number"
+    false then "odd number";
+
+/* Test number classification */
+even_class : classify_number 4;
+odd_class : classify_number 7;
+
+/* ===== SIMPLIFIED MULTI-VALUE VALIDATION ===== */
+
+/* Simplified validation - avoid complex and expressions */
+validate_name : name -> name != "";
+validate_age : age -> age >= 0;
+
+validate_user : name age ->
+  when (validate_name name) (validate_age age) is
+    true true then "valid user"
+    true false then "invalid age"
+    false true then "invalid name"
+    false false then "invalid user";
+
+/* Test user validation */
+valid_user : validate_user "Alice" 30;
+invalid_age : validate_user "Bob" -5;
+invalid_name : validate_user "" 25;
+
+/* ===== OUTPUT RESULTS ===== */
+
+/* Output FizzBuzz results */
+..out "FizzBuzz Results:";
+..out fizzbuzz_15;
+..out fizzbuzz_3;
+..out fizzbuzz_5;
+..out fizzbuzz_7;
+
+/* Output access control results */
+..out "Access Control Results:";
+..out admin_access;
+..out user_access;
+..out guest_access;
+
+/* Output number classification results */
+..out "Number Classification Results:";
+..out even_class;
+..out odd_class;
+
+/* Output user validation results */
+..out "User Validation Results:";
+..out valid_user;
+..out invalid_age;
+..out invalid_name;
+
+..out "Enhanced case statements test completed successfully"; 
\ No newline at end of file
diff --git a/js/scripting-lang/tests/22_parser_limitations.txt b/js/scripting-lang/tests/22_parser_limitations.txt
new file mode 100644
index 0000000..6d267b8
--- /dev/null
+++ b/js/scripting-lang/tests/22_parser_limitations.txt
@@ -0,0 +1,115 @@
+/* Unit Test: Parser Limitations for Enhanced Case Statements */
+/* Tests: Multi-value patterns with expressions, table access, function calls */
+
+/* ======================================== */
+/* MAIN BLOCKER: Multi-value patterns with expressions */
+/* ======================================== */
+
+/* Test 1: Basic multi-value with expressions in parentheses */
+test_multi_expr : x y -> 
+  when (x % 2) (y % 2) is
+    0 0 then "both even"
+    0 1 then "x even, y odd"
+    1 0 then "x odd, y even"
+    1 1 then "both odd";
+
+/* Test 2: FizzBuzz-style multi-value patterns */
+fizzbuzz_test : n ->
+  when (n % 3) (n % 5) is
+    0 0 then "FizzBuzz"
+    0 _ then "Fizz"
+    _ 0 then "Buzz"
+    _ _ then n;
+
+/* Test 3: Complex expressions in multi-value patterns */
+complex_multi : x y ->
+  when ((x + 1) % 2) ((y - 1) % 2) is
+    0 0 then "both transformed even"
+    0 1 then "x transformed even, y transformed odd"
+    1 0 then "x transformed odd, y transformed even"
+    1 1 then "both transformed odd";
+
+/* Test 4: Function calls in multi-value patterns */
+is_even : n -> n % 2 = 0;
+is_positive : n -> n > 0;
+
+test_func_multi : x y ->
+  when (is_even x) (is_positive y) is
+    true true then "x even and y positive"
+    true false then "x even and y not positive"
+    false true then "x odd and y positive"
+    false false then "x odd and y not positive";
+
+/* ======================================== */
+/* SECONDARY LIMITATIONS: Table access and function calls */
+/* ======================================== */
+
+/* Test 5: Table access in when expressions */
+user : {role: "admin", level: 5};
+test_table_access : u ->
+  when u.role is
+    "admin" then "admin user"
+    "user" then "regular user"
+    _ then "unknown role";
+
+/* Test 6: Function calls in when expressions */
+test_func_call : n ->
+  when (is_even n) is
+    true then "even number"
+    false then "odd number";
+
+/* Test 7: Complex function calls in when expressions */
+complex_func : n -> (n % 3 = 0) and (n % 5 = 0);
+test_complex_func : n ->
+  when (complex_func n) is
+    true then "divisible by both 3 and 5"
+    false then "not divisible by both";
+
+/* ======================================== */
+/* CONTROL TESTS: Should work with current parser */
+/* ======================================== */
+
+/* Test 8: Simple value matching (control) */
+test_simple : n ->
+  when n is
+    0 then "zero"
+    1 then "one"
+    _ then "other";
+
+/* Test 9: Single complex expressions with parentheses (control) */
+test_single_expr : n ->
+  when (n % 3) is
+    0 then "divisible by 3"
+    _ then "not divisible by 3";
+
+/* Test 10: Multiple simple values (control) */
+test_multi_simple : x y ->
+  when x y is
+    0 0 then "both zero"
+    0 _ then "x zero"
+    _ 0 then "y zero"
+    _ _ then "neither zero";
+
+/* ======================================== */
+/* TEST EXECUTION */
+/* ======================================== */
+
+/* Execute tests that should work */
+result1 : test_simple 5;
+result2 : test_single_expr 15;
+result3 : test_multi_simple 0 5;
+
+/* These should fail with current parser */
+result4 : test_multi_expr 4 6;  /* Should return "both even" */
+result5 : fizzbuzz_test 15;     /* Should return "FizzBuzz" */
+result6 : test_table_access user; /* Should return "admin user" */
+result7 : test_func_call 4;     /* Should return "even number" */
+
+/* Output results */
+..out result1;
+..out result2;
+..out result3;
+..out result4;
+..out result5;
+..out result6;
+..out result7; 
\ No newline at end of file
diff --git a/js/scripting-lang/tests/23_minus_operator_spacing.txt b/js/scripting-lang/tests/23_minus_operator_spacing.txt
new file mode 100644
index 0000000..510b997
--- /dev/null
+++ b/js/scripting-lang/tests/23_minus_operator_spacing.txt
@@ -0,0 +1,51 @@
+/* Test file for minus operator spacing functionality */
+/* This tests the new spacing-based ambiguity resolution for minus operator */
+
+..out "=== Minus Operator Spacing Tests ===";
+
+/* Basic unary minus tests */
+test1 : -5;
+test2 : -3.14;
+test3 : -10;
+test4 : -42;
+
+/* Basic binary minus tests */
+test5 : 5 - 3;
+test6 : 10 - 5;
+test7 : 15 - 7;
+test8 : 10 - 2.5;
+
+/* Legacy syntax tests (should continue to work) */
+test9 : (-5);
+test10 : (-3.14);
+test11 : (-10);
+test12 : 5-3;
+test13 : 15-7;
+
+/* Complex negative expressions */
+test14 : -10 - -100;
+test15 : -5 - -3;
+test16 : -20 - -30;
+
+/* Assertions to validate behavior */
+..assert test1 = -5;           /* Unary minus: -5 */
+..assert test2 = -3.14;        /* Unary minus: -3.14 */
+..assert test3 = -10;          /* Unary minus: -10 */
+..assert test4 = -42;          /* Unary minus: -42 */
+
+..assert test5 = 2;            /* Binary minus: 5 - 3 = 2 */
+..assert test6 = 5;            /* Binary minus: 10 - 5 = 5 */
+..assert test7 = 8;            /* Binary minus: 15 - 7 = 8 */
+..assert test8 = 7.5;          /* Binary minus: 10 - 2.5 = 7.5 */
+
+..assert test9 = -5;           /* Legacy: (-5) = -5 */
+..assert test10 = -3.14;       /* Legacy: (-3.14) = -3.14 */
+..assert test11 = -10;         /* Legacy: (-10) = -10 */
+..assert test12 = 2;           /* Legacy: 5-3 = 2 */
+..assert test13 = 8;           /* Legacy: 15-7 = 8 */
+
+..assert test14 = 90;          /* Complex: -10 - -100 = 90 */
+..assert test15 = -2;          /* Complex: -5 - -3 = -2 */
+..assert test16 = 10;          /* Complex: -20 - -30 = 10 */
+
+..out "=== Basic Minus Operator Spacing Tests Passed ===";
\ No newline at end of file
diff --git a/js/scripting-lang/tests/repl_demo.txt b/js/scripting-lang/tests/repl_demo.txt
new file mode 100644
index 0000000..c96f911
--- /dev/null
+++ b/js/scripting-lang/tests/repl_demo.txt
@@ -0,0 +1,180 @@
+/* REPL Demo - Comprehensive Language Feature Showcase */
+
+/* ===== BASIC OPERATIONS ===== */
+/* Arithmetic and function application */
+x : 5;
+y : 10;
+sum : x + y;
+product : x * y;
+difference : y - x;
+quotient : y / x;
+
+/* Function application and partial application */
+double : multiply 2;
+triple : multiply 3;
+add5 : add 5;
+result1 : double 10;
+result2 : add5 15;
+
+/* ===== TABLE OPERATIONS ===== */
+/* Array-like tables */
+numbers : {1, 2, 3, 4, 5};
+fruits : {"apple", "banana", "cherry", "date"};
+
+/* Key-value tables */
+person : {name: "Alice", age: 30, active: true, skills: {"JavaScript", "Python", "Rust"}};
+config : {debug: true, port: 3000, host: "localhost"};
+
+/* Mixed tables */
+mixed : {1, name: "Bob", 2, active: false, 3, "value"};
+
+/* Table access */
+first_number : numbers[1];
+person_name : person.name;
+mixed_name : mixed.name;
+
+/* ===== FUNCTIONAL PROGRAMMING ===== */
+/* Higher-order functions */
+doubled_numbers : map @double numbers;
+filtered_numbers : filter @(lessThan 3) numbers;
+sum_of_numbers : reduce @add 0 numbers;
+
+/* Function composition */
+compose_example : double via add5 via negate;
+result3 : compose_example 10;
+
+/* Pipeline operations */
+pipeline : numbers via map @double via filter @(greaterThan 5) via reduce @add 0;
+
+/* ===== PATTERN MATCHING ===== */
+/* When expressions */
+grade : 85;
+letter_grade : when grade {
+    >= 90: "A";
+    >= 80: "B";
+    >= 70: "C";
+    >= 60: "D";
+    default: "F";
+};
+
+/* Complex pattern matching */
+status : "active";
+access_level : when status {
+    "admin": "full";
+    "moderator": "limited";
+    "user": "basic";
+    default: "none";
+};
+
+/* ===== ADVANCED COMBINATORS ===== */
+/* Combinator examples */
+numbers2 : {2, 4, 6, 8, 10};
+evens : filter @(equals 0 via modulo 2) numbers2;
+squares : map @(power 2) numbers2;
+sum_squares : reduce @add 0 squares;
+
+/* Function composition with combinators */
+complex_pipeline : numbers via 
+    map @(multiply 2) via 
+    filter @(greaterThan 5) via 
+    map @(power 2) via 
+    reduce @add 0;
+
+/* ===== TABLE ENHANCEMENTS ===== */
+/* Table transformations */
+users : {
+    user1: {name: "Alice", age: 25, role: "admin"},
+    user2: {name: "Bob", age: 30, role: "user"},
+    user3: {name: "Charlie", age: 35, role: "moderator"}
+};
+
+/* Extract specific fields */
+names : map @(constant "name") users;
+ages : map @(constant "age") users;
+
+/* Filter by conditions */
+admins : filter @(equals "admin" via constant "role") users;
+young_users : filter @(lessThan 30 via constant "age") users;
+
+/* ===== REAL-WORLD EXAMPLES ===== */
+/* Data processing pipeline */
+data : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+processed : data via 
+    filter @(greaterThan 5) via 
+    map @(multiply 3) via 
+    filter @(lessThan 25);
+
+/* Configuration management */
+default_config : {port: 3000, host: "localhost", debug: false};
+user_config : {port: 8080, debug: true};
+merged_config : merge default_config user_config;
+
+/* ===== ERROR HANDLING EXAMPLES ===== */
+/* Safe operations */
+safe_divide : (x, y) => when y {
+    0: "Error: Division by zero";
+    default: x / y;
+};
+
+safe_result1 : safe_divide 10 2;
+safe_result2 : safe_divide 10 0;
+
+/* ===== PERFORMANCE EXAMPLES ===== */
+/* Large dataset processing */
+large_numbers : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
+processed_large : large_numbers via 
+    map @(power 2) via 
+    filter @(greaterThan 50) via 
+    reduce @add 0;
+
+/* ===== DEBUGGING EXAMPLES ===== */
+/* State inspection helpers */
+debug_state : {
+    numbers: numbers,
+    person: person,
+    processed: processed,
+    config: merged_config
+};
+
+/* ===== EXPORT EXAMPLES ===== */
+/* Exportable configurations */
+export_config : {
+    version: "1.0.0",
+    features: {"tables", "functions", "pattern-matching"},
+    examples: {
+        basic: "Basic arithmetic and function application",
+        advanced: "Complex functional pipelines",
+        real_world: "Data processing examples"
+    }
+};
+
+/* ===== COMPREHENSIVE SHOWCASE ===== */
+/* This demonstrates all major language features in one pipeline */
+comprehensive_example : {
+    input: numbers,
+    doubled: map @double numbers,
+    filtered: filter @(greaterThan 3) numbers,
+    composed: double via add5 via negate,
+    pattern_matched: when (length numbers) {
+        > 5: "Large dataset";
+        > 3: "Medium dataset";
+        default: "Small dataset";
+    },
+    final_result: numbers via 
+        map @(multiply 2) via 
+        filter @(greaterThan 5) via 
+        reduce @add 0
+};
+
+/* Output results for verification */
+..out "REPL Demo completed successfully!";
+..out "All language features demonstrated:";
+..out "  ✓ Basic operations and arithmetic";
+..out "  ✓ Table literals and access";
+..out "  ✓ Function application and composition";
+..out "  ✓ Pattern matching with when expressions";
+..out "  ✓ Higher-order functions and combinators";
+..out "  ✓ Table transformations and pipelines";
+..out "  ✓ Real-world data processing examples";
+..out "  ✓ Error handling and safe operations";
+..out "  ✓ Performance and debugging features"; 
\ No newline at end of file