tracks/go/exercises/strain/strain_test.go in trackler-2.0.8.6 vs tracks/go/exercises/strain/strain_test.go in trackler-2.0.8.7

- old
+ new

@@ -19,16 +19,10 @@ "testing" ) const targetTestVersion = 1 -func TestTestVersion(t *testing.T) { - if testVersion != targetTestVersion { - t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion) - } -} - func lt10(x int) bool { return x < 10 } func gt10(x int) bool { return x > 10 } func odd(x int) bool { return x&1 == 1 } func even(x int) bool { return x&1 == 0 } @@ -49,30 +43,10 @@ {even, Ints{1, 2, 3, 4, 5}, Ints{2, 4}}, } -func TestKeepInts(t *testing.T) { - for _, test := range keepTests { - // setup here copies test.list, preserving the nil value if it is nil - // and making a fresh copy of the underlying array otherwise. - cp := test.list - if cp != nil { - cp = append(Ints{}, cp...) - } - switch res := cp.Keep(test.pred); { - case !reflect.DeepEqual(cp, test.list): - t.Fatalf("Ints%v.Keep() should not modify its reciever. "+ - "Found %v, reciever should stay %v", - test.list, cp, test.list) - case !reflect.DeepEqual(res, test.want): - t.Fatalf("Ints%v.Keep() = %v, want %v", - test.list, res, test.want) - } - } -} - var discardTests = []struct { pred func(int) bool list Ints want Ints }{ @@ -88,42 +62,67 @@ {even, Ints{1, 2, 3, 4, 5}, Ints{1, 3, 5}}, } +func TestTestVersion(t *testing.T) { + if testVersion != targetTestVersion { + t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion) + } +} + +func TestKeepInts(t *testing.T) { + for _, test := range keepTests { + // setup here copies test.list, preserving the nil value if it is nil + // and making a fresh copy of the underlying array otherwise. + cp := test.list + if cp != nil { + cp = append(Ints{}, cp...) + } + switch res := cp.Keep(test.pred); { + case !reflect.DeepEqual(cp, test.list): + t.Fatalf("%#v.Keep() should not modify its receiver. "+ + "Found %#v, receiver should stay %#v", + test.list, cp, test.list) + case !reflect.DeepEqual(res, test.want): + t.Fatalf("%#v.Keep()\ngot: %#v\nwant: %#v", + test.list, res, test.want) + } + } +} + func TestDiscardInts(t *testing.T) { for _, test := range discardTests { cp := test.list if cp != nil { cp = append(Ints{}, cp...) // dup underlying array } switch res := cp.Discard(test.pred); { case !reflect.DeepEqual(cp, test.list): - t.Fatalf("Ints%v.Discard() should not modify its reciever. "+ - "Found %v, reciever should stay %v", + t.Fatalf("%#v.Discard() should not modify its receiver. "+ + "Found %#v, receiver should stay %#v", test.list, cp, test.list) case !reflect.DeepEqual(res, test.want): - t.Fatalf("Ints%v.Discard() = %v, want %v", + t.Fatalf("%#v.Discard()\ngot: %#v\nwant: %#v", test.list, res, test.want) } } } func TestKeepStrings(t *testing.T) { zword := func(s string) bool { return len(s) > 0 && s[0] == 'z' } - list := Strings{"apple", "zebra", "banana", "zombies", "cherimoya", "zelot"} - want := Strings{"zebra", "zombies", "zelot"} + list := Strings{"apple", "zebra", "banana", "zombies", "cherimoya", "zealot"} + want := Strings{"zebra", "zombies", "zealot"} cp := append(Strings{}, list...) // make copy, as with TestInts switch res := cp.Keep(zword); { case !reflect.DeepEqual(cp, list): - t.Fatalf("Strings%v.Keep() should not modify its reciever. "+ - "Found %v, reciever should stay %v", + t.Fatalf("%#v.Keep() should not modify its receiver. "+ + "Found %#v, receiver should stay %#v", list, cp, list) case !reflect.DeepEqual(res, want): - t.Fatalf("Strings%v.Keep() = %v, want %v", - list, res, want) + t.Fatalf("%#v.Keep()\ngot: %#v\nwant: %#v", list, res, want) } } func TestKeepLists(t *testing.T) { has5 := func(l []int) bool { @@ -150,15 +149,14 @@ {1, 2, 5}, } cp := append(Lists{}, list...) switch res := cp.Keep(has5); { case !reflect.DeepEqual(cp, list): - t.Fatalf("Lists%v.Keep() should not modify its reciever. "+ - "Found %v, reciever should stay %v", + t.Fatalf("%#v.Keep() should not modify its receiver. "+ + "Found %#v, receiver should stay %#v", list, cp, list) case !reflect.DeepEqual(res, want): - t.Fatalf("Lists%v.Keep() = %v, want %v", - list, res, want) + t.Fatalf("%#v.Keep()\ngot: %#v\nwant: %#v", list, res, want) } } func BenchmarkKeepInts(b *testing.B) { for i := 0; i < b.N; i++ {