@@ -7,38 +7,69 @@ import (
77 "github.com/line/line-bot-sdk-go/v8/linebot/messaging_api"
88)
99
10+ func assertJSONFields (t * testing.T , jsonData []byte , expected map [string ]any , absentKeys []string ) {
11+ var got map [string ]any
12+ if err := json .Unmarshal (jsonData , & got ); err != nil {
13+ t .Fatalf ("Invalid JSON: %v" , err )
14+ }
15+
16+ for k , v := range expected {
17+ val , ok := got [k ]
18+ if ! ok {
19+ t .Errorf ("Expected field %q to be present, but it's missing" , k )
20+ continue
21+ }
22+
23+ // If the deserialized value is float64, convert it to int before comparison
24+ if f , ok := val .(float64 ); ok {
25+ val = int (f )
26+ }
27+
28+ if val != v {
29+ t .Errorf ("Expected %q=%v, got %v" , k , v , val )
30+ }
31+ }
32+
33+ for _ , k := range absentKeys {
34+ if _ , ok := got [k ]; ok {
35+ t .Errorf ("Expected field %q to be absent, but it exists" , k )
36+ }
37+ }
38+ }
39+
1040func TestLimitSerialization (t * testing.T ) {
1141 tests := []struct {
12- name string
13- limit messaging_api.Limit
14- expected string
42+ name string
43+ limit messaging_api.Limit
44+ expected map [string ]any
45+ absentKeys []string
1546 }{
1647 {
1748 name : "Max is set" ,
1849 limit : messaging_api.Limit {Max : 10 , UpToRemainingQuota : true },
19- expected : ` {"max":10,"upToRemainingQuota":true}` ,
50+ expected : map [ string ] any {"max" : 10 , "upToRemainingQuota" : true },
2051 },
2152 {
22- name : "Max is zero (omitempty)" ,
23- limit : messaging_api.Limit {Max : 0 , UpToRemainingQuota : true },
24- expected : `{"upToRemainingQuota":true}` ,
53+ name : "Max is zero (omitempty)" ,
54+ limit : messaging_api.Limit {Max : 0 , UpToRemainingQuota : true },
55+ expected : map [string ]any {"upToRemainingQuota" : true },
56+ absentKeys : []string {"max" },
2557 },
2658 {
27- name : "Max is zero and UpToRemainingQuota is false" ,
28- limit : messaging_api.Limit {Max : 0 , UpToRemainingQuota : false },
29- expected : `{"upToRemainingQuota":false}` ,
59+ name : "Max is zero and UpToRemainingQuota is false" ,
60+ limit : messaging_api.Limit {Max : 0 , UpToRemainingQuota : false },
61+ expected : map [string ]any {"upToRemainingQuota" : false },
62+ absentKeys : []string {"max" },
3063 },
3164 }
3265
3366 for _ , tt := range tests {
3467 t .Run (tt .name , func (t * testing.T ) {
3568 data , err := json .Marshal (tt .limit )
3669 if err != nil {
37- t .Fatalf ("Failed to marshal Limit: %v" , err )
38- }
39- if string (data ) != tt .expected {
40- t .Errorf ("Expected JSON: %s, got: %s" , tt .expected , string (data ))
70+ t .Fatalf ("marshal error: %v" , err )
4171 }
72+ assertJSONFields (t , data , tt .expected , tt .absentKeys )
4273 })
4374 }
4475}
0 commit comments