groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [1/2] groovy git commit: GROOVY-8347: @AutoFinal: tweaks to tests
Date Thu, 12 Oct 2017 00:11:18 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X d9a9518ef -> f3c08a6d6


GROOVY-8347: @AutoFinal: tweaks to tests


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f3c08a6d
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f3c08a6d
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f3c08a6d

Branch: refs/heads/GROOVY_2_6_X
Commit: f3c08a6d621ed92fd2657c32547e48042c358415
Parents: 226f1d8
Author: paulk <paulk@asert.com.au>
Authored: Thu Oct 12 00:30:39 2017 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Thu Oct 12 10:11:03 2017 +1000

----------------------------------------------------------------------
 .../AutoFinalTransformBlackBoxTest.groovy       | 212 +++++++++----------
 .../transform/AutoFinalTransformTest.groovy     |  47 ++++
 2 files changed, 152 insertions(+), 107 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/f3c08a6d/src/test/org/codehaus/groovy/transform/AutoFinalTransformBlackBoxTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/AutoFinalTransformBlackBoxTest.groovy
b/src/test/org/codehaus/groovy/transform/AutoFinalTransformBlackBoxTest.groovy
index 4e7bf26..b06e265 100644
--- a/src/test/org/codehaus/groovy/transform/AutoFinalTransformBlackBoxTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/AutoFinalTransformBlackBoxTest.groovy
@@ -25,132 +25,130 @@ import org.junit.runners.JUnit4
 
 import java.util.Map.Entry
 
-
 /**
  * Tests for the {@code @AutoFinal} AST transform.
  */
