-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintjson_test.go
More file actions
48 lines (43 loc) · 1.08 KB
/
Copy pathprintjson_test.go
File metadata and controls
48 lines (43 loc) · 1.08 KB
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
package pretty
import (
"math"
"testing"
)
func TestAsJSON(t *testing.T) {
t.Run("simple struct", func(t *testing.T) {
data, err := asJSON(struct{ Name string }{Name: "test"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "{\n \"Name\": \"test\"\n}"
if got := string(data); got != want {
t.Errorf("asJSON() = %q, want %q", got, want)
}
})
t.Run("byte slice", func(t *testing.T) {
data, err := asJSON([]byte(`hello`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := `"aGVsbG8="` // base64 encoded
if got := string(data); got != want {
t.Errorf("asJSON() = %q, want %q", got, want)
}
})
t.Run("custom indent", func(t *testing.T) {
data, err := asJSON(map[string]int{"a": 1}, "\t")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "{\n\t\"a\": 1\n}"
if got := string(data); got != want {
t.Errorf("asJSON() = %q, want %q", got, want)
}
})
t.Run("marshal error", func(t *testing.T) {
_, err := asJSON(math.NaN())
if err == nil {
t.Fatal("expected error for NaN, got nil")
}
})
}