blots-wasm 0.12.0

WebAssembly bindings for Blots, a small, simple, expression-oriented programming language.
Documentation
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#[cfg(test)]
mod format_blots_tests {
    use crate::*;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    fn test_format_blots_simple() {
        let source = "x = [1, 2, 3]";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(formatted, "x = [1, 2, 3]");
    }

    #[wasm_bindgen_test]
    fn test_format_blots_multiline() {
        let source = "{name: \"Alice\", age: 30, email: \"alice@example.com\", address: \"123 Main Street\"}";
        let result = format_blots(source, Some(30)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();
        // Record should be formatted across multiple lines
        assert!(formatted.contains("\n"));
        assert!(formatted.contains("{"));
    }

    #[wasm_bindgen_test]
    fn test_format_blots_preserves_comments() {
        let source = "// This is a comment\nx = [1, 2, 3]\n// Another comment\ny = x + 1";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();
        assert!(formatted.contains("// This is a comment"));
        assert!(formatted.contains("// Another comment"));
        assert!(formatted.contains("x = [1, 2, 3]"));
        assert!(formatted.contains("y = x + 1"));
    }

    #[wasm_bindgen_test]
    fn test_format_blots_default_max_columns() {
        let source = "x = 1";
        let result = format_blots(source, None).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(formatted, "x = 1");
    }

    #[wasm_bindgen_test]
    fn test_format_blots_invalid_syntax() {
        let source = "x = [1, 2,";
        let result = format_blots(source, Some(80));
        assert!(result.is_err());
    }

    #[wasm_bindgen_test]
    fn test_format_blots_multiple_statements() {
        let source = "x = 1\ny = 2\nz = x + y";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();
        let lines: Vec<&str> = formatted.lines().collect();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "x = 1");
        assert_eq!(lines[1], "y = 2");
        assert_eq!(lines[2], "z = x + y");
    }

    #[wasm_bindgen_test]
    fn test_format_blots_end_of_line_comments() {
        let source = "x = 5  // this is a comment\ny = 10";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();
        let lines: Vec<&str> = formatted.lines().collect();
        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0], "x = 5  // this is a comment");
        assert_eq!(lines[1], "y = 10");
    }

    #[wasm_bindgen_test]
    fn test_format_blots_mixed_comments() {
        let source = "// standalone comment\nx = 1  // end-of-line comment\ny = 2";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();
        let lines: Vec<&str> = formatted.lines().collect();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "// standalone comment");
        assert_eq!(lines[1], "x = 1  // end-of-line comment");
        assert_eq!(lines[2], "y = 2");
    }

    #[wasm_bindgen_test]
    fn test_format_blots_preserves_empty_lines() {
        let source = "x = 1\n\ny = 2\n\n\n\nz = 3";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();

        // Should preserve up to 2 empty lines between statements
        // 1 empty line between x and y (preserved)
        // 4 empty lines between y and z (capped at 2)
        assert_eq!(formatted, "x = 1\n\ny = 2\n\n\nz = 3");

        let lines: Vec<&str> = formatted.lines().collect();
        assert_eq!(lines.len(), 6); // 3 statements + 1 blank + 2 blanks
    }

    #[wasm_bindgen_test]
    fn test_format_blots_no_empty_lines() {
        let source = "x = 1\ny = 2\nz = 3";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();

        // Should NOT add empty lines when there were none
        assert_eq!(formatted, "x = 1\ny = 2\nz = 3");

        let lines: Vec<&str> = formatted.lines().collect();
        assert_eq!(lines.len(), 3);
    }

    #[wasm_bindgen_test]
    fn test_format_blots_preserves_output_keyword() {
        let source = "output x = 42\noutput y = [1, 2, 3]\nz = 10";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();

        // Output keywords should be preserved
        assert!(formatted.contains("output x = 42"));
        assert!(formatted.contains("output y = [1, 2, 3]"));
        assert!(formatted.contains("z = 10"));

        let lines: Vec<&str> = formatted.lines().collect();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "output x = 42");
        assert_eq!(lines[1], "output y = [1, 2, 3]");
        assert_eq!(lines[2], "z = 10");
    }

    #[wasm_bindgen_test]
    fn test_format_blots_output_with_via() {
        let source = "output result = items via i => i * 2";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();

        // Output keyword should be preserved with via expression
        assert!(formatted.contains("output result = items via"));
        assert!(formatted.starts_with("output result"));
    }

    #[wasm_bindgen_test]
    fn test_format_blots_comments_inside_list() {
        // Test that comments inside lists are preserved
        let source = "[\n// leading comment\n1,\n2, // trailing comment\n]";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();

        assert!(
            formatted.contains("// leading comment"),
            "Leading comment inside list should be preserved. Got: {}",
            formatted
        );
        assert!(
            formatted.contains("// trailing comment"),
            "Trailing comment inside list should be preserved. Got: {}",
            formatted
        );
    }

    #[wasm_bindgen_test]
    fn test_format_blots_comments_inside_record() {
        // Test that comments inside records are preserved
        let source = "{\n// leading comment\na: 1,\nb: 2, // trailing comment\n}";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();

        assert!(
            formatted.contains("// leading comment"),
            "Leading comment inside record should be preserved. Got: {}",
            formatted
        );
        assert!(
            formatted.contains("// trailing comment"),
            "Trailing comment inside record should be preserved. Got: {}",
            formatted
        );
    }

    #[wasm_bindgen_test]
    fn test_format_blots_comments_inside_nested_structures() {
        // Test that comments are preserved in nested lists/records
        let source = "{\n  items: [\n    // first item\n    1,\n    2, // second\n  ],\n}";
        let result = format_blots(source, Some(80)).unwrap();
        let formatted: String = serde_wasm_bindgen::from_value(result).unwrap();

        assert!(
            formatted.contains("// first item"),
            "Comment in nested list should be preserved. Got: {}",
            formatted
        );
        assert!(
            formatted.contains("// second"),
            "Trailing comment in nested list should be preserved. Got: {}",
            formatted
        );
    }
}

