Fix missing error checks and test

This commit is contained in:
Balakrishnan Balasubramanian 2022-12-28 01:36:04 -05:00
parent f80aae9e03
commit a30aae9bf4
3 changed files with 14 additions and 12 deletions

View File

@ -86,15 +86,15 @@ outer:
PersistReqC <- g
}
func (g *GList) Toggle(text string) error {
func (g *GList) Toggle(text string) {
for i, item := range g.Items {
if item.Text == text {
g.Items[i].Checked = !g.Items[i].Checked
PersistReqC <- g
return nil
return
}
}
return fmt.Errorf("not found:%s", text)
log.Printf("item not found in toggle, chat: %d, item: %s\n", g.ChatID, text)
}
func (g *GList) ClearChecked() {

View File

@ -2,34 +2,32 @@ package glist
import (
"fmt"
"sync"
"testing"
)
func TestGList(t *testing.T) {
var ti int
var m sync.Mutex
g := GList{ti, m, 4342, nil, []Entry{{"foo", true}}}
g := NewGList(4342, "foo")
data, err := g.GenSendListReq()
if err != nil {
t.Fatal(err)
}
expected := `{"chat_id":4342,"text":"List:","reply_markup":{"inline_keyboard":[[{"text":"foo","callback_data":"foo"}],[{"text":"clear checked","callback_data":"clear"}]]}}`
expected := `{"chat_id":4342,"text":"List:","reply_markup":{"inline_keyboard":[[{"text":"foo","callback_data":"foo"}],[{"text":"clear checked","callback_data":"clear"}]]}}`
if expected != string(data) {
t.Fatalf("expected: %s\n got:%s\n", expected, string(data))
}
}
func TestSplit(t *testing.T) {
g := GList{}
g := &GList{}
//This resets the channel, so test does not try to persist
PersistReqC = make(chan *GList, 50)
g.Add("foo")
g.Add("bar\nfoo\nblah")
g.Add("foo")
g.Add("lskfj")
expected := `{0 {0 0} 0 <nil> [{foo false} {bar false} {blah false} {lskfj false}]}`
g.Toggle("foo")
expected := `&{0 {0 0} 0 <nil> [{foo true} {bar false} {blah false} {lskfj false}]}`
actual := fmt.Sprintf("%v", g)
if expected != actual {
t.Fatalf("expected: %s\n got:%s\n", expected, actual)

View File

@ -70,7 +70,11 @@ func main() {
}
botPath := fmt.Sprintf("/bot%s", apiToken)
http.HandleFunc(botPath, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
_, err := w.Write([]byte("ok"))
if err != nil {
log.Println(err)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
@ -118,7 +122,7 @@ func main() {
chatID := update.CallbackQuery.Message.Chat.ID
g, ok := chats.Load(chatID)
if !ok {
log.Println("Chat not found: %s", chatID)
log.Printf("Chat not found: %d\n", chatID)
return
}
gl := g.(*glist.GList)