@@ -2,6 +2,8 @@ package value
22
33import (
44 "database/sql/driver"
5+ "encoding/json"
6+ "errors"
57 "reflect"
68 "testing"
79 "time"
@@ -25,6 +27,28 @@ func unwrapPtr(v interface{}) interface{} {
2527 return reflect .ValueOf (v ).Elem ().Interface ()
2628}
2729
30+ type jsonUnmarshaller struct {
31+ bytes []byte
32+ }
33+
34+ func (json * jsonUnmarshaller ) UnmarshalJSON (bytes []byte ) error {
35+ json .bytes = bytes
36+
37+ return nil
38+ }
39+
40+ var _ json.Unmarshaler = & jsonUnmarshaller {}
41+
42+ type jsonUnmarshallerBroken struct {
43+ bytes []byte
44+ }
45+
46+ func (json * jsonUnmarshallerBroken ) UnmarshalJSON (_ []byte ) error {
47+ return errors .New ("unmarshal error" )
48+ }
49+
50+ var _ json.Unmarshaler = & jsonUnmarshallerBroken {}
51+
2852func loadLocation (t * testing.T , name string ) * time.Location {
2953 loc , err := time .LoadLocation (name )
3054 require .NoError (t , err )
@@ -138,6 +162,75 @@ func TestCastTo(t *testing.T) {
138162 err : ErrCannotCast ,
139163 },
140164
165+ // JSONValue
166+ {
167+ name : xtest .CurrentFileLine (),
168+ value : JSONValue (`{"test": "text"}"` ),
169+ dst : ptr [string ](),
170+ exp : `{"test": "text"}"` ,
171+ err : nil ,
172+ },
173+ {
174+ name : xtest .CurrentFileLine (),
175+ value : JSONValue (`{"test":"text"}"` ),
176+ dst : ptr [Value ](),
177+ exp : JSONValue (`{"test":"text"}"` ),
178+ err : nil ,
179+ },
180+ {
181+ name : xtest .CurrentFileLine (),
182+ value : OptionalValue (JSONValue (`{"test": "text"}"` )),
183+ dst : ptr [* []byte ](),
184+ exp : value2ptr ([]byte (`{"test": "text"}"` )),
185+ err : nil ,
186+ },
187+ {
188+ name : xtest .CurrentFileLine (),
189+ value : JSONValue (`{"test":"text"}"` ),
190+ dst : ptr [[]byte ](),
191+ exp : []byte (`{"test":"text"}"` ),
192+ err : nil ,
193+ },
194+ {
195+ name : xtest .CurrentFileLine (),
196+ value : JSONValue (`{"test": "text"}"` ),
197+ dst : ptr [jsonUnmarshaller ](),
198+ exp : jsonUnmarshaller {[]byte (`{"test": "text"}"` )},
199+ err : nil ,
200+ },
201+ {
202+ name : xtest .CurrentFileLine (),
203+ value : JSONValue (`{"test": "text"}"` ),
204+ dst : ptr [jsonUnmarshallerBroken ](),
205+ err : ErrCannotCast ,
206+ },
207+ {
208+ name : xtest .CurrentFileLine (),
209+ value : OptionalValue (JSONValue (`{"test": "text"}"` )),
210+ dst : ptr [jsonUnmarshaller ](),
211+ exp : jsonUnmarshaller {[]byte (`{"test": "text"}"` )},
212+ err : nil ,
213+ },
214+ {
215+ name : xtest .CurrentFileLine (),
216+ value : OptionalValue (JSONValue (`{"test": "text"}"` )),
217+ dst : ptr [jsonUnmarshallerBroken ](),
218+ err : ErrCannotCast ,
219+ },
220+ {
221+ name : xtest .CurrentFileLine (),
222+ value : JSONValue (`{"test": "text"}"` ),
223+ dst : ptr [int ](),
224+ err : ErrCannotCast ,
225+ },
226+ {
227+ name : xtest .CurrentFileLine (),
228+ value : OptionalValue (JSONValue (`{"test": "text"}"` )),
229+ dst : ptr [int ](),
230+ err : ErrCannotCast ,
231+ },
232+
233+ // JSONDocumentValue
141234 {
142235 name : xtest .CurrentFileLine (),
143236 value : JSONDocumentValue (`{"test": "text"}"` ),
@@ -166,6 +259,44 @@ func TestCastTo(t *testing.T) {
166259 exp : []byte (`{"test":"text"}"` ),
167260 err : nil ,
168261 },
262+ {
263+ name : xtest .CurrentFileLine (),
264+ value : JSONDocumentValue (`{"test": "text"}"` ),
265+ dst : ptr [jsonUnmarshaller ](),
266+ exp : jsonUnmarshaller {[]byte (`{"test": "text"}"` )},
267+ err : nil ,
268+ },
269+ {
270+ name : xtest .CurrentFileLine (),
271+ value : JSONDocumentValue (`{"test": "text"}"` ),
272+ dst : ptr [jsonUnmarshallerBroken ](),
273+ err : ErrCannotCast ,
274+ },
275+ {
276+ name : xtest .CurrentFileLine (),
277+ value : OptionalValue (JSONDocumentValue (`{"test": "text"}"` )),
278+ dst : ptr [jsonUnmarshaller ](),
279+ exp : jsonUnmarshaller {[]byte (`{"test": "text"}"` )},
280+ err : nil ,
281+ },
282+ {
283+ name : xtest .CurrentFileLine (),
284+ value : OptionalValue (JSONDocumentValue (`{"test": "text"}"` )),
285+ dst : ptr [jsonUnmarshallerBroken ](),
286+ err : ErrCannotCast ,
287+ },
288+ {
289+ name : xtest .CurrentFileLine (),
290+ value : JSONDocumentValue (`{"test": "text"}"` ),
291+ dst : ptr [int ](),
292+ err : ErrCannotCast ,
293+ },
294+ {
295+ name : xtest .CurrentFileLine (),
296+ value : OptionalValue (JSONDocumentValue (`{"test": "text"}"` )),
297+ dst : ptr [int ](),
298+ err : ErrCannotCast ,
299+ },
169300
170301 {
171302 name : xtest .CurrentFileLine (),
0 commit comments