-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathatom_test.go
More file actions
429 lines (348 loc) · 12.4 KB
/
atom_test.go
File metadata and controls
429 lines (348 loc) · 12.4 KB
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
package quickjs
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// TestAtomBasics tests basic Atom functionality
func TestAtomBasics(t *testing.T) {
rt := NewRuntime()
defer rt.Close()
ctx := rt.NewContext()
defer ctx.Close()
// Test string atom creation
atom := ctx.NewAtom("testProperty") // Changed: Atom() → NewAtom()
defer atom.Free()
require.EqualValues(t, "testProperty", atom.ToString()) // Changed: String() → ToString()
// Test ToValue method - now returns *Value
atomValue := atom.ToValue() // Changed: Value() → ToValue()
defer atomValue.Free()
require.True(t, atomValue.IsString())
require.EqualValues(t, "testProperty", atomValue.ToString()) // Changed: String() → ToString()
// Test index-based atom creation
atomIdx := ctx.NewAtomIdx(42) // Changed: AtomIdx() → NewAtomIdx()
defer atomIdx.Free()
require.EqualValues(t, "42", atomIdx.ToString()) // Changed: String() → ToString()
// Test empty string atom
emptyAtom := ctx.NewAtom("") // Changed: Atom() → NewAtom()
defer emptyAtom.Free()
require.EqualValues(t, "", emptyAtom.ToString()) // Changed: String() → ToString()
// Test zero index
zeroAtom := ctx.NewAtomIdx(0) // Changed: AtomIdx() → NewAtomIdx()
defer zeroAtom.Free()
require.EqualValues(t, "0", zeroAtom.ToString()) // Changed: String() → ToString()
}
// TestAtomSpecialCases tests special characters and edge cases
func TestAtomSpecialCases(t *testing.T) {
newTestContext := func(t *testing.T) *Context {
rt := NewRuntime()
ctx := rt.NewContext()
require.NotNil(t, ctx)
t.Cleanup(func() {
ctx.Close()
rt.Close()
})
return ctx
}
// Test various special characters and edge cases
testCases := []struct {
name string
input interface{}
expected string
}{
{"space", "hello world", "hello world"},
{"newlines", "test\nwith\nnewlines", "test\nwith\nnewlines"},
{"quotes", "test\"with\"quotes", "test\"with\"quotes"},
{"unicode", "测试中文", "测试中文"},
{"emoji", "🚀emoji🌟test", "🚀emoji🌟test"},
{"numeric_string", "123", "123"},
{"negative_number", "-42", "-42"},
{"float_string", "3.14159", "3.14159"},
{"large_index", uint32(999999), "999999"},
{"max_uint32", uint32(4294967295), "4294967295"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := newTestContext(t)
var atom *Atom
switch v := tc.input.(type) {
case string:
atom = ctx.NewAtom(v) // Changed: Atom() → NewAtom()
case uint32:
atom = ctx.NewAtomIdx(v) // Changed: AtomIdx() → NewAtomIdx()
}
defer atom.Free()
require.EqualValues(t, tc.expected, atom.ToString()) // Changed: String() → ToString()
// Test ToValue conversion - now returns *Value
atomValue := atom.ToValue() // Changed: Value() → ToValue()
defer atomValue.Free()
require.EqualValues(t, tc.expected, atomValue.ToString()) // Changed: String() → ToString()
})
}
// Test long string
ctx := newTestContext(t)
longString := strings.Repeat("a", 5000)
longAtom := ctx.NewAtom(longString) // Changed: Atom() → NewAtom()
defer longAtom.Free()
require.EqualValues(t, longString, longAtom.ToString()) // Changed: String() → ToString()
}
// TestAtomMemoryManagement tests proper memory management
func TestAtomMemoryManagement(t *testing.T) {
rt := NewRuntime()
defer rt.Close()
ctx := rt.NewContext()
defer ctx.Close()
// Test creating and freeing many atoms
for i := 0; i < 100; i++ {
atom := ctx.NewAtom("test") // Changed: Atom() → NewAtom()
atom.Free()
}
// Test creating atoms with different names
atoms := make([]*Atom, 50)
for i := 0; i < 50; i++ {
atoms[i] = ctx.NewAtom("property" + string(rune('A'+i%26))) // Changed: Atom() → NewAtom()
}
// Free all atoms
for i := 0; i < 50; i++ {
atoms[i].Free()
}
// Verify context still works
finalAtom := ctx.NewAtom("final") // Changed: Atom() → NewAtom()
defer finalAtom.Free()
require.EqualValues(t, "final", finalAtom.ToString()) // Changed: String() → ToString()
// Test multiple Free() calls (should not crash)
testAtom := ctx.NewAtom("test_multiple_free") // Changed: Atom() → NewAtom()
testAtom.Free()
// Second Free() should not crash (though not recommended)
}
// TestAtomWithObjects tests Atom usage with object properties
func TestAtomWithObjects(t *testing.T) {
rt := NewRuntime()
defer rt.Close()
ctx := rt.NewContext()
defer ctx.Close()
obj := ctx.NewObject() // Changed: Object() → NewObject()
defer obj.Free()
// Test setting and getting properties using atoms
propNames := []string{"name", "value", "flag", "data"}
propValues := []*Value{ // MODIFIED: now uses *Value slice
ctx.NewString("test"), // Changed: String() → NewString()
ctx.NewInt32(42), // Changed: Int32() → NewInt32()
ctx.NewBool(true), // Changed: Bool() → NewBool()
ctx.NewString("object_data"), // Changed: String() → NewString()
}
// Set properties
for i, name := range propNames {
obj.Set(name, propValues[i])
}
// Create atoms for property names and verify they work
for _, name := range propNames {
atom := ctx.NewAtom(name) // Changed: Atom() → NewAtom()
atomStr := atom.ToString() // Changed: String() → ToString()
require.EqualValues(t, name, atomStr)
// Verify the property exists
require.True(t, obj.Has(name))
// Test using atom string as property key
retrievedValue := obj.Get(atomStr)
defer retrievedValue.Free()
require.False(t, retrievedValue.IsNull())
atom.Free()
}
// Test property enumeration (tests propertyEnum indirectly)
names, err := obj.PropertyNames()
require.NoError(t, err)
require.GreaterOrEqual(t, len(names), len(propNames))
for _, expectedName := range propNames {
require.Contains(t, names, expectedName)
}
}
// TestAtomDeduplication tests atom deduplication behavior
func TestAtomDeduplication(t *testing.T) {
rt := NewRuntime()
defer rt.Close()
ctx := rt.NewContext()
defer ctx.Close()
// Test creating many atoms with the same name
sameName := "duplicateName"
atoms := make([]*Atom, 50)
for i := 0; i < 50; i++ {
atoms[i] = ctx.NewAtom(sameName) // Changed: Atom() → NewAtom()
require.EqualValues(t, sameName, atoms[i].ToString()) // Changed: String() → ToString()
}
// Free all atoms
for i := 0; i < 50; i++ {
atoms[i].Free()
}
// Verify context still works
finalAtom := ctx.NewAtom(sameName) // Changed: Atom() → NewAtom()
defer finalAtom.Free()
require.EqualValues(t, sameName, finalAtom.ToString()) // Changed: String() → ToString()
// Test string vs index atoms that produce same result
stringAtom := ctx.NewAtom("123") // Changed: Atom() → NewAtom()
defer stringAtom.Free()
indexAtom := ctx.NewAtomIdx(123) // Changed: AtomIdx() → NewAtomIdx()
defer indexAtom.Free()
require.EqualValues(t, stringAtom.ToString(), indexAtom.ToString()) // Changed: String() → ToString()
}
// TestAtomEdgeCases tests additional edge cases for better coverage
func TestAtomEdgeCases(t *testing.T) {
newTestContext := func(t *testing.T) *Context {
rt := NewRuntime()
ctx := rt.NewContext()
require.NotNil(t, ctx)
t.Cleanup(func() {
ctx.Close()
rt.Close()
})
return ctx
}
t.Run("AtomWithUnicodeStrings", func(t *testing.T) {
ctx := newTestContext(t)
// Test atom creation with various Unicode strings
unicodeStrings := []string{
"中文测试",
"🚀 emoji test",
"café ñoño",
"Здравствуй мир",
"こんにちは世界",
}
for _, unicodeStr := range unicodeStrings {
atom := ctx.NewAtom(unicodeStr) // Changed: Atom() → NewAtom()
defer atom.Free()
// Test ToString method with Unicode
result := atom.ToString() // Changed: String() → ToString()
require.Equal(t, unicodeStr, result)
// Test ToValue method with Unicode
val := atom.ToValue() // Changed: Value() → ToValue()
defer val.Free()
require.Equal(t, unicodeStr, val.ToString()) // Changed: String() → ToString()
}
})
t.Run("AtomWithSpecialCharacters", func(t *testing.T) {
ctx := newTestContext(t)
// Test atoms with special characters that could cause issues
specialStrings := []string{
"", // empty string
" ", // single space
"\n\t\r", // whitespace characters
"null", // JavaScript keyword
"undefined", // JavaScript keyword
"constructor", // JavaScript property name
"__proto__", // Special property
"toString", // Method name
}
for _, specialStr := range specialStrings {
atom := ctx.NewAtom(specialStr) // Changed: Atom() → NewAtom()
defer atom.Free()
require.Equal(t, specialStr, atom.ToString()) // Changed: String() → ToString()
val := atom.ToValue() // Changed: Value() → ToValue()
defer val.Free()
require.Equal(t, specialStr, val.ToString()) // Changed: String() → ToString()
}
})
t.Run("AtomWithLargeIndexes", func(t *testing.T) {
ctx := newTestContext(t)
// Test atoms created with various index values
indexes := []uint32{
0,
1,
42,
100,
1000,
10000,
100000,
1000000,
4294967295, // max uint32
}
for _, index := range indexes {
atom := ctx.NewAtomIdx(index) // Changed: AtomIdx() → NewAtomIdx()
defer atom.Free()
expected := fmt.Sprintf("%d", index)
require.Equal(t, expected, atom.ToString()) // Changed: String() → ToString()
val := atom.ToValue() // Changed: Value() → ToValue()
defer val.Free()
require.Equal(t, expected, val.ToString()) // Changed: String() → ToString()
}
})
t.Run("AtomConsistency", func(t *testing.T) {
ctx := newTestContext(t)
// Test that atoms with same content are handled consistently
testString := "consistency_test"
atom1 := ctx.NewAtom(testString) // Changed: Atom() → NewAtom()
atom2 := ctx.NewAtom(testString) // Changed: Atom() → NewAtom()
defer atom1.Free()
defer atom2.Free()
// Both should return the same string
require.Equal(t, atom1.ToString(), atom2.ToString()) // Changed: String() → ToString()
// Values should also be equal
val1 := atom1.ToValue() // Changed: Value() → ToValue()
val2 := atom2.ToValue() // Changed: Value() → ToValue()
defer val1.Free()
defer val2.Free()
require.Equal(t, val1.ToString(), val2.ToString()) // Changed: String() → ToString()
})
t.Run("AtomPropertyAccess", func(t *testing.T) {
ctx := newTestContext(t)
// Test using atoms for property access
obj := ctx.NewObject() // Changed: Object() → NewObject()
defer obj.Free()
propName := "dynamicProperty"
propValue := "test value"
atom := ctx.NewAtom(propName) // Changed: Atom() → NewAtom()
defer atom.Free()
// Set property using atom string
obj.Set(atom.ToString(), ctx.NewString(propValue)) // Changed: String() → ToString(), String() → NewString()
// Get property back
retrievedValue := obj.Get(atom.ToString()) // Changed: String() → ToString()
defer retrievedValue.Free()
require.Equal(t, propValue, retrievedValue.ToString()) // Changed: String() → ToString()
// Also test using ToValue for property access
atomValue := atom.ToValue() // Changed: Value() → ToValue()
defer atomValue.Free()
// Verify the atom value is correct
require.Equal(t, propName, atomValue.ToString()) // Changed: String() → ToString()
})
}
func TestAtomDupAndContextAtomFromValue(t *testing.T) {
rt := NewRuntime()
defer rt.Close()
ctx := rt.NewContext()
defer ctx.Close()
name := ctx.NewString("dup-key")
defer name.Free()
atom := ctx.AtomFromValue(name)
require.NotNil(t, atom)
defer atom.Free()
require.Equal(t, "dup-key", atom.ToString())
dup := atom.Dup()
require.NotNil(t, dup)
defer dup.Free()
require.Equal(t, atom.ToString(), dup.ToString())
ctx2 := rt.NewContext()
defer ctx2.Close()
foreignVal := ctx2.NewString("foreign")
defer foreignVal.Free()
require.Nil(t, ctx.AtomFromValue(foreignVal))
obj := ctx.NewObject()
defer obj.Free()
objAtom := ctx.AtomFromValue(obj)
require.NotNil(t, objAtom)
defer objAtom.Free()
require.Equal(t, "[object Object]", objAtom.ToString())
badKey := ctx.Eval(`({ toString(){ throw new Error('boom') } })`)
defer badKey.Free()
require.False(t, badKey.IsException())
require.Nil(t, ctx.AtomFromValue(badKey))
require.Error(t, ctx.Exception())
var nilAtom *Atom
require.Nil(t, nilAtom.Dup())
var nilCtx *Context
require.Nil(t, nilCtx.AtomFromValue(name))
closedRT := NewRuntime()
closedCtx := closedRT.NewContext()
closedVal := closedCtx.NewString("closed")
closedCtx.Close()
require.Nil(t, closedCtx.AtomFromValue(closedVal))
closedRT.Close()
}