github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/cmd/contract_tests/unmarshaler/unmarshal_test.go (about) 1 package unmarshaler 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestUnmarshalElementJSON(t *testing.T) { 10 var unmarshalJSONTests = []struct { 11 json string 12 expected interface{} 13 }{ 14 { 15 `"30"`, 16 "30", 17 }, 18 { 19 "30", 20 float64(30), 21 }, 22 { 23 "null", 24 nil, 25 }, 26 { 27 "[]", 28 []interface{}{}, 29 }, 30 { 31 `["a", "b", "c"]`, 32 []interface{}{"a", "b", "c"}, 33 }, 34 { 35 "{}", 36 map[string]interface{}{}, 37 }, 38 { 39 `{"key1":"value1", "key2":"value2"}`, 40 map[string]interface{}{"key1": "value1", "key2": "value2"}, 41 }, 42 { 43 testJSON1, 44 map[string]interface{}{ 45 "key1": float64(119), 46 "sub1": map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": "value3"}}, 47 "sub3": map[string]interface{}{"key4": "value4", "key5": "value5"}}, 48 }, 49 { 50 testJSON2, 51 map[string]interface{}{ 52 "key1": float64(119), 53 "sub1": []interface{}{map[string]interface{}{ 54 "key2": "value2", "sub2": map[string]interface{}{"key3": []interface{}{"value2"}}}}, 55 "sub3": map[string]interface{}{"key4": "value2", "key5": "value2"}}, 56 }, 57 } 58 59 for _, tt := range unmarshalJSONTests { 60 t.Logf("unmarshal json test %s", tt.json) 61 { 62 unmarshaledJSON := UnmarshalJSON(&tt.json) 63 require.Equal(t, tt.expected, unmarshaledJSON.Body) 64 } 65 } 66 } 67 68 func TestUnmarshalElementYAML(t *testing.T) { 69 var unmarshalJSONTests = []struct { 70 yaml string 71 expected interface{} 72 }{ 73 { 74 `"30"`, 75 "30", 76 }, 77 { 78 "30", 79 30, 80 }, 81 { 82 "null", 83 nil, 84 }, 85 { 86 "[]", 87 []interface{}{}, 88 }, 89 { 90 `["a", "b", "c"]`, 91 []interface{}{"a", "b", "c"}, 92 }, 93 { 94 "{}", 95 map[string]interface{}{}, 96 }, 97 { 98 "key1: value1\nkey2: value2", 99 map[string]interface{}{"key1": "value1", "key2": "value2"}, 100 }, 101 { 102 testYAML1, 103 map[string]interface{}{ 104 "key1": 119, 105 "sub1": map[string]interface{}{"key2": "value2", "sub2": map[string]interface{}{"key3": "value3"}}, 106 "sub3": map[string]interface{}{"key4": "value4", "key5": "value5"}}, 107 }, 108 { 109 testYAML2, 110 map[string]interface{}{ 111 "key1": 119, 112 "sub1": []interface{}{map[string]interface{}{ 113 "key2": "value2", "sub2": map[string]interface{}{"key3": []interface{}{"value3"}}}}, 114 "sub3": map[string]interface{}{"key4": "value4", "key5": "value5"}}, 115 }, 116 } 117 118 for _, tt := range unmarshalJSONTests { 119 t.Logf("unmarshal yaml test %s", tt.yaml) 120 { 121 unmarshaledYAML := UnmarshalYAML(&tt.yaml) 122 require.Equal(t, tt.expected, unmarshaledYAML.Body) 123 } 124 } 125 } 126 127 func TestGetAndSetProperty(t *testing.T) { 128 testJSON := testJSON1 129 unmarshaledJSON := UnmarshalJSON(&testJSON) 130 require.Equal(t, float64(119), unmarshaledJSON.GetProperty("key1")) 131 require.Equal(t, "value2", unmarshaledJSON.GetProperty("sub1", "key2")) 132 require.Equal(t, "value3", unmarshaledJSON.GetProperty("sub1", "sub2", "key3")) 133 require.Equal(t, "value4", unmarshaledJSON.GetProperty("sub3", "key4")) 134 require.Equal(t, "value5", unmarshaledJSON.GetProperty("sub3", "key5")) 135 136 unmarshaledJSON.SetProperty([]string{"key1"}, "newValue1") 137 unmarshaledJSON.SetProperty([]string{"sub1", "sub2", "key3"}, "newValue2") 138 139 require.Equal(t, "newValue1", unmarshaledJSON.GetProperty("key1")) 140 require.Equal(t, "newValue2", unmarshaledJSON.GetProperty("sub1", "sub2", "key3")) 141 } 142 143 func TestDeleteProposer(t *testing.T) { 144 testJSON := testJSON1 145 unmarshaledJSON := UnmarshalJSON(&testJSON) 146 147 unmarshaledJSON.DeleteProperty("sub3", "key5") 148 require.Nil(t, unmarshaledJSON.GetProperty("sub3", "key5")) 149 } 150 151 const ( 152 testJSON1 = `{"key1":119, "sub1":{"key2":"value2", "sub2":{"key3":"value3"}}, 153 "sub3":{"key4":"value4", "key5":"value5"}}` 154 testJSON2 = `{"key1":119, "sub1":[{"key2":"value2", "sub2":{"key3":["value2"]}}], 155 "sub3":{"key4":"value2", "key5":"value2"}}` 156 testYAML1 = ` 157 key1: 119 158 sub1: 159 key2: value2 160 sub2: 161 key3: value3 162 sub3: 163 key4: value4 164 key5: value5 165 ` 166 testYAML2 = ` 167 key1: 119 168 sub1: 169 - key2: value2 170 sub2: 171 key3: [value3] 172 sub3: 173 key4: value4 174 key5: value5 175 ` 176 )