-
Notifications
You must be signed in to change notification settings - Fork 519
/
result_test.go
1200 lines (972 loc) · 26.1 KB
/
result_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// A facebook graph api client in go.
// https://github.com/huandu/facebook/
//
// Copyright 2012, Huan Du
// Licensed under the MIT license
// https://github.com/huandu/facebook/blob/master/LICENSE
package facebook
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
)
type AllTypes struct {
AnonymousStruct1
*AnonymousStruct2
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float32 float32
Float64 float64
StringInt Int
StringInt8 Int8
StringInt16 Int16
StringInt32 Int32
StringInt64 Int64
StringUint Uint
StringUint8 Uint8
StringUint16 Uint16
StringUint32 Uint32
StringUint64 Uint64
StringFloat32 Float32
StringFloat64 Float64
String string
ArrayOfInt []int
MapOfString map[string]string
NestedStruct *NestedStruct
ArrayOfStruct []NestedStruct
}
type AnonymousStruct1 struct {
AnonymousInt1 int
AnonymousString1 string
AnonymousArrayOfString1 []string
}
type AnonymousStruct2 struct {
AnonymousInt2 int
AnonymousString2 string
AnonymousArrayOfString2 []string
}
type NestedStruct struct {
Int int
String string
ArrayOfString []string
}
type ParamsStruct struct {
Foo string
Bar *ParamsNestedStruct
Renamed int `facebook:"changed"`
AlwaysVisible float64
Zero string `facebook:",omitempty"`
}
type ParamsNestedStruct struct {
AAA int
BBB string
CCC bool
}
type FieldTagStruct struct {
Field1 string `facebook:"field2"`
Required string `facebook:",required"`
Foo string `facebook:"bar,required"`
CanAbsent string
}
type MessageTag struct {
ID string
Name string
Type string
}
type MessageTags map[string][]*MessageTag
type NullStruct struct {
Null *int
}
// custom unmarshaler.
type CustomMarshaler struct {
Name string
Hash int64
Extra bool `facebook:",required"` // this field is ignored due to Unmarshaler.
}
type customMarshaler struct {
Name string
Hash int64
}
func (cm *CustomMarshaler) UnmarshalJSON(data []byte) error {
var c customMarshaler
err := json.Unmarshal(data, &c)
if err != nil {
return err
}
cm.Name = c.Name
cm.Hash = c.Hash
if cm.Name == "bar" {
return fmt.Errorf("sorry but i don't like `bar`")
}
return nil
}
type CustomMarshalerStruct struct {
Marshaler CustomMarshaler
}
func TestResultDecode(t *testing.T) {
strNormal := `{
"anonymous_int1": 123,
"anonymous_string2": "abc",
"int": 1234,
"int8": 23,
"int16": 12345,
"int32": -127372843,
"int64": 192438483489298,
"uint": 1283829,
"uint8": 233,
"uint16": 62121,
"uint32": 3083747392,
"uint64": 2034857382993849,
"float32": 9382.38429,
"float64": 3984.293848292,
"string_int": "1234",
"string_int8": "23",
"string_int16": "12345",
"string_int32": "-127372843",
"string_int64": "192438483489298",
"string_uint": "1283829",
"string_uint8": "233",
"string_uint16": "62121",
"string_uint32": "3083747392",
"string_uint64": "2034857382993849",
"string_float32": "9382.38429",
"string_float64": "3984.293848292",
"map_of_string": {"a": "1", "b": "2"},
"array_of_int": [12, 34, 56],
"string": "abcd",
"notused": 1234,
"nested_struct": {
"string": "hello",
"int": 123,
"array_of_string": ["a", "b", "c"]
},
"array_of_struct": [
{
"string": "world",
"int": 456,
"array_of_string": ["d", "e"]
},
{
"string": ":)",
"int": 789,
"array_of_string": ["f", "g", "h"]
}
]
}`
strOverflow := `{
"int": 1234,
"int8": 23,
"int16": 12345,
"int32": -127372843,
"int64": 192438483489298,
"uint": 1283829,
"uint8": 233,
"uint16": 62121,
"uint32": 383083747392,
"uint64": 2034857382993849,
"float32": 9382.38429,
"float64": 3984.293848292,
"string_int": "1234",
"string_int8": "23",
"string_int16": "12345",
"string_int32": "-127372843",
"string_int64": "192438483489298",
"string_uint": "1283829",
"string_uint8": "233",
"string_uint16": "62121",
"string_uint32": "383083747392",
"string_uint64": "2034857382993849",
"string_float32": "9382.38429",
"string_float64": "3984.293848292",
"string": "abcd",
"map_of_string": {"a": "1", "b": "2"},
"array_of_int": [12, 34, 56],
"string": "abcd",
"notused": 1234,
"nested_struct": {
"string": "hello",
"int": 123,
"array_of_string": ["a", "b", "c"]
}
}`
strMissAField := `{
"int": 1234,
"int8": 23,
"int16": 12345,
"int32": -127372843,
"missed": "int64",
"uint": 1283829,
"uint8": 233,
"uint16": 62121,
"uint32": 383083747392,
"uint64": 2034857382993849,
"float32": 9382.38429,
"float64": 3984.293848292,
"string_uint": "789",
"string_float32": "10.234",
"string": "abcd",
"map_of_string": {"a": "1", "b": "2"},
"array_of_int": [12, 34, 56],
"string": "abcd",
"notused": 1234,
"nested_struct": {
"string": "hello",
"int": 123,
"array_of_string": ["a", "b", "c"]
}
}`
var result Result
var err error
var normal, withError AllTypes
var anInt int
var aStringUint Uint
var aStringFloat32 Float32
err = json.Unmarshal([]byte(strNormal), &result)
if err != nil {
t.Fatalf("cannot unmarshal json string. [e:%v]", err)
}
err = result.Decode(&normal)
if err != nil {
t.Fatalf("cannot decode normal struct. [e:%v]", err)
}
if normal.AnonymousInt1 != 123 {
t.Fatalf("Fail to decode AnonymousInt1. [value:%v]", normal.AnonymousInt1)
}
if normal.AnonymousString2 != "abc" {
t.Fatalf("Fail to decode AnonymousString2. [value:%v]", normal.AnonymousString2)
}
if expected := 2; len(normal.ArrayOfStruct) != expected {
t.Fatalf("invalid len of ArrayOfStruct. [expected:%v] [actual:%v]", expected, len(normal.ArrayOfStruct))
}
if expected := ":)"; normal.ArrayOfStruct[1].String != expected {
t.Fatalf("invalid content of ArrayOfStruct. [expected:%v] [actual:%v]", expected, normal.ArrayOfStruct[1].String)
}
err = json.Unmarshal([]byte(strOverflow), &result)
if err != nil {
t.Fatalf("cannot unmarshal json string. [e:%v]", err)
}
err = result.Decode(&withError)
if err == nil {
t.Fatalf("struct should be overflow")
}
t.Logf("overflow struct. e:%v", err)
err = json.Unmarshal([]byte(strMissAField), &result)
if err != nil {
t.Fatalf("cannot unmarshal json string. [e:%v]", err)
}
err = result.Decode(&withError)
if err == nil {
t.Fatalf("a field in struct should absent in json map.")
}
t.Logf("miss-a-field struct. e:%v", err)
err = result.DecodeField("array_of_int.2", &anInt)
if err != nil {
t.Fatalf("cannot decode array item. [e:%v]", err)
}
if anInt != 56 {
t.Fatalf("invalid array value. expected 56, actual %v", anInt)
}
err = result.DecodeField("nested_struct.int", &anInt)
if err != nil {
t.Fatalf("cannot decode nested struct item. [e:%v]", err)
}
if anInt != 123 {
t.Fatalf("invalid array value. expected 123, actual %v", anInt)
}
err = result.DecodeField("string_uint", &aStringUint)
if err != nil {
t.Fatalf("cannot decode `string_uint`. [e:%v]", err)
}
if aStringUint != 789 {
t.Fatalf("invalid uint value. expected 789, actual %v", aStringUint)
}
err = result.DecodeField("string_float32", &aStringFloat32)
if err != nil {
t.Fatalf("cannot decode `string_float32`. [e:%v]", err)
}
if aStringFloat32 != 10.234 {
t.Fatalf("invalid uint value. expected 10.234, actual %v", aStringFloat32)
}
}
func TestStructFieldTag(t *testing.T) {
strNormalField := `{
"field2": "hey",
"required": "my",
"bar": "dear"
}`
strMissingField2Field := `{
"field1": "hey",
"required": "my",
"bar": "dear"
}`
strMissingRequiredField := `{
"field1": "hey",
"bar": "dear",
"can_absent": "babe"
}`
strMissingBarField := `{
"field1": "hey",
"required": "my"
}`
var result Result
var value FieldTagStruct
var err error
err = json.Unmarshal([]byte(strNormalField), &result)
if err != nil {
t.Fatalf("cannot unmarshal json string. [e:%v]", err)
}
err = result.Decode(&value)
if err != nil {
t.Fatalf("cannot decode struct. [e:%v]", err)
}
result = Result{}
value = FieldTagStruct{}
err = json.Unmarshal([]byte(strMissingField2Field), &result)
if err != nil {
t.Fatalf("cannot unmarshal json string. [e:%v]", err)
}
err = result.Decode(&value)
if err != nil {
t.Fatalf("cannot decode struct. [e:%v]", err)
}
if value.Field1 != "" {
t.Fatalf("value field1 should be kept unchanged. [field1:%v]", value.Field1)
}
result = Result{}
value = FieldTagStruct{}
err = json.Unmarshal([]byte(strMissingRequiredField), &result)
if err != nil {
t.Fatalf("cannot unmarshal json string. [e:%v]", err)
}
err = result.Decode(&value)
if err == nil {
t.Fatalf("should fail to decode struct.")
}
t.Logf("expected decode error. [e:%v]", err)
result = Result{}
value = FieldTagStruct{}
err = json.Unmarshal([]byte(strMissingBarField), &result)
if err != nil {
t.Fatalf("cannot unmarshal json string. [e:%v]", err)
}
err = result.Decode(&value)
if err == nil {
t.Fatalf("should fail to decode struct.")
}
t.Logf("expected decode error. [e:%v]", err)
}
type myTime struct {
time.Time
}
func TestDecodeField(t *testing.T) {
date1 := "2015-01-03T11:15:01Z"
date2 := "2014-03-04T11:15:01.123Z"
jsonStr := `{
"int": 1234,
"array": ["abcd", "efgh"],
"map": {
"key1": 5678,
"nested_map": {
"key2": "ijkl",
"key3": [{
"key4": "mnop"
}, {
"key5": 9012
}]
}
},
"message_tags": {
"2": [
{
"id": "4838901",
"name": "Foo Bar",
"type": "page"
},
{
"id": "293450302",
"name": "Player Rocks",
"type": "page"
}
]
},
"nullStruct": {
"null": null
},
"timestamp": "` + date1 + `",
"custom_timestamp": "` + date2 + `"
}`
var result Result
var err error
var anInt int
var aString string
var aSlice []string
var subResults []Result
var aNull = NullStruct{
Null: &anInt,
}
var aTimestamp time.Time
var aCustomTimestamp myTime
err = json.Unmarshal([]byte(jsonStr), &result)
if err != nil {
t.Fatalf("invalid json string. [e:%v]", err)
}
err = result.DecodeField("int", &anInt)
if err != nil {
t.Fatalf("cannot decode int field. [e:%v]", err)
}
if anInt != 1234 {
t.Fatalf("expected int value is 1234. [int:%v]", anInt)
}
err = result.DecodeField("array.0", &aString)
if err != nil {
t.Fatalf("cannot decode array.0 field. [e:%v]", err)
}
if aString != "abcd" {
t.Fatalf("expected array.0 value is 'abcd'. [string:%v]", aString)
}
err = result.DecodeField("array.1", &aString)
if err != nil {
t.Fatalf("cannot decode array.1 field. [e:%v]", err)
}
if aString != "efgh" {
t.Fatalf("expected array.1 value is 'abcd'. [string:%v]", aString)
}
err = result.DecodeField("array.2", &aString)
if err == nil {
t.Fatalf("array.2 doesn't exist. expect an error.")
}
err = result.DecodeField("map.key1", &anInt)
if err != nil {
t.Fatalf("cannot decode map.key1 field. [e:%v]", err)
}
if anInt != 5678 {
t.Fatalf("expected map.key1 value is 5678. [int:%v]", anInt)
}
err = result.DecodeField("map.nested_map.key2", &aString)
if err != nil {
t.Fatalf("cannot decode map.nested_map.key2 field. [e:%v]", err)
}
if aString != "ijkl" {
t.Fatalf("expected map.nested_map.key2 value is 'ijkl'. [string:%v]", aString)
}
err = result.DecodeField("array", &aSlice)
if err != nil {
t.Fatalf("cannot decode array field. [e:%v]", err)
}
if len(aSlice) != 2 || aSlice[0] != "abcd" || aSlice[1] != "efgh" {
t.Fatalf("expected array value is ['abcd', 'efgh']. [slice:%v]", aSlice)
}
err = result.DecodeField("map.nested_map.key3", &subResults)
if err != nil {
t.Fatalf("cannot decode map.nested_map.key3 field. [e:%v]", err)
}
if len(subResults) != 2 {
t.Fatalf("expected sub results len is 2. [len:%v] [results:%v]", len(subResults), subResults)
}
err = subResults[0].DecodeField("key4", &aString)
if err != nil {
t.Fatalf("cannot decode key4 field in sub result. [e:%v]", err)
}
if aString != "mnop" {
t.Fatalf("expected map.nested_map.key2 value is 'mnop'. [string:%v]", aString)
}
err = subResults[1].DecodeField("key5", &anInt)
if err != nil {
t.Fatalf("cannot decode key5 field. [e:%v]", err)
}
if anInt != 9012 {
t.Fatalf("expected key5 value is 9012. [int:%v]", anInt)
}
err = result.DecodeField("message_tags.2.0.id", &aString)
if err != nil {
t.Fatalf("cannot decode message_tags.2.0.id field. [e:%v]", err)
}
if aString != "4838901" {
t.Fatalf("expected message_tags.2.0.id value is '4838901'. [string:%v]", aString)
}
var messageTags MessageTags
err = result.DecodeField("message_tags", &messageTags)
if err != nil {
t.Fatalf("cannot decode message_tags field. [e:%v]", err)
}
if len(messageTags) != 1 {
t.Fatalf("expect messageTags have only 1 element. [len:%v]", len(messageTags))
}
aString = messageTags["2"][1].ID
if aString != "293450302" {
t.Fatalf("expect messageTags.2.1.id value is '293450302'. [value:%v]", aString)
}
err = result.DecodeField("nullStruct", &aNull)
if err != nil {
t.Fatalf("cannot decode nullStruct field. [e:%v]", err)
}
if aNull.Null != nil {
t.Fatalf("expect aNull.Null is reset to nil.")
}
err = result.DecodeField("timestamp", &aTimestamp)
if err != nil {
t.Fatalf("cannot decode timestamp field. [e:%v]", err)
}
t1, _ := time.Parse(time.RFC3339, date1)
if !aTimestamp.Equal(t1) {
t.Fatalf("expect aTimestamp date to be %v [value:%v]", date1, aTimestamp.String())
}
err = result.DecodeField("custom_timestamp", &aCustomTimestamp)
if err != nil {
t.Fatalf("cannot decode custom_timestamp field. [e:%v]", err)
}
t2, _ := time.Parse(time.RFC3339, date2)
if !aCustomTimestamp.Equal(t2) {
t.Fatalf("expect aCustomTimestamp date to be %v [value:%v]", date2, aCustomTimestamp.String())
}
var timeStruct struct {
Timestamp time.Time `facebook:",required"`
CustomTimestamp myTime `facebook:",required"`
}
err = result.Decode(&timeStruct)
if err != nil {
t.Fatalf("cannot decode time struct. [e:%v]", err)
}
if !timeStruct.Timestamp.Equal(t1) {
t.Fatalf("expect timeStruct.Timestamp date to be %v [value:%v]", date1, aTimestamp.String())
}
if !timeStruct.CustomTimestamp.Equal(t2) {
t.Fatalf("expect timeStruct.CustomTimestamp date to be %v [value:%v]", date2, aCustomTimestamp.Time.String())
}
}
type FacebookFriend struct {
ID string `facebook:",required"`
Name string `facebook:",required"`
}
type FacebookFriends struct {
Friends []FacebookFriend `facebook:"data,required"`
}
func TestPagingResultDecode(t *testing.T) {
res := Result{
"data": []interface{}{
map[string]interface{}{
"name": "friend 1",
"id": "1",
},
map[string]interface{}{
"name": "friend 2",
"id": "2",
},
},
"paging": map[string]interface{}{
"next": "https://graph.facebook.com/...",
},
}
paging, err := newPagingResult(nil, res)
if err != nil {
t.Fatalf("cannot create paging result. [e:%v]", err)
}
var friends FacebookFriends
if err := paging.Decode(&friends); err != nil {
t.Fatalf("cannot decode paging result. [e:%v]", err)
}
if len(friends.Friends) != 2 {
t.Fatalf("expect to have 2 friends. [len:%v]", len(friends.Friends))
}
if friends.Friends[0].Name != "friend 1" {
t.Fatalf("expect name to be 'friend 1'. [name:%v]", friends.Friends[0].Name)
}
if friends.Friends[0].ID != "1" {
t.Fatalf("expect id to be '1'. [id:%v]", friends.Friends[0].ID)
}
if friends.Friends[1].Name != "friend 2" {
t.Fatalf("expect name to be 'friend 2'. [name:%v]", friends.Friends[1].Name)
}
if friends.Friends[1].ID != "2" {
t.Fatalf("expect id to be '2'. [id:%v]", friends.Friends[1].ID)
}
}
func TestPagingResult(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}
session := &Session{}
session.SetAccessToken(FB_TEST_VALID_ACCESS_TOKEN)
res, err := session.Get("/me/posts", Params{
"limit": 2,
})
if err != nil {
t.Fatalf("cannot get my posts. [e:%v]", err)
}
paging, err := res.Paging(session)
if err != nil {
t.Fatalf("cannot get paging information. [e:%v]", err)
}
data := paging.Data()
if len(data) != 2 {
t.Fatalf("expect to have only 2 post. [len:%v]", len(data))
}
t.Logf("result: %v", res)
t.Logf("previous: %v", paging.previous)
noMore, err := paging.Previous()
if err != nil {
t.Fatalf("cannot get paging information. [e:%v]", err)
}
if !noMore {
t.Fatalf("should have no more post. %v", *paging.paging.Paging)
}
_, err = paging.Next()
if err != nil {
t.Fatalf("cannot get paging information. [e:%v]", err)
}
data = paging.Data()
if len(data) != 2 {
t.Fatalf("expect to have only 2 post. [len:%v]", len(data))
}
_, err = paging.Next()
if err != nil {
t.Fatalf("cannot get paging information. [e:%v]", err)
}
if len(paging.Data()) != 2 {
t.Fatalf("expect to have only 2 post. [len:%v]", len(paging.Data()))
}
}
func TestDecodeLargeInteger(t *testing.T) {
bigIntegers := []int64{
1<<53 - 2,
1<<53 - 1,
1 << 53,
1<<53 + 1,
1<<53 + 2,
1<<54 - 2,
1<<54 - 1,
1 << 54,
1<<54 + 1,
1<<54 + 2,
1<<60 - 2,
1<<60 - 1,
1 << 60,
1<<60 + 1,
1<<60 + 2,
1<<63 - 2,
1<<63 - 1,
-(1<<53 - 2),
-(1<<53 - 1),
-(1 << 53),
-(1<<53 + 1),
-(1<<53 + 2),
-(1<<54 - 2),
-(1<<54 - 1),
-(1 << 54),
-(1<<54 + 1),
-(1<<54 + 2),
-(1<<60 - 2),
-(1<<60 - 1),
-(1 << 60),
-(1<<60 + 1),
-(1<<60 + 2),
-(1<<53 - 2),
-(1<<63 - 1),
-(1 << 63),
}
jsonStr := `{
"integers": [%v]
}`
buf := &bytes.Buffer{}
for _, v := range bigIntegers {
buf.WriteString(fmt.Sprintf("%v", v))
buf.WriteRune(',')
}
buf.WriteRune('0')
json := fmt.Sprintf(jsonStr, buf.String())
res, err := MakeResult([]byte(json))
if err != nil {
t.Fatalf("cannot make result on test json string. [e:%v]", err)
}
var actualIntegers []int64
err = res.DecodeField("integers", &actualIntegers)
if err != nil {
t.Fatalf("cannot decode integers from json. [e:%v]", err)
}
if len(actualIntegers) != len(bigIntegers)+1 {
t.Fatalf("count of decoded integers is not correct. [expected:%v] [actual:%v]", len(bigIntegers)+1, len(actualIntegers))
}
for k := range bigIntegers {
if bigIntegers[k] != actualIntegers[k] {
t.Logf("expected integers: %v", bigIntegers)
t.Logf("actual integers: %v", actualIntegers)
t.Fatalf("a decoded integer is not expected. [expected:%v] [actual:%v]", bigIntegers[k], actualIntegers[k])
}
}
}
func TestMakeSliceResult(t *testing.T) {
jsonStr := `{
"error": {
"message": "Invalid OAuth access token.",
"type": "OAuthException",
"code": 190
}
}`
var res []Result
err := makeResult([]byte(jsonStr), &res)
if err == nil {
t.Fatalf("makeResult must fail")
}
fbErr, ok := err.(*Error)
if !ok {
t.Fatalf("error must be a facebook error. [e:%v]", err)
}
if fbErr.Code != 190 {
t.Fatalf("invalid facebook error. [e:%v]", fbErr.Error())
}
}
func TestMakeSliceResultWithNilElements(t *testing.T) {
jsonStr := `[
null,
{
"foo": "bar"
},
null
]`
var res []Result
err := makeResult([]byte(jsonStr), &res)
if err != nil {
t.Fatalf("fail to decode results. [e:%v]", err)
}
if len(res) != 3 {
t.Fatalf("expect 3 elements in res. [res:%v]", res)
}
if res[0] != nil || res[1] == nil || res[2] != nil {
t.Fatalf("decoded res is not expected. [res:%v]", res)
}
if res[1]["foo"].(string) != "bar" {
t.Fatalf("decode res is not expected. [res:%v]", res)
}
}
// case for #39.
func TestResultDecodeNumberString(t *testing.T) {
res := Result{
"int": json.Number("1234"),
"string": json.Number("1234"),
"float": json.Number("1234"),
}
var intValue int64
var strValue string
var floatValue float32
var err error
err = res.DecodeField("int", &intValue)
if err != nil {
t.Fatalf("fail to decode field `int`. [e:%v]", err)
}
if intValue != 1234 {
t.Fatalf("unexpected int value. [expect:1234] [actual:%v]", intValue)
}
err = res.DecodeField("string", &strValue)
if err != nil {
t.Fatalf("fail to decode field `string`. [e:%v]", err)
}
if strValue != "1234" {
t.Fatalf("unexpected string value. [expect:1234] [actual:%v]", strValue)
}
err = res.DecodeField("float", &floatValue)
if err != nil {
t.Fatalf("fail to decode field `float`. [e:%v]", err)
}
if floatValue != 1234 {
t.Fatalf("unexpected float value. [expect:1234] [actual:%v]", floatValue)
}
}
func TestResultDecodeUnmarshaler(t *testing.T) {
correctJSON := `{
"marshaler": {
"name": "foo",
"hash": 123456
}
}`
errorJSON := `{
"marshaler": {
"name": "bar",
"hash": 123456
}
}`
var correctResult, errorResult Result
err1 := makeResult([]byte(correctJSON), &correctResult)
err2 := makeResult([]byte(errorJSON), &errorResult)
if err1 != nil || err2 != nil {
t.Fatalf("invalid test case input. [e1:%v] [e2:%v]", err1, err2)
}