Fix tests by adjusting to platform behavior
This commit is contained in:
committed by
Space
parent
d500d03baa
commit
46408ffd9d
@@ -33,3 +33,13 @@ public actual val supportsSuppressedExceptions: Boolean get() = true
|
|||||||
public actual val supportsNamedCapturingGroup: Boolean get() = true
|
public actual val supportsNamedCapturingGroup: Boolean get() = true
|
||||||
|
|
||||||
public actual val regexSplitUnicodeCodePointHandling: Boolean get() = true
|
public actual val regexSplitUnicodeCodePointHandling: Boolean get() = true
|
||||||
|
|
||||||
|
public actual object BackReferenceHandling {
|
||||||
|
actual val captureLargestValidIndex: Boolean get() = true
|
||||||
|
|
||||||
|
actual val notYetDefinedGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
actual val notYetDefinedNamedGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
actual val enclosingGroup: HandlingOption = HandlingOption.MATCH_NOTHING
|
||||||
|
actual val nonExistentGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
actual val nonExistentNamedGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
}
|
||||||
@@ -21,3 +21,17 @@ public expect val supportsSuppressedExceptions: Boolean
|
|||||||
public expect val supportsNamedCapturingGroup: Boolean
|
public expect val supportsNamedCapturingGroup: Boolean
|
||||||
|
|
||||||
public expect val regexSplitUnicodeCodePointHandling: Boolean
|
public expect val regexSplitUnicodeCodePointHandling: Boolean
|
||||||
|
|
||||||
|
public enum class HandlingOption {
|
||||||
|
MATCH_NOTHING, THROW, IGNORE_BACK_REFERENCE_EXPRESSION
|
||||||
|
}
|
||||||
|
|
||||||
|
public expect object BackReferenceHandling {
|
||||||
|
val captureLargestValidIndex: Boolean
|
||||||
|
|
||||||
|
val notYetDefinedGroup: HandlingOption
|
||||||
|
val notYetDefinedNamedGroup: HandlingOption
|
||||||
|
val enclosingGroup: HandlingOption
|
||||||
|
val nonExistentGroup: HandlingOption
|
||||||
|
val nonExistentNamedGroup: HandlingOption
|
||||||
|
}
|
||||||
@@ -30,3 +30,13 @@ actual val supportsSuppressedExceptions: Boolean get() = true
|
|||||||
public actual val supportsNamedCapturingGroup: Boolean get() = true
|
public actual val supportsNamedCapturingGroup: Boolean get() = true
|
||||||
|
|
||||||
public actual val regexSplitUnicodeCodePointHandling: Boolean get() = true
|
public actual val regexSplitUnicodeCodePointHandling: Boolean get() = true
|
||||||
|
|
||||||
|
public actual object BackReferenceHandling {
|
||||||
|
actual val captureLargestValidIndex: Boolean get() = false
|
||||||
|
|
||||||
|
actual val notYetDefinedGroup: HandlingOption = HandlingOption.IGNORE_BACK_REFERENCE_EXPRESSION
|
||||||
|
actual val notYetDefinedNamedGroup: HandlingOption = HandlingOption.IGNORE_BACK_REFERENCE_EXPRESSION
|
||||||
|
actual val enclosingGroup: HandlingOption = HandlingOption.IGNORE_BACK_REFERENCE_EXPRESSION
|
||||||
|
actual val nonExistentGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
actual val nonExistentNamedGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
}
|
||||||
@@ -44,3 +44,13 @@ public actual val supportsSuppressedExceptions: Boolean get() = !isJava6
|
|||||||
public actual val supportsNamedCapturingGroup: Boolean get() = !isJava6
|
public actual val supportsNamedCapturingGroup: Boolean get() = !isJava6
|
||||||
|
|
||||||
public actual val regexSplitUnicodeCodePointHandling: Boolean get() = false
|
public actual val regexSplitUnicodeCodePointHandling: Boolean get() = false
|
||||||
|
|
||||||
|
public actual object BackReferenceHandling {
|
||||||
|
actual val captureLargestValidIndex: Boolean get() = true
|
||||||
|
|
||||||
|
actual val notYetDefinedGroup: HandlingOption = HandlingOption.MATCH_NOTHING
|
||||||
|
actual val notYetDefinedNamedGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
actual val enclosingGroup: HandlingOption = HandlingOption.MATCH_NOTHING
|
||||||
|
actual val nonExistentGroup: HandlingOption = HandlingOption.MATCH_NOTHING
|
||||||
|
actual val nonExistentNamedGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
}
|
||||||
@@ -9,6 +9,8 @@ package test.text
|
|||||||
|
|
||||||
import test.regexSplitUnicodeCodePointHandling
|
import test.regexSplitUnicodeCodePointHandling
|
||||||
import test.supportsNamedCapturingGroup
|
import test.supportsNamedCapturingGroup
|
||||||
|
import test.BackReferenceHandling
|
||||||
|
import test.HandlingOption
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
class RegexTest {
|
class RegexTest {
|
||||||
@@ -216,13 +218,16 @@ class RegexTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// capture the largest valid group index
|
// capture the largest valid group index
|
||||||
"(\\w+), yes \\12".toRegex().let { regex ->
|
"(\\w+), yes \\12".let { pattern ->
|
||||||
val match = regex.find("Do you copy? Sir, yes Sir2")!!
|
if (BackReferenceHandling.captureLargestValidIndex) {
|
||||||
assertEquals("Sir, yes Sir2", match.value)
|
val match = pattern.toRegex().find("Do you copy? Sir, yes Sir2")!!
|
||||||
assertEquals("Sir", match.groups[1]?.value)
|
assertEquals("Sir, yes Sir2", match.value)
|
||||||
|
assertEquals("Sir", match.groups[1]?.value)
|
||||||
|
} else {
|
||||||
|
// JS throws SyntaxError
|
||||||
|
assertFails { pattern.toRegex() }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// back reference to non-existent group
|
|
||||||
assertNull("a(a)\\21".toRegex().find("aaaa1"))
|
|
||||||
|
|
||||||
// back reference to a group with large index
|
// back reference to a group with large index
|
||||||
"0(1(2(3(4(5(6(7(8(9(A(B(C))))))))\\11))))".toRegex().let { regex ->
|
"0(1(2(3(4(5(6(7(8(9(A(B(C))))))))\\11))))".toRegex().let { regex ->
|
||||||
@@ -232,10 +237,9 @@ class RegexTest {
|
|||||||
assertEquals("456789ABCBC", match.groups[4]?.value)
|
assertEquals("456789ABCBC", match.groups[4]?.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// back reference to an enclosing group
|
testInvalidBackReference(BackReferenceHandling.nonExistentGroup, pattern = "a(a)\\2")
|
||||||
assertNull("a(a\\1)".toRegex().find("aaaa"))
|
testInvalidBackReference(BackReferenceHandling.enclosingGroup, pattern = "a(a\\1)")
|
||||||
// back reference to not yet available group
|
testInvalidBackReference(BackReferenceHandling.notYetDefinedGroup, pattern = "a\\1(a)")
|
||||||
assertNull("a\\1(a)".toRegex().find("aaaa"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun matchNamedGroupsWithBackReference() {
|
@Test fun matchNamedGroupsWithBackReference() {
|
||||||
@@ -249,38 +253,51 @@ class RegexTest {
|
|||||||
assertNull(regex.find("Do you copy? Sir, yes I do!"))
|
assertNull(regex.find("Do you copy? Sir, yes I do!"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// back reference to an enclosing group
|
testInvalidBackReference(BackReferenceHandling.nonExistentNamedGroup, pattern = "a(a)\\k<name>")
|
||||||
assertNull("a(?<first>a\\k<first>)".toRegex().find("aaaa"))
|
testInvalidBackReference(BackReferenceHandling.enclosingGroup, pattern = "a(?<first>a\\k<first>)")
|
||||||
// back reference to not yet available group
|
testInvalidBackReference(BackReferenceHandling.notYetDefinedNamedGroup, pattern = "a\\k<first>(?<first>a)")
|
||||||
assertFailsWith<IllegalArgumentException> {
|
}
|
||||||
"a\\k<first>(?<first>a)".toRegex()
|
|
||||||
|
private fun testInvalidBackReference(option: HandlingOption, pattern: String, input: CharSequence = "aaaa", matchValue: String = "aa") {
|
||||||
|
when (option) {
|
||||||
|
HandlingOption.IGNORE_BACK_REFERENCE_EXPRESSION ->
|
||||||
|
assertEquals(matchValue, pattern.toRegex().find(input)?.value)
|
||||||
|
HandlingOption.THROW ->
|
||||||
|
// TODO: should fail with IllegalArgumentException, but JS fails with SyntaxError
|
||||||
|
assertFails { pattern.toRegex() }
|
||||||
|
HandlingOption.MATCH_NOTHING ->
|
||||||
|
assertNull(pattern.toRegex().find(input))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun incompleteNamedGroupDeclaration() {
|
@Test fun invalidNamedGroupDeclaration() {
|
||||||
if (!supportsNamedCapturingGroup) return
|
if (!supportsNamedCapturingGroup) return
|
||||||
|
|
||||||
assertFailsWith<IllegalArgumentException> {
|
// TODO: should fail with IllegalArgumentException, but JS fails with SyntaxError
|
||||||
|
|
||||||
|
assertFails {
|
||||||
"(?<".toRegex()
|
"(?<".toRegex()
|
||||||
}
|
}
|
||||||
assertFailsWith<IllegalArgumentException> {
|
assertFails {
|
||||||
"(?<)".toRegex()
|
"(?<)".toRegex()
|
||||||
}
|
}
|
||||||
assertFailsWith<IllegalArgumentException> {
|
assertFails {
|
||||||
"(?<name".toRegex()
|
"(?<name".toRegex()
|
||||||
}
|
}
|
||||||
assertFailsWith<IllegalArgumentException> {
|
assertFails {
|
||||||
"(?<name)".toRegex()
|
"(?<name)".toRegex()
|
||||||
}
|
}
|
||||||
assertFailsWith<IllegalArgumentException> {
|
assertFails {
|
||||||
"(?<name>".toRegex()
|
"(?<name>".toRegex()
|
||||||
}
|
}
|
||||||
assertFailsWith<IllegalArgumentException> {
|
assertFails {
|
||||||
"(?<>\\w+), yes \\k<>".toRegex()
|
"(?<>\\w+), yes \\k<>".toRegex()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Test comment mode enabled and group name is separated by space, (before, in the middle, after)
|
// TODO: Test comment mode enabled and group name is separated by space, (before, in the middle, after)
|
||||||
|
// TODO: Test comment mode enabled and back reference group index is separated by space, (before, in the middle, after)
|
||||||
|
// TODO: Test back reference with \0
|
||||||
|
|
||||||
@Test fun matchMultiline() {
|
@Test fun matchMultiline() {
|
||||||
val regex = "^[a-z]*$".toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
val regex = "^[a-z]*$".toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||||
|
|||||||
@@ -31,3 +31,13 @@ actual val supportsSuppressedExceptions: Boolean get() = true
|
|||||||
public actual val supportsNamedCapturingGroup: Boolean get() = true
|
public actual val supportsNamedCapturingGroup: Boolean get() = true
|
||||||
|
|
||||||
public actual val regexSplitUnicodeCodePointHandling: Boolean get() = true
|
public actual val regexSplitUnicodeCodePointHandling: Boolean get() = true
|
||||||
|
|
||||||
|
public actual object BackReferenceHandling {
|
||||||
|
actual val captureLargestValidIndex: Boolean get() = true
|
||||||
|
|
||||||
|
actual val notYetDefinedGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
actual val notYetDefinedNamedGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
actual val enclosingGroup: HandlingOption = HandlingOption.MATCH_NOTHING
|
||||||
|
actual val nonExistentGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
actual val nonExistentNamedGroup: HandlingOption = HandlingOption.THROW
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user