1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package api
17
18 import (
19 "testing"
20
21 "github.com/google/go-cmp/cmp"
22 "github.com/google/go-cmp/cmp/cmpopts"
23 )
24
25 func Test_Collection(t *testing.T) {
26
27 vals := []string{"foo", "bar", "baz", "baz", "baz"}
28
29 t.Run("Unique", func(t *testing.T) {
30 unq := NewUniq()
31 unq.Add(vals...)
32
33 if len(unq.Values()) != 3 {
34 t.Errorf("expected 3 unique values, got %d", len(unq.Values()))
35 }
36 expected := []string{"foo", "bar", "baz"}
37 if !testEqualNoOrder(t, expected, unq.Values()) {
38 t.Errorf("expected %v, got %v", expected, unq.Values())
39 }
40 })
41
42 t.Run("Collection", func(t *testing.T) {
43
44 uniq1 := []string{"foo", "bar", "baz"}
45 uniq2 := []string{"foo", "bar", "baz"}
46 uniq3 := []string{"corge", "grault", "garply", "foo"}
47
48 tests := []struct {
49 name string
50 operator string
51 expected []string
52 }{
53 {name: "with 'and' operator",
54 operator: "and",
55 expected: []string{"foo"},
56 },
57 {name: "with 'or' operator",
58 operator: "or",
59 expected: []string{"foo", "bar", "baz", "corge", "grault", "garply"},
60 },
61 }
62
63 for _, test := range tests {
64 t.Run(test.name, func(t *testing.T) {
65 c := NewCollection(test.operator)
66 c.Add(uniq1)
67 c.Add(uniq2)
68 c.Add(uniq3)
69
70 if !testEqualNoOrder(t, test.expected, c.Values()) {
71 t.Errorf("expected %v, got %v", test.expected, c.Values())
72 }
73 })
74 }
75
76 })
77
78 }
79
80
81 func testEqualNoOrder(t *testing.T, expected, actual []string) bool {
82 t.Helper()
83 less := func(a, b string) bool { return a < b }
84 return cmp.Diff(actual, expected, cmpopts.SortSlices(less)) == ""
85 }
86
View as plain text