From da8a879f65619f699400434bd59f24ec6bac0d8b Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 29 Jul 2020 16:06:05 +0000 Subject: [PATCH] removing the old examples --- examples/std_test.go | 83 -------------------------------------------- examples/tea_test.go | 80 ------------------------------------------ examples/thing.go | 43 ----------------------- 3 files changed, 206 deletions(-) delete mode 100644 examples/std_test.go delete mode 100644 examples/tea_test.go delete mode 100644 examples/thing.go diff --git a/examples/std_test.go b/examples/std_test.go deleted file mode 100644 index 855191e..0000000 --- a/examples/std_test.go +++ /dev/null @@ -1,83 +0,0 @@ -// +build std - -package thing - -import ( - "testing" -) - -func TestSet(t *testing.T) { - tests := []struct { - key string - value string - bad bool - }{ - {"foo", "bar", false}, - {"foo", "a-value", false}, - {"foo", "a value", false}, - {"one-two-three", "whatever", false}, - {"one-two-three", "what ever", false}, - {"one two three", "whatever", true}, - {" one-two-three", "whatever", true}, - } - - for _, test := range tests { - thing := new(Thing) - err := thing.Set(test.key, test.value) - if !test.bad && err != nil { - t.Errorf("should be able to set %q=%q but saw error %v", test.key, test.value, err) - } - if test.bad && err == nil { - t.Errorf("able to set bad values %q=%q", test.key, test.value) - } - } -} - -func TestHas(t *testing.T) { - var ( - key = "foo" - value = "bar" - thing = &Thing{ - data: map[string]string{ - key: value, - }, - } - ) - - if !thing.Has(key) { - t.Errorf("missing expected key %q", key) - } -} - -func TestGet(t *testing.T) { - var ( - key = "foo" - value = "bar" - thing = &Thing{ - data: map[string]string{ - key: value, - }, - } - ) - - if v := thing.Get(key); v != value { - t.Errorf("read value %q, expected %q", v, value) - } -} - -func TestKeys(t *testing.T) { - goodKeys := []string{"one", "two", "3", "one-two-three", "steve?"} - badKeys := []string{"o n e", "one two three", " one", "one "} - - for _, key := range goodKeys { - if err := validateKey(key); err != nil { - t.Errorf("key %q should be valid but saw validation error %v", key, err) - } - } - - for _, key := range badKeys { - if err := validateKey(key); err == nil { - t.Errorf("key %q should be invalid but passed validation", key) - } - } -} diff --git a/examples/tea_test.go b/examples/tea_test.go deleted file mode 100644 index 0b2613e..0000000 --- a/examples/tea_test.go +++ /dev/null @@ -1,80 +0,0 @@ -// +build tea - -package thing - -import ( - "fmt" - "testing" - - "github.com/jordanorelli/tea" -) - -type empty struct{} - -func (e *empty) Run(t *testing.T) {} - -type testThingSetup struct { - Thing *Thing `tea:"save"` -} - -func (test *testThingSetup) Run(t *testing.T) { - t.Logf("[%s] running testThingSetup", t.Name()) - if test.Thing != nil { - t.Fatal("should be nil") - } - test.Thing = new(Thing) -} - -func (test *testThingSetup) After(t *testing.T) { - t.Logf("[%s] testThingSetup after", t.Name()) -} - -func (test testThingSetup) String() string { return "thingSetup" } - -type setKey struct { - Thing *Thing `tea:"load"` - key string - value string - bad bool -} - -func (test setKey) String() string { - return fmt.Sprintf("setKey:%s", test.key) -} - -func (test *setKey) Run(t *testing.T) { - t.Logf("[%s] running setKey key: %q value: %q", t.Name(), test.key, test.value) - - // test.Thing is automatically propagated from the prior test by tea! - err := test.Thing.Set(test.key, test.value) - if !test.bad && err != nil { - t.Errorf("should be able to set %q=%q but saw error %v", test.key, test.value, err) - } - if test.bad && err == nil { - t.Errorf("able to set bad values %q=%q", test.key, test.value) - } -} - -func (test *setKey) After(t *testing.T) { - t.Logf("[%s] setKey after key: %q value: %q", t.Name(), test.key, test.value) -} - -func TestThing(t *testing.T) { - root := tea.New(new(testThingSetup)) - - root.Child(&setKey{key: "alice", value: "apple"}) - root.Child(&setKey{key: "bob", value: "banana"}) - root.Child(new(empty)).Child(&setKey{key: "carol", value: "cherry"}) - root.Child(&setKey{bad: true}) - - bob := root.Child(&setKey{key: "b ob", value: "banana"}) - bob.Child(&setKey{key: "car-el", value: "cherry"}) - dave := bob.Child(&setKey{key: "dave", value: "durian"}) - dave.Child(&setKey{key: "evan", value: "elderberry"}) - - root.Child(&setKey{key: "al ice", bad: true}) - root.Child(&setKey{key: " alice", bad: true}) - root.Child(&setKey{key: "alice ", bad: true}) - - tea.Run(t, root) -} diff --git a/examples/thing.go b/examples/thing.go deleted file mode 100644 index e3b63d4..0000000 --- a/examples/thing.go +++ /dev/null @@ -1,43 +0,0 @@ -package thing - -import ( - "errors" - "fmt" - "strings" -) - -var ( - ErrInvalidKey = errors.New("invalid key") - ErrNotFound = errors.New("not found") -) - -func validateKey(key string) error { - if strings.TrimSpace(key) == "" { - return fmt.Errorf("%w: key cannot be empty string", ErrInvalidKey) - } - if strings.Count(key, " ") > 0 { - return fmt.Errorf("%w: key cannot contain spaces", ErrInvalidKey) - } - return nil -} - -type Thing struct { - data map[string]string -} - -func (t *Thing) Get(key string) string { - return t.data[key] -} - -func (t *Thing) Set(key, value string) error { - if err := validateKey(key); err != nil { - return fmt.Errorf("unable to set value for key %q: %w", key, err) - } - t.data = map[string]string{key: value} - return nil -} - -func (t *Thing) Has(key string) bool { - _, ok := t.data[key] - return ok -}