-
Notifications
You must be signed in to change notification settings - Fork 0
/
arb_test.go
68 lines (62 loc) · 1.55 KB
/
arb_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
package arb
import (
"os"
"strings"
"testing"
)
// TestRead tests reading from an io.Reader
func TestRead(t *testing.T) {
jsonString := "{ \"key\": \"value\" }"
expected := "value"
reader := strings.NewReader(jsonString)
arb, err := Read(reader)
if err != nil {
t.Fatal("failed to read json string from reader:", err)
}
if arb["key"] != expected {
t.Fatalf("unexpected value for key %s, expected %s", arb["key"], expected)
}
}
// TestReadBytes tests reading from a byte slice
func TestReadBytes(t *testing.T) {
jsonString := "{ \"key\": \"value\" }"
expected := "value"
arb, err := ReadBytes([]byte(jsonString))
if err != nil {
t.Fatal("failed to read json string from bytes:", err)
}
if arb["key"] != expected {
t.Fatalf("unexpected value for key %s, expected %s", arb["key"], expected)
}
}
// TestWrite tests writing from an io.Write
func TestWrite(t *testing.T) {
arb := New()
arb["hello"] = "world"
arb["number"] = 2222
arb["nothing"] = nil
child := New()
child["isChild"] = true
arb["child"] = child
err := arb.Write(os.Stdout)
if err != nil {
t.Fatal("failed to write:", err)
}
}
// TestReadBytes tests reading from a byte slice
func TestGetString(t *testing.T) {
arb := New()
arb["hello"] = "world"
arb["number"] = 2222
result, err := arb.GetString("hello")
expected := "world"
if err != nil {
t.Fatal("failed get string:", err)
}
if result != expected {
t.Fatalf("result was %s, expected %s", result, expected)
}
if _, err := arb.GetString("number"); err == nil {
t.Fatalf("number %d is not a string", arb["number"])
}
}