#[cfg(test)]
mod evaluate_tests {
    use crate::*;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    fn test_evaluate_simple_expression() {
        let source = "output result = 1 + 2";
        let inputs = serde_wasm_bindgen::to_value(&serde_json::json!({})).unwrap();
        let result = evaluate(source, inputs).unwrap();
        let result_obj: serde_json::Value = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(result_obj["outputs"], serde_json::json!(["result"]));
    }

    #[wasm_bindgen_test]
    fn test_evaluate_with_inputs() {
        let source = "output doubled = inputs.x * 2";
        // Create inputs using SerializableValue directly
        use indexmap::IndexMap;
        let mut inputs_map: IndexMap<String, SerializableValue> = IndexMap::new();
        inputs_map.insert("x".to_string(), SerializableValue::Number(5.0));
        let inputs = serde_wasm_bindgen::to_value(&inputs_map).unwrap();

        let result = evaluate(source, inputs).unwrap();
        let result_obj: serde_json::Value = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(result_obj["outputs"], serde_json::json!(["doubled"]));

        // Check that we got a binding for doubled
        let bindings = &result_obj["bindings"];
        assert_eq!(bindings["doubled"]["Number"], serde_json::json!(10));
    }

    #[wasm_bindgen_test]
    fn test_evaluate_input_reference() {
        let source = "output result = inputs.x + inputs.y";
        // Create inputs using SerializableValue directly
        use indexmap::IndexMap;
        let mut inputs_map: IndexMap<String, SerializableValue> = IndexMap::new();
        inputs_map.insert("x".to_string(), SerializableValue::Number(3.0));
        inputs_map.insert("y".to_string(), SerializableValue::Number(7.0));
        let inputs = serde_wasm_bindgen::to_value(&inputs_map).unwrap();

        let result = evaluate(source, inputs).unwrap();
        let result_obj: serde_json::Value = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(
            result_obj["bindings"]["result"]["Number"],
            serde_json::json!(10)
        );
    }

