[1/2] groovy git commit: GROOVY-8347: @AutoFinal: tweaks to tests

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[1/2] groovy git commit: GROOVY-8347: @AutoFinal: tweaks to tests

paulk
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 683f7ed84 -> eb31bfe71


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/eb31bfe7
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/eb31bfe7
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/eb31bfe7

Branch: refs/heads/GROOVY_2_5_X
Commit: eb31bfe71068d471a57220c39b1d329cf8fd75f3
Parents: b153698
Author: paulk <[hidden email]>
Authored: Thu Oct 12 00:30:39 2017 +1000
Committer: paulk <[hidden email]>
Committed: Thu Oct 12 10:11:41 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/eb31bfe7/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/eb31bfe7/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'
+        '''
+    }
 }

Reply | Threaded
Open this post in threaded view
|

[2/2] groovy git commit: GROOVY-8347: AutoFinalTransformBlackBoxTest: Added @AutoFinal(enabled=false) testing (closes #615)

paulk
GROOVY-8347: AutoFinalTransformBlackBoxTest: Added @AutoFinal(enabled=false) testing (closes #615)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: b153698b005cd0399189646a1b566838c7ed5981
Parents: 683f7ed
Author: mgroovy <[hidden email]>
Authored: Tue Oct 10 19:03:43 2017 +0200
Committer: paulk <[hidden email]>
Committed: Thu Oct 12 10:11:41 2017 +1000

----------------------------------------------------------------------
 .../AutoFinalTransformBlackBoxTest.groovy       | 33 +++++++++++++++-----
 1 file changed, 25 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/b153698b/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 d9625ed..4e7bf26 100644
--- a/src/test/org/codehaus/groovy/transform/AutoFinalTransformBlackBoxTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/AutoFinalTransformBlackBoxTest.groovy
@@ -23,6 +23,8 @@ import org.junit.Test
 import org.junit.runner.RunWith
 import org.junit.runners.JUnit4
 
+import java.util.Map.Entry
+
 
 /**
  * Tests for the {@code @AutoFinal} AST transform.
@@ -63,42 +65,57 @@ class AutoFinalTransformBlackBoxTest extends CompilableTestSupport {
         return param0
       }
     """
-    final script = autoFinalTestScript(true, classPart, "final foo = new $autoFinalTestClassName(); foo.foo()" )
+    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(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)
+    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")
   }
 
-  void assertAutoFinalTestScriptWithoutAnnotation(final String paramName, final String classPart) {
-    final script = autoFinalTestScript(false, classPart)
+  // @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 String classPart, final String scriptPart = '') {
+
+  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 = """
             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' : ''}
+            ${autoFinalAnnotationQ ? "@AutoFinal${autoFinalAnnotationParamatersTerm}" : ''}
             class $autoFinalTestClassName {
                 $classPart
             }