This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
fulfillment_test.go
95 lines (89 loc) · 2.22 KB
/
fulfillment_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
package dialogflow
import (
"errors"
"fmt"
"reflect"
"testing"
)
// wrong type that fails on marshal and implements the RichMessage interface
type wrong struct{}
func (wrong) GetKey() string {
return "wrong"
}
func (wrong) MarshalJSON() ([]byte, error) {
return []byte(""), errors.New("That's wrong")
}
func TestForGoogle(t *testing.T) {
type args struct {
r RichMessage
}
tests := []struct {
name string
args args
want Message
}{
{"should generate rich message for google", args{PayloadWrapper{"hello"}}, Message{Platform: ActionsOnGoogle, RichMessage: PayloadWrapper{"hello"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ForGoogle(tt.args.r); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ForGoogle() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleForGoogle() {
output := "Hello World !"
fulfillment := Fulfillment{
FulfillmentMessages: Messages{
ForGoogle(SingleSimpleResponse(output, output)),
},
}
fmt.Println(fulfillment)
}
func TestMessage_MarshalJSON(t *testing.T) {
type fields struct {
Platform Platform
RichMessage RichMessage
}
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{
{"should marshal", fields{}, []byte(`{}`), false},
{"should marshal with platform", fields{Platform: ActionsOnGoogle}, []byte(`{"platform": "ACTIONS_ON_GOOGLE"}`), false},
{
"should marshal with platform and message",
fields{Platform: ActionsOnGoogle, RichMessage: SingleSimpleResponse("hi", "hi")},
[]byte(`{"platform": "ACTIONS_ON_GOOGLE", "simpleResponses": {"simpleResponses":[{"textToSpeech":"hi","displayText":"hi"}]}}`),
false,
},
{
"should fail because of message",
fields{RichMessage: wrong{}},
[]byte(""),
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Message{
Platform: tt.fields.Platform,
RichMessage: tt.fields.RichMessage,
}
got, err := m.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("Message.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
return
}
if err := JSONEqual(got, tt.want); err != nil {
t.Errorf("Message.MarshalJSON() error =%v", err)
}
})
}
}