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
14 changes: 14 additions & 0 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ func getStandardTemplate(name string) (result *template.Template, err error) {
}

var standardTemplates = map[string]string{
"Equals": `
// Tests that all elements of one {{.Plural}} match the corresponding elements of another {{.Plural}}. See: http://clipperhouse.github.io/gen/#Equals
func ({{.Receiver}} {{.Plural}}) Equals(other {{.Plural}}) bool {
if len({{.Receiver}}) != len(other) {
return false
}
for i, v := range rcv {
if v != other[i] {
return false
}
}
return true
}
`,
"All": `
// Tests that all elements of {{.Plural}} return true for the passed func. See: http://clipperhouse.github.io/gen/#All
func ({{.Receiver}} {{.Plural}}) All(fn func({{.Pointer}}{{.Singular}}) bool) bool {
Expand Down
15 changes: 14 additions & 1 deletion test/movie_gen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// gen *models.Movie
// this file was auto-generated using github.com/clipperhouse/gen
// Thu, 12 Dec 2013 02:55:49 UTC
// Mon, 06 Jan 2014 23:29:08 UTC

// Sort functions are a modification of http://golang.org/pkg/sort/#Sort
// Copyright 2009 The Go Authors. All rights reserved.
Expand Down Expand Up @@ -78,6 +78,19 @@ func (rcv Movies) Each(fn func(*Movie)) {
}
}

// Tests that all elements of one Movies match the corresponding elements of another Movies. See: http://clipperhouse.github.io/gen/#Equals
func (rcv Movies) Equals(other Movies) bool {
if len(rcv) != len(other) {
return false
}
for i, v := range rcv {
if v != other[i] {
return false
}
}
return true
}

// Returns the first element that returns true for the passed func. Returns error if no elements return true. See: http://clipperhouse.github.io/gen/#First
func (rcv Movies) First(fn func(*Movie) bool) (result *Movie, err error) {
for _, v := range rcv {
Expand Down
31 changes: 31 additions & 0 deletions test/movie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,37 @@ func getTests() map[string][]test {
false,
},
}

tests["Equals"] = []test{
test{
func() (interface{}, error) {
return none.Equals(none), nil
},
true,
false,
},
test{
func() (interface{}, error) {
return many.Equals(many), nil
},
true,
false,
},
test{
func() (interface{}, error) {
return many.Equals(Movies{first, second}), nil
},
false,
false,
},
test{
func() (interface{}, error) {
return Movies{first, third}.Equals(Movies{first, second}), nil
},
false,
false,
},
}
return tests
}

Expand Down