Skip to content

Commit c5f2783

Browse files
committed
Add some common test cases for formatters and apply to xml, yaml, and json
1 parent c0419b4 commit c5f2783

File tree

10 files changed

+173
-34
lines changed

10 files changed

+173
-34
lines changed

formatters/json.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
package formatters
22

33
import (
4-
"fmt"
4+
"encoding/json"
55

6-
"github.com/nytlabs/mxj"
76
"github.com/stilvoid/please/util"
87
)
98

109
func formatJSON(in interface{}) (string, error) {
1110
in = util.ForceStringKeys(in)
1211

13-
inMap, ok := in.(map[string]interface{})
12+
bytes, err := json.MarshalIndent(in, "", " ")
1413

15-
if !ok {
16-
return fmt.Sprintf("\"%s\"", in), nil
17-
}
18-
19-
m := mxj.Map(inMap)
20-
21-
bytes, err := m.JsonIndent("", " ")
22-
23-
if err != nil {
24-
return "", err
25-
}
26-
27-
return string(bytes), nil
14+
return string(bytes), err
2815
}

formatters/json_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package formatters
2+
3+
import "testing"
4+
5+
func TestJSON(t *testing.T) {
6+
expecteds := []string{
7+
"123",
8+
"456.789",
9+
"\"abc\"",
10+
"true",
11+
"false",
12+
"null",
13+
"[\n 123,\n \"abc\"\n]",
14+
"{\n \"foo\": \"bar\"\n}",
15+
"{\n \"123\": [\n \"baz\",\n \"quux\"\n ]\n}",
16+
"{\n \"true\": {\n \"null\": null\n }\n}",
17+
"[\n 456,\n \"def\",\n {\n \"3\": 4\n }\n]",
18+
}
19+
20+
if len(expecteds) != len(testCases) {
21+
t.Fatalf("insufficient test cases implemented")
22+
}
23+
24+
for i, expected := range expecteds {
25+
testCase := testCases[i]
26+
27+
actual, err := formatJSON(testCase)
28+
29+
if err != nil {
30+
t.Errorf("unexpected error: %v", err)
31+
}
32+
33+
if actual != expected {
34+
t.Errorf("unexpected '%v', want '%v'", actual, expected)
35+
}
36+
}
37+
}

formatters/test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package formatters
2+
3+
var testCases = []interface{}{
4+
123,
5+
456.789,
6+
"abc",
7+
true,
8+
false,
9+
nil,
10+
[]interface{}{123, "abc"},
11+
map[interface{}]interface{}{ // A map
12+
"foo": "bar", // To a value
13+
},
14+
map[interface{}]interface{}{ // A map
15+
123: []interface{}{"baz", "quux"}, // To an array
16+
},
17+
map[interface{}]interface{}{ // A map
18+
true: map[interface{}]interface{}{ // To another map
19+
nil: nil,
20+
},
21+
},
22+
[]interface{}{ // An array
23+
456, // Of values
24+
"def",
25+
map[interface{}]interface{}{ // With a map
26+
3: 4,
27+
},
28+
},
29+
}

formatters/xml.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package formatters
22

33
import (
4+
"reflect"
5+
46
"github.com/clbanning/anyxml"
57
"github.com/stilvoid/please/util"
68
)
79

