[ai-completion] ML-3971 add tests for relaxed EM

GitOrigin-RevId: ba3e62fee6501f8fb9a7cca37e779d871f4d6475
This commit is contained in:
Oleg Taratukhin
2024-09-27 21:22:54 +02:00
committed by intellij-monorepo-bot
parent b47c41525a
commit 06ee57b078

View File

@@ -1,38 +1,39 @@
package com.intellij.cce.metric
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class RelaxedExactMatchTest {
private data class ProcessingTestCase(
private data class Sample(
val prefix: String,
val completion: String,
val middle: String,
val suffix: String,
val strip: Boolean,
val strip: Boolean = false,
)
private val processingCases = listOf(
ProcessingTestCase(
Sample(
prefix = "hi\nre",
completion = "turn main\ndef fun",
middle = "turn main\ndef fun",
suffix = "ction()::\nendfun",
strip = false,
) to listOf("return main", "def function()::"),
ProcessingTestCase(
Sample(
prefix = "class MyClas",
completion = "s:\ndef __init__(self):\npass\n",
middle = "s:\ndef __init__(self):\npass\n",
suffix = "\n def method(self):\n pass\n",
strip = false,
) to listOf("class MyClass:", "def __init__(self):", "pass"),
ProcessingTestCase(
Sample(
prefix = "class MyClas",
completion = "s:\ndef __init__(self):\npass\n",
middle = "s:\ndef __init__(self):\npass\n",
suffix = "",
strip = true,
) to listOf("classMyClass", "def__init__self", "pass"),
)
private fun ProcessingTestCase.process(): List<String> = RelaxedSimilarityUtils.preProcessLines(
completion = completion,
private fun Sample.process(): List<String> = RelaxedSimilarityUtils.preProcessLines(
completion = middle,
prefix = prefix,
suffix = suffix,
stripChars = strip,
@@ -40,6 +41,66 @@ class RelaxedExactMatchTest {
@Test
fun `test line pre-processing`() = processingCases.forEach {
(inputs, outputs) -> Assertions.assertEquals(outputs, inputs.process())
(inputs, outputs) -> assertEquals(outputs, inputs.process())
}
private fun runRelaxedExactMatch(
middle: String,
completion: String,
expected: RelaxedSimilarityUtils.RelaxedResult,
prefix: String = "",
suffix: String = "",
stripChars: Boolean = false,
) = assertEquals(expected, RelaxedSimilarityUtils.computeRelaxedExactMatch(
middle = middle,
completion = completion,
prefix = prefix,
suffix = suffix,
stripChars = stripChars,
))
@Test
fun `test computeRelaxedExactMatch with exact match one line`() = runRelaxedExactMatch(
middle = "hello",
completion = "hello",
expected = RelaxedSimilarityUtils.RelaxedResult.MULTI,
)
@Test
fun `test computeRelaxedExactMatch with exact match multiple lines`() = runRelaxedExactMatch(
middle = "hello\nworld",
completion = "hello\nworld",
expected = RelaxedSimilarityUtils.RelaxedResult.MULTI,
)
@Test
fun `test computeRelaxedExactMatch with exact match multiple lines but in different order`() = runRelaxedExactMatch(
middle = "hello\nworld",
completion = "world\nhello",
expected = RelaxedSimilarityUtils.RelaxedResult.MULTI,
)
@Test
fun `test computeRelaxedExactMatch with partial match in first line`() = runRelaxedExactMatch(
middle = "llo\nworld",
completion = "llo",
prefix = "he",
expected = RelaxedSimilarityUtils.RelaxedResult.MULTI,
)
@Test
fun `test computeRelaxedExactMatch with partial match in last line`() = runRelaxedExactMatch(
middle = "hello\nworld",
completion = "world\nbye",
expected = RelaxedSimilarityUtils.RelaxedResult.ANY,
)
@Test
fun `test computeRelaxedExactMatch no match`() = runRelaxedExactMatch(
prefix = "hello\n",
middle = "world\n",
completion = "kotlin\n",
suffix = "\nbye\n",
expected = RelaxedSimilarityUtils.RelaxedResult.NO,
)
}