From f643780c473c13fe879f4ee917d276b4f214fd5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Stemmer?= Date: Sun, 26 Jun 2022 14:23:58 +0100 Subject: [PATCH] parser/gotest: Add tests for internal package collector --- .../internal/collector/collector_test.go | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 parser/gotest/internal/collector/collector_test.go diff --git a/parser/gotest/internal/collector/collector_test.go b/parser/gotest/internal/collector/collector_test.go new file mode 100644 index 0000000..1479ba4 --- /dev/null +++ b/parser/gotest/internal/collector/collector_test.go @@ -0,0 +1,91 @@ +package collector + +import ( + "strconv" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestClear(t *testing.T) { + o := New() + o.Append(1, "1") + o.Append(2, "2") + o.Clear(1) + + want := []string(nil) + got := o.Get(1) + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("Clear(1) did not clear output (-want +got):\n%s", diff) + } + + want = []string{"2"} + got = o.Get(2) + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("Clear(1) cleared wrong output (-want +got):\n%s", diff) + } +} + +func TestAppendAndGet(t *testing.T) { + o := New() + o.Append(1, "1.1") + o.Append(1, "1.2") + o.Append(2, "2") + o.Append(1, "1.3") + + want := []string{"1.1", "1.2", "1.3"} + got := o.Get(1) + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("Append() incorrect (-want +got):\n%s", diff) + } +} + +func TestContains(t *testing.T) { + o := New() + o.Append(1, "1") + o.Append(2, "2") + o.Clear(1) + + if !o.Contains(2) { + t.Errorf("Contains(1) incorrect, got true want false") + } + for i := -100; i < 100; i++ { + if i != 2 && o.Contains(i) { + t.Errorf("Contains(%d) incorrect, got true want false", i) + } + } +} + +func TestGetAll(t *testing.T) { + o := New() + for i := 1; i <= 10; i++ { + o.Append(i%3, strconv.Itoa(i)) + } + + want := []string{"1", "2", "4", "5", "7", "8", "10"} + got := o.GetAll(1, 2) + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("GetAll(1, 2) incorrect (-want +got):\n%s", diff) + } +} + +func TestMerge(t *testing.T) { + o := New() + for i := 1; i <= 10; i++ { + o.Append(i%3, strconv.Itoa(i)) + } + + o.Merge(2, 1) + + want := []string{"1", "2", "4", "5", "7", "8", "10"} + got := o.Get(1) + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("Get(1) after Merge(2, 1) incorrect (-want +got):\n%s", diff) + } + + want = []string(nil) + got = o.Get(2) + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("Get(2) after Merge(2, 1) incorrect (-want +got):\n%s", diff) + } +}