-
Notifications
You must be signed in to change notification settings - Fork 535
/
value_test.go
67 lines (58 loc) · 1.1 KB
/
value_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
package lorca
import (
"bytes"
"encoding/json"
"errors"
"testing"
)
var errTest = errors.New("fail")
func TestValueError(t *testing.T) {
v := value{err: errTest}
if v.Err() != errTest {
t.Fail()
}
v = value{raw: json.RawMessage(`"hello"`)}
if v.Err() != nil {
t.Fail()
}
}
func TestValuePrimitive(t *testing.T) {
v := value{raw: json.RawMessage(`42`)}
if v.Int() != 42 {
t.Fail()
}
v = value{raw: json.RawMessage(`"hello"`)}
if v.Int() != 0 || v.String() != "hello" {
t.Fail()
}
v = value{err: errTest}
if v.Int() != 0 || v.String() != "" {
t.Fail()
}
}
func TestValueComplex(t *testing.T) {
v := value{raw: json.RawMessage(`["foo", 42.3, {"x": 5}]`)}
if len(v.Array()) != 3 {
t.Fail()
}
if v.Array()[0].String() != "foo" {
t.Fail()
}
if v.Array()[1].Float() != 42.3 {
t.Fail()
}
if v.Array()[2].Object()["x"].Int() != 5 {
t.Fail()
}
}
func TestRawValue(t *testing.T) {
var v Value
v = value{raw: json.RawMessage(nil)}
if v.Bytes() != nil {
t.Fail()
}
v = value{raw: json.RawMessage(`"hello"`)}
if !bytes.Equal(v.Bytes(), []byte(`"hello"`)) {
t.Fail()
}
}