    #[wasm_bindgen_test]
    fn test_evaluate_list_operations() {
        let source = "output result = [1, 2, 3] + [4, 5, 6]";
        let inputs = serde_wasm_bindgen::to_value(&serde_json::json!({})).unwrap();
        let result = evaluate(source, inputs).unwrap();
        let result_obj: serde_json::Value = serde_wasm_bindgen::from_value(result).unwrap();
        // Check that result is a List serializable value
        assert!(result_obj["bindings"]["result"]["List"].is_array());
        let list = &result_obj["bindings"]["result"]["List"];
        assert_eq!(list[0]["Number"], serde_json::json!(5));
        assert_eq!(list[1]["Number"], serde_json::json!(7));
        assert_eq!(list[2]["Number"], serde_json::json!(9));
    }

    #[wasm_bindgen_test]
    fn test_evaluate_multiline_source() {
        let source = "x = inputs.a * 2\ny = inputs.b + 10\noutput result = x + y";
        use indexmap::IndexMap;
        let mut inputs_map: IndexMap<String, SerializableValue> = IndexMap::new();
        inputs_map.insert("a".to_string(), SerializableValue::Number(5.0));
        inputs_map.insert("b".to_string(), SerializableValue::Number(3.0));
        let inputs = serde_wasm_bindgen::to_value(&inputs_map).unwrap();

        let result = evaluate(source, inputs).unwrap();
        let result_obj: serde_json::Value = serde_wasm_bindgen::from_value(result).unwrap();

        // Check the output
        assert_eq!(result_obj["outputs"], serde_json::json!(["result"]));
        // x = 5 * 2 = 10, y = 3 + 10 = 13, result = 10 + 13 = 23
        assert_eq!(
            result_obj["bindings"]["result"]["Number"],
            serde_json::json!(23)
        );

        // Check intermediate bindings
        assert_eq!(result_obj["bindings"]["x"]["Number"], serde_json::json!(10));
        assert_eq!(result_obj["bindings"]["y"]["Number"], serde_json::json!(13));
    }

    #[wasm_bindgen_test]
    fn test_evaluate_multiline_with_comments() {
        let source = "x = inputs.a * 2\ny = inputs.b + 10\n\n// hi\noutput result = x + y";
        use indexmap::IndexMap;
        let mut inputs_map: IndexMap<String, SerializableValue> = IndexMap::new();
        inputs_map.insert("a".to_string(), SerializableValue::Number(5.0));
        inputs_map.insert("b".to_string(), SerializableValue::Number(3.0));
        let inputs = serde_wasm_bindgen::to_value(&inputs_map).unwrap();

        let result = evaluate(source, inputs).unwrap();
        let result_obj: serde_json::Value = serde_wasm_bindgen::from_value(result).unwrap();

        // Check the output
        assert_eq!(result_obj["outputs"], serde_json::json!(["result"]));
        // x = 5 * 2 = 10, y = 3 + 10 = 13, result = 10 + 13 = 23
        assert_eq!(
            result_obj["bindings"]["result"]["Number"],
            serde_json::json!(23)
        );
    }

    #[wasm_bindgen_test]
    fn test_evaluate_invalid_syntax() {
        let source = "output result = 1 +";
        let inputs = serde_wasm_bindgen::to_value(&serde_json::json!({})).unwrap();
        let result = evaluate(source, inputs);
        assert!(result.is_err());
    }
}

#[cfg(test)]
mod tokenize_tests {
    use crate::*;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    fn test_tokenize_simple() {
        let source = "1 + 2";
        let result = tokenize(source).unwrap();
        let tokens: Vec<serde_json::Value> = serde_wasm_bindgen::from_value(result).unwrap();
        assert!(!tokens.is_empty());
    }

    #[wasm_bindgen_test]
    fn test_tokenize_with_identifiers() {
        let source = "x = 42";
        let result = tokenize(source).unwrap();
        let tokens: Vec<serde_json::Value> = serde_wasm_bindgen::from_value(result).unwrap();
        assert!(!tokens.is_empty());
    }

    #[wasm_bindgen_test]
    fn test_tokenize_complex_expression() {
        let source = "[1, 2, 3] + [4, 5, 6]";
        let result = tokenize(source).unwrap();
        let tokens: Vec<serde_json::Value> = serde_wasm_bindgen::from_value(result).unwrap();
        assert!(!tokens.is_empty());
    }
}

