Fix missing error checks and test
This commit is contained in:
parent
f80aae9e03
commit
a30aae9bf4
@ -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() {
|
||||
|
@ -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)
|
||||
|
8
main.go
8
main.go
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user