Skip to content

Commit

Permalink
test: add test for threshold checker
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 26, 2023
1 parent f22c2f6 commit dc03e56
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 1 deletion.
2 changes: 1 addition & 1 deletion unit-core/src/main/kotlin/cc/unitmesh/core/Instruction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class SimpleInstruction(
data class Instruction(
var instruction: String,
var input: String,
val output: String,
var output: String,
) {
override fun toString(): String {
throw Exception("we don't support toString() for Instruction, please call render()")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cc.unitmesh.pick.threshold.InsQualityThreshold
import cc.unitmesh.quality.CodeQualityType
import kotlinx.serialization.Serializable
import org.archguard.scanner.core.sca.CompositionDependency
import org.jetbrains.annotations.TestOnly

@Serializable
data class WorkerContext(
Expand All @@ -20,4 +21,21 @@ data class WorkerContext(
val qualityThreshold: InsQualityThreshold = InsQualityThreshold(),
var compositionDependency: List<CompositionDependency> = listOf(),
) {

companion object {
@TestOnly
fun default(): WorkerContext {
return WorkerContext(
codeContextStrategies = listOf(),
qualityTypes = listOf(),
insOutputConfig = InsOutputConfig(),
pureDataFileName = "",
completionTypes = listOf(),
maxCompletionInOneFile = 0,
completionTypeSize = 0,
qualityThreshold = InsQualityThreshold(),
compositionDependency = listOf(),
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package cc.unitmesh.pick.threshold

import cc.unitmesh.core.Instruction
import cc.unitmesh.pick.worker.WorkerContext
import cc.unitmesh.pick.worker.job.InstructionFileJob
import org.archguard.scanner.analyser.count.FileJob
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class ThresholdCheckerTest {
private val context = WorkerContext.default()

@Test
fun shouldReturnTrueWhenFileMeetsThresholdCriteria() {
// given
val job = InstructionFileJob(
FileJob(
extension = "java",
complexity = 5,
binary = false,
generated = false,
minified = false,
bytes = 10000,
content = "some code".toByteArray()
)
)

// when
val result = ThresholdChecker(context).isMetThreshold(job)

// then
assertTrue(result)
}

@Test
fun shouldReturnFalseWhenFileDoesNotMeetExtensionCriteria() {
// given
val job = InstructionFileJob(
FileJob (
extension = "txt"
)
)

// when
val result = ThresholdChecker(context).isMetThreshold(job)

// then
assertFalse(result)
}

@Test
fun shouldReturnFalseWhenFileDoesNotMeetComplexityCriteria() {
// given
val job = InstructionFileJob(
FileJob(
extension = "java",
complexity = (InsQualityThreshold.MAX_COMPLEXITY + 1).toLong()
)
)

// when
val result = ThresholdChecker(context).isMetThreshold(job)

// then
assertFalse(result)
}

@Test
fun shouldReturnFalseWhenFileIsBinary() {
// given
val job = InstructionFileJob(
FileJob(
extension = "java",
binary = true
)
)

// when
val result = ThresholdChecker(context).isMetThreshold(job)

// then
assertFalse(result)
}

@Test
fun shouldReturnFalseWhenFileIsGenerated() {
// given
val job = InstructionFileJob(
FileJob(
extension = "java",
generated = true
)
)

// when
val result = ThresholdChecker(context).isMetThreshold(job)

// then
assertFalse(result)
}

@Test
fun shouldReturnFalseWhenFileIsMinified() {
// given
val job = InstructionFileJob(
FileJob(
extension = "java",
minified = true
)
)

// when
val result = ThresholdChecker(context).isMetThreshold(job)

// then
assertFalse(result)
}

@Test
fun shouldReturnFalseWhenFileSizeExceedsThreshold() {
// given
val job = InstructionFileJob(
FileJob(
extension = "java",
bytes = (InsQualityThreshold.MAX_FILE_SIZE + 1).toLong()
)
)

// when
val result = ThresholdChecker(context).isMetThreshold(job)

// then
assertFalse(result)
}


@Test
fun shouldReturnTrueWhenInstructionMeetsThresholdCriteria() {
// given
val ins = Instruction(
instruction = "some instruction",
input = "some input",
output = "some output"
)

// when
val result = ThresholdChecker(context).isMetThreshold(ins)

// then
assertTrue(result)
}
}

0 comments on commit dc03e56

Please sign in to comment.