about summary refs log tree commit diff stats
path: root/js/baba-yaga/test-js-interop.baba
blob: 5f84396e7b18cd29817131400a966c345e96c8e8 (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
// test-js-interop.baba - Test JavaScript interop functionality

// Test 1: Basic Math.abs call
io.out "=== Test 1: Math.abs ===" ;

absResult : io.callJS "Math.abs" [-42];
io.out "Math.abs(-42) result:";
io.out absResult;

// Test 2: JSON parsing
io.out "=== Test 2: JSON Parsing ===" ;

jsonStr : "{\"name\": \"Alice\", \"age\": 30, \"active\": true}";
parseResult : io.callJS "JSON.parse" [jsonStr];

io.out "JSON.parse result:";
io.out parseResult;

// Test 3: Property access
io.out "=== Test 3: Property Access ===" ;

nameResult : when parseResult is
  Ok obj then io.getProperty obj "name"
  Err msg then Err msg;

io.out "Name property:";
io.out nameResult;

// Test 4: Array conversion
io.out "=== Test 4: Array Conversion ===" ;

babaList : [1, 2, 3, 4, 5];
jsArray : io.listToJSArray babaList;
stringifyResult : io.callJS "JSON.stringify" [jsArray];

io.out "List to JS array to JSON:";
io.out stringifyResult;

// Test 5: Table to Object conversion
io.out "=== Test 5: Table to Object ===" ;

babaTable : {x: 100, y: 200, label: "point"};
jsObject : io.tableToObject babaTable;
tableJsonResult : io.callJS "JSON.stringify" [jsObject];

io.out "Table to JS object to JSON:";
io.out tableJsonResult;

// Test 6: Round-trip conversion
io.out "=== Test 6: Round-trip Conversion ===" ;

originalData : {
  users: ["Alice", "Bob", "Charlie"],
  count: 3,
  active: true
};

// Convert to JS object
jsObj : io.tableToObject originalData;

// Convert to JSON string
jsonResult : io.callJS "JSON.stringify" [jsObj];

// Parse back to JS object
parseBackResult : when jsonResult is
  Ok jsonStr then io.callJS "JSON.parse" [jsonStr]
  Err msg then Err msg;

// Convert back to Baba Yaga table
finalResult : when parseBackResult is
  Ok jsObj then io.objectToTable jsObj
  Err msg then Err msg;

io.out "Round-trip result:";
io.out finalResult;

// Test 7: Error handling
io.out "=== Test 7: Error Handling ===" ;

errorResult : io.callJS "nonExistentFunction" [42];
io.out "Error result (should be Err):";
io.out errorResult;

// Test 8: Property existence check
io.out "=== Test 8: Property Existence ===" ;

testObj : io.callJS "JSON.parse" ["{\"existing\": true}"];
hasExisting : when testObj is
  Ok obj then io.hasProperty obj "existing"
  Err _ then false;

hasMissing : when testObj is
  Ok obj then io.hasProperty obj "missing"
  Err _ then false;

io.out "Has 'existing' property:";
io.out hasExisting;
io.out "Has 'missing' property:";
io.out hasMissing;

io.out "=== All Tests Complete ===";