@@ -12,6 +12,7 @@ import (
12
12
"fmt"
13
13
"reflect"
14
14
"testing"
15
+ "time"
15
16
16
17
"github.com/google/go-cmp/cmp"
17
18
"github.com/stretchr/testify/require"
@@ -302,3 +303,73 @@ func TestNullBytes(t *testing.T) {
302
303
}
303
304
})
304
305
}
306
+
307
+ func TestMarshalExtJSONIndent (t * testing.T ) {
308
+ type indentTestCase struct {
309
+ name string
310
+ val interface {}
311
+ expectedExtJSON string
312
+ }
313
+
314
+ // expectedExtJSON must be written as below because single-quoted
315
+ // literal strings capture undesired code formatting tabs
316
+ testCases := []indentTestCase {
317
+ {
318
+ "empty val" ,
319
+ struct {}{},
320
+ `{}` ,
321
+ },
322
+ {
323
+ "embedded struct" ,
324
+ struct {
325
+ Embedded interface {} `json:"embedded"`
326
+ Foo string `json:"foo"`
327
+ }{
328
+ Embedded : struct {
329
+ Name string `json:"name"`
330
+ Word string `json:"word"`
331
+ }{
332
+ Name : "test" ,
333
+ Word : "word" ,
334
+ },
335
+ Foo : "bar" ,
336
+ },
337
+ "{\n \t \" embedded\" : {\n \t \t \" name\" : \" test\" ,\n \t \t \" word\" : \" word\" \n \t },\n \t \" foo\" : \" bar\" \n }" ,
338
+ },
339
+ {
340
+ "date struct" ,
341
+ struct {
342
+ Foo string `json:"foo"`
343
+ Date time.Time `json:"date"`
344
+ }{
345
+ Foo : "bar" ,
346
+ Date : time .Date (2000 , time .January , 1 , 12 , 0 , 0 , 0 , time .UTC ),
347
+ },
348
+ "{\n \t \" foo\" : \" bar\" ,\n \t \" date\" : {\n \t \t \" $date\" : {\n \t \t \t \" $numberLong\" : \" 946728000000\" \n \t \t }\n \t }\n }" ,
349
+ },
350
+ {
351
+ "float struct" ,
352
+ struct {
353
+ Foo string `json:"foo"`
354
+ Float float32 `json:"float"`
355
+ }{
356
+ Foo : "bar" ,
357
+ Float : 3.14 ,
358
+ },
359
+ "{\n \t \" foo\" : \" bar\" ,\n \t \" float\" : {\n \t \t \" $numberDouble\" : \" 3.140000104904175\" \n \t }\n }" ,
360
+ },
361
+ }
362
+
363
+ for _ , tc := range testCases {
364
+ tc := tc
365
+ t .Run (tc .name , func (t * testing.T ) {
366
+ t .Parallel ()
367
+ extJSONBytes , err := MarshalExtJSONIndent (tc .val , true , false , "" , "\t " )
368
+ assert .Nil (t , err , "Marshal indent error: %v" , err )
369
+
370
+ expectedExtJSONBytes := []byte (tc .expectedExtJSON )
371
+
372
+ assert .Equal (t , expectedExtJSONBytes , extJSONBytes , "expected:\n %s\n got:\n %s" , expectedExtJSONBytes , extJSONBytes )
373
+ })
374
+ }
375
+ }
0 commit comments