-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add json test coverage (#1425)
Signed-off-by: Terry Howe <[email protected]>
- Loading branch information
Terry Howe
authored
Jun 22, 2024
1 parent
780c7da
commit 13d2816
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package output | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_PrintPrettyJSON(t *testing.T) { | ||
builder := &strings.Builder{} | ||
given := map[string]int{"bob": 5} | ||
expected := "{\n \"bob\": 5\n}\n" | ||
err := PrintPrettyJSON(builder, given) | ||
if err != nil { | ||
t.Error("Expected no error got <" + err.Error() + ">") | ||
} | ||
actual := builder.String() | ||
if expected != actual { | ||
t.Error("Expected <" + expected + "> not equal to actual <" + actual + ">") | ||
} | ||
} | ||
|
||
func Test_PrintJSON(t *testing.T) { | ||
builder := &strings.Builder{} | ||
given := []byte("{\"bob\":5}") | ||
expected := "{\n \"bob\": 5\n}\n" | ||
err := PrintJSON(builder, given, true) | ||
if err != nil { | ||
t.Error("Expected no error got <" + err.Error() + ">") | ||
} | ||
actual := builder.String() | ||
if expected != actual { | ||
t.Error("Expected <" + expected + "> not equal to actual <" + actual + ">") | ||
} | ||
} | ||
|
||
func Test_PrintJSON_ugly(t *testing.T) { | ||
builder := &strings.Builder{} | ||
given := []byte("{\"bob\":5}") | ||
expected := "{\"bob\":5}" | ||
err := PrintJSON(builder, given, false) | ||
if err != nil { | ||
t.Error("Expected no error got <" + err.Error() + ">") | ||
} | ||
actual := builder.String() | ||
if expected != actual { | ||
t.Error("Expected <" + expected + "> not equal to actual <" + actual + ">") | ||
} | ||
} | ||
|
||
func Test_ToMap(t *testing.T) { | ||
type test struct { | ||
Name string `json:"name"` | ||
Number int `json:"number"` | ||
} | ||
given := test{Name: "bob", Number: 5} | ||
actual, err := ToMap(given) | ||
if err != nil { | ||
t.Error("Expected no error got <" + err.Error() + ">") | ||
} | ||
value, ok := actual["name"] | ||
if ok == false { | ||
t.Errorf("Expected key name does not exist %v", actual) | ||
} | ||
if value != "bob" { | ||
t.Errorf("Expected value bob not equal to actual %v", value) | ||
} | ||
value, ok = actual["number"] | ||
if ok == false { | ||
t.Errorf("Expected key number does not exist %v", actual) | ||
} | ||
if value != 5.0 { | ||
t.Errorf("Expected value 5 not equal to actual %v", value) | ||
} | ||
for k := range actual { | ||
switch k { | ||
case "name": | ||
case "number": | ||
default: | ||
t.Error("Expected key name or number not equal to actual <" + k + ">") | ||
} | ||
} | ||
} | ||
|
||
func Test_ToMap_error(t *testing.T) { | ||
type testError struct { | ||
Name string `json:"name"` | ||
Cycle *testError `json:"cycle"` | ||
} | ||
given := testError{Name: "bob"} | ||
given.Cycle = &given | ||
_, err := ToMap(given) | ||
if err == nil { | ||
t.Error("Expected error") | ||
} | ||
} |