-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathatom_deprecated_test.go
More file actions
297 lines (246 loc) · 7.84 KB
/
atom_deprecated_test.go
File metadata and controls
297 lines (246 loc) · 7.84 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
package quickjs
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestDeprecatedAtomAPIs tests all deprecated Atom methods to ensure they still work
// Each deprecated method is called once for test coverage
func TestDeprecatedAtomAPIs(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("DeprecatedAtomStringMethod", func(t *testing.T) {
ctx := newTestContext(t)
// Test deprecated String() method on Atom
atom := ctx.NewAtom("test atom")
defer atom.Free()
// Test deprecated String() method
result1 := atom.String()
require.Equal(t, "test atom", result1)
// Compare with new ToString() method
result2 := atom.ToString()
require.Equal(t, "test atom", result2)
// Both should return the same value
require.Equal(t, result1, result2)
})
t.Run("DeprecatedAtomValueMethod", func(t *testing.T) {
ctx := newTestContext(t)
// Test deprecated Value() method on Atom
atom := ctx.NewAtom("value test")
defer atom.Free()
// Test deprecated Value() method
val1 := atom.Value()
defer val1.Free()
require.True(t, val1.IsString())
require.Equal(t, "value test", val1.ToString())
// Compare with new ToValue() method
val2 := atom.ToValue()
defer val2.Free()
require.True(t, val2.IsString())
require.Equal(t, "value test", val2.ToString())
// Both should create equivalent values
require.Equal(t, val1.ToString(), val2.ToString())
})
t.Run("DeprecatedAtomMethods", func(t *testing.T) {
// Test various scenarios with deprecated Atom methods
testCases := []string{
"simple",
"with spaces",
"with-dashes",
"with_underscores",
"UPPERCASE",
"mixedCase",
"123numbers",
"special!@#chars",
"", // empty string
}
for _, testCase := range testCases {
t.Run("AtomCase_"+testCase, func(t *testing.T) {
ctx := newTestContext(t)
atom := ctx.NewAtom(testCase)
defer atom.Free()
// Test deprecated String() method
stringResult := atom.String()
require.Equal(t, testCase, stringResult)
// Test deprecated Value() method
valueResult := atom.Value()
defer valueResult.Free()
require.Equal(t, testCase, valueResult.ToString())
})
}
})
t.Run("DeprecatedAtomWithPropertyAccess", func(t *testing.T) {
ctx := newTestContext(t)
// Test deprecated Atom methods in property access scenarios
obj := ctx.NewObject()
defer obj.Free()
atom := ctx.NewAtom("testProperty")
defer atom.Free()
// Set property using atom string (corrected)
propertyName := atom.String() // deprecated method
obj.Set(propertyName, ctx.NewString("property value"))
// Get property using deprecated atom methods
retrievedValue := obj.Get(propertyName)
defer retrievedValue.Free()
require.Equal(t, "property value", retrievedValue.ToString())
// Also test with deprecated Value() method
atomValue := atom.Value() // deprecated method
defer atomValue.Free()
require.Equal(t, "testProperty", atomValue.ToString())
})
t.Run("DeprecatedAtomComparison", func(t *testing.T) {
ctx := newTestContext(t)
// Test that deprecated methods work consistently
atom1 := ctx.NewAtom("comparison test")
atom2 := ctx.NewAtom("comparison test")
defer atom1.Free()
defer atom2.Free()
// Using deprecated String() method
str1 := atom1.String()
str2 := atom2.String()
require.Equal(t, str1, str2)
// Using deprecated Value() method
val1 := atom1.Value()
val2 := atom2.Value()
defer val1.Free()
defer val2.Free()
require.Equal(t, val1.ToString(), val2.ToString())
})
}
// TestDeprecatedAtomEdgeCases tests edge cases with deprecated Atom methods
func TestDeprecatedAtomEdgeCases(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("DeprecatedAtomWithUnicodeStrings", func(t *testing.T) {
// Test deprecated methods with Unicode strings
unicodeStrings := []string{
"中文测试",
"🚀 emoji test",
"café ñoño",
"Здравствуй мир",
"こんにちは世界",
}
for _, unicodeStr := range unicodeStrings {
t.Run("Unicode_"+unicodeStr, func(t *testing.T) {
ctx := newTestContext(t)
atom := ctx.NewAtom(unicodeStr)
defer atom.Free()
// Test deprecated String() method with Unicode
result := atom.String()
require.Equal(t, unicodeStr, result)
// Test deprecated Value() method with Unicode
val := atom.Value()
defer val.Free()
require.Equal(t, unicodeStr, val.ToString())
})
}
})
t.Run("DeprecatedAtomMemoryManagement", func(t *testing.T) {
ctx := newTestContext(t)
// Test that deprecated methods don't cause memory issues
for i := 0; i < 100; i++ {
atom := ctx.NewAtom("memory test " + string(rune('A'+i%26)))
// Use deprecated methods
_ = atom.String()
val := atom.Value()
val.Free()
atom.Free()
}
// If we reach here without crashing, memory management is working
require.True(t, true)
})
t.Run("DeprecatedAtomWithLongStrings", func(t *testing.T) {
ctx := newTestContext(t)
// Test deprecated methods with very long strings
longString := ""
for i := 0; i < 1000; i++ {
longString += "test"
}
atom := ctx.NewAtom(longString)
defer atom.Free()
// Test deprecated String() method with long string
result := atom.String()
require.Equal(t, longString, result)
require.Len(t, result, 4000) // 1000 * "test"
// Test deprecated Value() method with long string
val := atom.Value()
defer val.Free()
require.Equal(t, longString, val.ToString())
})
}
// TestDeprecatedAtomInteractionWithContext tests how deprecated Atom methods interact with Context
func TestDeprecatedAtomInteractionWithContext(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("DeprecatedAtomInJavaScriptCode", func(t *testing.T) {
ctx := newTestContext(t)
// Create atom using new API
atom := ctx.NewAtom("globalTestVar")
defer atom.Free()
// Use deprecated String() method to get property name
propName := atom.String()
// Set global variable using the property name from deprecated method
ctx.Globals().Set(propName, ctx.NewString("test value"))
// Verify the property was set correctly
result := ctx.Eval("globalTestVar")
defer result.Free()
require.Equal(t, "test value", result.ToString())
})
t.Run("DeprecatedAtomValueInJavaScript", func(t *testing.T) {
ctx := newTestContext(t)
// Create atom
atom := ctx.NewAtom("javascript interaction")
defer atom.Free()
// Use deprecated Value() method to create JavaScript value
atomValue := atom.Value() // deprecated method
// Set as global variable
ctx.Globals().Set("atomStringValue", atomValue)
// Use in JavaScript
result := ctx.Eval("atomStringValue + ' extended'")
defer result.Free()
require.Equal(t, "javascript interaction extended", result.ToString())
})
t.Run("DeprecatedAtomWithJavaScriptExecution", func(t *testing.T) {
ctx := newTestContext(t)
// Test using deprecated atom methods in JavaScript execution context
atom := ctx.NewAtom("dynamicProperty")
defer atom.Free()
// Create an object
obj := ctx.NewObject()
// Use deprecated String() method to set property
propName := atom.String()
obj.Set(propName, ctx.NewString("dynamic value"))
// Set object as global
ctx.Globals().Set("testObj", obj)
// Use deprecated Value() method in JavaScript context
atomValue := atom.Value()
ctx.Globals().Set("propNameValue", atomValue)
// Test accessing the property using the atom value
result := ctx.Eval("testObj[propNameValue]")
defer result.Free()
require.Equal(t, "dynamic value", result.ToString())
})
}