Skip to content

Commit 13d2816

Browse files
author
Terry Howe
authored
chore: Add json test coverage (oras-project#1425)
Signed-off-by: Terry Howe <[email protected]>
1 parent 780c7da commit 13d2816

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Copyright The ORAS Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package output
17+
18+
import (
19+
"strings"
20+
"testing"
21+
)
22+
23+
func Test_PrintPrettyJSON(t *testing.T) {
24+
builder := &strings.Builder{}
25+
given := map[string]int{"bob": 5}
26+
expected := "{\n \"bob\": 5\n}\n"
27+
err := PrintPrettyJSON(builder, given)
28+
if err != nil {
29+
t.Error("Expected no error got <" + err.Error() + ">")
30+
}
31+
actual := builder.String()
32+
if expected != actual {
33+
t.Error("Expected <" + expected + "> not equal to actual <" + actual + ">")
34+
}
35+
}
36+
37+
func Test_PrintJSON(t *testing.T) {
38+
builder := &strings.Builder{}
39+
given := []byte("{\"bob\":5}")
40+
expected := "{\n \"bob\": 5\n}\n"
41+
err := PrintJSON(builder, given, true)
42+
if err != nil {
43+
t.Error("Expected no error got <" + err.Error() + ">")
44+
}
45+
actual := builder.String()
46+
if expected != actual {
47+
t.Error("Expected <" + expected + "> not equal to actual <" + actual + ">")
48+
}
49+
}
50+
51+
func Test_PrintJSON_ugly(t *testing.T) {
52+
builder := &strings.Builder{}
53+
given := []byte("{\"bob\":5}")
54+
expected := "{\"bob\":5}"
55+
err := PrintJSON(builder, given, false)
56+
if err != nil {
57+
t.Error("Expected no error got <" + err.Error() + ">")
58+
}
59+
actual := builder.String()
60+
if expected != actual {
61+
t.Error("Expected <" + expected + "> not equal to actual <" + actual + ">")
62+
}
63+
}
64+
65+
func Test_ToMap(t *testing.T) {
66+
type test struct {
67+
Name string `json:"name"`
68+
Number int `json:"number"`
69+
}
70+
given := test{Name: "bob", Number: 5}
71+
actual, err := ToMap(given)
72+
if err != nil {
73+
t.Error("Expected no error got <" + err.Error() + ">")
74+
}
75+
value, ok := actual["name"]
76+
if ok == false {
77+
t.Errorf("Expected key name does not exist %v", actual)
78+
}
79+
if value != "bob" {
80+
t.Errorf("Expected value bob not equal to actual %v", value)
81+
}
82+
value, ok = actual["number"]
83+
if ok == false {
84+
t.Errorf("Expected key number does not exist %v", actual)
85+
}
86+
if value != 5.0 {
87+
t.Errorf("Expected value 5 not equal to actual %v", value)
88+
}
89+
for k := range actual {
90+
switch k {
91+
case "name":
92+
case "number":
93+
default:
94+
t.Error("Expected key name or number not equal to actual <" + k + ">")
95+
}
96+
}
97+
}
98+
99+
func Test_ToMap_error(t *testing.T) {
100+
type testError struct {
101+
Name string `json:"name"`
102+
Cycle *testError `json:"cycle"`
103+
}
104+
given := testError{Name: "bob"}
105+
given.Cycle = &given
106+
_, err := ToMap(given)
107+
if err == nil {
108+
t.Error("Expected error")
109+
}
110+
}

0 commit comments

Comments
 (0)