Skip to content

Commit

Permalink
feat(unittest): add simple way to get test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 22, 2023
1 parent 5c64dcc commit 2f5c7dc
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ interface UnitTestService {
*/
fun lookupRelevantClass(codeFunction: CodeFunction, dataStruct: CodeDataStruct): List<CodeDataStruct>
}

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ data class WorkerContext(
val completionTypeSize: Int,
val insQualityThreshold: InsQualityThreshold = InsQualityThreshold(),
var compositionDependency: List<CompositionDependency> = listOf()
)
) {
var testFramework: List<String> = listOf()
}

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class WorkerManager(private val workerContext: WorkerContext) {
}

workerContext.compositionDependency = dependencies
workerContext.testFramework = TestFrameworkIdentifier(language, dependencies).identify()
}

fun addJobByThreshold(job: InstructionFileJob) {
Expand Down
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",
)
}

0 comments on commit 2f5c7dc

Please sign in to comment.