forked from lazytiger/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 1
/
v8_util_test.go
76 lines (55 loc) · 1.64 KB
/
v8_util_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
package v8
import "testing"
import "runtime"
func Test_JSON(t *testing.T) {
engine.NewContext(nil).Scope(func(cs ContextScope) {
json := `{"a":1,"b":2,"c":"xyz","e":true,"f":false,"g":null,"h":[4,5,6]}`
value := cs.ParseJSON(json)
if value == nil {
t.Fatal(`value == nil`)
}
if value.IsObject() == false {
t.Fatal(`value == false`)
}
if string(ToJSON(value)) != json {
t.Fatal(`string(ToJSON(value)) != json`)
}
object := value.ToObject()
if object.GetProperty("a").ToInt32() != 1 {
t.Fatal(`object.GetProperty("a").ToInt32() != 1`)
}
if object.GetProperty("b").ToInt32() != 2 {
t.Fatal(`object.GetProperty("b").ToInt32() != 2`)
}
if object.GetProperty("c").ToString() != "xyz" {
t.Fatal(`object.GetProperty("c").ToString() != "xyz"`)
}
if object.GetProperty("e").IsTrue() == false {
t.Fatal(`object.GetProperty("e").IsTrue() == false`)
}
if object.GetProperty("f").IsFalse() == false {
t.Fatal(`object.GetProperty("f").IsFalse() == false`)
}
if object.GetProperty("g").IsNull() == false {
t.Fatal(`object.GetProperty("g").IsNull() == false`)
}
array := object.GetProperty("h").ToArray()
if array.Length() != 3 {
t.Fatal(`array.Length() != 3`)
}
if array.GetElement(0).ToInt32() != 4 {
t.Fatal(`array.GetElement(0).ToInt32() != 4`)
}
if array.GetElement(1).ToInt32() != 5 {
t.Fatal(`array.GetElement(1).ToInt32() != 5`)
}
if array.GetElement(2).ToInt32() != 6 {
t.Fatal(`array.GetElement(2).ToInt32() != 6`)
}
json = `"\"\/\r\n\t\b\\"`
if string(ToJSON(cs.ParseJSON(json))) != json {
t.Fatal(`ToJSON(cs.ParseJSON(json)) != json`)
}
})
runtime.GC()
}