forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_goslice_reflect_test.go
162 lines (140 loc) · 2.7 KB
/
object_goslice_reflect_test.go
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
package goja
import "testing"
func TestGoSliceReflectBasic(t *testing.T) {
const SCRIPT = `
var sum = 0;
for (var i = 0; i < a.length; i++) {
sum += a[i];
}
sum;
`
r := New()
r.Set("a", []int{1, 2, 3, 4})
v, err := r.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if i := v.ToInteger(); i != 10 {
t.Fatalf("Expected 10, got: %d", i)
}
}
func TestGoSliceReflectIn(t *testing.T) {
const SCRIPT = `
var idx = "";
for (var i in a) {
idx += i;
}
idx;
`
r := New()
r.Set("a", []int{1, 2, 3, 4})
v, err := r.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if i := v.String(); i != "0123" {
t.Fatalf("Expected '0123', got: '%s'", i)
}
}
func TestGoSliceReflectSet(t *testing.T) {
const SCRIPT = `
a[0] = 33;
a[1] = 333;
a[2] = "42";
a[3] = {};
a[4] = 0;
`
r := New()
a := []int8{1, 2, 3, 4}
r.Set("a", a)
_, err := r.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if a[0] != 33 {
t.Fatalf("a[0] = %d, expected 33", a[0])
}
if a[1] != 77 {
t.Fatalf("a[1] = %d, expected 77", a[0])
}
if a[2] != 42 {
t.Fatalf("a[2] = %d, expected 42", a[0])
}
if a[3] != 0 {
t.Fatalf("a[3] = %d, expected 0", a[0])
}
}
func TestGoSliceReflectPush(t *testing.T) {
r := New()
t.Run("Can push to array by array ptr", func(t *testing.T) {
a := []int8{1}
r.Set("a", &a)
_, err := r.RunString(`a.push (10)`)
if err != nil {
t.Fatal(err)
}
if a[1] != 10 {
t.Fatalf("a[1] = %d, expected 10", a[1])
}
})
t.Run("Can push to array by struct ptr", func(t *testing.T) {
type testStr struct {
A []int
}
a := testStr{
A: []int{2},
}
r.Set("a", &a)
_, err := r.RunString(`a.A.push (10)`)
if err != nil {
t.Fatal(err)
}
if a.A[1] != 10 {
t.Fatalf("a[1] = %v, expected 10", a)
}
})
}
func TestGoSliceReflectProto(t *testing.T) {
const SCRIPT = `
a.join(",")
`
r := New()
a := []int8{1, 2, 3, 4}
r.Set("a", a)
ret, err := r.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if s := ret.String(); s != "1,2,3,4" {
t.Fatalf("Unexpected result: '%s'", s)
}
}
type gosliceReflect_withMethods []interface{}
func (s gosliceReflect_withMethods) Method() bool {
return true
}
func TestGoSliceReflectMethod(t *testing.T) {
const SCRIPT = `
typeof a === "object" && a[0] === 42 && a.Method() === true;
`
vm := New()
a := make(gosliceReflect_withMethods, 1)
a[0] = 42
vm.Set("a", a)
v, err := vm.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if !v.StrictEquals(valueTrue) {
t.Fatalf("Expected true, got %v", v)
}
}
func TestGoSliceReflectGetStr(t *testing.T) {
r := New()
v := r.ToValue([]string{"test"})
if o, ok := v.(*Object); ok {
if e := o.Get("0").Export(); e != "test" {
t.Fatalf("Unexpected o.Get(\"0\"): %v", e)
}
}
}