groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 08/08: Add a test for Builder and its 4 strategies
Date Fri, 28 Jun 2019 17:21:55 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit a76ed956032591531b6451bb2b375927ab008b8b
Author: aalmiray <aalmiray@gmail.com>
AuthorDate: Tue Jun 11 20:24:41 2019 +0200

    Add a test for Builder and its 4 strategies
---
 .../generated/AbstractGeneratedAstTestCase.groovy  |   8 ++
 .../groovy/generated/BuilderGeneratedTest.groovy   | 127 +++++++++++++++++++++
 2 files changed, 135 insertions(+)

diff --git a/src/test/groovy/generated/AbstractGeneratedAstTestCase.groovy b/src/test/groovy/generated/AbstractGeneratedAstTestCase.groovy
index 873f7f2..2bf4933 100644
--- a/src/test/groovy/generated/AbstractGeneratedAstTestCase.groovy
+++ b/src/test/groovy/generated/AbstractGeneratedAstTestCase.groovy
@@ -12,6 +12,14 @@ abstract class AbstractGeneratedAstTestCase {
         new GroovyClassLoader().parseClass(str)
     }
 
+    protected final void assertClassIsAnnotated(Class<?> classUnderTest) {
+        assert classUnderTest.getAnnotation(Generated)
+    }
+
+    protected final void assertClassIsNotAnnotated(Class<?> classUnderTest) {
+        assert !classUnderTest.getAnnotation(Generated)
+    }
+
     protected final void assertConstructorIsAnnotated(Class<?> classUnderTest, Class...
paramTypes) {
         assert findConstructor(classUnderTest, paramTypes).getAnnotation(Generated)
     }
diff --git a/src/test/groovy/generated/BuilderGeneratedTest.groovy b/src/test/groovy/generated/BuilderGeneratedTest.groovy
new file mode 100644
index 0000000..c6288d0
--- /dev/null
+++ b/src/test/groovy/generated/BuilderGeneratedTest.groovy
@@ -0,0 +1,127 @@
+package groovy.generated
+
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+@CompileStatic
+class BuilderGeneratedTest extends AbstractGeneratedAstTestCase {
+    final Class<?> classWithDefaultStrategy = parseClass('''import groovy.transform.builder.*
+      @Builder(builderStrategy=DefaultStrategy)
+      class ClassUnderTest {
+          String name
+      }''')
+
+    final Class<?> classWithSimpleStrategy = parseClass('''import groovy.transform.builder.*
+      @Builder(builderStrategy=SimpleStrategy)
+      class ClassUnderTest {
+          String name
+      }''')
+
+    final Class<?> classWithExternalStrategy = parseClass('''
+      class ClassUnderTest {
+          String name
+      }''')
+
+    final Class<?> builderClassWithExternalStrategy = new GroovyClassLoader(classWithExternalStrategy.classLoader).parseClass('''import
groovy.transform.builder.*
+      @Builder(builderStrategy=ExternalStrategy, forClass=ClassUnderTest)
+      class ClassUnderTestBuilder { }''')
+
+
+    final Class<?> classWithInitializerStrategy = parseClass('''import groovy.transform.builder.*
+      @Builder(builderStrategy=InitializerStrategy)
+      class ClassUnderTest {
+          String name
+      }''')
+
+    @Test
+    void test_default_builder_is_annotated() {
+        assertMethodIsAnnotated(classWithDefaultStrategy, 'builder')
+    }
+
+    @Test
+    void test_default_class_is_not_annotated() {
+        assertClassIsNotAnnotated(classWithDefaultStrategy)
+    }
+
+    @Test
+    void test_default_builder_class_is_annotated() {
+        Class<?> builderClass = classWithDefaultStrategy.classes.find { it.name.endsWith('Builder')
}
+        assertClassIsAnnotated(builderClass)
+    }
+
+    @Test
+    void test_default_name_is_annotated() {
+        Class<?> builderClass = classWithDefaultStrategy.classes.find { it.name.endsWith('Builder')
}
+        assertMethodIsAnnotated(builderClass, 'name', String)
+    }
+
+    @Test
+    void test_default_build_is_annotated() {
+        Class<?> builderClass = classWithDefaultStrategy.classes.find { it.name.endsWith('Builder')
}
+        assertMethodIsAnnotated(builderClass, 'build')
+    }
+
+    @Test
+    void test_simple_class_is_not_annotated() {
+        assertClassIsNotAnnotated(classWithSimpleStrategy)
+    }
+
+    @Test
+    void test_simple_setName_is_annotated() {
+        assertExactMethodIsAnnotated(classWithSimpleStrategy, 'setName', classWithSimpleStrategy,
String)
+    }
+
+    @Test
+    void test_external_class_is_not_annotated() {
+        assertClassIsNotAnnotated(classWithExternalStrategy)
+    }
+
+    @Test
+    void test_external_builder_class_is_not_annotated() {
+        assertClassIsNotAnnotated(builderClassWithExternalStrategy)
+    }
+
+    @Test
+    void test_external_name_is_annotated() {
+        assertMethodIsAnnotated(builderClassWithExternalStrategy, 'name', String)
+    }
+
+    @Test
+    void test_external_build_is_annotated() {
+        assertMethodIsAnnotated(builderClassWithExternalStrategy, 'build')
+    }
+
+    @Test
+    void test_initializer_class_is_not_annotated() {
+        assertClassIsNotAnnotated(classWithInitializerStrategy)
+    }
+
+    @Test
+    void test_initializer_class_constructor_is_annotated() {
+        Class<?> initializerClass = classWithInitializerStrategy.classes.find { it.name.endsWith('Initializer')
}
+        assertConstructorIsAnnotated(classWithInitializerStrategy, initializerClass)
+    }
+
+    @Test
+    void test_initializer_createInitializer_is_annotated() {
+        assertMethodIsAnnotated(classWithInitializerStrategy, 'createInitializer')
+    }
+
+    @Test
+    void test_inner_initializer_class_is_annotated() {
+        Class<?> initializerClass = classWithInitializerStrategy.classes.find { it.name.endsWith('Initializer')
}
+        assertClassIsAnnotated(initializerClass)
+    }
+
+    @Test
+    void test_inner_initializer_name_is_annotated() {
+        Class<?> initializerClass = classWithInitializerStrategy.classes.find { it.name.endsWith('Initializer')
}
+        assertMethodIsAnnotated(initializerClass, 'name', String)
+    }
+
+    @Test
+    void test_inner_initializer_create_is_annotated() {
+        Class<?> initializerClass = classWithInitializerStrategy.classes.find { it.name.endsWith('Initializer')
}
+        assertMethodIsAnnotated(initializerClass, 'create')
+    }
+}
\ No newline at end of file


Mime
View raw message