Fix snippets for includeFields and cache options of EqualsAndHashCode to show usages of EqualsAndHashCode and not ToString (closes #300)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/084b4d87 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/084b4d87 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/084b4d87 Branch: refs/heads/master Commit: 084b4d87d188abc49d9e73f5ac03d1dd38d6d1ba Parents: 305131f Author: Marcin Erdmann <erd...@gmail.com> Authored: Wed Mar 30 20:31:14 2016 +0100 Committer: pascalschumacher <pascalschumac...@gmx.net> Committed: Thu Mar 31 11:12:10 2016 +0200 ---------------------------------------------------------------------- src/spec/doc/core-metaprogramming.adoc | 4 +- .../test/CodeGenerationASTTransformsTest.groovy | 50 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/084b4d87/src/spec/doc/core-metaprogramming.adoc ---------------------------------------------------------------------- diff --git a/src/spec/doc/core-metaprogramming.adoc b/src/spec/doc/core-metaprogramming.adoc index 31a5ea7..bae0245 100644 --- a/src/spec/doc/core-metaprogramming.adoc +++ b/src/spec/doc/core-metaprogramming.adoc @@ -861,12 +861,12 @@ include::{projectdir}/src/spec/test/CodeGenerationASTTransformsTest.groovy[tags= |includeFields|False|Should fields be included in equals/hashCode, in addition to properties| [source,groovy] ---- -include::{projectdir}/src/spec/test/CodeGenerationASTTransformsTest.groovy[tags=tostring_example_includeFields,indent=0] +include::{projectdir}/src/spec/test/CodeGenerationASTTransformsTest.groovy[tags=equalshashcode_example_includeFields,indent=0] ---- |cache|False|Cache the hashCode computation. Should only be set to true if the class is immutable.| [source,groovy] ---- -include::{projectdir}/src/spec/test/CodeGenerationASTTransformsTest.groovy[tags=tostring_example_cache,indent=0] +include::{projectdir}/src/spec/test/CodeGenerationASTTransformsTest.groovy[tags=equalshashcode_example_cache,indent=0] ---- |useCanEqual|True|Should equals call canEqual helper method.|See http://www.artima.com/lejava/articles/equality.html |======================================================================= http://git-wip-us.apache.org/repos/asf/groovy/blob/084b4d87/src/spec/test/CodeGenerationASTTransformsTest.groovy ---------------------------------------------------------------------- diff --git a/src/spec/test/CodeGenerationASTTransformsTest.groovy b/src/spec/test/CodeGenerationASTTransformsTest.groovy index 8bdbd75..29335cd 100644 --- a/src/spec/test/CodeGenerationASTTransformsTest.groovy +++ b/src/spec/test/CodeGenerationASTTransformsTest.groovy @@ -298,6 +298,56 @@ assert p1.hashCode() != p2.hashCode() // end::equalshashcode_example_super[] ''' + + assertScript ''' +// tag::equalshashcode_example_includeFields[] +import groovy.transform.EqualsAndHashCode + +@EqualsAndHashCode(includeFields=true) +class Person { + private String firstName + + Person(String firstName) { + this.firstName = firstName + } +} + +def p1 = new Person('Jack') +def p2 = new Person('Jack') +def p3 = new Person('Bob') + +assert p1 == p2 +assert p1 != p3 +// end::equalshashcode_example_includeFields[] +''' + + assertScript ''' +// tag::equalshashcode_example_cache[] +import groovy.transform.EqualsAndHashCode +import groovy.transform.Immutable + +@Immutable +class SlowHashCode { + int hashCode() { + sleep 100 + 127 + } +} + +@EqualsAndHashCode(cache=true) +@Immutable +class Person { + SlowHashCode slowHashCode = new SlowHashCode() +} + +def p = new Person() +p.hashCode() + +def start = System.currentTimeMillis() +p.hashCode() +assert System.currentTimeMillis() - start < 100 +// end::equalshashcode_example_cache[] +''' } void testTupleConstructor() {