#[cfg(test)]
mod get_built_in_function_names_tests {
    use crate::*;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    fn test_get_built_in_function_names() {
        let result = get_built_in_function_names().unwrap();
        let names: Vec<String> = serde_wasm_bindgen::from_value(result).unwrap();
        assert!(!names.is_empty());
        assert!(names.contains(&"map".to_string()));
        assert!(names.contains(&"filter".to_string()));
        assert!(names.contains(&"reduce".to_string()));
        assert!(names.contains(&"sum".to_string()));
    }
}

#[cfg(test)]
mod get_constants_tests {
    use crate::*;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    fn test_get_constants() {
        let result = get_constants().unwrap();
        let constants: serde_json::Value = serde_wasm_bindgen::from_value(result).unwrap();
        assert!(constants["constants"].is_object());
        let const_obj = &constants["constants"]["Record"];
        assert!(const_obj["pi"]["Number"].is_number());
        assert!(const_obj["e"]["Number"].is_number());
    }
}

#[cfg(test)]
mod evaluate_inline_expressions_tests {
    use crate::*;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    fn test_evaluate_inline_expressions_single() {
        let expressions = serde_wasm_bindgen::to_value(&vec!["1 + 2"]).unwrap();
        let inputs = serde_wasm_bindgen::to_value(&serde_json::json!({})).unwrap();
        let result = evaluate_inline_expressions(expressions, inputs).unwrap();
        let results: Vec<serde_json::Value> = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0]["value"]["Number"], serde_json::json!(3));
    }

    #[wasm_bindgen_test]
    fn test_evaluate_inline_expressions_multiple() {
        let expressions = serde_wasm_bindgen::to_value(&vec!["x * 2", "x + 5", "x - 1"]).unwrap();
        // Create inputs using SerializableValue directly
        use indexmap::IndexMap;
        let mut inputs_map: IndexMap<String, SerializableValue> = IndexMap::new();
        inputs_map.insert("x".to_string(), SerializableValue::Number(10.0));
        let inputs = serde_wasm_bindgen::to_value(&inputs_map).unwrap();

        let result = evaluate_inline_expressions(expressions, inputs).unwrap();
        let results: Vec<serde_json::Value> = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(results.len(), 3);
        assert_eq!(results[0]["value"]["Number"], serde_json::json!(20));
        assert_eq!(results[1]["value"]["Number"], serde_json::json!(15));
        assert_eq!(results[2]["value"]["Number"], serde_json::json!(9));
    }

    #[wasm_bindgen_test]
    fn test_evaluate_inline_expressions_with_error() {
        let expressions = serde_wasm_bindgen::to_value(&vec!["1 + 2", "invalid +"]).unwrap();
        let inputs = serde_wasm_bindgen::to_value(&serde_json::json!({})).unwrap();
        let result = evaluate_inline_expressions(expressions, inputs).unwrap();
        let results: Vec<serde_json::Value> = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0]["value"]["Number"], serde_json::json!(3));
        assert!(results[1]["error"].is_string());
    }

    #[wasm_bindgen_test]
    fn test_evaluate_inline_expressions_with_inputs() {
        let expressions =
            serde_wasm_bindgen::to_value(&vec!["\"Hello, \" + name", "age >= 18"]).unwrap();
        // Create inputs using SerializableValue directly
        use indexmap::IndexMap;
        let mut inputs_map: IndexMap<String, SerializableValue> = IndexMap::new();
        inputs_map.insert(
            "name".to_string(),
            SerializableValue::String("Alice".to_string()),
        );
        inputs_map.insert("age".to_string(), SerializableValue::Number(30.0));
        let inputs = serde_wasm_bindgen::to_value(&inputs_map).unwrap();

        let result = evaluate_inline_expressions(expressions, inputs).unwrap();
        let results: Vec<serde_json::Value> = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(results.len(), 2);
        assert_eq!(
            results[0]["value"]["String"],
            serde_json::json!("Hello, Alice")
        );
        assert_eq!(results[1]["value"]["Bool"], serde_json::json!(true));
    }
}