|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | +) |
| 9 | + |
| 10 | +func TestClear(t *testing.T) { |
| 11 | + o := New() |
| 12 | + o.Append(1, "1") |
| 13 | + o.Append(2, "2") |
| 14 | + o.Clear(1) |
| 15 | + |
| 16 | + want := []string(nil) |
| 17 | + got := o.Get(1) |
| 18 | + if diff := cmp.Diff(want, got); diff != "" { |
| 19 | + t.Errorf("Clear(1) did not clear output (-want +got):\n%s", diff) |
| 20 | + } |
| 21 | + |
| 22 | + want = []string{"2"} |
| 23 | + got = o.Get(2) |
| 24 | + if diff := cmp.Diff(want, got); diff != "" { |
| 25 | + t.Errorf("Clear(1) cleared wrong output (-want +got):\n%s", diff) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestAppendAndGet(t *testing.T) { |
| 30 | + o := New() |
| 31 | + o.Append(1, "1.1") |
| 32 | + o.Append(1, "1.2") |
| 33 | + o.Append(2, "2") |
| 34 | + o.Append(1, "1.3") |
| 35 | + |
| 36 | + want := []string{"1.1", "1.2", "1.3"} |
| 37 | + got := o.Get(1) |
| 38 | + if diff := cmp.Diff(want, got); diff != "" { |
| 39 | + t.Errorf("Append() incorrect (-want +got):\n%s", diff) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestContains(t *testing.T) { |
| 44 | + o := New() |
| 45 | + o.Append(1, "1") |
| 46 | + o.Append(2, "2") |
| 47 | + o.Clear(1) |
| 48 | + |
| 49 | + if !o.Contains(2) { |
| 50 | + t.Errorf("Contains(1) incorrect, got true want false") |
| 51 | + } |
| 52 | + for i := -100; i < 100; i++ { |
| 53 | + if i != 2 && o.Contains(i) { |
| 54 | + t.Errorf("Contains(%d) incorrect, got true want false", i) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestGetAll(t *testing.T) { |
| 60 | + o := New() |
| 61 | + for i := 1; i <= 10; i++ { |
| 62 | + o.Append(i%3, strconv.Itoa(i)) |
| 63 | + } |
| 64 | + |
| 65 | + want := []string{"1", "2", "4", "5", "7", "8", "10"} |
| 66 | + got := o.GetAll(1, 2) |
| 67 | + if diff := cmp.Diff(want, got); diff != "" { |
| 68 | + t.Errorf("GetAll(1, 2) incorrect (-want +got):\n%s", diff) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestMerge(t *testing.T) { |
| 73 | + o := New() |
| 74 | + for i := 1; i <= 10; i++ { |
| 75 | + o.Append(i%3, strconv.Itoa(i)) |
| 76 | + } |
| 77 | + |
| 78 | + o.Merge(2, 1) |
| 79 | + |
| 80 | + want := []string{"1", "2", "4", "5", "7", "8", "10"} |
| 81 | + got := o.Get(1) |
| 82 | + if diff := cmp.Diff(want, got); diff != "" { |
| 83 | + t.Errorf("Get(1) after Merge(2, 1) incorrect (-want +got):\n%s", diff) |
| 84 | + } |
| 85 | + |
| 86 | + want = []string(nil) |
| 87 | + got = o.Get(2) |
| 88 | + if diff := cmp.Diff(want, got); diff != "" { |
| 89 | + t.Errorf("Get(2) after Merge(2, 1) incorrect (-want +got):\n%s", diff) |
| 90 | + } |
| 91 | +} |
0 commit comments