-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(unittest): add simple way to get test framework
- Loading branch information
Showing
5 changed files
with
169 additions
and
1 deletion.
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
79 changes: 79 additions & 0 deletions
79
unit-picker/src/main/kotlin/cc/unitmesh/pick/worker/TestFrameworkIdentifier.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,79 @@ | ||
package cc.unitmesh.pick.worker | ||
|
||
import org.archguard.scanner.core.sca.CompositionDependency | ||
|
||
class TestFrameworkIdentifier(val language: String, val dependencies: List<CompositionDependency>) { | ||
fun identify(): List<String> { | ||
return when (language) { | ||
"java" -> identifyJava() | ||
"typescript" -> identifyTypescript() | ||
else -> listOf() | ||
} | ||
} | ||
|
||
private fun identifyTypescript(): List<String> { | ||
val testFrameworks = dependencies | ||
.filter { it.depName in testFrameworkList } | ||
.map { it.depName } | ||
|
||
return testFrameworks | ||
} | ||
|
||
// 在类中定义 testFrameworkList,使其可复用 | ||
companion object { | ||
private val testFrameworkList = listOf( | ||
"jest", | ||
"mocha", | ||
"jasmine", | ||
"ava", | ||
"tape", | ||
"qunit", | ||
"web-component-tester", | ||
"testem", | ||
"webdriverio", | ||
"nightwatch", | ||
"puppeteer", | ||
"protractor", | ||
"cypress", | ||
"test" | ||
) | ||
} | ||
|
||
|
||
private fun identifyJava(): List<String> { | ||
val testFrameworks = mutableListOf<String>() | ||
// junit, testng, spock, cucumber, karate, jbehave, jgiven, concordion, junit5, test, assertj, hamcrest, truth, fest, easytest, jmockit, mockito, powermock, mockk, wiremock, rest-assured, restassured, res | ||
val frameworkDepName = listOf( | ||
"junit:junit", | ||
"org.testng:testng", | ||
"org.spockframework:spock-core", | ||
"io.cucumber:cucumber-java", | ||
"com.intuit.karate:karate-junit5", | ||
"org.jbehave:jbehave-core", | ||
"org.jgiven:jgiven-junit5", | ||
"org.concordion:concordion", | ||
"org.junit.jupiter:junit-jupiter", | ||
"org.assertj:assertj-core", | ||
"org.hamcrest:hamcrest", | ||
"com.google.truth:truth", | ||
"org.easytesting:fest-assert", | ||
"org.easetech:easytest-core", | ||
"org.jmockit:jmockit", | ||
"org.mockito:mockito-core", | ||
"org.powermock:powermock-core", | ||
"io.mockk:mockk", | ||
"com.github.tomakehurst:wiremock", | ||
"org.springframework.boot:spring-boot-starter-test", | ||
) | ||
|
||
for (dep in dependencies) { | ||
frameworkDepName.contains(dep.depGroup + ":" + dep.depArtifact).let { | ||
if (it) { | ||
testFrameworks.add(dep.depName) | ||
} | ||
} | ||
} | ||
|
||
return testFrameworks | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
unit-picker/src/test/kotlin/cc/unitmesh/pick/worker/TestFrameworkIdentifierTest.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,85 @@ | ||
package cc.unitmesh.pick.worker; | ||
|
||
import org.archguard.scanner.core.sca.CompositionDependency | ||
import org.junit.jupiter.api.Test | ||
|
||
class TestFrameworkIdentifierTest { | ||
|
||
@Test | ||
fun shouldIdentifyJavaTestFrameworks() { | ||
// given | ||
val language = "java" | ||
val dependencies = listOf( | ||
CompositionDependency.default("junit:junit", "org.junit.jupiter", "junit-jupiter"), | ||
CompositionDependency.default("org.mockito:mockito-core", "org.mockito", "mockito-core"), | ||
CompositionDependency.default("com.intuit.karate:karate-junit5", "com.intuit.karate", "karate-junit5") | ||
) | ||
val testFrameworkIdentifier = TestFrameworkIdentifier(language, dependencies) | ||
|
||
// when | ||
val identifiedFrameworks = testFrameworkIdentifier.identify() | ||
|
||
// then | ||
val expectedFrameworks = listOf( | ||
"junit:junit", "org.mockito:mockito-core", | ||
"com.intuit.karate:karate-junit5" | ||
) | ||
assert(identifiedFrameworks == expectedFrameworks) | ||
} | ||
|
||
@Test | ||
fun shouldIdentifyTypescriptTestFrameworks() { | ||
// given | ||
val language = "typescript" | ||
val dependencies = listOf( | ||
CompositionDependency.default("jest", "some.group", "jest"), | ||
CompositionDependency.default("mocha", "another.group", "mocha"), | ||
CompositionDependency.default("jasmine", "third.group", "jasmine") | ||
) | ||
val testFrameworkIdentifier = TestFrameworkIdentifier(language, dependencies) | ||
|
||
// when | ||
val identifiedFrameworks = testFrameworkIdentifier.identify() | ||
|
||
// then | ||
val expectedFrameworks = listOf("jest", "mocha", "jasmine") | ||
assert(identifiedFrameworks == expectedFrameworks) | ||
} | ||
|
||
@Test | ||
fun shouldReturnEmptyListForUnknownLanguage() { | ||
// given | ||
val language = "unknown" | ||
val dependencies = listOf( | ||
CompositionDependency.default("dummy", "some.group", "dummy"), | ||
CompositionDependency.default("test", "another.group", "test") | ||
) | ||
val testFrameworkIdentifier = TestFrameworkIdentifier(language, dependencies) | ||
|
||
// when | ||
val identifiedFrameworks = testFrameworkIdentifier.identify() | ||
|
||
// then | ||
assert(identifiedFrameworks.isEmpty()) | ||
} | ||
} | ||
|
||
private fun CompositionDependency.Companion.default( | ||
name: String, | ||
group: String, | ||
artifactId: String, | ||
): CompositionDependency { | ||
return CompositionDependency( | ||
name = name, | ||
depName = name, | ||
depGroup = group, | ||
depArtifact = artifactId, | ||
packageManager = "maven", | ||
depVersion = "1.0.0", | ||
version = "1.0.0", | ||
path = "some/path", | ||
depScope = "compile", | ||
id = "some-id", | ||
parentId = "some-parent-id", | ||
) | ||
} |