handle empty text and multi-line text
This commit is contained in:
		@@ -6,6 +6,7 @@ import (
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
@@ -69,15 +70,17 @@ func (g *GList) persist() {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *GList) Add(text string) error {
 | 
			
		||||
	for _, item := range g.Items {
 | 
			
		||||
		if item.Text == text {
 | 
			
		||||
			return fmt.Errorf("dupe:%s", text)
 | 
			
		||||
func (g *GList) Add(t string) {
 | 
			
		||||
outer:
 | 
			
		||||
	for _, text := range strings.Split(t, "\n") {
 | 
			
		||||
		for _, item := range g.Items {
 | 
			
		||||
			if item.Text == text {
 | 
			
		||||
				continue outer
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		g.Items = append(g.Items, Entry{text, false})
 | 
			
		||||
	}
 | 
			
		||||
	g.Items = append(g.Items, Entry{text, false})
 | 
			
		||||
	reqs <- g
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *GList) Toggle(text string) error {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package glist
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
@@ -13,8 +14,23 @@ func TestGList(t *testing.T) {
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	expected := `{"chat_id":4342,"text":"ok","reply_markup":{"inline_keyboard":[[{"text":"✓ foo","callback_data":"foo"}]]}}`
 | 
			
		||||
 | 
			
		||||
	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{}
 | 
			
		||||
	reqs = 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}]}`
 | 
			
		||||
	actual := fmt.Sprintf("%v", g)
 | 
			
		||||
	if expected != actual {
 | 
			
		||||
		t.Fatalf("expected: %s\n got:%s\n", expected, actual)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user