Repository navigation
Expand file tree
/
Copy pathjson_test.go
More file actions
106 lines (81 loc) · 2.44 KB
/
Copy pathjson_test.go
File metadata and controls
106 lines (81 loc) · 2.44 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
99
100
101
102
103
104
105
106
package main
import (
"bytes"
"errors"
"testing"
)
type successPayload struct {
Name string `json:"name"`
Count int `json:"count"`
}
func TestWriteJSONCompact(t *testing.T) {
t.Parallel()
var stdout bytes.Buffer
err := writeJSON(&stdout, successPayload{Name: "alpha", Count: 2}, false)
if err != nil {
t.Fatalf("writeJSON returned error: %v", err)
}
want := "{\"name\":\"alpha\",\"count\":2}\n"
if stdout.String() != want {
t.Fatalf("stdout = %q, want %q", stdout.String(), want)
}
}
func TestWriteJSONPretty(t *testing.T) {
t.Parallel()
var stdout bytes.Buffer
err := writeJSON(&stdout, successPayload{Name: "alpha", Count: 2}, true)
if err != nil {
t.Fatalf("writeJSON returned error: %v", err)
}
want := "{\n \"name\": \"alpha\",\n \"count\": 2\n}\n"
if stdout.String() != want {
t.Fatalf("stdout = %q, want %q", stdout.String(), want)
}
}
func TestWriteErrorJSONCompact(t *testing.T) {
t.Parallel()
var stderr bytes.Buffer
err := writeErrorJSON(&stderr, errorCodeRuntime, "boom", false)
if err != nil {
t.Fatalf("writeErrorJSON returned error: %v", err)
}
want := "{\"error\":{\"code\":\"runtime_error\",\"message\":\"boom\"}}\n"
if stderr.String() != want {
t.Fatalf("stderr = %q, want %q", stderr.String(), want)
}
}
func TestWriteErrorJSONPretty(t *testing.T) {
t.Parallel()
var stderr bytes.Buffer
err := writeErrorJSON(&stderr, errorCodeUsage, "missing command", true)
if err != nil {
t.Fatalf("writeErrorJSON returned error: %v", err)
}
want := "{\n \"error\": {\n \"code\": \"usage_error\",\n \"message\": \"missing command\"\n }\n}\n"
if stderr.String() != want {
t.Fatalf("stderr = %q, want %q", stderr.String(), want)
}
}
func TestMutationResultJSON(t *testing.T) {
t.Parallel()
result := mutationResult{File: "book.xlsx", Operation: operationCellSet, Success: true}
jsonBytes, err := marshalJSON(result, false)
if err != nil {
t.Fatalf("marshalJSON returned error: %v", err)
}
want := "{\"file\":\"book.xlsx\",\"operation\":\"cell_set\",\"success\":true}\n"
if string(jsonBytes) != want {
t.Fatalf("mutation JSON = %q, want %q", string(jsonBytes), want)
}
}
func TestWriteJSONWriteError(t *testing.T) {
t.Parallel()
err := writeJSON(errWriter{}, successPayload{Name: "alpha", Count: 2}, false)
if err == nil {
t.Fatal("writeJSON error = nil, want non-nil")
}
}
type errWriter struct{}
func (errWriter) Write(_ []byte) (int, error) {
return 0, errors.New("write failed")
}