1package objx_test 2 3import ( 4 "testing" 5 6 "github.com/stretchr/objx" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9) 10 11var TestMap = objx.Map{ 12 "name": "Tyler", 13 "address": objx.Map{ 14 "city": "Salt Lake City", 15 "state": "UT", 16 }, 17 "numbers": []interface{}{"one", "two", "three", "four", "five"}, 18} 19 20type Convertable struct { 21 name string 22} 23 24type Unconvertable struct { 25 name string 26} 27 28func (c *Convertable) MSI() map[string]interface{} { 29 return objx.Map{"name": c.name} 30} 31 32func TestMapCreation(t *testing.T) { 33 o := objx.New(nil) 34 assert.Nil(t, o) 35 36 o = objx.New("Tyler") 37 assert.Nil(t, o) 38 39 unconvertable := &Unconvertable{name: "Tyler"} 40 o = objx.New(unconvertable) 41 assert.Nil(t, o) 42 43 convertable := &Convertable{name: "Tyler"} 44 o = objx.New(convertable) 45 require.NotNil(t, convertable) 46 assert.Equal(t, "Tyler", o["name"]) 47 48 o = objx.MSI() 49 assert.NotNil(t, o) 50 51 o = objx.MSI("name", "Tyler") 52 require.NotNil(t, o) 53 assert.Equal(t, o["name"], "Tyler") 54 55 o = objx.MSI(1, "a") 56 assert.Nil(t, o) 57 58 o = objx.MSI("a") 59 assert.Nil(t, o) 60 61 o = objx.MSI("a", "b", "c") 62 assert.Nil(t, o) 63} 64 65func TestMapValure(t *testing.T) { 66 m := objx.Map{ 67 "a": 1, 68 } 69 v := m.Value() 70 71 assert.Equal(t, m, v.ObjxMap()) 72} 73 74func TestMapMustFromJSONWithError(t *testing.T) { 75 _, err := objx.FromJSON(`"name":"Mat"}`) 76 assert.Error(t, err) 77} 78 79func TestMapFromJSON(t *testing.T) { 80 o := objx.MustFromJSON(`{"name":"Mat"}`) 81 82 require.NotNil(t, o) 83 assert.Equal(t, "Mat", o["name"]) 84} 85 86func TestMapFromJSONWithError(t *testing.T) { 87 var m objx.Map 88 89 assert.Panics(t, func() { 90 m = objx.MustFromJSON(`"name":"Mat"}`) 91 }) 92 assert.Nil(t, m) 93} 94 95func TestConversionJSONInt(t *testing.T) { 96 jsonString := 97 `{ 98 "a": 1, 99 "b": { 100 "data": 1 101 }, 102 "c": [1], 103 "d": [[1]] 104 }` 105 m, err := objx.FromJSON(jsonString) 106 107 assert.Nil(t, err) 108 require.NotNil(t, m) 109 assert.Equal(t, 1, m.Get("a").Int()) 110 assert.Equal(t, 1, m.Get("b.data").Int()) 111 112 assert.True(t, m.Get("c").IsInterSlice()) 113 assert.Equal(t, 1, m.Get("c").InterSlice()[0]) 114 115 assert.True(t, m.Get("d").IsInterSlice()) 116 assert.Equal(t, []interface{}{1}, m.Get("d").InterSlice()[0]) 117} 118 119func TestJSONSliceInt(t *testing.T) { 120 jsonString := 121 `{ 122 "a": [ 123 {"b": 1}, 124 {"c": 2} 125 ] 126 }` 127 m, err := objx.FromJSON(jsonString) 128 129 assert.Nil(t, err) 130 require.NotNil(t, m) 131 assert.Equal(t, []objx.Map{{"b": 1}, {"c": 2}}, m.Get("a").ObjxMapSlice()) 132} 133 134func TestJSONSliceMixed(t *testing.T) { 135 jsonString := 136 `{ 137 "a": [ 138 {"b": 1}, 139 "a" 140 ] 141 }` 142 m, err := objx.FromJSON(jsonString) 143 144 assert.Nil(t, err) 145 require.NotNil(t, m) 146 147 assert.Nil(t, m.Get("a").ObjxMapSlice()) 148} 149 150func TestMapFromBase64String(t *testing.T) { 151 base64String := "eyJuYW1lIjoiTWF0In0=" 152 o, err := objx.FromBase64(base64String) 153 154 require.NoError(t, err) 155 assert.Equal(t, o.Get("name").Str(), "Mat") 156 assert.Equal(t, objx.MustFromBase64(base64String).Get("name").Str(), "Mat") 157} 158 159func TestMapFromBase64StringWithError(t *testing.T) { 160 base64String := "eyJuYW1lIjoiTWFasd0In0=" 161 _, err := objx.FromBase64(base64String) 162 163 assert.Error(t, err) 164 assert.Panics(t, func() { 165 objx.MustFromBase64(base64String) 166 }) 167} 168 169func TestMapFromSignedBase64String(t *testing.T) { 170 base64String := "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6" 171 172 o, err := objx.FromSignedBase64(base64String, "key") 173 174 require.NoError(t, err) 175 assert.Equal(t, o.Get("name").Str(), "Mat") 176 assert.Equal(t, objx.MustFromSignedBase64(base64String, "key").Get("name").Str(), "Mat") 177} 178 179func TestMapFromSignedBase64StringWithError(t *testing.T) { 180 base64String := "eyJuYW1lasdIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6" 181 _, err := objx.FromSignedBase64(base64String, "key") 182 assert.Error(t, err) 183 assert.Panics(t, func() { 184 objx.MustFromSignedBase64(base64String, "key") 185 }) 186 187 base64String = "eyJuYW1lasdIjoiTWF0In0=67ee82916f90b2c0d68c903266e8998c9ef0c3d6" 188 _, err = objx.FromSignedBase64(base64String, "key") 189 assert.Error(t, err) 190 assert.Panics(t, func() { 191 objx.MustFromSignedBase64(base64String, "key") 192 }) 193 194 base64String = "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6_junk" 195 _, err = objx.FromSignedBase64(base64String, "key") 196 assert.Error(t, err) 197 assert.Panics(t, func() { 198 objx.MustFromSignedBase64(base64String, "key") 199 }) 200} 201 202func TestMapFromURLQuery(t *testing.T) { 203 m, err := objx.FromURLQuery("name=tyler&state=UT") 204 205 assert.NoError(t, err) 206 require.NotNil(t, m) 207 assert.Equal(t, "tyler", m.Get("name").Str()) 208 assert.Equal(t, "UT", m.Get("state").Str()) 209} 210 211func TestMapMustFromURLQuery(t *testing.T) { 212 m := objx.MustFromURLQuery("name=tyler&state=UT") 213 214 require.NotNil(t, m) 215 assert.Equal(t, "tyler", m.Get("name").Str()) 216 assert.Equal(t, "UT", m.Get("state").Str()) 217} 218 219func TestMapFromURLQueryWithError(t *testing.T) { 220 m, err := objx.FromURLQuery("%") 221 222 assert.Error(t, err) 223 assert.Nil(t, m) 224 assert.Panics(t, func() { 225 objx.MustFromURLQuery("%") 226 }) 227} 228