From bd40d1535e9b83caa1954c636ed8139916b4e113 Mon Sep 17 00:00:00 2001 From: Rick Date: Mon, 6 Jan 2014 23:34:18 +0000 Subject: [PATCH] Added generation of `Equals` method (as per issue 10 https://github.com/clipperhouse/gen/issues/10). --- templates.go | 14 ++++++++++++++ test/movie_gen.go | 15 ++++++++++++++- test/movie_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/templates.go b/templates.go index af68e61..6def45b 100644 --- a/templates.go +++ b/templates.go @@ -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 { diff --git a/test/movie_gen.go b/test/movie_gen.go index 587ef10..fc8030a 100644 --- a/test/movie_gen.go +++ b/test/movie_gen.go @@ -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. @@ -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 { diff --git a/test/movie_test.go b/test/movie_test.go index 1de8f57..43d68f8 100644 --- a/test/movie_test.go +++ b/test/movie_test.go @@ -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 }