-
-// Execute single test:
-// gradlew :test --build-cache --tests org.codehaus.groovy.transform.AutoFinalTransformTest
 @RunWith(JUnit4)
 class AutoFinalTransformBlackBoxTest extends CompilableTestSupport {
 
-  @Test
-  void testAutoFinal_Closure() {
-    assertAutoFinalClassTestScript("param0", "String foo() { final cls = { String param0
-> param0 = 'abc'; param0 }; cls() }")
-  }
-
-  @Test
-  void testAutoFinal_ClosureInClosure() {
-    assertAutoFinalClassTestScript("param1", "String foo() { final cls0 = { String param0
-> final cls1 = { String param1 -> param1 = 'xyz'; param1 }; cls1() }; cls0() }")
-  }
-
-  @Test
-  void testAutoFinal_ClassMethod_Param0() {
-    assertAutoFinalClassTestScript("param0", "String foo(String param0, param1) {  param0
= 'abc'; param0 }")
-  }
-
-  @Test
-  void testAutoFinal_ClassMethod_Param1() {
-    assertAutoFinalClassTestScript("param1", "String foo(String param0, param1) {  param1
= new Object(); param1 }")
-  }
-
-  // Check default parameters are not negatively impacted by @AutoFinal
-  @Test
-  void testAutoFinalClassMethodDefaultParameters() {
-    final String classPart = """
-      String foo(String param0 = 'XyZ', param1 = Closure.IDENTITY) { 
-        assert param0.equals('XyZ')
-        assert param1.is(Closure.IDENTITY)
-        return param0 
-      }
-    """
-    final script = autoFinalTestScript(true, [:], classPart, "final foo = new $autoFinalTestClassName();
foo.foo()" )
-    assert script.contains('@AutoFinal')
-    assertScript(script)
-  }
-
-
-
-  void assertAutoFinalClassTestScript(final String paramName, final String classPart) {
-    assertAutoFinalTestScriptWithAnnotation(paramName, classPart)
-    assertAutoFinalTestScriptWithoutAnnotation(classPart)
-    assertAutoFinalTestScriptWithDisabledAnnotation(classPart)
-  }
-
-  //
-  // Checks Groovy compiler behavior when putting the passed classPart into an @AutoFinal
annotated class
-  //
-
-  // @AutoFinal
-  void assertAutoFinalTestScriptWithAnnotation(final String paramName, final String classPart)
{
-    final script = autoFinalTestScript(true, [:], classPart)
-    assert script.contains('@AutoFinal')
-    final result = shouldNotCompile(script)
-    println "\nassertAutoFinalTestScript result: |$result|\n\n"
-    assert result.contains("The parameter [$paramName] is declared final but is reassigned")
-  }
-
-  // @AutoFinal(enabled=false)
-  void assertAutoFinalTestScriptWithDisabledAnnotation(final String classPart) {
-    final script = autoFinalTestScript(true, [enabled:false], classPart)
-    assert script.contains('@AutoFinal(enabled=false)')
-    shouldCompile(script)
-  }
-
-  // No annotation
-  void assertAutoFinalTestScriptWithoutAnnotation(final String classPart) {
-    final script = autoFinalTestScript(false, null, classPart)
-    assert !script.contains('@AutoFinal')
-    shouldCompile(script)
-  }
-
-
-  String autoFinalTestScript(final boolean autoFinalAnnotationQ, final Map<String,Object>
autoFinalAnnotationParamaters, final String classPart, final String scriptPart = '') {
-    assert !autoFinalAnnotationQ || (autoFinalAnnotationParamaters != null); assert classPart
-    final String autoFinalAnnotationParamatersTerm = autoFinalAnnotationParamaters ? "(${autoFinalAnnotationParamaters.collect
{ final Entry<String, Object> e -> "$e.key=$e.value" }.join(', ')})" : ''
-    final String script = """
+    @Test
+    void testAutoFinal_Closure() {
+        assertAutoFinalClassTestScript("param0", "String foo() { final cls = { String param0
-> param0 = 'abc'; param0 }; cls() }")
+    }
+
+    @Test
+    void testAutoFinal_ClosureInClosure() {
+        assertAutoFinalClassTestScript("param1", "String foo() { final cls0 = { String param0
-> final cls1 = { String param1 -> param1 = 'xyz'; param1 }; cls1() }; cls0() }")
+    }
+
+    @Test
+    void testAutoFinal_ClassMethod_Param0() {
+        assertAutoFinalClassTestScript("param0", "String foo(String param0, param1) {  param0
= 'abc'; param0 }")
+    }
+
+    @Test
+    void testAutoFinal_ClassMethod_Param1() {
+        assertAutoFinalClassTestScript("param1", "String foo(String param0, param1) {  param1
= new Object(); param1 }")
+    }
+
+    // Check default parameters are not negatively impacted by @AutoFinal
+    @Test
+    void testAutoFinalClassMethodDefaultParameters() {
+        final String classPart = """
+        String foo(String param0 = 'XyZ', param1 = Closure.IDENTITY) { 
+            assert param0.equals('XyZ')
+            assert param1.is(Closure.IDENTITY)
+            return param0 
+        }
+        """
+        final script = autoFinalTestScript(true, [:], classPart, "final foo = new $autoFinalTestClassName();
foo.foo()")
+        assert script.contains('@AutoFinal')
+        assertScript(script)
+    }
+
+
+    void assertAutoFinalClassTestScript(final String paramName, final String classPart) {
+        assertAutoFinalTestScriptWithAnnotation(paramName, classPart)
+        assertAutoFinalTestScriptWithoutAnnotation(classPart)
+        assertAutoFinalTestScriptWithDisabledAnnotation(classPart)
+    }
+
+    //
+    // Checks Groovy compiler behavior when putting the passed classPart into an @AutoFinal
annotated class
+    //
+
+    // @AutoFinal
+    void assertAutoFinalTestScriptWithAnnotation(final String paramName, final String classPart)
{
+        final script = autoFinalTestScript(true, [:], classPart)
+        assert script.contains('@AutoFinal')
+        final result = shouldNotCompile(script)
+        //println "\nassertAutoFinalTestScript result: |$result|\n\n"
+        assert result.contains("The parameter [$paramName] is declared final but is reassigned")
+    }
+
+    // @AutoFinal(enabled=false)
+    void assertAutoFinalTestScriptWithDisabledAnnotation(final String classPart) {
+        final script = autoFinalTestScript(true, [enabled: false], classPart)
+        assert script.contains('@AutoFinal(enabled=false)')
+        shouldCompile(script)
+    }
+
+    // No annotation
+    void assertAutoFinalTestScriptWithoutAnnotation(final String classPart) {
+        final script = autoFinalTestScript(false, null, classPart)
+        assert !script.contains('@AutoFinal')
+        shouldCompile(script)
+    }
+
+
+    String autoFinalTestScript(
+            final boolean hasAnnotation,
+            final Map<String, Object> annotationParameters,
+            final String classPart, final String scriptPart = '') {
+        assert !hasAnnotation || (annotationParameters != null); assert classPart
+        final String annotationParametersTerm = annotationParameters ? "(${annotationParameters.collect
{ final Entry<String, Object> e -> "$e.key=$e.value" }.join(', ')})" : ''
+        final String script = """
             import groovy.transform.AutoFinal
             import groovy.transform.ASTTest
             import static org.codehaus.groovy.control.CompilePhase.SEMANTIC_ANALYSIS
             import static java.lang.reflect.Modifier.isFinal
 