810
func formatXML(in interface{}) (string, error) {
911
in = util.ForceStringKeys(in)
1012

13+
if reflect.TypeOf(in) == nil {
14+
in = ""
15+
}
16+
1117
bytes, err := anyxml.XmlIndent(in, "", " ")
1218

1319
if err != nil {

formatters/xml_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package formatters
2+
3+
import "testing"
4+
5+
func TestXML(t *testing.T) {
6+
expecteds := []string{
7+
"<doc>123</doc>",
8+
"<doc>456.789</doc>",
9+
"<doc>abc</doc>",
10+
"<doc>true</doc>",
11+
"<doc>false</doc>",
12+
"<doc></doc>",
13+
"<doc>\n <element>123</element>\n <element>abc</element>\n</doc>",
14+
"<foo>bar</foo>",
15+
"<doc>\n <123>baz</123>\n <123>quux</123>\n</doc>",
16+
"<true>\n<null/>\n</true>",
17+
"<doc>\n <element>456</element>\n <element>def</element>\n <3>4</3>\n</doc>",
18+
}
19+
20+
if len(expecteds) != len(testCases) {
21+
//t.Fatalf("insufficient test cases implemented")
22+
}
23+
24+
for i, expected := range expecteds {
25+
testCase := testCases[i]
26+
27+
actual, err := formatXML(testCase)
28+
29+
if err != nil {
30+
t.Errorf("unexpected error: %v", err)
31+
}
32+
33+
if actual != expected {
34+
t.Errorf("unexpected '%v', want '%v'", actual, expected)
35+
}
36+
}
37+
}

formatters/yaml.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
package formatters
22

3-
import (
4-
"github.com/stilvoid/please/util"
5-
"gopkg.in/yaml.v2"
6-
)
3+
import "gopkg.in/yaml.v2"
74

85
func formatYAML(in interface{}) (string, error) {
9-
in = util.ForceStringKeys(in)
10-
116
bytes, err := yaml.Marshal(in)
127

13-
if err != nil {
14-
return "", err
15-
}
16-
17-
return string(bytes), nil
8+
// We strip off the trailing newline that yaml.v2 seems to insist on
9+
return string(bytes[:len(bytes)-1]), err
1810
}

formatters/yaml_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package formatters
2+
3+
import "testing"
4+
5+
func TestYAML(t *testing.T) {
6+
expecteds := []string{
7+
"123",
8+
"456.789",
9+
"abc",
10+
"true",
11+
"false",
12+
"null",
13+
"- 123\n- abc",
14+
"foo: bar",
15+
"123:\n- baz\n- quux",
16+
"true:\n null: null",
17+
"- 456\n- def\n- 3: 4",
18+
}
19+
20+
if len(expecteds) != len(testCases) {
21+
t.Fatalf("insufficient test cases implemented")
22+
}
23+
24+
for i, expected := range expecteds {
25+
testCase := testCases[i]
26+
27+
actual, err := formatYAML(testCase)
28+
29+
if err != nil {
30+
t.Errorf("unexpected error: %v", err)
31+
}
32+
33+
if actual != expected {
34+
t.Errorf("unexpected '%v', want '%v'", actual, expected)
35+
}
36+
}
37+
}

util/filter_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@ func TestFilter(t *testing.T) {
1212
"top": "I am the top",
1313
"bottom": map[int]interface{}{
1414
16: "left",
15-
13: []string{
15+
13: []interface{}{
1616
"right 1",
17-
"right 2",
17+
map[string]interface{}{
18+
"deeper": "we go",
19+
},
1820
},
1921
},
2022
}
2123

2224
cases := map[string]interface{}{
2325
"top": "I am the top",
24-
"bottom.16": "left",
25-
"bottom.13.1": "right 2",
26+
"bottom": input["bottom"],
27+
"bottom.16": input["bottom"].(map[int]interface{})[16],
28+
"bottom.13": input["bottom"].(map[int]interface{})[13],
29+
"bottom.13.0": input["bottom"].(map[int]interface{})[13].([]interface{})[0],
30+
"bottom.13.1": input["bottom"].(map[int]interface{})[13].([]interface{})[1],
2631
}
2732

2833
for path, expected := range cases {
@@ -32,7 +37,7 @@ func TestFilter(t *testing.T) {
3237
t.Fail()
3338
}
3439

35-
if expected != actual {
40+
if !reflect.DeepEqual(actual, expected) {
3641
t.Errorf("case failed: %v vs %v", expected, actual)
3742
}
3843
}

util/map.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ func ForceStringKeys(in interface{}) interface{} {
1414
case reflect.Map:
1515
newMap := make(map[string]interface{}, val.Len())
1616

17+
var stringKey string
18+
1719
for _, key := range val.MapKeys() {
18-
stringKey := fmt.Sprint(key.Interface())
20+
if reflect.TypeOf(key.Interface()) == nil {
21+
stringKey = "null"
22+
} else {
23+
stringKey = fmt.Sprint(key.Interface())
24+
}
25+
1926
newMap[stringKey] = ForceStringKeys(val.MapIndex(key).Interface())
2027
}
2128

util/map_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func TestForceStringKeys(t *testing.T) {
9-
input := map[int]interface{}{
9+
input := map[interface{}]interface{}{
1010
13: []interface{}{
1111
"foo",
1212
map[int]interface{}{
@@ -18,6 +18,7 @@ func TestForceStringKeys(t *testing.T) {
1818
1: []interface{}{"foo", "bar"},
1919
2: "two",
2020
},
21+
nil: "derf",
2122
}
2223

2324
expected := map[string]interface{}{
@@ -32,6 +33,7 @@ func TestForceStringKeys(t *testing.T) {
3233
"1": []interface{}{"foo", "bar"},
3334
"2": "two",
3435
},
36+
"null": "derf",
3537
}
3638

3739
actual := ForceStringKeys(input)

0 commit comments

Comments
 (0)