-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjson_test.go
More file actions
98 lines (88 loc) · 2.54 KB
/
json_test.go
File metadata and controls
98 lines (88 loc) · 2.54 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
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
package main
import (
"flag"
"strings"
"testing"
)
func TestHighlightBodyJSON(t *testing.T) {
// Ensure colors are enabled for this test
originalNoColor := noColor
if noColor == nil {
noColor = flag.Bool("no-color", false, "disable colored output")
}
defer func() { noColor = originalNoColor }()
*noColor = false
data := []byte(`{"name":"Alice"}`)
out := string(highlightBody(data, "application/json"))
if out == string(data) {
t.Fatalf("expected JSON body to be highlighted")
}
if !strings.Contains(out, colorKey) || !strings.Contains(out, colorString) {
t.Errorf("highlighted JSON missing colors: %q", out)
}
}
func TestHighlightBodyJSONWithColorsDisabled(t *testing.T) {
// Save original state
originalNoColor := noColor
if noColor == nil {
noColor = flag.Bool("no-color", false, "disable colored output")
}
defer func() { noColor = originalNoColor }()
*noColor = true
data := []byte(`{"name":"Alice"}`)
out := string(highlightBody(data, "application/json"))
// Should still format JSON but without colors
if strings.Contains(out, colorKey) || strings.Contains(out, colorString) {
t.Errorf("highlighted JSON with no-color should not contain color codes: %q", out)
}
// Should still contain the formatted structure
if !strings.Contains(out, `"name"`) || !strings.Contains(out, `"Alice"`) {
t.Errorf("highlighted JSON should still contain the data: %q", out)
}
}
func TestHighlightJSONValue(t *testing.T) {
tests := []struct {
name string
input string
contains []string
}{
{
name: "simple object",
input: `{"key":"value"}`,
contains: []string{`"key"`, `"value"`},
},
{
name: "nested object",
input: `{"outer":{"inner":"value"}}`,
contains: []string{`"outer"`, `"inner"`, `"value"`},
},
{
name: "array",
input: `{"items":[1,2,3]}`,
contains: []string{`"items"`, "1", "2", "3"},
},
{
name: "boolean and null",
input: `{"bool":true,"null":null}`,
contains: []string{`"bool"`, "true", `"null"`, "null"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := highlightJSON([]byte(tt.input))
for _, expected := range tt.contains {
if !strings.Contains(result, expected) {
t.Errorf("Expected %q to contain %q", result, expected)
}
}
})
}
}
func TestHighlightJSONInvalid(t *testing.T) {
data := []byte(`{invalid json}`)
result := highlightJSON(data)
// Should return original data when JSON is invalid
if result != string(data) {
t.Errorf("Invalid JSON should be returned as-is, got: %q", result)
}
}