-            ${autoFinalAnnotationQ ? "@AutoFinal${autoFinalAnnotationParamatersTerm}" : ''}
+            ${hasAnnotation ? "@AutoFinal${annotationParametersTerm}" : ''}
             class $autoFinalTestClassName {
                 $classPart
             } 
 
             $scriptPart
         """
-    println "script: |$script|"
-    return script
-  }
-
-  String getAutoFinalTestClassName() {
-    'AutoFinalFoo'
-  }
-
-  /**
-   * Prints better readable, unabbreviated stack trace for passed Throwable
-   */
-  void printStackTrace(final Throwable throwable) {
-    println "${throwable.getClass().name}${throwable.message ? ": $throwable.message" : ""}"
-    throwable.stackTrace.each { println it }
-    final inner = throwable.cause
-    if(inner != null) {
-      println "Caused by........................................................................................."
-      printStackTrace(inner)
+        //println "script: |$script|"
+        return script
     }
-  }
 
-  Throwable shouldThrow(final String script) {
-    try {
-      final GroovyClassLoader gcl = new GroovyClassLoader()
-      gcl.parseClass(script, getTestClassName())
+    String getAutoFinalTestClassName() {
+        'AutoFinalFoo'
     }
-    catch(Throwable throwable) {
-      return throwable
+
+    /**
+     * Prints better readable, unabbreviated stack trace for passed Throwable
+     */
+    void printStackTrace(final Throwable throwable) {
+        println "${throwable.getClass().name}${throwable.message ? ": $throwable.message"
: ""}"
+        throwable.stackTrace.each { println it }
+        final inner = throwable.cause
+        if (inner != null) {
+            println "Caused by........................................................................................."
+            printStackTrace(inner)
+        }
+    }
+
+    Throwable shouldThrow(final String script) {
+        try {
+            final GroovyClassLoader gcl = new GroovyClassLoader()
+            gcl.parseClass(script, getTestClassName())
+        }
+        catch (Throwable throwable) {
+            return throwable
+        }
+        throw new Exception("Script was expected to throw here!")
     }
-    throw new Exception("Script was expected to throw here!")
-  }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/f3c08a6d/src/test/org/codehaus/groovy/transform/AutoFinalTransformTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/AutoFinalTransformTest.groovy b/src/test/org/codehaus/groovy/transform/AutoFinalTransformTest.groovy
index 339daa4..329af82 100644
--- a/src/test/org/codehaus/groovy/transform/AutoFinalTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/AutoFinalTransformTest.groovy
@@ -61,4 +61,51 @@ class AutoFinalTransformTest extends CompilableTestSupport {
             assert js.fullName(true, ', ') == 'Smith, John'
         '''
     }
+
+    void testAutoFinalOnClassButDisabledOnMethod() {
+        // use ASTTest here since final modifier isn't put into bytecode so not available
via reflection
+        assertScript '''
+            import groovy.transform.AutoFinal
+            import groovy.transform.ASTTest
+            import static org.codehaus.groovy.control.CompilePhase.SEMANTIC_ANALYSIS
+            import static java.lang.reflect.Modifier.isFinal
+
+            @ASTTest(phase=SEMANTIC_ANALYSIS, value = {
+                assert node.methods.size() == 2
+                node.methods[0].with {
+                    assert it.name == 'fullName'
+                    assert it.parameters.every{ p -> isFinal(p.modifiers) }
+                }
+                node.methods[1].with {
+                    assert it.name == 'initials'
+                    assert it.parameters.every{ p -> !isFinal(p.modifiers) }
+                }
+                assert node.constructors.size() == 1
+                node.constructors[0].with {
+                    assert it.parameters.every{ p -> isFinal(p.modifiers) }
+                }
+            })
+            @AutoFinal
+            class Person {
+                final String first, last
+                Person(String first, String last) {
+                    this.first = first
+                    this.last = last
+                }
+                String fullName(boolean reversed = false, String separator = ' ') {
+                    "${reversed ? last : first}$separator${reversed ? first : last}"
+                }
+                @AutoFinal(enabled=false)
+                String initials(boolean lower) {
+                    def raw = "${first[0]}${last[0]}"
+                    lower ? raw.toLowerCase() : raw
+                }
+            }
+
+            def js = new Person('John', 'Smith')
+            assert js.fullName() == 'John Smith'
+            assert js.fullName(true, ', ') == 'Smith, John'
+            assert js.initials(true) == 'js'
+        '''
+    }
 }


Mime
View raw message