From dc03e56d2ca8facc561b6e1f7cabfd81049bdc12 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 26 Dec 2023 20:35:52 +0800 Subject: [PATCH] test: add test for threshold checker --- .../kotlin/cc/unitmesh/core/Instruction.kt | 2 +- .../cc/unitmesh/pick/worker/WorkerContext.kt | 18 +++ .../pick/threshold/ThresholdCheckerTest.kt | 153 ++++++++++++++++++ 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 unit-picker/src/test/kotlin/cc/unitmesh/pick/threshold/ThresholdCheckerTest.kt diff --git a/unit-core/src/main/kotlin/cc/unitmesh/core/Instruction.kt b/unit-core/src/main/kotlin/cc/unitmesh/core/Instruction.kt index cbe16e15..bbdfc319 100644 --- a/unit-core/src/main/kotlin/cc/unitmesh/core/Instruction.kt +++ b/unit-core/src/main/kotlin/cc/unitmesh/core/Instruction.kt @@ -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()") diff --git a/unit-picker/src/main/kotlin/cc/unitmesh/pick/worker/WorkerContext.kt b/unit-picker/src/main/kotlin/cc/unitmesh/pick/worker/WorkerContext.kt index 7113a8a3..edec3446 100644 --- a/unit-picker/src/main/kotlin/cc/unitmesh/pick/worker/WorkerContext.kt +++ b/unit-picker/src/main/kotlin/cc/unitmesh/pick/worker/WorkerContext.kt @@ -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( @@ -20,4 +21,21 @@ data class WorkerContext( val qualityThreshold: InsQualityThreshold = InsQualityThreshold(), var compositionDependency: List = 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(), + ) + } + } } diff --git a/unit-picker/src/test/kotlin/cc/unitmesh/pick/threshold/ThresholdCheckerTest.kt b/unit-picker/src/test/kotlin/cc/unitmesh/pick/threshold/ThresholdCheckerTest.kt new file mode 100644 index 00000000..e4827516 --- /dev/null +++ b/unit-picker/src/test/kotlin/cc/unitmesh/pick/threshold/ThresholdCheckerTest.kt @@ -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) + } +}