-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
198 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclarationTest.java
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclarationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package net.sourceforge.pmd.lang.java.ast | ||
|
||
import io.kotlintest.should | ||
import io.kotlintest.specs.FunSpec | ||
import net.sourceforge.pmd.lang.ast.test.shouldBe | ||
import net.sourceforge.pmd.lang.java.ast.JavaVersion.Companion.Earliest | ||
import net.sourceforge.pmd.lang.java.ast.JavaVersion.Companion.Latest | ||
import net.sourceforge.pmd.lang.java.ast.JavaVersion.J1_8 | ||
import net.sourceforge.pmd.lang.java.ast.JavaVersion.J9 | ||
|
||
class ASTMethodDeclarationTest : FunSpec({ | ||
|
||
// notes about dsl: | ||
// * testGroup generates one test per "should" assertion that | ||
// uses a node matcher, which is nice to know which one failed | ||
// (without explicitly giving them each a specific name) | ||
// * the it::isPublic syntax allows including the property name in the error message in case of failure | ||
|
||
testGroup("Non-private interfaces members should be public", javaVersions = Earliest..Latest) { | ||
|
||
genClassHeader = "interface Bar" | ||
|
||
"int foo();" should matchDeclaration<ASTMethodDeclaration>(ignoreChildren = true) { | ||
it::isPublic shouldBe true | ||
it::isPrivate shouldBe false | ||
it::isSyntacticallyPublic shouldBe false | ||
} | ||
|
||
"public int kk();" should matchDeclaration<ASTMethodDeclaration>(ignoreChildren = true) { | ||
it::isPublic shouldBe true | ||
it::isPrivate shouldBe false | ||
it::isSyntacticallyPublic shouldBe true | ||
} | ||
|
||
"int FOO = 0;" should matchDeclaration<ASTFieldDeclaration>(ignoreChildren = true) { | ||
it::isPublic shouldBe true | ||
it::isPrivate shouldBe false | ||
it::isSyntacticallyPublic shouldBe false | ||
} | ||
|
||
"public int FOO = 0;" should matchDeclaration<ASTFieldDeclaration>(ignoreChildren = true) { | ||
it::isPublic shouldBe true | ||
it::isPrivate shouldBe false | ||
it::isSyntacticallyPublic shouldBe true | ||
} | ||
} | ||
|
||
parserTest("Private methods in interface should be private", J9..Latest) { | ||
|
||
"private int de() { return 1; }" should matchDeclaration<ASTMethodDeclaration>(ignoreChildren = true) { | ||
it::isPublic shouldBe false | ||
it::isPrivate shouldBe true | ||
it::isSyntacticallyPublic shouldBe false | ||
} | ||
|
||
} | ||
|
||
testGroup("Non-default methods in interfaces should be abstract", javaVersions = J1_8..Latest) { | ||
|
||
genClassHeader = "interface Bar" | ||
|
||
"int bar();" should matchDeclaration<ASTMethodDeclaration>(ignoreChildren = true) { | ||
it::isDefault shouldBe false | ||
it::isAbstract shouldBe true | ||
} | ||
|
||
"abstract int bar();" should matchDeclaration<ASTMethodDeclaration>(ignoreChildren = true) { | ||
it::isDefault shouldBe false | ||
it::isAbstract shouldBe true | ||
} | ||
|
||
} | ||
|
||
parserTest("Default methods in interfaces should not be abstract", javaVersions = J1_8..Latest) { | ||
|
||
genClassHeader = "interface Bar" | ||
|
||
"default int kar() { return 1; } " should matchDeclaration<ASTMethodDeclaration>(ignoreChildren = true) { | ||
it::isDefault shouldBe true | ||
it::isAbstract shouldBe false | ||
} | ||
|
||
// default abstract is an invalid combination of modifiers so we won't encounter it in real analysis | ||
} | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package net.sourceforge.pmd.lang.ast.test | ||
|
||
import io.kotlintest.Matcher | ||
import kotlin.reflect.KCallable | ||
import io.kotlintest.shouldBe as ktShouldBe | ||
|
||
/** | ||
* Extension to add the name of a property to error messages. | ||
* | ||
* @see [shouldBe]. | ||
*/ | ||
infix fun <N, V : N> KCallable<N>.shouldEqual(expected: V?) = | ||
assertWrapper(this, expected) { n, v -> n ktShouldBe v } | ||
|
||
private fun <N, V> assertWrapper(callable: KCallable<N>, right: V, asserter: (N, V) -> Unit) { | ||
|
||
fun formatName() = "::" + callable.name.removePrefix("get").decapitalize() | ||
|
||
val value: N = try { | ||
callable.call() | ||
} catch (e: Exception) { | ||
throw RuntimeException("Couldn't fetch value for property ${formatName()}", e) | ||
} | ||
|
||
try { | ||
asserter(value, right) | ||
} catch (e: AssertionError) { | ||
|
||
if (e.message?.contains("expected:") == true) { | ||
// the exception has no path, let's add one | ||
throw AssertionError(e.message!!.replace("expected:", "expected property ${formatName()} to be")) | ||
} | ||
|
||
throw e | ||
} | ||
} | ||
|
||
/** | ||
* Extension to add the name of the property to error messages. | ||
* Use with double colon syntax, eg `it::isIntegerLiteral shouldBe true`. | ||
* For properties synthesized from Java getters starting with "get", you | ||
* have to use the name of the getter instead of that of the generated | ||
* property (with the get prefix). | ||
* | ||
* If this conflicts with [io.kotlintest.shouldBe], use the equivalent [shouldEqual] | ||
* | ||
*/ | ||
infix fun <N, V : N> KCallable<N>.shouldBe(expected: V?) = this.shouldEqual(expected) | ||
|
||
infix fun <T> KCallable<T>.shouldBe(expected: Matcher<T>) = assertWrapper(this, expected) { n, v -> n ktShouldBe v } |