forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_go_struct_test.go
78 lines (64 loc) · 1.5 KB
/
type_go_struct_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
package otto
import (
"testing"
)
func TestGoStructEmbeddedFields(t *testing.T) {
type A struct {
A1 string `json:"a1"`
A2 string `json:"a2"`
A3 string `json:"a3"`
}
type B struct {
A
B1 string `json:"b1"`
}
tt(t, func() {
test, vm := test()
var b B
b.A1 = "a1"
b.A2 = "a2"
b.A3 = "a3"
b.B1 = "b1"
vm.Set("v", B{A{"a1", "a2", "a3"}, "b1"})
test(`[v.a1,v.a2,v.a3,v.b1]`, "a1,a2,a3,b1")
})
}
func TestGoStructNilBoolPointerField(t *testing.T) {
type S struct {
A int `json:"a"`
B *bool `json:"b"`
C interface{} `json:"c"`
}
tt(t, func() {
test, vm := test()
vm.Set("s", S{A: 1, B: nil, C: nil})
test(`'a' in s`, true)
test(`typeof s.a`, "number")
test(`'b' in s`, true)
test(`typeof s.b`, "undefined")
test(`'c' in s`, true)
test(`typeof s.c`, "undefined")
})
}
func TestGoStructError(t *testing.T) {
type S1 struct {
A string `json:"a"`
B string `json:"b"`
}
type S2 struct {
A []S1 `json:"a"`
B S1 `json:"b"`
}
type S3 struct {
A []S2 `json:"a"`
B S2 `json:"b"`
}
tt(t, func() {
test, vm := test()
vm.Set("fn", func(s *S3) string { return "cool" })
test(
`(function() { try { fn({a:[{a:[{c:"x"}]}]}) } catch (ex) { return ex } })()`,
`TypeError: can't convert to *otto.S3: couldn't convert property "a" of otto.S3: couldn't convert element 0 of []otto.S2: couldn't convert property "a" of otto.S2: couldn't convert element 0 of []otto.S1: can't convert property "c" of otto.S1: field does not exist`,
)
})
}