Skip to content

Commit

Permalink
Add tests for counters
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie committed Jan 24, 2016
1 parent f2df789 commit e125fb5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions report/counters_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package report

import (
"testing"

"github.com/weaveworks/scope/test"
"github.com/weaveworks/scope/test/reflect"
)

func TestCountersAdd(t *testing.T) {
want := EmptyCounters.
Add("foo", 3)
have := EmptyCounters.
Add("foo", 1).
Add("foo", 2)
if !reflect.DeepEqual(want, have) {
t.Errorf(test.Diff(want, have))
}

This comment has been minimized.

Copy link
@paulbellamy

paulbellamy Jan 25, 2016

Contributor

This check is covered by the two cases below.

This comment has been minimized.

Copy link
@tomwilkie

tomwilkie Jan 25, 2016

Author Contributor

Not exactly. This check ensures there aren't other entries and exercises the deepequals method, whilst the check below tests the lookup positive and negative cases. All these test were directed by coverage.

This comment has been minimized.

Copy link
@paulbellamy

paulbellamy Jan 25, 2016

Contributor

Then, I'd rather have an explicit test for there being no other entries.

If you want coverage of the deepequals method that should be done in TestCountersDeepEquals.

if v, ok := have.Lookup("foo"); !ok || v != 3 {
t.Errorf("foo != 3")
}
if v, ok := have.Lookup("bar"); ok || v != 0 {
t.Errorf("bar != nil")
}
}

func TestCountersMerge(t *testing.T) {
for name, c := range map[string]struct {
a, b, want Counters
}{
"Empty a": {
a: EmptyCounters,
b: EmptyCounters.
Add("foo", 1),
want: EmptyCounters.
Add("foo", 1),
},
"Empty b": {
a: EmptyCounters.
Add("foo", 1),
b: EmptyCounters,
want: EmptyCounters.
Add("foo", 1),
},
"Disparate keys": {
a: EmptyCounters.
Add("foo", 1),
b: EmptyCounters.
Add("bar", 2),
want: EmptyCounters.
Add("foo", 1).
Add("bar", 2),
},
"Key merge": {
a: EmptyCounters.
Add("foo", 1),
b: EmptyCounters.
Add("foo", 2),
want: EmptyCounters.
Add("foo", 3),
},
} {
if have := c.a.Merge(c.b); !reflect.DeepEqual(c.want, have) {
t.Errorf("%s:\n%s", name, test.Diff(c.want, have))
}
}
}

func TestCountersEncoding(t *testing.T) {
want := EmptyCounters.
Add("foo", 1).
Add("bar", 2)

{
gobs, err := want.GobEncode()
if err != nil {
t.Fatal(err)
}
have := EmptyCounters
have.GobDecode(gobs)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}

{
json, err := want.MarshalJSON()
if err != nil {
t.Fatal(err)
}
have := EmptyCounters
have.UnmarshalJSON(json)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
}
File renamed without changes.

0 comments on commit e125fb5

Please sign in to comment.