-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathreflect_test.go
121 lines (104 loc) · 2.7 KB
/
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
package gojs
import (
"testing"
)
func TestNewValueWithNil(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
val := ctx.NewValue(nil)
if val.Type() != TypeNull {
t.Errorf("ctx.ValueType did not return TypeNull")
}
if !val.IsNull() {
t.Errorf("ctx.IsNull did not return true")
}
}
func TestNewValueWithInt(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
val := ctx.NewValue(int(4))
if val.Type() != TypeNumber {
t.Errorf("ctx.ValueType did not return TypeNumber")
}
if !val.IsNumber() {
t.Errorf("ctx.IsNumber did not return true")
}
if val.ToNumberOrDie() != 4 {
t.Errorf("ctx.ToNumberOrDie did not return correct value")
}
}
func TestNewValueWithUint(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
val := ctx.NewValue(uint(4))
if val.Type() != TypeNumber {
t.Errorf("ctx.ValueType did not return TypeNumber")
}
if !val.IsNumber() {
t.Errorf("ctx.IsNumber did not return true")
}
if val.ToNumberOrDie() != 4 {
t.Errorf("ctx.ToNumberOrDie did not return correct value")
}
}
func TestNewValueWithFloat(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
val := ctx.NewValue(4.5)
if val.Type() != TypeNumber {
t.Errorf("ctx.ValueType did not return TypeNumber")
}
if !val.IsNumber() {
t.Errorf("ctx.IsNumber did not return true")
}
if val.ToNumberOrDie() != 4.5 {
t.Errorf("ctx.ToNumberOrDie did not return correct value")
}
}
func TestNewValueWithString(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
val := ctx.NewValue("Some text.")
if val.Type() != TypeString {
t.Errorf("ctx.ValueType did not return TypeString")
}
if !val.IsString() {
t.Errorf("ctx.IsString did not return true")
}
if val.ToStringOrDie() != "Some text." {
t.Errorf("ctx.ToStringOrDie did not return correct value")
}
}
func TestNewValueWithFunc(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
val := ctx.NewValue(func() int { return 1 })
if val.Type() != TypeObject {
t.Errorf("ctx.ValueType did not return TypeObject")
}
if !val.IsObject() {
t.Errorf("ctx.IsObject did not return true")
}
if !val.ToObjectOrDie().IsFunction() {
t.Errorf("ctx.IsFunction did not return true")
}
val2, err := val.ToObjectOrDie().CallAsFunction(nil, nil)
if err != nil || val2 == nil {
t.Errorf("Error executing native function (%v)", err)
}
if val2.ToNumberOrDie() != 1 {
t.Errorf("Native function did not return the correct value")
}
}
func TestNewValueWithObject(t *testing.T) {
ctx := NewContext()
defer ctx.Release()
obj := &reflect_object{-1, 2, 3.5, "four"}
val := ctx.NewValue(obj)
if val.Type() != TypeObject {
t.Errorf("ctx.ValueType did not return TypeObject")
}
if !val.IsObject() {
t.Errorf("ctx.IsObject did not return true")
}
}