about summary refs log tree commit diff stats
path: root/js/scripting-lang/tests/unit/17_table_enhancements.txt
blob: 5e67ff65ebe7f9324e5f465e0138c71dbc14f71d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* 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 : t.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 */
classify_func : x -> when x is 0 then "zero" 1 then "one" _ then "other";
classifier : {
    classify: classify_func
};

/* 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";