Skip to content

Commit bb9e60b

Browse files
committed
fix: added TextButton & ButtonTypeText
1 parent 4f22dca commit bb9e60b

File tree

2 files changed

+312
-9
lines changed

2 files changed

+312
-9
lines changed

botkb/buttons.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ type Button interface {
88
}
99

1010
const (
11-
ButtonTypeData ButtonType = iota
11+
ButtonTypeText ButtonType = iota
12+
ButtonTypeData
1213
ButtonTypeURL
1314
ButtonTypeSwitchInlineQuery
1415
ButtonTypeSwitchInlineQueryCurrentChat
1516
)
1617

18+
var _ Button = (*TextButton)(nil)
1719
var _ Button = (*DataButton)(nil)
1820
var _ Button = (*UrlButton)(nil)
1921
var _ Button = (*SwitchInlineQueryButton)(nil)
@@ -23,9 +25,25 @@ func NewDataButton(text, data string) *DataButton {
2325
return &DataButton{Text: text, Data: data}
2426
}
2527

28+
type TextButton struct {
29+
Text string `json:"text"`
30+
}
31+
32+
func NewTextButton(text string) *TextButton {
33+
return &TextButton{Text: text}
34+
}
35+
36+
func (t TextButton) GetText() string {
37+
return t.Text
38+
}
39+
40+
func (t TextButton) ButtonType() ButtonType {
41+
return ButtonTypeText
42+
}
43+
2644
type DataButton struct {
27-
Text string
28-
Data string
45+
Text string `json:"text"`
46+
Data string `json:"data"`
2947
}
3048

