Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ var distinct = &typewriter.Template{
Text: `
// Distinct returns a new {{.SliceName}} whose elements are unique. See: http://clipperhouse.github.io/gen/#Distinct
func (rcv {{.SliceName}}) Distinct() (result {{.SliceName}}) {
appended := make(map[{{.Type}}]bool)
appended := make(map[{{.Type.Name}}]bool)
for _, v := range rcv {
if !appended[v] {
result = append(result, v)
appended[v] = true
}
{{ if .Type.Pointer -}}
if !appended[*v] {
result = append(result, v)
appended[*v] = true
}
{{ else -}}
if !appended[v] {
result = append(result, v)
appended[v] = true
}
{{ end -}}
}
return result
}
Expand Down
19 changes: 19 additions & 0 deletions test/aggregate[t]_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ func TestAggregateOther(t *testing.T) {
t.Errorf("AggregateOther should be %v, got %v", expected1, aggregate1)
}
}

func TestPointerAggregateOther(t *testing.T) {
things := PointerThingSlice{
{"First", 60},
{"Second", -20},
{"Third", 100},
}

sum := func(state Other, x *PointerThing) Other {
return state + x.Number
}

aggregate1 := things.AggregateOther(sum)
expected1 := Other(140)

if aggregate1 != expected1 {
t.Errorf("AggregateOther should be %v, got %v", expected1, aggregate1)
}
}
32 changes: 32 additions & 0 deletions test/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,35 @@ func TestAll(t *testing.T) {
t.Errorf("All should evaulate true for empty slices")
}
}

func TestPointerAll(t *testing.T) {
things := PointerThingSlice{
{"First", 60},
{"Second", -20},
{"Third", 100},
}

all1 := things.All(func(x *PointerThing) bool {
return x.Name == "First"
})

if all1 {
t.Errorf("All should be false for Name == 'First'")
}

all2 := things.All(func(x *PointerThing) bool {
return x.Name == "First" || x.Name == "Second" || x.Name == "Third"
})

if !all2 {
t.Errorf("All should be true")
}

all3 := PointerThingSlice{}.All(func(x *PointerThing) bool {
return false
})

if !all3 {
t.Errorf("All should evaulate true for empty slices")
}
}
32 changes: 32 additions & 0 deletions test/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,35 @@ func TestAny(t *testing.T) {
t.Errorf("Any should evaulate false for empty slices")
}
}

func TestPointerAny(t *testing.T) {
things := PointerThingSlice{
{"First", 60},
{"Second", -20},
{"Third", 100},
}

any1 := things.Any(func(x *PointerThing) bool {
return x.Name == "Dummy"
})

if any1 {
t.Errorf("Any should not evaulate true for Name == Dummy")
}

any2 := things.Any(func(x *PointerThing) bool {
return x.Number > 50
})

if !any2 {
t.Errorf("Any should evaulate true for Number > 50")
}

any3 := PointerThingSlice{}.Any(func(x *PointerThing) bool {
return true
})

if any3 {
t.Errorf("Any should evaulate false for empty slices")
}
}
30 changes: 30 additions & 0 deletions test/average[t]_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,33 @@ func TestAverageOther(t *testing.T) {
t.Errorf("Average should fail on empty slice")
}
}

func TestPointerAverageOther(t *testing.T) {
things := PointerThingSlice{
{"First", 60},
{"Second", -10},
{"Third", 100},
}

number := func(x *PointerThing) Other {
return x.Number
}

average1, err := things.AverageOther(number)

if err != nil {
t.Errorf("Average should succeed")
}

expected1 := Other(50)

if average1 != expected1 {
t.Errorf("Average should be %v, got %v", expected1, average1)
}

average2, err := PointerThingSlice{}.AverageOther(number)

if err == nil || average2 != 0 {
t.Errorf("Average should fail on empty slice")
}
}
40 changes: 40 additions & 0 deletions test/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,43 @@ func TestCount(t *testing.T) {
t.Errorf("Count should find no items in an empty slice")
}
}

func TestPointerCount(t *testing.T) {
things := PointerThingSlice{
{"First", 60},
{"Second", 20},
{"Third", 100},
}

count1 := things.Count(func(x *PointerThing) bool {
return x.Name == "Second"
})

if count1 != 1 {
t.Errorf("Count should find one item Name == Second")
}

count2 := things.Count(func(x *PointerThing) bool {
return x.Number > 50
})

if count2 != 2 {
t.Errorf("Count should find 2 items for Number > 50")
}

count3 := things.Count(func(x *PointerThing) bool {
return x.Name == "Dummy"
})

if count3 != 0 {
t.Errorf("Count should no items for Name == Dummy")
}

count4 := PointerThingSlice{}.Count(func(x *PointerThing) bool {
return true
})

if count4 != 0 {
t.Errorf("Count should find no items in an empty slice")
}
}
21 changes: 21 additions & 0 deletions test/distinct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,24 @@ func TestDistinct(t *testing.T) {
t.Errorf("Distinct should exclude be %v, but found %v", should, distinct1)
}
}

func TestPointerDistinct(t *testing.T) {
things := PointerThingSlice{
{"First", 0},
{"Second", 0},
{"First", 0},
{"Third", 0},
}

should := PointerThingSlice{
{"First", 0},
{"Second", 0},
{"Third", 0},
}

distinct1 := things.Distinct()

if !reflect.DeepEqual(distinct1, should) {
t.Errorf("Distinct should exclude be %v, but found %v", should, distinct1)
}
}
26 changes: 26 additions & 0 deletions test/distinctby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,29 @@ func TestDistinctBy(t *testing.T) {
t.Errorf("DistinctBy should be %v, but got %v", expected, distinctby1)
}
}

func TestPointerDistinctBy(t *testing.T) {
things := PointerThingSlice{
{"First", 0},
{"Second", 9},
{"First", 4},
{"Third", 9},
{"Fourth", 5},
{"Fifth", 4},
}

expected := PointerThingSlice{
{"First", 0},
{"Second", 9},
{"First", 4},
{"Fourth", 5},
}

distinctby1 := things.DistinctBy(func(a, b *PointerThing) bool {
return a.Number == b.Number
})

if !reflect.DeepEqual(distinctby1, expected) {
t.Errorf("DistinctBy should be %v, but got %v", expected, distinctby1)
}
}
37 changes: 37 additions & 0 deletions test/first_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,40 @@ func TestFirst(t *testing.T) {
t.Errorf("First should fail on empty slice")
}
}

func TestPointerFirst(t *testing.T) {
things := PointerThingSlice{
{"First", 0},
{"Second", 0},
{"Third", 0},
}

f1, err1 := things.First(func(x *PointerThing) bool {
return x.Name == "Third"
})

if err1 != nil {
t.Errorf("First should succeed when finding Name == Third")
}

expected1 := &PointerThing{"Third", 0}
if *f1 != *expected1 {
t.Errorf("First should find %v, but found %v", expected1, f1)
}

_, err2 := things.First(func(x *PointerThing) bool {
return x.Name == "Dummy"
})

if err2 == nil {
t.Errorf("First should fail when finding Name == Dummy")
}

_, err3 := PointerThingSlice{}.First(func(x *PointerThing) bool {
return true
})

if err3 == nil {
t.Errorf("First should fail on empty slice")
}
}
29 changes: 29 additions & 0 deletions test/groupby[t]_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,32 @@ func TestGroupByOther(t *testing.T) {
t.Errorf("GroupByOther should be %v, got %v", expected1, groupby1)
}
}

func TestPointerGroupByOther(t *testing.T) {
things := PointerThingSlice{
{"First", 60},
{"Second", -10},
{"Third", 100},
{"Fourth", -10},
{"Fifth", 60},
}

number := func(x *PointerThing) Other {
return x.Number
}

groupby1 := things.GroupByOther(number)
expected1 := map[Other]PointerThingSlice{
-10: {{"Second", -10}, {"Fourth", -10}},
60: {{"First", 60}, {"Fifth", 60}},
100: {{"Third", 100}},
}

if len(groupby1) != len(expected1) {
t.Errorf("GroupByInt result should have %d elements, has %d", len(expected1), len(groupby1))
}

if !reflect.DeepEqual(groupby1, expected1) {
t.Errorf("GroupByOther should be %v, got %v", expected1, groupby1)
}
}
28 changes: 28 additions & 0 deletions test/max[t]_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,31 @@ func TestMaxOther(t *testing.T) {
t.Errorf("Max should fail on empty slice")
}
}

func TestPointerMaxOther(t *testing.T) {
things := PointerThingSlice{
{"First", 60},
{"Second", -20},
{"Third", 100},
}

number := func(x *PointerThing) Other {
return x.Number
}

max1, err := things.MaxOther(number)

if err != nil {
t.Errorf("MaxOther should succeed")
}

if max1 != 100 {
t.Errorf("MaxOther should be %v, got %v", 100, max1)
}

max2, err := PointerThingSlice{}.MaxOther(number)

if err == nil || max2 != 0 {
t.Errorf("Max should fail on empty slice")
}
}
31 changes: 31 additions & 0 deletions test/maxby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,34 @@ func TestMaxBy(t *testing.T) {
t.Errorf("MaxBy Number should fail on empty slice")
}
}

func TestPointerMaxBy(t *testing.T) {
things := PointerThingSlice{
{"First", 60},
{"Second", -20},
{"Third", 100},
{"Fourth", 10},
}

max1, err1 := things.MaxBy(func(a, b *PointerThing) bool {
return a.Number < b.Number
})

if err1 != nil {
t.Errorf("MaxBy Number should succeed")
}

expected1 := &PointerThing{"Third", 100}

if *max1 != *expected1 {
t.Errorf("MaxBy Number should return %v, got %v", expected1, max1)
}

_, err2 := PointerThingSlice{}.MaxBy(func(a, b *PointerThing) bool {
return true
})

if err2 == nil {
t.Errorf("MaxBy Number should fail on empty slice")
}
}
Loading