Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
7 changed files
with
161 additions
and
55 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
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
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
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
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 |
---|---|---|
@@ -1,50 +1,8 @@ | ||
package wily | ||
|
||
import com.intellij.codeInspection.LocalInspectionToolSession | ||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import com.intellij.testFramework.fixtures.BasePlatformTestCase | ||
import com.jetbrains.python.PythonFileType | ||
import com.jetbrains.python.inspections.PyInspection | ||
import com.jetbrains.python.inspections.PyInspectionVisitor | ||
import com.jetbrains.python.psi.* | ||
import com.jetbrains.python.psi.resolve.PyResolveContext | ||
import com.jetbrains.python.psi.types.TypeEvalContext | ||
import com.nhaarman.mockitokotlin2.* | ||
import org.jetbrains.annotations.NotNull | ||
import org.mockito.ArgumentMatchers.contains | ||
import org.mockito.Mockito | ||
|
||
|
||
open class TestTask: BasePlatformTestCase() { | ||
fun <inspector: PyInspection>testCodeAssignmentStatement(code: String, times: Int = 1, check: Checks.CheckType, filename: String = "test.py", instance: inspector){ | ||
ApplicationManager.getApplication().runReadAction { | ||
val mockHolder = mock<ProblemsHolder> { | ||
on { registerProblem(any<PsiElement>(), contains(check.Code), anyVararg<LocalQuickFix>()) } doAnswer {} | ||
on { registerProblem(any<PsiElement>(), contains(check.Code), any<ProblemHighlightType>(), anyVararg<LocalQuickFix>()) } doAnswer {} | ||
} | ||
val testFile = this.createLightFile(filename, PythonFileType.INSTANCE.language, code); | ||
val mockLocalSession = mock<LocalInspectionToolSession> { | ||
on { file } doReturn (testFile) | ||
} | ||
assertNotNull(testFile) | ||
val testVisitor = instance.buildVisitor(mockHolder, true, mockLocalSession) as PyInspectionVisitor | ||
|
||
val expr: @NotNull MutableCollection<PyAssignmentStatement> = PsiTreeUtil.findChildrenOfType(testFile, PyAssignmentStatement::class.java) | ||
assertNotNull(expr) | ||
expr.forEach { e -> | ||
testVisitor.visitPyAssignmentStatement(e) | ||
} | ||
try { | ||
Mockito.verify(mockHolder, Mockito.times(times)).registerProblem(any<PsiElement>(), contains(check.Code), anyVararg<LocalQuickFix>()) | ||
} catch (a: AssertionError){ | ||
Mockito.verify(mockHolder, Mockito.times(times)).registerProblem(any<PsiElement>(), contains(check.Code), any<ProblemHighlightType>(), anyVararg<LocalQuickFix>()) | ||
} | ||
Mockito.verify(mockLocalSession, Mockito.times(1)).file | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
src/test/kotlin/wily/visitors/CyclomaticRecursiveVisitorTest.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,103 @@ | ||
package wily.visitors | ||
|
||
import com.intellij.openapi.application.ApplicationManager | ||
import com.jetbrains.python.PythonFileType | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
import wily.TestTask | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
internal class CyclomaticRecursiveVisitorTest: TestTask() { | ||
|
||
@BeforeAll | ||
override fun setUp() { | ||
super.setUp() | ||
} | ||
|
||
@AfterAll | ||
override fun tearDown(){ | ||
super.tearDown() | ||
} | ||
|
||
private fun visitCode(code: String): CyclomaticRecursiveVisitor { | ||
val visitor = CyclomaticRecursiveVisitor() | ||
ApplicationManager.getApplication().runReadAction { | ||
val testFile = this.createLightFile("test.py", PythonFileType.INSTANCE.language, code); | ||
visitor.visitFile(testFile) | ||
} | ||
return visitor | ||
} | ||
|
||
@Test | ||
fun `test zero CC for empty code`(){ | ||
val code = """ | ||
""".trimIndent() | ||
val v = visitCode(code) | ||
assertEquals(v.complexity(), 0) | ||
} | ||
|
||
@Test | ||
fun `test simple statement`(){ | ||
val code = """ | ||
a = 1 | ||
""".trimIndent() | ||
val v = visitCode(code) | ||
assertEquals(v.complexity(), 0) | ||
} | ||
|
||
@Test | ||
fun `test series of simple statements`(){ | ||
val code = """ | ||
a = 1 | ||
b = 2 | ||
c = 3 | ||
""".trimIndent() | ||
val v = visitCode(code) | ||
assertEquals(v.complexity(), 0) | ||
} | ||
|
||
@Test | ||
fun `test simple if`(){ | ||
val code = """ | ||
if a = 1: | ||
pass | ||
""".trimIndent() | ||
val v = visitCode(code) | ||
assertEquals(v.complexity(), 1) | ||
} | ||
|
||
@Test | ||
fun `test nested if`(){ | ||
val code = """ | ||
if a = 1: | ||
if b = 2: | ||
pass | ||
""".trimIndent() | ||
val v = visitCode(code) | ||
assertEquals(v.complexity(), 2) | ||
} | ||
|
||
@Test | ||
fun `test simple for loop`(){ | ||
val code = """ | ||
for a in b: | ||
pass | ||
""".trimIndent() | ||
val v = visitCode(code) | ||
assertEquals(v.complexity(), 1) | ||
} | ||
|
||
@Test | ||
fun `test simple while loop`(){ | ||
val code = """ | ||
while a is True: | ||
pass | ||
""".trimIndent() | ||
val v = visitCode(code) | ||
assertEquals(v.complexity(), 1) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/kotlin/wily/visitors/HalsteadRecursiveVisitorTest.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,43 @@ | ||
package wily.visitors | ||
|
||
import com.intellij.openapi.application.ApplicationManager | ||
import com.jetbrains.python.PythonFileType | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
import wily.TestTask | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
internal class HalsteadRecursiveVisitorTest: TestTask() { | ||
|
||
@BeforeAll | ||
override fun setUp() { | ||
super.setUp() | ||
} | ||
|
||
@AfterAll | ||
override fun tearDown(){ | ||
super.tearDown() | ||
} | ||
|
||
private fun visitCode(code: String): HalsteadRecursiveVisitor { | ||
val visitor = HalsteadRecursiveVisitor() | ||
ApplicationManager.getApplication().runReadAction { | ||
val testFile = this.createLightFile("test.py", PythonFileType.INSTANCE.language, code); | ||
visitor.visitFile(testFile) | ||
} | ||
return visitor | ||
} | ||
|
||
@Test | ||
fun `test simple statement`(){ | ||
val code = """ | ||
a = 1 | ||
""".trimIndent() | ||
val v = visitCode(code) | ||
assertEquals(v.vocabulary(), 3) | ||
assertEquals(v.length(), 3) | ||
} | ||
} |