3149
func (b DataButton) GetText() string {
@@ -37,8 +55,8 @@ func (DataButton) ButtonType() ButtonType {
3755
}
3856

3957
type UrlButton struct {
40-
Text string
41-
URL string
58+
Text string `json:"text"`
59+
URL string `json:"url"`
4260
}
4361

4462
func NewUrlButton(text, url string) *UrlButton {
@@ -54,8 +72,8 @@ func (b UrlButton) GetText() string {
5472
}
5573

5674
type SwitchInlineQueryButton struct {
57-
Text string
58-
Query string
75+
Text string `json:"text"`
76+
Query string `json:"query"`
5977
}
6078

6179
func NewSwitchInlineQueryButton(text, query string) *SwitchInlineQueryButton {
@@ -71,8 +89,8 @@ func (b SwitchInlineQueryButton) GetText() string {
7189
}
7290

7391
type SwitchInlineQueryCurrentChatButton struct {
74-
Text string
75-
Query string
92+
Text string `json:"text"`
93+
Query string `json:"query"`
7694
}
7795

7896
func NewSwitchInlineQueryCurrentChatButton(text, query string) *SwitchInlineQueryCurrentChatButton {

botkb/buttons_test.go

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
package botkb
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestTextButton(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
button TextButton
11+
wantText string
12+
wantType ButtonType
13+
}{
14+
{
15+
name: "Basic TextButton",
16+
button: TextButton{Text: "Click me"},
17+
wantText: "Click me",
18+
wantType: ButtonTypeText,
19+
},
20+
{
21+
name: "Empty TextButton",
22+
button: TextButton{Text: ""},
23+
wantText: "",
24+
wantType: ButtonTypeText,
25+
},
26+
}
27+
28+
for _, tt := range tests {
29+
t.Run(tt.name, func(t *testing.T) {
30+
if got := tt.button.GetText(); got != tt.wantText {
31+
t.Errorf("TextButton.GetText() = %v, want %v", got, tt.wantText)
32+
}
33+
if got := tt.button.ButtonType(); got != tt.wantType {
34+
t.Errorf("TextButton.ButtonType() = %v, want %v", got, tt.wantType)
35+
}
36+
})
37+
}
38+
}
39+
40+
func TestNewTextButton(t *testing.T) {
41+
tests := []struct {
42+
name string
43+
text string
44+
wantText string
45+
wantType ButtonType
46+
}{
47+
{
48+
name: "Basic NewTextButton",
49+
text: "Click me",
50+
wantText: "Click me",
51+
wantType: ButtonTypeText,
52+
},
53+
{
54+
name: "Empty text NewTextButton",
55+
text: "",
56+
wantText: "",
57+
wantType: ButtonTypeText,
58+
},
59+
}
60+
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
button := NewTextButton(tt.text)
64+
65+
if button.Text != tt.text {
66+
t.Errorf("NewTextButton().Text = %v, want %v", button.Text, tt.text)
67+
}
68+
69+
if got := button.GetText(); got != tt.wantText {
70+
t.Errorf("TextButton.GetText() = %v, want %v", got, tt.wantText)
71+
}
72+
if got := button.ButtonType(); got != tt.wantType {
73+
t.Errorf("TextButton.ButtonType() = %v, want %v", got, tt.wantType)
74+
}
75+
})
76+
}
77+
}
78+
79+
func TestDataButton(t *testing.T) {
80+
tests := []struct {
81+
name string
82+
text string
83+
data string
84+
wantText string
85+
wantType ButtonType
86+
}{
87+
{
88+
name: "Basic DataButton",
89+
text: "Click me",
90+
data: "button_data",
91+
wantText: "Click me",
92+
wantType: ButtonTypeData,
93+
},
94+
{
95+
name: "Empty text DataButton",
96+
text: "",
97+
data: "button_data",
98+
wantText: "",
99+
wantType: ButtonTypeData,
100+
},
101+
{
102+
name: "Empty data DataButton",
103+
text: "Click me",
104+
data: "",
105+
wantText: "Click me",
106+
wantType: ButtonTypeData,
107+
},
108+
}
109+
110+
for _, tt := range tests {
111+
t.Run(tt.name, func(t *testing.T) {
112+
button := NewDataButton(tt.text, tt.data)
113+
114+
if button.Text != tt.text {
115+
t.Errorf("NewDataButton().Text = %v, want %v", button.Text, tt.text)
116+
}
117+
if button.Data != tt.data {
118+
t.Errorf("NewDataButton().Data = %v, want %v", button.Data, tt.data)
119+
}
120+
121+
if got := button.GetText(); got != tt.wantText {
122+
t.Errorf("DataButton.GetText() = %v, want %v", got, tt.wantText)
123+
}
124+
if got := button.ButtonType(); got != tt.wantType {
125+
t.Errorf("DataButton.ButtonType() = %v, want %v", got, tt.wantType)
126+
}
127+
})
128+
}
129+
}
130+
131+
func TestUrlButton(t *testing.T) {
132+
tests := []struct {
133+
name string
134+
text string
135+
url string
136+
wantText string
137+
wantType ButtonType
138+
}{
139+
{
140+
name: "Basic UrlButton",
141+
text: "Visit website",
142+
url: "https://example.com",
143+
wantText: "Visit website",
144+
wantType: ButtonTypeURL,
145+
},
146+
{
147+
name: "Empty text UrlButton",
148+
text: "",
149+
url: "https://example.com",
150+
wantText: "",
151+
wantType: ButtonTypeURL,
152+
},
153+
{
154+
name: "Empty URL UrlButton",
155+
text: "Visit website",
156+
url: "",
157+
wantText: "Visit website",
158+
wantType: ButtonTypeURL,
159+
},
160+
}
161+
162+
for _, tt := range tests {
163+
t.Run(tt.name, func(t *testing.T) {
164+
button := NewUrlButton(tt.text, tt.url)
165+
166+
if button.Text != tt.text {
167+
t.Errorf("NewUrlButton().Text = %v, want %v", button.Text, tt.text)
168+
}
169+
if button.URL != tt.url {
170+
t.Errorf("NewUrlButton().URL = %v, want %v", button.URL, tt.url)
171+
}
172+
173+
if got := button.GetText(); got != tt.wantText {
174+
t.Errorf("UrlButton.GetText() = %v, want %v", got, tt.wantText)
175+
}
176+
if got := button.ButtonType(); got != tt.wantType {
177+
t.Errorf("UrlButton.ButtonType() = %v, want %v", got, tt.wantType)
178+
}
179+
})
180+
}
181+
}
182+
183+
func TestSwitchInlineQueryButton(t *testing.T) {
184+
tests := []struct {
185+
name string
186+
text string
187+
query string
188+
wantText string
189+
wantType ButtonType
190+
}{
191+
{
192+
name: "Basic SwitchInlineQueryButton",
193+
text: "Search",
194+
query: "query_string",
195+
wantText: "Search",
196+
wantType: ButtonTypeSwitchInlineQuery,
197+
},
198+
{
199+
name: "Empty text SwitchInlineQueryButton",
200+
text: "",
201+
query: "query_string",
202+
wantText: "",
203+
wantType: ButtonTypeSwitchInlineQuery,
204+
},
205+
{
206+
name: "Empty query SwitchInlineQueryButton",
207+
text: "Search",
208+
query: "",
209+
wantText: "Search",
210+
wantType: ButtonTypeSwitchInlineQuery,
211+
},
212+
}
213+
214+
for _, tt := range tests {
215+
t.Run(tt.name, func(t *testing.T) {
216+
button := NewSwitchInlineQueryButton(tt.text, tt.query)
217+
218+
if button.Text != tt.text {
219+
t.Errorf("NewSwitchInlineQueryButton().Text = %v, want %v", button.Text, tt.text)
220+
}
221+
if button.Query != tt.query {
222+
t.Errorf("NewSwitchInlineQueryButton().Query = %v, want %v", button.Query, tt.query)
223+
}
224+
225+
if got := button.GetText(); got != tt.wantText {
226+
t.Errorf("SwitchInlineQueryButton.GetText() = %v, want %v", got, tt.wantText)
227+
}
228+
if got := button.ButtonType(); got != tt.wantType {
229+
t.Errorf("SwitchInlineQueryButton.ButtonType() = %v, want %v", got, tt.wantType)
230+
}
231+
})
232+
}
233+
}
234+
235+
func TestSwitchInlineQueryCurrentChatButton(t *testing.T) {
236+
tests := []struct {
237+
name string
238+
text string
239+
query string
240+
wantText string
241+
wantType ButtonType
242+
}{
243+
{
244+
name: "Basic SwitchInlineQueryCurrentChatButton",
245+
text: "Search here",
246+
query: "query_string",
247+
wantText: "Search here",
248+
wantType: ButtonTypeSwitchInlineQueryCurrentChat,
249+
},
250+
{
251+
name: "Empty text SwitchInlineQueryCurrentChatButton",
252+
text: "",
253+
query: "query_string",
254+
wantText: "",
255+
wantType: ButtonTypeSwitchInlineQueryCurrentChat,
256+
},
257+
{
258+
name: "Empty query SwitchInlineQueryCurrentChatButton",
259+
text: "Search here",
260+
query: "",
261+
wantText: "Search here",
262+
wantType: ButtonTypeSwitchInlineQueryCurrentChat,
263+
},
264+
}
265+
266+
for _, tt := range tests {
267+
t.Run(tt.name, func(t *testing.T) {
268+
button := NewSwitchInlineQueryCurrentChatButton(tt.text, tt.query)
269+
270+
if button.Text != tt.text {
271+
t.Errorf("NewSwitchInlineQueryCurrentChatButton().Text = %v, want %v", button.Text, tt.text)
272+
}
273+
if button.Query != tt.query {
274+
t.Errorf("NewSwitchInlineQueryCurrentChatButton().Query = %v, want %v", button.Query, tt.query)
275+
}
276+
277+
if got := button.GetText(); got != tt.wantText {
278+
t.Errorf("SwitchInlineQueryCurrentChatButton.GetText() = %v, want %v", got, tt.wantText)
279+
}
280+
if got := button.ButtonType(); got != tt.wantType {
281+
t.Errorf("SwitchInlineQueryCurrentChatButton.ButtonType() = %v, want %v", got, tt.wantType)
282+
}
283+
})
284+
}
285+
}

0 commit comments

Comments
 (0)