-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from line/feature/fix_dredd_test
Fix dredd test
- Loading branch information
Showing
10 changed files
with
395 additions
and
123 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
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
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
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,57 @@ | ||
package unmarshaler | ||
|
||
import ( | ||
"encoding/json" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type UnmarshalledArbitraryObject struct { | ||
Body interface{} | ||
} | ||
|
||
func (obj *UnmarshalledArbitraryObject) GetProperty(keys ...string) interface{} { | ||
body := obj.Body | ||
for _, key := range keys { | ||
body = body.(map[string]interface{})[key] | ||
} | ||
return body | ||
} | ||
|
||
func (obj *UnmarshalledArbitraryObject) SetProperty(keys []string, value interface{}) { | ||
prevKeys := keys[:len(keys)-1] | ||
lastKey := keys[len(keys)-1] | ||
|
||
body := obj.Body | ||
for _, key := range prevKeys { | ||
body = body.(map[string]interface{})[key] | ||
} | ||
body.(map[string]interface{})[lastKey] = value | ||
} | ||
|
||
func (obj *UnmarshalledArbitraryObject) DeleteProperty(keys ...string) { | ||
prevKeys := keys[:len(keys)-1] | ||
lastKey := keys[len(keys)-1] | ||
|
||
body := obj.Body | ||
for _, key := range prevKeys { | ||
body = body.(map[string]interface{})[key] | ||
} | ||
delete(body.(map[string]interface{}), lastKey) | ||
} | ||
|
||
func UnmarshalJSON(str *string) UnmarshalledArbitraryObject { | ||
return UnmarshalledArbitraryObject{unmarshalArbitraryFormat(json.Unmarshal, str)} | ||
} | ||
|
||
func UnmarshalYAML(str *string) UnmarshalledArbitraryObject { | ||
return UnmarshalledArbitraryObject{unmarshalArbitraryFormat(yaml.Unmarshal, str)} | ||
} | ||
|
||
func unmarshalArbitraryFormat(unmarshal func([]byte, interface{}) error, str *string) interface{} { | ||
var body interface{} | ||
err := unmarshal([]byte(*str), &body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return body | ||
} |
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,172 @@ | ||
package unmarshaler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnmarshalElementJSON(t *testing.T) { | ||
var unmarshalJSONTests = []struct { | ||
json string | ||
expected interface{} | ||
}{ | ||
{ | ||
`"30"`, | ||
"30", | ||
}, | ||
{ | ||
"30", | ||
float64(30), | ||
}, | ||
{ | ||
"null", | ||
nil, | ||
}, | ||
{ | ||
"[]", | ||
[]interface{}{}, | ||
}, | ||
{ | ||
`["a", "b", "c"]`, | ||
[]interface{}{"a", "b", "c"}, | ||
}, | ||
{ | ||
"{}", | ||
map[string]interface{}{}, | ||
}, | ||
{ | ||
`{"key1":"value1", "key2":"value2"}`, | ||
map[string]interface{}{"key1": "value1", "key2": "value2"}, | ||
}, | ||
{ | ||
testJSON1, | ||
map[string]interface{}{ | ||
"key1": float64(119), | ||
"sub1": map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": "value3"}}, | ||
"sub3": map[string]interface{}{"key4": "value4", "key5": "value5"}}, | ||
}, | ||
{ | ||
testJSON2, | ||
map[string]interface{}{ | ||
"key1": float64(119), | ||
"sub1": []interface{}{map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": []interface{}{"value2"}}}}, | ||
"sub3": map[string]interface{}{"key4": "value2", "key5": "value2"}}, | ||
}, | ||
} | ||
|
||
for _, tt := range unmarshalJSONTests { | ||
t.Logf("unmarshal json test %s", tt.json) | ||
{ | ||
unmarshaledJSON := UnmarshalJSON(&tt.json) | ||
require.Equal(t, tt.expected, unmarshaledJSON.Body) | ||
} | ||
} | ||
} | ||
|
||
func TestUnmarshalElementYAML(t *testing.T) { | ||
var unmarshalJSONTests = []struct { | ||
yaml string | ||
expected interface{} | ||
}{ | ||
{ | ||
`"30"`, | ||
"30", | ||
}, | ||
{ | ||
"30", | ||
30, | ||
}, | ||
{ | ||
"null", | ||
nil, | ||
}, | ||
{ | ||
"[]", | ||
[]interface{}{}, | ||
}, | ||
{ | ||
`["a", "b", "c"]`, | ||
[]interface{}{"a", "b", "c"}, | ||
}, | ||
{ | ||
"{}", | ||
map[string]interface{}{}, | ||
}, | ||
{ | ||
"key1: value1\nkey2: value2", | ||
map[string]interface{}{"key1": "value1", "key2": "value2"}, | ||
}, | ||
{ | ||
testYAML1, | ||
map[string]interface{}{ | ||
"key1": 119, | ||
"sub1": map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": "value3"}}, | ||
"sub3": map[string]interface{}{"key4": "value4", "key5": "value5"}}, | ||
}, | ||
{ | ||
testYAML2, | ||
map[string]interface{}{ | ||
"key1": 119, | ||
"sub1": []interface{}{map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": []interface{}{"value3"}}}}, | ||
"sub3": map[string]interface{}{"key4": "value4", "key5": "value5"}}, | ||
}, | ||
} | ||
|
||
for _, tt := range unmarshalJSONTests { | ||
t.Logf("unmarshal yaml test %s", tt.yaml) | ||
{ | ||
unmarshaledYAML := UnmarshalYAML(&tt.yaml) | ||
require.Equal(t, tt.expected, unmarshaledYAML.Body) | ||
} | ||
} | ||
} | ||
|
||
func TestGetAndSetProperty(t *testing.T) { | ||
testJSON := testJSON1 | ||
unmarshaledJSON := UnmarshalJSON(&testJSON) | ||
require.Equal(t, float64(119), unmarshaledJSON.GetProperty("key1")) | ||
require.Equal(t, "value2", unmarshaledJSON.GetProperty("sub1", "key2")) | ||
require.Equal(t, "value3", unmarshaledJSON.GetProperty("sub1", "sub2", "key3")) | ||
require.Equal(t, "value4", unmarshaledJSON.GetProperty("sub3", "key4")) | ||
require.Equal(t, "value5", unmarshaledJSON.GetProperty("sub3", "key5")) | ||
|
||
unmarshaledJSON.SetProperty([]string{"key1"}, "newValue1") | ||
unmarshaledJSON.SetProperty([]string{"sub1", "sub2", "key3"}, "newValue2") | ||
|
||
require.Equal(t, "newValue1", unmarshaledJSON.GetProperty("key1")) | ||
require.Equal(t, "newValue2", unmarshaledJSON.GetProperty("sub1", "sub2", "key3")) | ||
} | ||
|
||
func TestDeleteProposer(t *testing.T) { | ||
testJSON := testJSON1 | ||
unmarshaledJSON := UnmarshalJSON(&testJSON) | ||
|
||
unmarshaledJSON.DeleteProperty("sub3", "key5") | ||
require.Nil(t, unmarshaledJSON.GetProperty("sub3", "key5")) | ||
} | ||
|
||
const ( | ||
testJSON1 = `{"key1":119, "sub1":{"key2":"value2", "sub2":{"key3":"value3"}}, "sub3":{"key4":"value4", "key5":"value5"}}` | ||
testJSON2 = `{"key1":119, "sub1":[{"key2":"value2", "sub2":{"key3":["value2"]}}], "sub3":{"key4":"value2", "key5":"value2"}}` | ||
testYAML1 = ` | ||
key1: 119 | ||
sub1: | ||
key2: value2 | ||
sub2: | ||
key3: value3 | ||
sub3: | ||
key4: value4 | ||
key5: value5 | ||
` | ||
testYAML2 = ` | ||
key1: 119 | ||
sub1: | ||
- key2: value2 | ||
sub2: | ||
key3: [value3] | ||
sub3: | ||
key4: value4 | ||
key5: value5 | ||
` | ||
) |
Oops, something went wrong.