Skip to content

Commit cc64275

Browse files
committed
Add output tests
1 parent 17ef495 commit cc64275

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

output/output_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2025 Adobe. All rights reserved.
2+
// This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License. You may obtain a copy
4+
// of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
//
6+
// Unless required by applicable law or agreed to in writing, software distributed under
7+
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8+
// OF ANY KIND, either express or implied. See the License for the specific language
9+
// governing permissions and limitations under the License.
10+
11+
package output
12+
13+
import (
14+
"bytes"
15+
"io"
16+
"os"
17+
"path/filepath"
18+
"testing"
19+
)
20+
21+
// captureStdout captures the output of a function that writes to stdout.
22+
func captureStdout(t *testing.T, fn func()) string {
23+
t.Helper()
24+
old := os.Stdout
25+
r, w, err := os.Pipe()
26+
if err != nil {
27+
t.Fatalf("failed to create pipe: %v", err)
28+
}
29+
os.Stdout = w
30+
31+
fn()
32+
33+
w.Close()
34+
os.Stdout = old
35+
36+
var buf bytes.Buffer
37+
if _, err := io.Copy(&buf, r); err != nil {
38+
t.Fatalf("failed to read pipe: %v", err)
39+
}
40+
return buf.String()
41+
}
42+
43+
// loadExpected reads the expected output from a golden file in testdata/.
44+
func loadExpected(t *testing.T, filename string) string {
45+
t.Helper()
46+
data, err := os.ReadFile(filepath.Join("testdata", filename))
47+
if err != nil {
48+
t.Fatalf("failed to read golden file %s: %v", filename, err)
49+
}
50+
return string(data)
51+
}
52+
53+
func TestPrintPrettyJSON(t *testing.T) {
54+
tests := []struct {
55+
name string
56+
input string
57+
file string
58+
}{
59+
{name: "compact object is prettified", input: `{"name":"John","age":30}`, file: "compact_object.json"},
60+
{name: "already indented JSON is normalized", input: "{\n \"key\": \"value\"\n}", file: "already_indented.json"},
61+
{name: "compact array is prettified", input: `[{"id":1},{"id":2}]`, file: "compact_array.json"},
62+
{name: "empty object", input: `{}`, file: "empty_object.json"},
63+
{name: "empty array", input: `[]`, file: "empty_array.json"},
64+
{name: "nested objects", input: `{"a":{"b":{"c":1}}}`, file: "nested_objects.json"},
65+
{name: "non-JSON is printed as-is", input: "this is not JSON", file: "non_json.txt"},
66+
{name: "empty string is printed as-is", input: "", file: "empty_string.txt"},
67+
}
68+
69+
for _, tt := range tests {
70+
t.Run(tt.name, func(t *testing.T) {
71+
expected := loadExpected(t, tt.file)
72+
73+
got := captureStdout(t, func() {
74+
PrintPrettyJSON(tt.input)
75+
})
76+
77+
if got != expected {
78+
t.Errorf("PrintPrettyJSON(%q)\ngot:\n%s\nexpected output is in testdata/%s", tt.input, got, tt.file)
79+
}
80+
})
81+
}
82+
}

output/testdata/empty_string.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

output/testdata/non_json.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is not JSON

0 commit comments

Comments
 (0)