From 56fae9478dc5c70ba98b5ea851d8250b6ce901ae Mon Sep 17 00:00:00 2001 From: Mark Whitaker Date: Thu, 18 Feb 2021 10:18:56 +0000 Subject: [PATCH] Remove deprecated features (#22) * Remove deprecated features Remove public constructor to enforce `regex { }` syntax. Remove start/end group methods to enforce `group { }` syntax. Remove logging. * Updated README.md for 3.0 --- README.md | 84 +- src/main/kotlin/RegexBuilder.kt | 325 +-- src/main/kotlin/RegexBuilderException.kt | 16 - src/main/kotlin/RegexQuantifier.kt | 27 +- src/test/kotlin/RegexBuilderDslLoggingTest.kt | 214 -- src/test/kotlin/RegexBuilderDslTest.kt | 2562 ----------------- src/test/kotlin/RegexBuilderLoggingTest.kt | 590 ---- src/test/kotlin/RegexBuilderTest.kt | 549 ++-- 8 files changed, 375 insertions(+), 3992 deletions(-) delete mode 100644 src/main/kotlin/RegexBuilderException.kt delete mode 100644 src/test/kotlin/RegexBuilderDslLoggingTest.kt delete mode 100644 src/test/kotlin/RegexBuilderDslTest.kt delete mode 100644 src/test/kotlin/RegexBuilderLoggingTest.kt diff --git a/README.md b/README.md index e2a3487..77e715d 100644 --- a/README.md +++ b/README.md @@ -15,63 +15,22 @@ It offers a lot of benefits over using raw regex syntax in strings: It is fully documented in the [project wiki](https://github.com/markwhitaker/RegexToolbox.kt/wiki). -## New in 2.2 +## Breaking changes in 3.0 -### Logging +All `RegexBuilder` features that were deprecated in version 2.4 have been removed in 3.0. -Use the new `addLogger()` method to connect a logger of your choice and see how your regex is built, step by step. For example: - -```kotlin -val regex = RegexBuilder() - .addLogger { - println(it) - } - .wordBoundary() - .text("Regex") - .anyOf("Builder", "Toolbox") - .wordBoundary() - .buildRegex() -``` - -or this: - -```kotlin -val regex = regex { - addLogger { - println(it) - } - wordBoundary() - text("Regex") - anyOf("Builder", "Toolbox") - wordBoundary() -} -``` - -will output this to your console: - -```text -RegexBuilder: wordBoundary(): \b -RegexBuilder: text("Regex"): Regex -RegexBuilder: anyOf("Builder", "Toolbox"): (?:Builder|Toolbox) -RegexBuilder: wordBoundary(): \b -RegexBuilder: buildRegex(): \bRegex(?:Builder|Toolbox)\b -``` - -## New in 2.0 - -### New builder syntax - -Version 2.0 introduces a simplified [type-safe builder syntax](https://kotlinlang.org/docs/reference/type-safe-builders.html) for cleaner, less error-prone and more Kotliny code. Go from this: - -```kotlin -val regex = RegexBuilder() - .startGroup() - .letter() - .digit() - .buildRegex() // ERROR: forgot to call endGroup() -``` +|Removed|Replaced with| +|---|---| +|`RegexBuilder()` public constructor|`regex { ... }`| +|`buildRegex()`|`regex { ... }`| +|`buildPattern()`|`pattern { ... }`| +|`startGroup() ... endGroup()`|`group { ... }`| +|`startNamedGroup() ... endGroup()`|`namedGroup { ... }`| +|`startNonCapturingGroup() ... endGroup()`|`nonCapturingGroup { ... }`| +|`addLogger()`|No replacement: logging has been removed.| -to this: +In a nutshell: the old Java-style is completed removed in 3.0. +RegexToolbox.kt now exclusively uses the new [type-safe builder syntax](https://kotlinlang.org/docs/reference/type-safe-builders.html) introduced in 2.0, for example: ```kotlin val regex = regex { @@ -82,23 +41,6 @@ val regex = regex { } // Yay! No need to call buildRegex() ``` -The old syntax is still supported if that's your preference (and for consistency with the sibling projects listed at the bottom of the page). - -### Quantifiers - -`RegexQuantifier` is now a sealed class and its methods have become objects or classes. The old syntax is still supported but deprecated and will be removed in a future version. - -|Old syntax|becomes| -|---|---| -|`RegexQuantifier.zeroOrOne()`|`RegexQuantifier.ZeroOrOne`| -|`RegexQuantifier.zeroOrMore()`|`RegexQuantifier.ZeroOrMore`| -|`RegexQuantifier.oneOrMore()`|`RegexQuantifier.OneOrMore`| -|`RegexQuantifier.exactly(1)`|`RegexQuantifier.Exactly(1)`| -|`RegexQuantifier.atLeast(1)`|`RegexQuantifier.AtLeast(1)`| -|`RegexQuantifier.noMoreThan(1)`|`RegexQuantifier.NoMoreThan(1)`| -|`RegexQuantifier.between(1,2)`|`RegexQuantifier.Between(1,2)`| -|`[quantifier].butAsFewAsPossible()`|`[quantifier].butAsFewAsPossible`| - ## Usage (Gradle) RegexToolbox.kt is [hosted on JitPack](https://jitpack.io/#markwhitaker/RegexToolbox.kt). diff --git a/src/main/kotlin/RegexBuilder.kt b/src/main/kotlin/RegexBuilder.kt index c4a85c4..db6422a 100644 --- a/src/main/kotlin/RegexBuilder.kt +++ b/src/main/kotlin/RegexBuilder.kt @@ -15,109 +15,8 @@ import java.util.regex.Pattern * } * */ -class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: please use the new syntax") constructor() { +class RegexBuilder internal constructor() { private val stringBuilder = StringBuilder() - private var openGroupCount = 0 - private var logFunction: (s: String) -> Unit = {} - private var prefix = DEFAULT_PREFIX - - /** - * Interface to a logger attached by the client code which will receive log messages as the regex is built. - */ - @Deprecated("Logging will be removed in version 3.0") - interface Logger { - /** - * Log a [message] to a debugger or logging framework - */ - @Deprecated("Logging will be removed in version 3.0") - fun log(message: String) - } - - /** - * Build and return a [Regex] object from the current builder state. - * After calling this the builder is cleared and ready to re-use. - * - * @param options Any number of regex options to apply to the regex - * @return [Regex] as built - * @throws RegexBuilderException An error occurred when building the regex - */ - @Throws(RegexBuilderException::class) - @Deprecated("The old syntax will be removed in version 3.0: please use regex") - fun buildRegex(vararg options: RegexOptions): Regex = - when (openGroupCount) { - 0 -> { - val stringBuilt = stringBuilder.toString() - log("buildRegex()", stringBuilt) - val regex = Regex( - stringBuilt, - options.map { it.kotlinRegexOption }.toSet() - ) - stringBuilder.setLength(0) - regex - } - 1 -> { - throw RegexBuilderException("A group has been started but not ended", stringBuilder) - } - else -> { - throw RegexBuilderException("$openGroupCount groups have been started but not ended", stringBuilder) - } - } - - /** - * Build and return a [Pattern] object from the current builder state. - * Note that the normal Kotlin thing to do is to use [buildRegex], but this is for legacy cases where you need a Pattern. - * After calling this the builder is cleared and ready to re-use. - * - * @param options Any number of regex options to apply to the regex - * @return [Regex] as built - * @throws RegexBuilderException An error occurred when building the regex - */ - @Throws(RegexBuilderException::class) - @Deprecated("The old syntax will be removed in version 3.0: please use pattern") - fun buildPattern(vararg options: RegexOptions): Pattern = buildRegex(*options).toPattern() - - /** - * Attach a [logger] to this builder using this [Logger] interface. The builder will emit logging messages to it as - * the regex is built with the prefix "RegexBuilder". - */ - @Deprecated("Logging will be removed in version 3.0") - fun addLogger(logger: Logger): RegexBuilder { - return addLogger{ - logger.log(it) - } - } - - /** - * Attach a [logger] to this builder using this [Logger] interface. The builder will emit logging messages to it as - * the regex is built with the provided [prefix]. - */ - @Deprecated("Logging will be removed in version 3.0") - fun addLogger(prefix: String, logger: Logger): RegexBuilder { - return addLogger(prefix) { - logger.log(it) - } - } - - /** - * Attach a logger to this builder using the provided log function. The builder will emit logging messages to it - * as the regex is built with the prefix "RegexBuilder". - */ - @Deprecated("Logging will be removed in version 3.0") - fun addLogger(logFunction: (s: String) -> Unit): RegexBuilder { - this.logFunction = logFunction - return this - } - - /** - * Attach a logger to this builder using the provided log function. The builder will emit logging messages to it - * as the regex is built with the provided [prefix]. - */ - @Deprecated("Logging will be removed in version 3.0") - fun addLogger(prefix: String, logFunction: (s: String) -> Unit): RegexBuilder { - this.logFunction = logFunction - this.prefix = prefix - return this - } /** * Add text to the regex. Any regex special characters will be escaped as necessary @@ -137,13 +36,12 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p fun text(text: String, quantifier: RegexQuantifier? = null): RegexBuilder { val safeText = makeSafeForRegex(text) return if (quantifier == null) { - addPart("text(\"$text\")", safeText) + addPart(safeText) } else { - addPartInNonCapturingGroup("text(\"$text\", ${quantifier.name})", safeText, quantifier) + addPartInNonCapturingGroup(safeText, quantifier) } } - /** * Add literal regex text to the regex. Regex special characters will NOT be escaped. * Only call this if you're comfortable with regex syntax. @@ -162,9 +60,9 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p */ fun regexText(text: String, quantifier: RegexQuantifier? = null): RegexBuilder = if (quantifier == null) { - addPart("regexText(\"$text\")", text) + addPart(text) } else { - addPartInNonCapturingGroup("regexText(\"$text\", ${quantifier.name})", text, quantifier) + addPartInNonCapturingGroup(text, quantifier) } /** @@ -174,7 +72,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun anyCharacter(quantifier: RegexQuantifier? = null) = - addPart("anyCharacter(${nameOf(quantifier)})", ".", quantifier) + addPart(".", quantifier) /** * Add an element to match any single whitespace character. @@ -183,7 +81,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun whitespace(quantifier: RegexQuantifier? = null) = - addPart("whitespace(${nameOf(quantifier)})", "\\s", quantifier) + addPart("\\s", quantifier) /** * Add an element to match any single non-whitespace character. @@ -192,14 +90,14 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun nonWhitespace(quantifier: RegexQuantifier? = null) = - addPart("nonWhitespace(${nameOf(quantifier)})", "\\S", quantifier) + addPart("\\S", quantifier) /** * Add an element to represent any amount of white space, including none. This is just a convenient alias for * `whitespace(RegexQuantifier.zeroOrMore())`. */ fun possibleWhitespace() = - addPart("possibleWhitespace()", "\\s", RegexQuantifier.ZeroOrMore) + addPart("\\s", RegexQuantifier.ZeroOrMore) /** * Add an element to match a single space character. If you want to match any kind of white space, use @@ -209,7 +107,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun space(quantifier: RegexQuantifier? = null) = - addPart("space(${nameOf(quantifier)})", " ", quantifier) + addPart(" ", quantifier) /** * Add an element to match a single tab character. @@ -218,7 +116,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun tab(quantifier: RegexQuantifier? = null) = - addPart("tab(${nameOf(quantifier)})", "\\t", quantifier) + addPart("\\t", quantifier) /** * Add an element to match a single line feed character. @@ -227,7 +125,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun lineFeed(quantifier: RegexQuantifier? = null) = - addPart("lineFeed(${nameOf(quantifier)})", "\\n", quantifier) + addPart("\\n", quantifier) /** * Add an element to match a single carriage return character. @@ -236,7 +134,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun carriageReturn(quantifier: RegexQuantifier? = null) = - addPart("carriageReturn(${nameOf(quantifier)})", "\\r", quantifier) + addPart("\\r", quantifier) /** * Add an element to match any single decimal digit (0-9). @@ -245,7 +143,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun digit(quantifier: RegexQuantifier? = null) = - addPart("digit(${nameOf(quantifier)})", "\\d", quantifier) + addPart("\\d", quantifier) /** * Add an element to match any character that is not a decimal digit (0-9). @@ -254,7 +152,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun nonDigit(quantifier: RegexQuantifier? = null) = - addPart("nonDigit(${nameOf(quantifier)})", "\\D", quantifier) + addPart("\\D", quantifier) /** * Add an element to match any Unicode letter. @@ -263,7 +161,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun letter(quantifier: RegexQuantifier? = null) = - addPart("letter(${nameOf(quantifier)})", "\\p{L}", quantifier) + addPart("\\p{L}", quantifier) /** * Add an element to match any character that is not a Unicode letter. @@ -272,7 +170,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun nonLetter(quantifier: RegexQuantifier? = null) = - addPart("nonLetter(${nameOf(quantifier)})", "\\P{L}", quantifier) + addPart("\\P{L}", quantifier) /** * Add an element to match any upper-case Unicode letter. @@ -281,7 +179,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun uppercaseLetter(quantifier: RegexQuantifier? = null) = - addPart("uppercaseLetter(${nameOf(quantifier)})", "\\p{Lu}", quantifier) + addPart("\\p{Lu}", quantifier) /** * Add an element to match any lowercase Unicode letter. @@ -290,7 +188,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun lowercaseLetter(quantifier: RegexQuantifier? = null) = - addPart("lowercaseLetter(${nameOf(quantifier)})", "\\p{Ll}", quantifier) + addPart("\\p{Ll}", quantifier) /** * Add an element to match any Unicode letter or decimal digit. @@ -299,7 +197,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun letterOrDigit(quantifier: RegexQuantifier? = null) = - addPart("letterOrDigit(${nameOf(quantifier)})", "[\\p{L}0-9]", quantifier) + addPart("[\\p{L}0-9]", quantifier) /** * Add an element to match any character that is not a Unicode letter or a decimal digit. @@ -308,7 +206,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun nonLetterOrDigit(quantifier: RegexQuantifier? = null) = - addPart("nonLetterOrDigit(${nameOf(quantifier)})", "[^\\p{L}0-9]", quantifier) + addPart("[^\\p{L}0-9]", quantifier) /** * Add an element to match any hexadecimal digit (a-f, A-F, 0-9) @@ -317,7 +215,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun hexDigit(quantifier: RegexQuantifier? = null) = - addPart("hexDigit(${nameOf(quantifier)})", "[0-9A-Fa-f]", quantifier) + addPart("[0-9A-Fa-f]", quantifier) /** * Add an element to match any uppercase hexadecimal digit (A-F, 0-9) @@ -326,7 +224,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun uppercaseHexDigit(quantifier: RegexQuantifier? = null) = - addPart("uppercaseHexDigit(${nameOf(quantifier)})", "[0-9A-F]", quantifier) + addPart("[0-9A-F]", quantifier) /** * Add an element to match any lowercase hexadecimal digit (a-f, 0-9) @@ -335,7 +233,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun lowercaseHexDigit(quantifier: RegexQuantifier? = null) = - addPart("lowercaseHexDigit(${nameOf(quantifier)})", "[0-9a-f]", quantifier) + addPart("[0-9a-f]", quantifier) /** * Add an element to match any character that is not a hexadecimal digit (a-f, A-F, 0-9) @@ -344,7 +242,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun nonHexDigit(quantifier: RegexQuantifier? = null) = - addPart("nonHexDigit(${nameOf(quantifier)})", "[^0-9A-Fa-f]", quantifier) + addPart("[^0-9A-Fa-f]", quantifier) /** * Add an element to match any Unicode letter, decimal digit or underscore @@ -353,7 +251,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun wordCharacter(quantifier: RegexQuantifier? = null) = - addPart("wordCharacter(${nameOf(quantifier)})", "[\\p{L}0-9_]", quantifier) + addPart("[\\p{L}0-9_]", quantifier) /** * Add an element to match any character that is not a Unicode letter, decimal digit or underscore @@ -362,7 +260,7 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun nonWordCharacter(quantifier: RegexQuantifier? = null) = - addPart("nonWordCharacter(${nameOf(quantifier)})", "[^\\p{L}0-9_]", quantifier) + addPart("[^\\p{L}0-9_]", quantifier) /** * Add an element (a character class) to match any of the characters provided. @@ -372,14 +270,9 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun anyCharacterFrom(characters: String, quantifier: RegexQuantifier? = null): RegexBuilder { - val method = if (quantifier == null) { - "anyCharacterFrom(\"$characters\")" - } else { - "anyCharacterFrom(\"$characters\", ${quantifier.name})" - } // Build a character class, remembering to escape any ] character if passed in val safeCharacters = makeSafeForCharacterClass(characters) - return addPart(method, "[$safeCharacters]", quantifier) + return addPart("[$safeCharacters]", quantifier) } /** @@ -390,14 +283,9 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun anyCharacterExcept(characters: String, quantifier: RegexQuantifier? = null): RegexBuilder { - val method = if (quantifier == null) { - "anyCharacterExcept(\"$characters\")" - } else { - "anyCharacterExcept(\"$characters\", ${quantifier.name})" - } // Build a character class, remembering to escape any ] character if passed in val safeCharacters = makeSafeForCharacterClass(characters) - return addPart(method, "[^$safeCharacters]", quantifier) + return addPart("[^$safeCharacters]", quantifier) } /** @@ -408,24 +296,19 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @return The current [RegexBuilder] object, for method chaining */ fun anyOf(strings: List, quantifier: RegexQuantifier? = null): RegexBuilder { - return if (strings.isEmpty()) { - log("anyOf()", "strings list is empty, so doing nothing") - this - } else if (strings.size == 1 && quantifier == null) { - addPart("anyOf(\"${strings.first()}\")", makeSafeForRegex(strings.first())) - } else { - val stringsJoinedForLogging = strings.joinToString(", ") { - "\"$it\"" + return when { + strings.isEmpty() -> { + this } - val method = if (quantifier == null) { - "anyOf($stringsJoinedForLogging)" - } else { - "anyOf($stringsJoinedForLogging, ${quantifier.name})" + strings.size == 1 && quantifier == null -> { + addPart(makeSafeForRegex(strings.first())) } - val stringsSafeAndJoined = strings.joinToString("|") { - makeSafeForRegex(it) + else -> { + val stringsSafeAndJoined = strings.joinToString("|") { + makeSafeForRegex(it) + } + addPartInNonCapturingGroup(stringsSafeAndJoined, quantifier) } - addPartInNonCapturingGroup(method, stringsSafeAndJoined, quantifier) } } @@ -436,7 +319,8 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * @param quantifier Quantifier to apply to this element * @return The current [RegexBuilder] object, for method chaining */ - fun anyOf(vararg strings: String, quantifier: RegexQuantifier? = null): RegexBuilder = anyOf(strings.toList(), quantifier) + fun anyOf(vararg strings: String, quantifier: RegexQuantifier? = null): RegexBuilder = + anyOf(strings.toList(), quantifier) // ZERO-WIDTH ASSERTIONS @@ -445,14 +329,14 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * * @return The current [RegexBuilder] object, for method chaining */ - fun startOfString() = addPart("startOfString()", "^") + fun startOfString() = addPart("^") /** * Add a zero-width anchor element to match the end of the string * * @return The current [RegexBuilder] object, for method chaining */ - fun endOfString() = addPart("endOfString()", "$") + fun endOfString() = addPart("$") /** * Add a zero-width anchor element to match the boundary between an alphanumeric/underscore character @@ -460,115 +344,84 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p * * @return The current [RegexBuilder] object, for method chaining */ - fun wordBoundary() = addPart("wordBoundary()", "\\b") + fun wordBoundary() = addPart("\\b") // GROUPS /** - * Start a capture group. Capture groups have two purposes: they group part of the expression so - * it can have quantifiers applied to it, and they capture the results of each group match and - * allow you to access them afterwards using [MatchResult.groups]. - * - * If you don't want to capture the group match, use [startNonCapturingGroup]. - * - * Note: all groups must be ended with [endGroup] before calling [buildRegex]. - * - * @return The current [RegexBuilder] object, for method chaining - */ - @Deprecated("The old syntax will be removed in version 3.0: please use group") - fun startGroup(): RegexBuilder { - openGroupCount++ - return addPart("startGroup()", "(") - } - - /** - * Start a non-capturing group. Non-capturing groups group part of the expression so - * it can have quantifiers applied to it, but do not capture the results of each group match, meaning - * you can't access them afterwards using [MatchResult.groups]. + * Create a capture group. Capture groups have two purposes: they group part of the expression so it can have + * quantifiers applied to it, and they capture the results of each group match and allow you to access them + * afterwards using [MatchResult.groups]. * - * If you want to capture group results, use [startGroup] or [startNamedGroup]. - * - * Note: all groups must be ended with [endGroup] before calling [buildRegex]. + * If you don't want to capture the group match, use [nonCapturingGroup]. * + * @param quantifier Quantifier to apply to this group * @return The current [RegexBuilder] object, for method chaining */ - @Deprecated("The old syntax will be removed in version 3.0: please use nonCapturingGroup") - fun startNonCapturingGroup(): RegexBuilder { - openGroupCount++ - return addPart("startNonCapturingGroup()", "(?:") - } + fun group(quantifier: RegexQuantifier? = null, buildSteps: RegexBuilder.() -> Unit) = + startGroup().apply(buildSteps).endGroup(quantifier) /** - * Start a named capture group. Capture groups have two purposes: they group part of the expression so - * it can have quantifiers applied to it, and they capture the results of each group match and - * allow you to access them afterwards using [MatchResult.groups]. Named capture groups can be accessed by - * indexing into [MatchResult.groups] with the assigned name as well as a numerical index. + * Create a named capture group. Capture groups have two purposes: they group part of the expression so it can have + * quantifiers applied to it, and they capture the results of each group match and allow you to access them + * afterwards using [MatchResult.groups]. Named capture groups can be accessed by indexing into [MatchResult.groups] + * with the assigned name or a numerical index. * - * If you don't want to capture the group match, use [startNonCapturingGroup]. - * - * Note: all groups must be ended with [endGroup] before calling [buildRegex]. + * If you don't want to capture the group match, use [nonCapturingGroup]. * * @param name Name used to identify the group + * @param quantifier Quantifier to apply to this group * @return The current [RegexBuilder] object, for method chaining */ - @Deprecated("The old syntax will be removed in version 3.0: please use namedGroup") - fun startNamedGroup(name: String): RegexBuilder { - openGroupCount++ - return addPart("startNamedGroup(\"$name\")", "(?<$name>") - } + fun namedGroup(name: String, quantifier: RegexQuantifier? = null, buildSteps: RegexBuilder.() -> Unit) = + startNamedGroup(name).apply(buildSteps).endGroup(quantifier) /** - * End the innermost group previously started with [startGroup], [startNonCapturingGroup] or - * [startNamedGroup]. + * Create a non-capturing group. Non-capturing groups group part of the expression so it can have quantifiers + * applied to it, but do not capture the results of each group match, meaning you can't access them afterwards using + * [MatchResult.groups]. + * + * If you want to capture group results, use [group] or [namedGroup]. * * @param quantifier Quantifier to apply to this group * @return The current [RegexBuilder] object, for method chaining - * @throws RegexBuilderException A group has not been started */ - @Throws(RegexBuilderException::class) - @Deprecated("The old syntax will be removed in version 3.0: please use new group syntax") - fun endGroup(quantifier: RegexQuantifier? = null): RegexBuilder { - if (openGroupCount == 0) { - throw RegexBuilderException( - "Cannot call endGroup() until a group has been started with startGroup()", - stringBuilder - ) - } + fun nonCapturingGroup(quantifier: RegexQuantifier? = null, buildSteps: RegexBuilder.() -> Unit) = + startNonCapturingGroup().apply(buildSteps).endGroup(quantifier) + + // INTERNAL METHODS - openGroupCount-- - return addPart("endGroup(${nameOf(quantifier)})", ")", quantifier) + internal fun buildRegex(vararg options: RegexOptions): Regex { + val stringBuilt = stringBuilder.toString() + val regex = Regex( + stringBuilt, + options.map { it.kotlinRegexOption }.toSet() + ) + stringBuilder.setLength(0) + return regex } - // GROUPS (DSL) + internal fun buildPattern(vararg options: RegexOptions): Pattern = buildRegex(*options).toPattern() - fun group(quantifier: RegexQuantifier? = null, buildSteps: RegexBuilder.() -> Unit) = - startGroup().apply(buildSteps).endGroup(quantifier) + // PRIVATE METHODS - fun namedGroup(name: String, quantifier: RegexQuantifier? = null, buildSteps: RegexBuilder.() -> Unit) = - startNamedGroup(name).apply(buildSteps).endGroup(quantifier) + private fun startGroup(): RegexBuilder = addPart("(") - fun nonCapturingGroup(quantifier: RegexQuantifier? = null, buildSteps: RegexBuilder.() -> Unit) = - startNonCapturingGroup().apply(buildSteps).endGroup(quantifier) + private fun startNonCapturingGroup(): RegexBuilder = addPart("(?:") - // PRIVATE METHODS + private fun startNamedGroup(name: String): RegexBuilder = addPart("(?<$name>") - private fun addPart(method: String, part: String, quantifier: RegexQuantifier? = null): RegexBuilder { - val quantifierString = quantifier?.toString() ?: "" - log(method, "$part$quantifierString") + private fun endGroup(quantifier: RegexQuantifier? = null): RegexBuilder = addPart(")", quantifier) + + private fun addPart(part: String, quantifier: RegexQuantifier? = null): RegexBuilder { stringBuilder .append(part) .append(quantifier?.toString() ?: "") return this } - private fun addPartInNonCapturingGroup( - method: String, - part: String, - quantifier: RegexQuantifier? = null): RegexBuilder = addPart(method, "(?:$part)", quantifier) - - private fun log(method: String, message: String) { - logFunction("$prefix: $method: $message") - } + private fun addPartInNonCapturingGroup(part: String, quantifier: RegexQuantifier? = null): RegexBuilder = + addPart("(?:$part)", quantifier) private fun makeSafeForCharacterClass(text: String): String { var safeText = text @@ -603,10 +456,4 @@ class RegexBuilder @Deprecated("The old syntax will be removed in version 3.0: p .replace("}", "\\}") .replace("|", "\\|") } - - private fun nameOf(quantifier: RegexQuantifier?) = quantifier?.name ?: "" - - companion object { - private const val DEFAULT_PREFIX = "RegexBuilder" - } } \ No newline at end of file diff --git a/src/main/kotlin/RegexBuilderException.kt b/src/main/kotlin/RegexBuilderException.kt deleted file mode 100644 index f634a15..0000000 --- a/src/main/kotlin/RegexBuilderException.kt +++ /dev/null @@ -1,16 +0,0 @@ -package uk.co.mainwave.regextoolboxkotlin - -/** - * Exception thrown by [RegexBuilder] methods - */ -@Deprecated("RegexBuilderException will be removed in version 3.0") -class RegexBuilderException( - message: String, - stringBuilder: StringBuilder -) : Exception(message) { - /** - * The regex string at the time the exception was thrown - */ - @Suppress("unused") - val regexString = stringBuilder.toString() -} \ No newline at end of file diff --git a/src/main/kotlin/RegexQuantifier.kt b/src/main/kotlin/RegexQuantifier.kt index 6b5a543..7adfb71 100644 --- a/src/main/kotlin/RegexQuantifier.kt +++ b/src/main/kotlin/RegexQuantifier.kt @@ -4,7 +4,6 @@ package uk.co.mainwave.regextoolboxkotlin * Quantifiers that can be applied to regex elements or groups */ sealed class RegexQuantifier( - internal val name: String, private val regexString: String ) { override fun toString() = regexString @@ -18,7 +17,7 @@ sealed class RegexQuantifier( * * @return A greedy quantifier: use [butAsFewAsPossible] to make it lazy */ - object ZeroOrMore : RegexQuantifier("ZeroOrMore", "*"), Greedy { + object ZeroOrMore : RegexQuantifier("*"), Greedy { override val butAsFewAsPossible: RegexQuantifier = LazyZeroOrMore } @@ -27,7 +26,7 @@ sealed class RegexQuantifier( * * @return A greedy quantifier: use [butAsFewAsPossible] to make it lazy */ - object OneOrMore : RegexQuantifier("OneOrMore", "+"), Greedy { + object OneOrMore : RegexQuantifier("+"), Greedy { override val butAsFewAsPossible: RegexQuantifier = LazyOneOrMore } @@ -36,7 +35,7 @@ sealed class RegexQuantifier( * * @return A greedy quantifier: use [butAsFewAsPossible] to make it lazy */ - object ZeroOrOne : RegexQuantifier("ZeroOrOne", "?"), Greedy { + object ZeroOrOne : RegexQuantifier("?"), Greedy { override val butAsFewAsPossible: RegexQuantifier = LazyZeroOrOne } @@ -47,7 +46,7 @@ sealed class RegexQuantifier( * @return A quantifier that is neither lazy nor greedy: it will match the exact number of element occurrences * specified */ - class Exactly(times: Int) : RegexQuantifier("Exactly($times)", "{$times}") + class Exactly(times: Int) : RegexQuantifier("{$times}") /** * Quantifier to match at least a minimum number of occurrences of the preceding element @@ -55,7 +54,7 @@ sealed class RegexQuantifier( * @param minimum The minimum number of occurrences to match * @return A greedy quantifier: use [butAsFewAsPossible] to make it lazy */ - class AtLeast(minimum: Int) : RegexQuantifier("AtLeast($minimum)", "{$minimum,}"), Greedy { + class AtLeast(minimum: Int) : RegexQuantifier("{$minimum,}"), Greedy { override val butAsFewAsPossible: RegexQuantifier = LazyAtLeast(minimum) } @@ -65,7 +64,7 @@ sealed class RegexQuantifier( * @param maximum The maximum number of occurrences to match * @return A greedy quantifier: use [butAsFewAsPossible] to make it lazy */ - class NoMoreThan(maximum: Int) : RegexQuantifier("NoMoreThan($maximum)", "{0,$maximum}"), Greedy { + class NoMoreThan(maximum: Int) : RegexQuantifier("{0,$maximum}"), Greedy { override val butAsFewAsPossible: RegexQuantifier = LazyNoMoreThan(maximum) } @@ -76,14 +75,14 @@ sealed class RegexQuantifier( * @param maximum The maximum number of occurrences to match * @return A greedy quantifier: use [butAsFewAsPossible] to make it lazy */ - class Between(minimum: Int, maximum: Int) : RegexQuantifier("Between($minimum, $maximum)", "{$minimum,$maximum}"), Greedy { + class Between(minimum: Int, maximum: Int) : RegexQuantifier("{$minimum,$maximum}"), Greedy { override val butAsFewAsPossible: RegexQuantifier = LazyBetween(minimum, maximum) } - private object LazyZeroOrMore : RegexQuantifier("ZeroOrMore.butAsFewAsPossible", "*?") - private object LazyOneOrMore : RegexQuantifier("OneOrMore.butAsFewAsPossible", "+?") - private object LazyZeroOrOne : RegexQuantifier("ZeroOrOne.butAsFewAsPossible", "??") - private class LazyAtLeast(minimum: Int) : RegexQuantifier("AtLeast($minimum).butAsFewAsPossible", "{$minimum,}?") - private class LazyNoMoreThan(maximum: Int) : RegexQuantifier("NoMoreThan($maximum).butAsFewAsPossible", "{0,$maximum}?") - private class LazyBetween(minimum: Int, maximum: Int) : RegexQuantifier("Between($minimum, $maximum).butAsFewAsPossible", "{$minimum,$maximum}?") + private object LazyZeroOrMore : RegexQuantifier("*?") + private object LazyOneOrMore : RegexQuantifier("+?") + private object LazyZeroOrOne : RegexQuantifier("??") + private class LazyAtLeast(minimum: Int) : RegexQuantifier("{$minimum,}?") + private class LazyNoMoreThan(maximum: Int) : RegexQuantifier("{0,$maximum}?") + private class LazyBetween(minimum: Int, maximum: Int) : RegexQuantifier("{$minimum,$maximum}?") } diff --git a/src/test/kotlin/RegexBuilderDslLoggingTest.kt b/src/test/kotlin/RegexBuilderDslLoggingTest.kt deleted file mode 100644 index 6c4a92c..0000000 --- a/src/test/kotlin/RegexBuilderDslLoggingTest.kt +++ /dev/null @@ -1,214 +0,0 @@ -package uk.co.mainwave.regextoolboxkotlin - -import io.mockk.mockk -import io.mockk.verify -import org.junit.Assert.assertEquals -import org.junit.Before -import org.junit.Test - -class RegexBuilderDslLoggingTest { - private var logOutput: String = "" - private lateinit var regexBuilder: RegexBuilder - - @Before - fun setUp() { - logOutput = "" - regexBuilder = RegexBuilder().addLogger { message -> - logOutput = message - } - } - - @Test - fun testAddLoggerInterface() { - val logger = mockk(relaxUnitFun = true) - - regex { - addLogger(logger) - text("Hello") - whitespace(RegexQuantifier.OneOrMore) - text("world", RegexQuantifier.ZeroOrOne) - } - - verify { logger.log("RegexBuilder: text(\"Hello\"): Hello") } - verify { logger.log("RegexBuilder: whitespace(OneOrMore): \\s+") } - verify { logger.log("RegexBuilder: text(\"world\", ZeroOrOne): (?:world)?") } - verify { logger.log("RegexBuilder: buildRegex(): Hello\\s+(?:world)?") } - } - - @Test - fun testAddLoggerInterfaceWithTag() { - val logger = mockk(relaxUnitFun = true) - - regex { - addLogger("TEST", logger) - text("Hello") - whitespace(RegexQuantifier.OneOrMore) - text("world", RegexQuantifier.ZeroOrOne) - } - - verify { logger.log("TEST: text(\"Hello\"): Hello") } - verify { logger.log("TEST: whitespace(OneOrMore): \\s+") } - verify { logger.log("TEST: text(\"world\", ZeroOrOne): (?:world)?") } - verify { logger.log("TEST: buildRegex(): Hello\\s+(?:world)?") } - } - - @Test - fun testAddLoggerLambda() { - val list = mutableListOf() - - regex { - addLogger { - list.add(it) - } - text("Hello") - whitespace(RegexQuantifier.OneOrMore) - text("world", RegexQuantifier.ZeroOrOne) - } - - assertEquals(4, list.size) - assertEquals("RegexBuilder: text(\"Hello\"): Hello", list[0]) - assertEquals("RegexBuilder: whitespace(OneOrMore): \\s+", list[1]) - assertEquals("RegexBuilder: text(\"world\", ZeroOrOne): (?:world)?", list[2]) - assertEquals("RegexBuilder: buildRegex(): Hello\\s+(?:world)?", list[3]) - } - - @Test - fun testAddLoggerLambdaWithTag() { - val list = mutableListOf() - - regex { - addLogger("TEST") { - list.add(it) - } - text("Hello") - whitespace(RegexQuantifier.OneOrMore) - text("world", RegexQuantifier.ZeroOrOne) - } - - assertEquals(4, list.size) - assertEquals("TEST: text(\"Hello\"): Hello", list[0]) - assertEquals("TEST: whitespace(OneOrMore): \\s+", list[1]) - assertEquals("TEST: text(\"world\", ZeroOrOne): (?:world)?", list[2]) - assertEquals("TEST: buildRegex(): Hello\\s+(?:world)?", list[3]) - } - - @Test - fun testGroup() { - val list = mutableListOf() - - regex { - addLogger { - list.add(it) - } - group { - text("abc") - } - } - - assertEquals(4, list.size) - assertEquals("RegexBuilder: startGroup(): (", list[0]) - assertEquals("RegexBuilder: text(\"abc\"): abc", list[1]) - assertEquals("RegexBuilder: endGroup(): )", list[2]) - assertEquals("RegexBuilder: buildRegex(): (abc)", list[3]) - } - - @Test - fun testGroupWithQuantifier() { - val list = mutableListOf() - - regex { - addLogger { - list.add(it) - } - group(RegexQuantifier.ZeroOrMore) { - text("abc") - } - } - - assertEquals(4, list.size) - assertEquals("RegexBuilder: startGroup(): (", list[0]) - assertEquals("RegexBuilder: text(\"abc\"): abc", list[1]) - assertEquals("RegexBuilder: endGroup(ZeroOrMore): )*", list[2]) - assertEquals("RegexBuilder: buildRegex(): (abc)*", list[3]) - } - - @Test - fun testNonCapturingGroup() { - val list = mutableListOf() - - regex { - addLogger { - list.add(it) - } - nonCapturingGroup { - text("abc") - } - } - - assertEquals(4, list.size) - assertEquals("RegexBuilder: startNonCapturingGroup(): (?:", list[0]) - assertEquals("RegexBuilder: text(\"abc\"): abc", list[1]) - assertEquals("RegexBuilder: endGroup(): )", list[2]) - assertEquals("RegexBuilder: buildRegex(): (?:abc)", list[3]) - } - - @Test - fun testNonCapturingGroupWithQuantifier() { - val list = mutableListOf() - - regex { - addLogger { - list.add(it) - } - nonCapturingGroup(RegexQuantifier.ZeroOrMore) { - text("abc") - } - } - - assertEquals(4, list.size) - assertEquals("RegexBuilder: startNonCapturingGroup(): (?:", list[0]) - assertEquals("RegexBuilder: text(\"abc\"): abc", list[1]) - assertEquals("RegexBuilder: endGroup(ZeroOrMore): )*", list[2]) - assertEquals("RegexBuilder: buildRegex(): (?:abc)*", list[3]) - } - - @Test - fun testNamedGroup() { - val list = mutableListOf() - - regex { - addLogger { - list.add(it) - } - namedGroup("name") { - text("abc") - } - } - - assertEquals(4, list.size) - assertEquals("RegexBuilder: startNamedGroup(\"name\"): (?", list[0]) - assertEquals("RegexBuilder: text(\"abc\"): abc", list[1]) - assertEquals("RegexBuilder: endGroup(): )", list[2]) - assertEquals("RegexBuilder: buildRegex(): (?abc)", list[3]) - } - - @Test - fun testNamedGroupWithQuantifier() { - val list = mutableListOf() - - regex { - addLogger { - list.add(it) - } - namedGroup("name", RegexQuantifier.ZeroOrMore) { - text("abc") - } - } - - assertEquals(4, list.size) - assertEquals("RegexBuilder: startNamedGroup(\"name\"): (?", list[0]) - assertEquals("RegexBuilder: text(\"abc\"): abc", list[1]) - assertEquals("RegexBuilder: endGroup(ZeroOrMore): )*", list[2]) - assertEquals("RegexBuilder: buildRegex(): (?abc)*", list[3]) - } -} \ No newline at end of file diff --git a/src/test/kotlin/RegexBuilderDslTest.kt b/src/test/kotlin/RegexBuilderDslTest.kt deleted file mode 100644 index 3c4acb0..0000000 --- a/src/test/kotlin/RegexBuilderDslTest.kt +++ /dev/null @@ -1,2562 +0,0 @@ -package uk.co.mainwave.regextoolboxkotlin - -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertNotNull -import org.junit.Assert.assertNull -import org.junit.Assert.assertTrue -import org.junit.Test -import uk.co.mainwave.regextoolboxkotlin.RegexOptions.IGNORE_CASE -import uk.co.mainwave.regextoolboxkotlin.RegexOptions.MULTILINE -import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier.AtLeast -import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier.Between -import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier.Exactly -import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier.NoMoreThan -import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier.OneOrMore -import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier.ZeroOrMore -import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier.ZeroOrOne -import java.util.regex.Pattern - -class RegexBuilderDslTest { - @Test - fun testBuildPattern() { - val pattern = pattern(IGNORE_CASE, MULTILINE) { - text("cat") - } - - assertEquals("cat", pattern.toString()) - assertEquals(Pattern.CASE_INSENSITIVE, pattern.flags() and Pattern.CASE_INSENSITIVE) - assertEquals(Pattern.MULTILINE, pattern.flags() and Pattern.MULTILINE) - } - - @Test - fun testSimpleText() { - val regex = regex { - text("cat") - } - - assertEquals("cat", regex.toString()) - assertTrue(regex.matches("cat")) - assertTrue(regex.containsMatchIn("scatter")) - assertFalse(regex.matches("Cat")) - assertFalse(regex.matches("dog")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testSimpleTextWithQuantifier() { - val regex = regex { - text("cat", Exactly(2)) - } - - assertEquals("(?:cat){2}", regex.toString()) - assertFalse(regex.containsMatchIn("cat")) - assertTrue(regex.matches("catcat")) - assertTrue(regex.containsMatchIn("catcatcat")) - assertFalse(regex.containsMatchIn("scatter")) - assertFalse(regex.containsMatchIn("Cat")) - assertFalse(regex.containsMatchIn("dog")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testSimpleTextCaseInsensitive() { - val regex = regex(IGNORE_CASE) { - text("cat") - } - - assertEquals("cat", regex.toString()) - assertTrue(regex.matches("cat")) - assertTrue(regex.containsMatchIn("scatter")) - assertTrue(regex.matches("Cat")) - assertFalse(regex.containsMatchIn("dog")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testSimpleTextWithRegexCharacters() { - val regex = regex { - text("\\.+*?[]{}()|^$") - } - - assertEquals("\\\\\\.\\+\\*\\?\\[\\]\\{\\}\\(\\)\\|\\^\\$", regex.toString()) - assertTrue(regex.containsMatchIn("\\.+*?[]{}()|^$")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testRegexText() { - val regex = regex { - regexText("^\\scat\\b") - } - - assertEquals("^\\scat\\b", regex.toString()) - assertTrue(regex.containsMatchIn(" cat")) - assertTrue(regex.containsMatchIn(" cat.")) - assertTrue(regex.containsMatchIn("\tcat ")) - assertTrue(regex.containsMatchIn(" cat-")) - assertTrue(regex.containsMatchIn(" cat ")) - assertFalse(regex.containsMatchIn("cat")) - assertFalse(regex.containsMatchIn(" catheter")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAnyCharacter() { - val regex = regex { - anyCharacter() - } - - assertEquals(".", regex.toString()) - assertTrue(regex.containsMatchIn(" ")) - assertTrue(regex.containsMatchIn("a")) - assertTrue(regex.containsMatchIn("1")) - assertTrue(regex.containsMatchIn("\\")) - assertFalse(regex.containsMatchIn("")) - assertFalse(regex.containsMatchIn("\n")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testWhitespace() { - val regex = regex { - whitespace() - } - - assertEquals("\\s", regex.toString()) - assertTrue(regex.containsMatchIn(" ")) - assertTrue(regex.containsMatchIn("\t")) - assertTrue(regex.containsMatchIn("\r")) - assertTrue(regex.containsMatchIn("\n")) - assertTrue(regex.containsMatchIn("\r\n")) - assertTrue(regex.containsMatchIn("\t \t")) - assertTrue(regex.containsMatchIn(" hi!")) - assertFalse(regex.containsMatchIn("cat")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNonWhitespace() { - val regex = regex { - nonWhitespace() - } - - assertEquals("\\S", regex.toString()) - assertTrue(regex.containsMatchIn("a")) - assertTrue(regex.containsMatchIn("1")) - assertTrue(regex.containsMatchIn("-")) - assertTrue(regex.containsMatchIn("*")) - assertTrue(regex.containsMatchIn("abc")) - assertTrue(regex.containsMatchIn(" hi!")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("\t")) - assertFalse(regex.containsMatchIn("\r")) - assertFalse(regex.containsMatchIn("\n")) - assertFalse(regex.containsMatchIn("\t\t\r\n ")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testPossibleWhitespace() { - val regex = regex { - nonWhitespace() - possibleWhitespace() - nonWhitespace() - } - - assertEquals("\\S\\s*\\S", regex.toString()) - assertFalse(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("0")) - assertTrue(regex.containsMatchIn("999")) - assertTrue(regex.containsMatchIn("there's a digit in here s0mewhere")) - assertFalse(regex.containsMatchIn(" ")) - assertTrue(regex.containsMatchIn("abc")) - assertTrue(regex.containsMatchIn("xFFF")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testSpace() { - val regex = regex { - space() - } - - assertEquals(" ", regex.toString()) - assertTrue(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("\t")) - assertFalse(regex.containsMatchIn("\r")) - assertFalse(regex.containsMatchIn("\n")) - assertFalse(regex.containsMatchIn("\r\n")) - assertTrue(regex.containsMatchIn("\t \t")) - assertTrue(regex.containsMatchIn(" hi!")) - assertFalse(regex.containsMatchIn("cat")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testTab() { - val regex = regex { - tab() - } - - assertEquals("\\t", regex.toString()) - assertFalse(regex.containsMatchIn(" ")) - assertTrue(regex.containsMatchIn("\t")) - assertFalse(regex.containsMatchIn("\r")) - assertFalse(regex.containsMatchIn("\n")) - assertFalse(regex.containsMatchIn("\r\n")) - assertTrue(regex.containsMatchIn("\t \t")) - assertFalse(regex.containsMatchIn(" hi!")) - assertFalse(regex.containsMatchIn("cat")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testLineFeed() { - val regex = regex { - lineFeed() - } - - assertEquals("\\n", regex.toString()) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("\t")) - assertFalse(regex.containsMatchIn("\r")) - assertTrue(regex.containsMatchIn("\n")) - assertTrue(regex.containsMatchIn("\r\n")) - assertFalse(regex.containsMatchIn("\t \t")) - assertFalse(regex.containsMatchIn(" hi!")) - assertFalse(regex.containsMatchIn("cat")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testCarriageReturn() { - val regex = regex { - carriageReturn() - } - - assertEquals("\\r", regex.toString()) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("\t")) - assertTrue(regex.containsMatchIn("\r")) - assertFalse(regex.containsMatchIn("\n")) - assertTrue(regex.containsMatchIn("\r\n")) - assertFalse(regex.containsMatchIn("\t \t")) - assertFalse(regex.containsMatchIn(" hi!")) - assertFalse(regex.containsMatchIn("cat")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testDigit() { - val regex = regex { - digit() - } - - assertEquals("\\d", regex.toString()) - assertTrue(regex.containsMatchIn("1")) - assertTrue(regex.containsMatchIn("0")) - assertTrue(regex.containsMatchIn("999")) - assertTrue(regex.containsMatchIn("there's a digit in here s0mewhere")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("abc")) - assertFalse(regex.containsMatchIn("xFFF")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNonDigit() { - val regex = regex { - nonDigit() - } - - assertEquals("\\D", regex.toString()) - assertTrue(regex.containsMatchIn(" 1")) - assertTrue(regex.containsMatchIn("a0")) - assertTrue(regex.containsMatchIn("999_")) - assertTrue(regex.containsMatchIn("1,000")) - assertTrue(regex.containsMatchIn("there's a digit in here s0mewhere")) - assertFalse(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("0")) - assertFalse(regex.containsMatchIn("999")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testLetter() { - val regex = regex { - letter() - } - - assertEquals("\\p{L}", regex.toString()) - assertTrue(regex.containsMatchIn("a")) - assertTrue(regex.containsMatchIn("A")) - assertTrue(regex.containsMatchIn(" z")) - assertTrue(regex.containsMatchIn("text with spaces")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("%")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNonLetter() { - val regex = regex { - nonLetter() - } - - assertEquals("\\P{L}", regex.toString()) - assertTrue(regex.containsMatchIn(" 1")) - assertTrue(regex.containsMatchIn("0")) - assertTrue(regex.containsMatchIn("999_")) - assertTrue(regex.containsMatchIn("1,000")) - assertTrue(regex.containsMatchIn("text with spaces")) - assertFalse(regex.containsMatchIn("a")) - assertFalse(regex.containsMatchIn("ZZZ")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testUppercaseLetter() { - val regex = regex { - uppercaseLetter() - } - - assertEquals("\\p{Lu}", regex.toString()) - assertTrue(regex.containsMatchIn("A")) - assertTrue(regex.containsMatchIn(" Z")) - assertTrue(regex.containsMatchIn("text with Spaces")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("%")) - assertFalse(regex.containsMatchIn("s")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testLowercaseLetter() { - val regex = regex { - lowercaseLetter() - } - - assertEquals("\\p{Ll}", regex.toString()) - assertTrue(regex.containsMatchIn("a")) - assertTrue(regex.containsMatchIn(" z")) - assertTrue(regex.containsMatchIn("text with Spaces")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("%")) - assertFalse(regex.containsMatchIn("S")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testLetterOrDigit() { - val regex = regex { - letterOrDigit() - } - - assertEquals("[\\p{L}0-9]", regex.toString()) - assertTrue(regex.containsMatchIn("A")) - assertTrue(regex.containsMatchIn(" Z")) - assertTrue(regex.containsMatchIn("text with Spaces")) - assertFalse(regex.containsMatchIn(" ")) - assertTrue(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("%")) - assertFalse(regex.containsMatchIn("_")) - assertTrue(regex.containsMatchIn("s")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNonLetterOrDigit() { - val regex = regex { - nonLetterOrDigit() - } - - assertEquals("[^\\p{L}0-9]", regex.toString()) - assertFalse(regex.containsMatchIn("A")) - assertTrue(regex.containsMatchIn(" Z")) - assertTrue(regex.containsMatchIn("text with Spaces")) - assertTrue(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("1")) - assertTrue(regex.containsMatchIn("%")) - assertTrue(regex.containsMatchIn("_")) - assertFalse(regex.containsMatchIn("s")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testHexDigit() { - val regex = regex { - hexDigit() - } - - assertEquals("[0-9A-Fa-f]", regex.toString()) - assertTrue(regex.containsMatchIn("A")) - assertTrue(regex.containsMatchIn(" f")) - assertTrue(regex.containsMatchIn("text with Spaces")) - assertFalse(regex.containsMatchIn(" ")) - assertTrue(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("%")) - assertFalse(regex.containsMatchIn("_")) - assertFalse(regex.containsMatchIn("s")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testUppercaseHexDigit() { - val regex = regex { - uppercaseHexDigit() - } - - assertEquals("[0-9A-F]", regex.toString()) - assertTrue(regex.containsMatchIn("A")) - assertFalse(regex.containsMatchIn(" f")) - assertFalse(regex.containsMatchIn("text with Spaces")) - assertFalse(regex.containsMatchIn(" ")) - assertTrue(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("%")) - assertFalse(regex.containsMatchIn("_")) - assertFalse(regex.containsMatchIn("s")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testLowercaseHexDigit() { - val regex = regex { - lowercaseHexDigit() - } - - assertEquals("[0-9a-f]", regex.toString()) - assertFalse(regex.containsMatchIn("A")) - assertTrue(regex.containsMatchIn(" f")) - assertTrue(regex.containsMatchIn("text with Spaces")) - assertFalse(regex.containsMatchIn(" ")) - assertTrue(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("%")) - assertFalse(regex.containsMatchIn("_")) - assertFalse(regex.containsMatchIn("s")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNonHexDigit() { - val regex = regex { - nonHexDigit() - } - - assertEquals("[^0-9A-Fa-f]", regex.toString()) - assertFalse(regex.containsMatchIn("A")) - assertTrue(regex.containsMatchIn(" f")) - assertTrue(regex.containsMatchIn("text with Spaces")) - assertTrue(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("1")) - assertTrue(regex.containsMatchIn("%")) - assertTrue(regex.containsMatchIn("_")) - assertTrue(regex.containsMatchIn("s")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testWordCharacter() { - val regex = regex { - wordCharacter() - } - - assertEquals("[\\p{L}0-9_]", regex.toString()) - assertTrue(regex.containsMatchIn("A")) - assertTrue(regex.containsMatchIn(" Z")) - assertTrue(regex.containsMatchIn("text with Spaces")) - assertFalse(regex.containsMatchIn(" ")) - assertTrue(regex.containsMatchIn("1")) - assertFalse(regex.containsMatchIn("%")) - assertTrue(regex.containsMatchIn("_")) - assertTrue(regex.containsMatchIn("s")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNonWordCharacter() { - val regex = regex { - nonWordCharacter() - } - - assertEquals("[^\\p{L}0-9_]", regex.toString()) - assertFalse(regex.containsMatchIn("A")) - assertTrue(regex.containsMatchIn(" Z")) - assertTrue(regex.containsMatchIn("text with Spaces")) - assertTrue(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("1")) - assertTrue(regex.containsMatchIn("%")) - assertFalse(regex.containsMatchIn("_")) - assertFalse(regex.containsMatchIn("s")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAnyCharacterFrom() { - val regex = regex { - anyCharacterFrom("cat") - } - - assertEquals("[cat]", regex.toString()) - assertTrue(regex.containsMatchIn("cat")) - assertTrue(regex.containsMatchIn("parrot")) - assertTrue(regex.containsMatchIn("tiger")) - assertTrue(regex.containsMatchIn("cow")) - assertFalse(regex.containsMatchIn("CAT")) - assertFalse(regex.containsMatchIn("dog")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAnyCharacterFromWithCaretAtStart() { - val regex = regex { - anyCharacterFrom("^abc") - } - - assertEquals("[\\^abc]", regex.toString()) - assertTrue(regex.containsMatchIn("jazz")) - assertTrue(regex.containsMatchIn("_^_")) - assertTrue(regex.containsMatchIn("oboe")) - assertTrue(regex.containsMatchIn("cue")) - assertFalse(regex.containsMatchIn("CAT")) - assertFalse(regex.containsMatchIn("dog")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAnyCharacterFromWithHyphen() { - val regex = regex { - anyCharacterFrom("a-f") - } - - assertEquals("[a\\-f]", regex.toString()) - assertTrue(regex.containsMatchIn("a")) - assertTrue(regex.containsMatchIn("-")) - assertTrue(regex.containsMatchIn("f")) - assertFalse(regex.containsMatchIn("c")) - } - - @Test - fun testAnyCharacterFromWithCaretNotAtStart() { - val regex = regex { - anyCharacterFrom("a^bc") - } - - assertEquals("[a^bc]", regex.toString()) - assertTrue(regex.containsMatchIn("jazz")) - assertTrue(regex.containsMatchIn("_^_")) - assertTrue(regex.containsMatchIn("oboe")) - assertTrue(regex.containsMatchIn("cue")) - assertFalse(regex.containsMatchIn("CAT")) - assertFalse(regex.containsMatchIn("dog")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAnyCharacterExcept() { - val regex = regex { - anyCharacterExcept("cat") - } - - assertEquals("[^cat]", regex.toString()) - assertFalse(regex.containsMatchIn("cat")) - assertFalse(regex.containsMatchIn("tata")) - assertTrue(regex.containsMatchIn("parrot")) - assertTrue(regex.containsMatchIn("tiger")) - assertTrue(regex.containsMatchIn("cow")) - assertTrue(regex.containsMatchIn("CAT")) - assertTrue(regex.containsMatchIn("dog")) - assertTrue(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAnyOf() { - val regex = regex { - anyOf(listOf("cat", "dog", "|")) - } - - assertEquals("(?:cat|dog|\\|)", regex.toString()) - assertFalse(regex.containsMatchIn("ca do")) - assertTrue(regex.containsMatchIn("cat")) - assertTrue(regex.containsMatchIn("dog")) - assertTrue(regex.containsMatchIn("|")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAnyOfWithQuantifier() { - val regex = regex { - anyOf(listOf("cat", "dog", "|"), Exactly(2)) - } - - assertEquals("(?:cat|dog|\\|){2}", regex.toString()) - assertTrue(regex.containsMatchIn("catdog")) - assertTrue(regex.containsMatchIn("cat|dog")) - assertFalse(regex.containsMatchIn("cat")) - assertTrue(regex.containsMatchIn("catcat")) - assertTrue(regex.containsMatchIn("catcatcat")) - assertFalse(regex.containsMatchIn("dog")) - assertTrue(regex.containsMatchIn("dogdog")) - assertTrue(regex.containsMatchIn("dogdogdog")) - assertFalse(regex.containsMatchIn("|")) - assertTrue(regex.containsMatchIn("||")) - assertTrue(regex.containsMatchIn("|||")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testVarargAnyOf() { - val regex = regex { - anyOf("cat", "dog", "|") - } - - assertEquals("(?:cat|dog|\\|)", regex.toString()) - assertFalse(regex.containsMatchIn("ca do")) - assertTrue(regex.containsMatchIn("cat")) - assertTrue(regex.containsMatchIn("dog")) - assertTrue(regex.containsMatchIn("|")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testVarargAnyOfWithQuantifier() { - val regex = regex { - anyOf("cat", "dog", "|", quantifier = Exactly(2)) - } - - assertEquals("(?:cat|dog|\\|){2}", regex.toString()) - assertTrue(regex.containsMatchIn("catdog")) - assertTrue(regex.containsMatchIn("cat|dog")) - assertFalse(regex.containsMatchIn("cat")) - assertTrue(regex.containsMatchIn("catcat")) - assertTrue(regex.containsMatchIn("catcatcat")) - assertFalse(regex.containsMatchIn("dog")) - assertTrue(regex.containsMatchIn("dogdog")) - assertTrue(regex.containsMatchIn("dogdogdog")) - assertFalse(regex.containsMatchIn("|")) - assertTrue(regex.containsMatchIn("||")) - assertTrue(regex.containsMatchIn("|||")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAnyOfEmptyOrSingle() { - val anyOfEmptyRegex = regex { - anyOf(emptyList()) - } - - val anyOfSingleRegex = regex { - anyOf(listOf("cat")) - } - - assertEquals("", anyOfEmptyRegex.toString()) - assertEquals("cat", anyOfSingleRegex.toString()) - } - - @Test - fun testStartOfString() { - val regex = regex { - startOfString() - text("a") - } - - assertEquals("^a", regex.toString()) - assertTrue(regex.containsMatchIn("a")) - assertTrue(regex.containsMatchIn("aA")) - assertTrue(regex.containsMatchIn("a_")) - assertTrue(regex.containsMatchIn("a big gap")) - assertFalse(regex.containsMatchIn(" a space before")) - assertFalse(regex.containsMatchIn("A capital letter")) - assertFalse(regex.containsMatchIn("Aa")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testEndOfString() { - val regex = regex { - text("z") - endOfString() - } - - assertEquals("z$", regex.toString()) - assertTrue(regex.containsMatchIn("z")) - assertTrue(regex.containsMatchIn("zzz")) - assertTrue(regex.containsMatchIn("fizz buzz")) - assertFalse(regex.containsMatchIn("buzz!")) - assertFalse(regex.containsMatchIn("zzz ")) - assertFalse(regex.containsMatchIn("zZ")) - assertFalse(regex.containsMatchIn("z ")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testWordBoundary() { - val regex = regex { - text("a") - wordBoundary() - } - - assertEquals("a\\b", regex.toString()) - assertTrue(regex.containsMatchIn("a")) - assertTrue(regex.containsMatchIn("spa")) - assertTrue(regex.containsMatchIn("papa don't preach")) - assertTrue(regex.containsMatchIn("a dog")) - assertTrue(regex.containsMatchIn("a-dog")) - assertFalse(regex.containsMatchIn("an apple")) - assertFalse(regex.containsMatchIn("asp")) - assertFalse(regex.containsMatchIn("a1b")) - assertFalse(regex.containsMatchIn("a_b")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testSingleGroup() { - val regex = regex { - anyCharacter(ZeroOrMore) - group { - letter() - digit() - } - } - - assertEquals(".*(\\p{L}\\d)", regex.toString()) - - var match = regex.find("Class A1") - assertNotNull(match) - assertEquals("Class A1", match!!.value) - assertEquals("A1", match.groupValues[1]) - - match = regex.find("he likes F1 racing") - assertNotNull(match) - assertEquals("he likes F1", match!!.value) - assertEquals("F1", match.groupValues[1]) - - match = regex.find("A4 paper") - assertNotNull(match) - assertEquals("A4", match!!.value) - assertEquals("A4", match.groupValues[1]) - - match = regex.find("A 4-legged dog") - assertNull(match) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testRepeatGroup() { - val regex = regex { - group { - letter() - digit() - } - } - - assertEquals("(\\p{L}\\d)", regex.toString()) - - var matcher = regex.find("Class A1 f2 ZZ88") - val matches = mutableListOf() - while (matcher != null) { - matches.add(matcher.value) - matcher = matcher.next() - } - assertEquals(3, matches.size) - assertEquals("A1", matches[0]) - assertEquals("f2", matches[1]) - assertEquals("Z8", matches[2]) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNamedGroup() { - val regex = regex { - lowercaseLetter(OneOrMore) - namedGroup("test123") { - digit(OneOrMore) - } - lowercaseLetter(OneOrMore) - } - - assertEquals("\\p{Ll}+(?\\d+)\\p{Ll}+", regex.toString()) - - val match = regex.find("a99z") - assertNotNull(match) - assertEquals("a99z", match!!.value) - assertEquals("99", match.groupValues[1]) - assertEquals("99", match.groups["test123"]?.value) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNonCapturingGroup() { - val regex = regex { - lowercaseLetter(OneOrMore) - nonCapturingGroup { - digit(OneOrMore) - } - lowercaseLetter(OneOrMore) - } - - assertEquals("\\p{Ll}+(?:\\d+)\\p{Ll}+", regex.toString()) - - val match = regex.find("a99z") - assertNotNull(match) - assertEquals("a99z", match!!.value) - assertEquals(1, match.groups.size) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testMultipleGroups() { - val regex = regex { - group { - anyCharacter(ZeroOrMore) - } - group { - letter() - digit() - } - } - - assertEquals("(.*)(\\p{L}\\d)", regex.toString()) - - var match = regex.find("Class A1") - assertNotNull(match) - assertEquals("Class A1", match!!.value) - assertEquals("Class ", match.groupValues[1]) - assertEquals("A1", match.groupValues[2]) - - match = regex.find("he likes F1 racing") - assertNotNull(match) - assertEquals("he likes F1", match!!.value) - assertEquals("he likes ", match.groupValues[1]) - assertEquals("F1", match.groupValues[2]) - - match = regex.find("A4 paper") - assertNotNull(match) - assertEquals("A4", match!!.value) - assertEquals("", match.groupValues[1]) - assertEquals("A4", match.groupValues[2]) - - match = regex.find("A 4-legged dog") - assertNull(match) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNestedGroups() { - val regex = regex { - anyCharacter() // Omit first character from groups - group { - anyCharacter(ZeroOrMore) - group { - letter() - digit() - } - } - } - - assertEquals(".(.*(\\p{L}\\d))", regex.toString()) - - var match = regex.find("Class A1") - assertNotNull(match) - assertEquals("Class A1", match!!.value) - assertEquals("lass A1", match.groupValues[1]) - assertEquals("A1", match.groupValues[2]) - - match = regex.find("he likes F1 racing") - assertNotNull(match) - assertEquals("he likes F1", match!!.value) - assertEquals("e likes F1", match.groupValues[1]) - assertEquals("F1", match.groupValues[2]) - - match = regex.find(" A4 paper") - assertNotNull(match) - assertEquals(" A4", match!!.value) - assertEquals("A4", match.groupValues[1]) - assertEquals("A4", match.groupValues[2]) - - match = regex.find("A 4-legged dog") - assertNull(match) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testZeroOrMore() { - val regex = regex { - letter() - digit(ZeroOrMore) - letter() - } - - assertEquals("\\p{L}\\d*\\p{L}", regex.toString()) - assertTrue(regex.containsMatchIn("ab")) - assertTrue(regex.containsMatchIn("a1b")) - assertTrue(regex.containsMatchIn("a123b")) - assertFalse(regex.containsMatchIn("a 1 b")) - assertFalse(regex.containsMatchIn("a b")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testOneOrMore() { - val regex = regex { - letter() - digit(OneOrMore) - letter() - } - - assertEquals("\\p{L}\\d+\\p{L}", regex.toString()) - assertFalse(regex.containsMatchIn("ab")) - assertTrue(regex.containsMatchIn("a1b")) - assertTrue(regex.containsMatchIn("a123b")) - assertFalse(regex.containsMatchIn("a 1 b")) - assertFalse(regex.containsMatchIn("a b")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testZeroOrOne() { - val regex = regex { - letter() - digit(ZeroOrOne) - letter() - } - - assertEquals("\\p{L}\\d?\\p{L}", regex.toString()) - assertTrue(regex.containsMatchIn("ab")) - assertTrue(regex.containsMatchIn("a1b")) - assertFalse(regex.containsMatchIn("a123b")) - assertFalse(regex.containsMatchIn("a 1 b")) - assertFalse(regex.containsMatchIn("a b")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testExactlyNTimes() { - val regex = regex { - letter() - digit(Exactly(3)) - letter() - } - - assertEquals("\\p{L}\\d{3}\\p{L}", regex.toString()) - assertFalse(regex.containsMatchIn("ab")) - assertFalse(regex.containsMatchIn("a1b")) - assertFalse(regex.containsMatchIn("a12b")) - assertTrue(regex.containsMatchIn("a123b")) - assertFalse(regex.containsMatchIn("a1234b")) - assertFalse(regex.containsMatchIn("a12345b")) - assertFalse(regex.containsMatchIn("a 1 b")) - assertFalse(regex.containsMatchIn("a b")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAtLeastQuantifier() { - val regex = regex { - letter() - digit(AtLeast(3)) - letter() - } - - assertEquals("\\p{L}\\d{3,}\\p{L}", regex.toString()) - assertFalse(regex.containsMatchIn("ab")) - assertFalse(regex.containsMatchIn("a1b")) - assertFalse(regex.containsMatchIn("a12b")) - assertTrue(regex.containsMatchIn("a123b")) - assertTrue(regex.containsMatchIn("a1234b")) - assertTrue(regex.containsMatchIn("a12345b")) - assertFalse(regex.containsMatchIn("a 1 b")) - assertFalse(regex.containsMatchIn("a b")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNoMoreThanQuantifier() { - val regex = regex { - letter() - digit(NoMoreThan(3)) - letter() - } - - assertEquals("\\p{L}\\d{0,3}\\p{L}", regex.toString()) - assertTrue(regex.containsMatchIn("ab")) - assertTrue(regex.containsMatchIn("a1b")) - assertTrue(regex.containsMatchIn("a12b")) - assertTrue(regex.containsMatchIn("a123b")) - assertFalse(regex.containsMatchIn("a1234b")) - assertFalse(regex.containsMatchIn("a12345b")) - assertFalse(regex.containsMatchIn("a 1 b")) - assertFalse(regex.containsMatchIn("a b")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testBetweenMinMaxTimes() { - val regex = regex { - letter() - digit(Between(minimum = 2, maximum = 4)) - letter() - } - - assertEquals("\\p{L}\\d{2,4}\\p{L}", regex.toString()) - assertFalse(regex.containsMatchIn("ab")) - assertFalse(regex.containsMatchIn("a1b")) - assertTrue(regex.containsMatchIn("a12b")) - assertTrue(regex.containsMatchIn("a123b")) - assertTrue(regex.containsMatchIn("a1234b")) - assertFalse(regex.containsMatchIn("a12345b")) - assertFalse(regex.containsMatchIn("a 1 b")) - assertFalse(regex.containsMatchIn("a b")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testOptionMultiLine() { - val regex = regex(MULTILINE) { - startOfString() - text("find me!") - endOfString() - } - - assertEquals("^find me!$", regex.toString()) - assertTrue(regex.containsMatchIn("find me!")) - assertTrue(regex.containsMatchIn("find me!\nline 2")) - assertTrue(regex.containsMatchIn("line 1\nfind me!")) - assertTrue(regex.containsMatchIn("line 1\nfind me!\nline 3")) - assertFalse(regex.containsMatchIn(" find me!")) - assertFalse(regex.containsMatchIn("find me! ")) - assertFalse(regex.containsMatchIn(" find me! ")) - assertFalse(regex.containsMatchIn("")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testOptionIgnoreCase() { - val regex = regex(IGNORE_CASE) { - anyCharacterFrom("cat") - } - - assertEquals("[cat]", regex.toString()) - assertTrue(regex.containsMatchIn("cat")) - assertTrue(regex.containsMatchIn("tiger")) - assertTrue(regex.containsMatchIn("Ant")) - assertTrue(regex.containsMatchIn("CAT")) - assertTrue(regex.containsMatchIn(" A")) - assertFalse(regex.containsMatchIn("dog")) - assertFalse(regex.containsMatchIn(" ")) - assertFalse(regex.containsMatchIn("")) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testEmailAddress() { - // Very basic e-mail address checker! - val regex = regex { - startOfString() - nonWhitespace(AtLeast(2)) - text("@") - nonWhitespace(AtLeast(2)) - text(".") - nonWhitespace(AtLeast(2)) - endOfString() - } - - assertEquals("^\\S{2,}@\\S{2,}\\.\\S{2,}$", regex.toString()) - assertTrue(regex.containsMatchIn("test.user@mainwave.co.uk")) - assertTrue(regex.containsMatchIn("aa@bb.cc")) - assertTrue(regex.containsMatchIn("__@__.__")) - assertTrue(regex.containsMatchIn("..@.....")) - assertFalse(regex.containsMatchIn("aa@bb.c")) - assertFalse(regex.containsMatchIn("aa@b.cc")) - assertFalse(regex.containsMatchIn("a@bb.cc")) - assertFalse(regex.containsMatchIn("a@b.c")) - assertFalse(regex.containsMatchIn(" @ . ")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testUrl() { - // Very basic URL checker! - val regex = regex { - text("http") - text("s", ZeroOrOne) - text("://") - nonWhitespace(OneOrMore) - anyCharacterFrom("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/") // Valid last characters - } - - assertEquals("http(?:s)?://\\S+[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/]", regex.toString()) - assertTrue(regex.containsMatchIn("http://www.mainwave.co.uk")) - assertTrue(regex.containsMatchIn("https://www.mainwave.co.uk")) - assertFalse(regex.containsMatchIn("www.mainwave.co.uk")) - assertFalse(regex.containsMatchIn("ftp://www.mainwave.co.uk")) - - var match = regex.find("Go to http://www.mainwave.co.uk. Then click the link.") - assertNotNull(match) - assertEquals("http://www.mainwave.co.uk", match!!.value) - - match = regex.find("Go to https://www.mainwave.co.uk/test/, then click the link.") - assertNotNull(match) - assertEquals("https://www.mainwave.co.uk/test/", match!!.value) - - match = regex.find("Go to 'http://www.mainwave.co.uk' then click the link.") - assertNotNull(match) - assertEquals("http://www.mainwave.co.uk", match!!.value) - - match = regex.find("Go to \"http://www.mainwave.co.uk\" then click the link.") - assertNotNull(match) - assertEquals("http://www.mainwave.co.uk", match!!.value) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertFalse(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testIp4Address() { - // Very basic IPv4 address checker! - // (doesn't check values are in range, for example) - val regex = regex { - startOfString() - group(Exactly(3)) { - digit(Between(1, 3)) - text(".") - } - digit(Between(1, 3)) - endOfString() - } - - assertEquals("^(\\d{1,3}\\.){3}\\d{1,3}$", regex.toString()) - assertTrue(regex.containsMatchIn("10.1.1.100")) - assertTrue(regex.containsMatchIn("1.1.1.1")) - assertTrue(regex.containsMatchIn("0.0.0.0")) - assertTrue(regex.containsMatchIn("255.255.255.255")) - assertTrue(regex.containsMatchIn("999.999.999.999")) - assertFalse(regex.containsMatchIn("1.1.1.")) - assertFalse(regex.containsMatchIn("1.1.1.")) - assertFalse(regex.containsMatchIn("1.1.1.1.")) - assertFalse(regex.containsMatchIn("1.1.1.1.1")) - assertFalse(regex.containsMatchIn("1.1.1.1000")) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(DECIMAL_DIGITS)) - assertFalse(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertFalse(regex.containsMatchIn(IPV6_ADDRESS)) - assertFalse(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testZeroOrMoreButAsFewAsPossible() { - val regex = regex { - digit(ZeroOrMore.butAsFewAsPossible) - } - - assertEquals("\\d*?", regex.toString()) - val nonGreedyMatch = regex.find("999") - assertNotNull(nonGreedyMatch) - assertEquals("", nonGreedyMatch!!.value) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertTrue(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testOneOrMoreButAsFewAsPossible() { - val regex = regex { - digit(OneOrMore.butAsFewAsPossible) - } - - assertEquals("\\d+?", regex.toString()) - val nonGreedyMatch = regex.find("999") - assertNotNull(nonGreedyMatch) - assertEquals("9", nonGreedyMatch!!.value) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testAtLeastButAsFewAsPossible() { - val regex = regex { - digit(AtLeast(1).butAsFewAsPossible) - } - - assertEquals("\\d{1,}?", regex.toString()) - val nonGreedyMatch = regex.find("999") - assertNotNull(nonGreedyMatch) - assertEquals("9", nonGreedyMatch!!.value) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testBetweenButAsFewAsPossible() { - val regex = regex { - digit(Between(2, 100).butAsFewAsPossible) - } - - assertEquals("\\d{2,100}?", regex.toString()) - val nonGreedyMatch = regex.find("999") - assertNotNull(nonGreedyMatch) - assertEquals("99", nonGreedyMatch!!.value) - - assertFalse(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertFalse(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertFalse(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertFalse(regex.containsMatchIn(SYMBOLS)) - assertFalse(regex.containsMatchIn(WHITE_SPACE)) - assertFalse(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertFalse(regex.containsMatchIn(EMPTY)) - assertFalse(regex.containsMatchIn(SIMPLE_NAME)) - assertFalse(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertFalse(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testNoMoreThanButAsFewAsPossible() { - val regex = regex { - digit(NoMoreThan(2).butAsFewAsPossible) - } - - assertEquals("\\d{0,2}?", regex.toString()) - val nonGreedyMatch = regex.find("999") - assertNotNull(nonGreedyMatch) - assertEquals("", nonGreedyMatch!!.value) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertTrue(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } - - @Test - fun testZeroOrOneButAsFewAsPossible() { - val regex = regex { - digit(ZeroOrOne.butAsFewAsPossible) - } - - assertEquals("\\d??", regex.toString()) - val nonGreedyMatch = regex.find("999") - assertNotNull(nonGreedyMatch) - assertEquals("", nonGreedyMatch!!.value) - - assertTrue(regex.containsMatchIn(BOTH_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_LATIN_ALPHABET)) - assertTrue(regex.containsMatchIn(BOTH_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(UPPER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(LOWER_CASE_EXTENDED_ALPHABET)) - assertTrue(regex.containsMatchIn(DECIMAL_DIGITS)) - assertTrue(regex.containsMatchIn(BOTH_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(UPPER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(LOWER_CASE_HEX_DIGITS)) - assertTrue(regex.containsMatchIn(SYMBOLS)) - assertTrue(regex.containsMatchIn(WHITE_SPACE)) - assertTrue(regex.containsMatchIn(CONTROL_CHARACTERS)) - assertTrue(regex.containsMatchIn(EMPTY)) - assertTrue(regex.containsMatchIn(SIMPLE_NAME)) - assertTrue(regex.containsMatchIn(SIMPLE_EMAIL_ADDRESS)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTP_URL)) - assertTrue(regex.containsMatchIn(SIMPLE_HTTPS_URL)) - assertTrue(regex.containsMatchIn(IPV4_ADDRESS)) - assertTrue(regex.containsMatchIn(IPV6_ADDRESS)) - assertTrue(regex.containsMatchIn(MAC_ADDRESS)) - } -} \ No newline at end of file diff --git a/src/test/kotlin/RegexBuilderLoggingTest.kt b/src/test/kotlin/RegexBuilderLoggingTest.kt deleted file mode 100644 index c0159ef..0000000 --- a/src/test/kotlin/RegexBuilderLoggingTest.kt +++ /dev/null @@ -1,590 +0,0 @@ -package uk.co.mainwave.regextoolboxkotlin - -import io.mockk.mockk -import io.mockk.verify -import org.junit.Assert.assertEquals -import org.junit.Before -import org.junit.Test -import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier - -class RegexBuilderLoggingTest { - private var logOutput: String = "" - private lateinit var regexBuilder: RegexBuilder - - @Before - fun setUp() { - logOutput = "" - regexBuilder = RegexBuilder().addLogger { message -> - logOutput = message - } - } - - @Test - fun testAddLoggerInterface() { - val logger = mockk(relaxUnitFun = true) - - RegexBuilder() - .addLogger(logger) - .text("Hello") - .whitespace(RegexQuantifier.OneOrMore) - .text("world", RegexQuantifier.ZeroOrOne) - .buildRegex() - - verify { logger.log("RegexBuilder: text(\"Hello\"): Hello") } - verify { logger.log("RegexBuilder: whitespace(OneOrMore): \\s+") } - verify { logger.log("RegexBuilder: text(\"world\", ZeroOrOne): (?:world)?") } - verify { logger.log("RegexBuilder: buildRegex(): Hello\\s+(?:world)?") } - } - - @Test - fun testAddLoggerInterfaceWithTag() { - val logger = mockk(relaxUnitFun = true) - - RegexBuilder() - .addLogger("TEST", logger) - .text("Hello") - .whitespace(RegexQuantifier.OneOrMore) - .text("world", RegexQuantifier.ZeroOrOne) - .buildRegex() - - verify { logger.log("TEST: text(\"Hello\"): Hello") } - verify { logger.log("TEST: whitespace(OneOrMore): \\s+") } - verify { logger.log("TEST: text(\"world\", ZeroOrOne): (?:world)?") } - verify { logger.log("TEST: buildRegex(): Hello\\s+(?:world)?") } - } - - @Test - fun testAddLoggerLambda() { - val list = mutableListOf() - - RegexBuilder() - .addLogger { - list.add(it) - } - .text("Hello") - .whitespace(RegexQuantifier.OneOrMore) - .text("world", RegexQuantifier.ZeroOrOne) - .buildRegex() - - assertEquals(4, list.size) - assertEquals("RegexBuilder: text(\"Hello\"): Hello", list[0]) - assertEquals("RegexBuilder: whitespace(OneOrMore): \\s+", list[1]) - assertEquals("RegexBuilder: text(\"world\", ZeroOrOne): (?:world)?", list[2]) - assertEquals("RegexBuilder: buildRegex(): Hello\\s+(?:world)?", list[3]) - } - - @Test - fun testAddLoggerLambdaWithTag() { - val list = mutableListOf() - - RegexBuilder() - .addLogger("TEST") { - list.add(it) - } - .text("Hello") - .whitespace(RegexQuantifier.OneOrMore) - .text("world", RegexQuantifier.ZeroOrOne) - .buildRegex() - - assertEquals(4, list.size) - assertEquals("TEST: text(\"Hello\"): Hello", list[0]) - assertEquals("TEST: whitespace(OneOrMore): \\s+", list[1]) - assertEquals("TEST: text(\"world\", ZeroOrOne): (?:world)?", list[2]) - assertEquals("TEST: buildRegex(): Hello\\s+(?:world)?", list[3]) - } - - @Test - fun testText() { - regexBuilder.text("[a-z]") - assertEquals("RegexBuilder: text(\"[a-z]\"): \\[a-z\\]", logOutput) - } - - - @Test - fun testTextWithQuantifier() { - regexBuilder.text("[a-z]", RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: text(\"[a-z]\", ZeroOrMore): (?:\\[a-z\\])*", logOutput) - } - - @Test - fun testRegexText() { - regexBuilder.regexText("[a-z]") - assertEquals("RegexBuilder: regexText(\"[a-z]\"): [a-z]", logOutput) - } - - @Test - fun testRegexTextWithQuantifier() { - regexBuilder.regexText("[a-z]", RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: regexText(\"[a-z]\", ZeroOrMore): (?:[a-z])*", logOutput) - } - - @Test - fun testAnyCharacter() { - regexBuilder.anyCharacter() - assertEquals("RegexBuilder: anyCharacter(): .", logOutput) - } - - @Test - fun testAnyCharacterWithQuantifier() { - regexBuilder.anyCharacter(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: anyCharacter(ZeroOrMore): .*", logOutput) - } - - @Test - fun testWhitespace() { - regexBuilder.whitespace() - assertEquals("RegexBuilder: whitespace(): \\s", logOutput) - } - - @Test - fun testWhitespaceWithQuantifier() { - regexBuilder.whitespace(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: whitespace(ZeroOrMore): \\s*", logOutput) - } - - @Test - fun testNonWhitespace() { - regexBuilder.nonWhitespace() - assertEquals("RegexBuilder: nonWhitespace(): \\S", logOutput) - } - - @Test - fun testNonWhitespaceWithQuantifier() { - regexBuilder.nonWhitespace(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: nonWhitespace(ZeroOrMore): \\S*", logOutput) - } - - @Test - fun testPossibleWhitespace() { - regexBuilder.possibleWhitespace() - assertEquals("RegexBuilder: possibleWhitespace(): \\s*", logOutput) - } - - @Test - fun testSpace() { - regexBuilder.space() - assertEquals("RegexBuilder: space(): ", logOutput) - } - - @Test - fun testSpaceWithQuantifier() { - regexBuilder.space(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: space(ZeroOrMore): *", logOutput) - } - - @Test - fun testTab() { - regexBuilder.tab() - assertEquals("RegexBuilder: tab(): \\t", logOutput) - } - - @Test - fun testTabWithQuantifier() { - regexBuilder.tab(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: tab(ZeroOrMore): \\t*", logOutput) - } - - @Test - fun testLineFeed() { - regexBuilder.lineFeed() - assertEquals("RegexBuilder: lineFeed(): \\n", logOutput) - } - - @Test - fun testLineFeedWithQuantifier() { - regexBuilder.lineFeed(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: lineFeed(ZeroOrMore): \\n*", logOutput) - } - - @Test - fun testCarriageReturn() { - regexBuilder.carriageReturn() - assertEquals("RegexBuilder: carriageReturn(): \\r", logOutput) - } - - @Test - fun testCarriageReturnWithQuantifier() { - regexBuilder.carriageReturn(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: carriageReturn(ZeroOrMore): \\r*", logOutput) - } - - @Test - fun testDigit() { - regexBuilder.digit() - assertEquals("RegexBuilder: digit(): \\d", logOutput) - } - - @Test - fun testDigitWithQuantifier() { - regexBuilder.digit(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: digit(ZeroOrMore): \\d*", logOutput) - } - - @Test - fun testNonDigit() { - regexBuilder.nonDigit() - assertEquals("RegexBuilder: nonDigit(): \\D", logOutput) - } - - @Test - fun testNonDigitWithQuantifier() { - regexBuilder.nonDigit(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: nonDigit(ZeroOrMore): \\D*", logOutput) - } - - @Test - fun testLetter() { - regexBuilder.letter() - assertEquals("RegexBuilder: letter(): \\p{L}", logOutput) - } - - @Test - fun testLetterWithQuantifier() { - regexBuilder.letter(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: letter(ZeroOrMore): \\p{L}*", logOutput) - } - - @Test - fun testNonLetter() { - regexBuilder.nonLetter() - assertEquals("RegexBuilder: nonLetter(): \\P{L}", logOutput) - } - - @Test - fun testNonLetterWithQuantifier() { - regexBuilder.nonLetter(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: nonLetter(ZeroOrMore): \\P{L}*", logOutput) - } - - @Test - fun testUppercaseLetter() { - regexBuilder.uppercaseLetter() - assertEquals("RegexBuilder: uppercaseLetter(): \\p{Lu}", logOutput) - } - - @Test - fun testUppercaseLetterWithQuantifier() { - regexBuilder.uppercaseLetter(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: uppercaseLetter(ZeroOrMore): \\p{Lu}*", logOutput) - } - - @Test - fun testLowercaseLetter() { - regexBuilder.lowercaseLetter() - assertEquals("RegexBuilder: lowercaseLetter(): \\p{Ll}", logOutput) - } - - @Test - fun testLowercaseLetterWithQuantifier() { - regexBuilder.lowercaseLetter(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: lowercaseLetter(ZeroOrMore): \\p{Ll}*", logOutput) - } - - @Test - fun testLetterOrDigit() { - regexBuilder.letterOrDigit() - assertEquals("RegexBuilder: letterOrDigit(): [\\p{L}0-9]", logOutput) - } - - @Test - fun testLetterOrDigitWithQuantifier() { - regexBuilder.letterOrDigit(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: letterOrDigit(ZeroOrMore): [\\p{L}0-9]*", logOutput) - } - - @Test - fun testNonLetterOrDigit() { - regexBuilder.nonLetterOrDigit() - assertEquals("RegexBuilder: nonLetterOrDigit(): [^\\p{L}0-9]", logOutput) - } - - @Test - fun testNonLetterOrDigitWithQuantifier() { - regexBuilder.nonLetterOrDigit(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: nonLetterOrDigit(ZeroOrMore): [^\\p{L}0-9]*", logOutput) - } - - @Test - fun testHexDigit() { - regexBuilder.hexDigit() - assertEquals("RegexBuilder: hexDigit(): [0-9A-Fa-f]", logOutput) - } - - @Test - fun testHexDigitWithQuantifier() { - regexBuilder.hexDigit(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: hexDigit(ZeroOrMore): [0-9A-Fa-f]*", logOutput) - } - - @Test - fun testUppercaseHexDigit() { - regexBuilder.uppercaseHexDigit() - assertEquals("RegexBuilder: uppercaseHexDigit(): [0-9A-F]", logOutput) - } - - @Test - fun testUppercaseHexDigitWithQuantifier() { - regexBuilder.uppercaseHexDigit(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: uppercaseHexDigit(ZeroOrMore): [0-9A-F]*", logOutput) - } - - @Test - fun testLowercaseHexDigit() { - regexBuilder.lowercaseHexDigit() - assertEquals("RegexBuilder: lowercaseHexDigit(): [0-9a-f]", logOutput) - } - - @Test - fun testLowercaseHexDigitWithQuantifier() { - regexBuilder.lowercaseHexDigit(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: lowercaseHexDigit(ZeroOrMore): [0-9a-f]*", logOutput) - } - - @Test - fun testNonHexDigit() { - regexBuilder.nonHexDigit() - assertEquals("RegexBuilder: nonHexDigit(): [^0-9A-Fa-f]", logOutput) - } - - @Test - fun testNonHexDigitWithQuantifier() { - regexBuilder.nonHexDigit(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: nonHexDigit(ZeroOrMore): [^0-9A-Fa-f]*", logOutput) - } - - @Test - fun testWordCharacter() { - regexBuilder.wordCharacter() - assertEquals("RegexBuilder: wordCharacter(): [\\p{L}0-9_]", logOutput) - } - - @Test - fun testWordCharacterWithQuantifier() { - regexBuilder.wordCharacter(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: wordCharacter(ZeroOrMore): [\\p{L}0-9_]*", logOutput) - } - - @Test - fun testNonWordCharacter() { - regexBuilder.nonWordCharacter() - assertEquals("RegexBuilder: nonWordCharacter(): [^\\p{L}0-9_]", logOutput) - } - - @Test - fun testNonWordCharacterWithQuantifier() { - regexBuilder.nonWordCharacter(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: nonWordCharacter(ZeroOrMore): [^\\p{L}0-9_]*", logOutput) - } - - @Test - fun testAnyCharacterFrom() { - regexBuilder.anyCharacterFrom("abc") - assertEquals("RegexBuilder: anyCharacterFrom(\"abc\"): [abc]", logOutput) - } - - @Test - fun testAnyCharacterFromWithQuantifier() { - regexBuilder.anyCharacterFrom("abc", RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: anyCharacterFrom(\"abc\", ZeroOrMore): [abc]*", logOutput) - } - - @Test - fun testAnyCharacterExcept() { - regexBuilder.anyCharacterExcept("abc") - assertEquals("RegexBuilder: anyCharacterExcept(\"abc\"): [^abc]", logOutput) - } - - @Test - fun testAnyCharacterExceptWithQuantifier() { - regexBuilder.anyCharacterExcept("abc", RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: anyCharacterExcept(\"abc\", ZeroOrMore): [^abc]*", logOutput) - } - - @Test - fun testAnyOfEmpty() { - regexBuilder.anyOf(emptyList()) - assertEquals("RegexBuilder: anyOf(): strings list is empty, so doing nothing", logOutput) - } - - @Test - fun testAnyOfEmptyWithQuantifier() { - regexBuilder.anyOf(emptyList(), RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: anyOf(): strings list is empty, so doing nothing", logOutput) - } - - @Test - fun testAnyOfSingle() { - regexBuilder.anyOf(listOf("abc")) - assertEquals("RegexBuilder: anyOf(\"abc\"): abc", logOutput) - } - - @Test - fun testAnyOfSingleWithQuantifier() { - regexBuilder.anyOf(listOf("abc"), RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: anyOf(\"abc\", ZeroOrMore): (?:abc)*", logOutput) - } - - @Test - fun testAnyOfParamsSingle() { - regexBuilder.anyOf("abc") - assertEquals("RegexBuilder: anyOf(\"abc\"): abc", logOutput) - } - - @Test - fun testAnyOfMultiple() { - regexBuilder.anyOf(listOf("abc", "def")) - assertEquals("RegexBuilder: anyOf(\"abc\", \"def\"): (?:abc|def)", logOutput) - } - - @Test - fun testAnyOfMultipleWithQuantifier() { - regexBuilder.anyOf(listOf("abc", "def"), RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: anyOf(\"abc\", \"def\", ZeroOrMore): (?:abc|def)*", logOutput) - } - - @Test - fun testAnyOfParamsMultiple() { - regexBuilder.anyOf("abc", "def") - assertEquals("RegexBuilder: anyOf(\"abc\", \"def\"): (?:abc|def)", logOutput) - } - - @Test - fun testStartOfString() { - regexBuilder.startOfString() - assertEquals("RegexBuilder: startOfString(): ^", logOutput) - } - - @Test - fun testEndOfString() { - regexBuilder.endOfString() - assertEquals("RegexBuilder: endOfString(): $", logOutput) - } - - @Test - fun testWordBoundary() { - regexBuilder.wordBoundary() - assertEquals("RegexBuilder: wordBoundary(): \\b", logOutput) - } - - @Test - fun testStartGroup() { - regexBuilder.startGroup() - assertEquals("RegexBuilder: startGroup(): (", logOutput) - } - - @Test - fun testStartNonCapturingGroup() { - regexBuilder.startNonCapturingGroup() - assertEquals("RegexBuilder: startNonCapturingGroup(): (?:", logOutput) - } - - @Test - fun testNamedGroup() { - regexBuilder.startNamedGroup("bert") - assertEquals("RegexBuilder: startNamedGroup(\"bert\"): (?", logOutput) - } - - @Test - fun testEndGroup() { - regexBuilder.startGroup().endGroup() - assertEquals("RegexBuilder: endGroup(): )", logOutput) - } - - @Test - fun testQuantifierZeroOrMore() { - regexBuilder.anyCharacter(RegexQuantifier.ZeroOrMore) - assertEquals("RegexBuilder: anyCharacter(ZeroOrMore): .*", logOutput) - } - - @Test - fun testQuantifierZeroOrMoreButAsFewAsPossible() { - regexBuilder.anyCharacter(RegexQuantifier.ZeroOrMore.butAsFewAsPossible) - assertEquals("RegexBuilder: anyCharacter(ZeroOrMore.butAsFewAsPossible): .*?", logOutput) - } - - @Test - fun testQuantifierOneOrMore() { - regexBuilder.anyCharacter(RegexQuantifier.OneOrMore) - assertEquals("RegexBuilder: anyCharacter(OneOrMore): .+", logOutput) - } - - @Test - fun testQuantifierOneOrMoreButAsFewAsPossible() { - regexBuilder.anyCharacter(RegexQuantifier.OneOrMore.butAsFewAsPossible) - assertEquals("RegexBuilder: anyCharacter(OneOrMore.butAsFewAsPossible): .+?", logOutput) - } - - @Test - fun testQuantifierZeroOrOne() { - regexBuilder.anyCharacter(RegexQuantifier.ZeroOrOne) - assertEquals("RegexBuilder: anyCharacter(ZeroOrOne): .?", logOutput) - } - - @Test - fun testQuantifierZeroOrOneButAsFewAsPossible() { - regexBuilder.anyCharacter(RegexQuantifier.ZeroOrOne.butAsFewAsPossible) - assertEquals("RegexBuilder: anyCharacter(ZeroOrOne.butAsFewAsPossible): .??", logOutput) - } - - @Test - fun testQuantifierExactly() { - regexBuilder.anyCharacter(RegexQuantifier.Exactly(10)) - assertEquals("RegexBuilder: anyCharacter(Exactly(10)): .{10}", logOutput) - } - - @Test - fun testQuantifierAtLeast() { - regexBuilder.anyCharacter(RegexQuantifier.AtLeast(10)) - assertEquals("RegexBuilder: anyCharacter(AtLeast(10)): .{10,}", logOutput) - } - - @Test - fun testQuantifierAtLeastButAsFewAsPossible() { - regexBuilder.anyCharacter(RegexQuantifier.AtLeast(10).butAsFewAsPossible) - assertEquals("RegexBuilder: anyCharacter(AtLeast(10).butAsFewAsPossible): .{10,}?", logOutput) - } - - @Test - fun testQuantifierNoMoreThan() { - regexBuilder.anyCharacter(RegexQuantifier.NoMoreThan(10)) - assertEquals("RegexBuilder: anyCharacter(NoMoreThan(10)): .{0,10}", logOutput) - } - - @Test - fun testQuantifierNoMoreThanButAsFewAsPossible() { - regexBuilder.anyCharacter(RegexQuantifier.NoMoreThan(10).butAsFewAsPossible) - assertEquals("RegexBuilder: anyCharacter(NoMoreThan(10).butAsFewAsPossible): .{0,10}?", logOutput) - } - - @Test - fun testQuantifierBetween() { - regexBuilder.anyCharacter(RegexQuantifier.Between(10, 20)) - assertEquals("RegexBuilder: anyCharacter(Between(10, 20)): .{10,20}", logOutput) - } - - @Test - fun testQuantifierBetweenButAsFewAsPossible() { - regexBuilder.anyCharacter(RegexQuantifier.Between(10, 20).butAsFewAsPossible) - assertEquals("RegexBuilder: anyCharacter(Between(10, 20).butAsFewAsPossible): .{10,20}?", logOutput) - } - - @Test - fun testBuildRegex() { - regexBuilder - .text("hello") - .whitespace(RegexQuantifier.OneOrMore) - .text("world") - .buildRegex() - assertEquals("RegexBuilder: buildRegex(): hello\\s+world", logOutput) - } - - @Test - fun testBuildRegexWithOptions() { - regexBuilder - .text("hello") - .whitespace(RegexQuantifier.OneOrMore) - .text("world") - .buildRegex(RegexOptions.MULTILINE, RegexOptions.IGNORE_CASE) - assertEquals("RegexBuilder: buildRegex(): hello\\s+world", logOutput) - } -} \ No newline at end of file diff --git a/src/test/kotlin/RegexBuilderTest.kt b/src/test/kotlin/RegexBuilderTest.kt index b8a1d2f..7c5c32a 100644 --- a/src/test/kotlin/RegexBuilderTest.kt +++ b/src/test/kotlin/RegexBuilderTest.kt @@ -20,9 +20,9 @@ import java.util.regex.Pattern class RegexBuilderTest { @Test fun testBuildPattern() { - val pattern = RegexBuilder() - .text("cat") - .buildPattern(IGNORE_CASE, MULTILINE) + val pattern = pattern(IGNORE_CASE, MULTILINE) { + text("cat") + } assertEquals("cat", pattern.toString()) assertEquals(Pattern.CASE_INSENSITIVE, pattern.flags() and Pattern.CASE_INSENSITIVE) @@ -31,9 +31,9 @@ class RegexBuilderTest { @Test fun testSimpleText() { - val regex = RegexBuilder() - .text("cat") - .buildRegex() + val regex = regex { + text("cat") + } assertEquals("cat", regex.toString()) assertTrue(regex.matches("cat")) @@ -66,9 +66,9 @@ class RegexBuilderTest { @Test fun testSimpleTextWithQuantifier() { - val regex = RegexBuilder() - .text("cat", Exactly(2)) - .buildRegex() + val regex = regex { + text("cat", Exactly(2)) + } assertEquals("(?:cat){2}", regex.toString()) assertFalse(regex.containsMatchIn("cat")) @@ -103,9 +103,9 @@ class RegexBuilderTest { @Test fun testSimpleTextCaseInsensitive() { - val regex = RegexBuilder() - .text("cat") - .buildRegex(IGNORE_CASE) + val regex = regex(IGNORE_CASE) { + text("cat") + } assertEquals("cat", regex.toString()) assertTrue(regex.matches("cat")) @@ -138,9 +138,9 @@ class RegexBuilderTest { @Test fun testSimpleTextWithRegexCharacters() { - val regex = RegexBuilder() - .text("\\.+*?[]{}()|^$") - .buildRegex() + val regex = regex { + text("\\.+*?[]{}()|^$") + } assertEquals("\\\\\\.\\+\\*\\?\\[\\]\\{\\}\\(\\)\\|\\^\\$", regex.toString()) assertTrue(regex.containsMatchIn("\\.+*?[]{}()|^$")) @@ -170,9 +170,9 @@ class RegexBuilderTest { @Test fun testRegexText() { - val regex = RegexBuilder() - .regexText("^\\scat\\b") - .buildRegex() + val regex = regex { + regexText("^\\scat\\b") + } assertEquals("^\\scat\\b", regex.toString()) assertTrue(regex.containsMatchIn(" cat")) @@ -208,9 +208,9 @@ class RegexBuilderTest { @Test fun testAnyCharacter() { - val regex = RegexBuilder() - .anyCharacter() - .buildRegex() + val regex = regex { + anyCharacter() + } assertEquals(".", regex.toString()) assertTrue(regex.containsMatchIn(" ")) @@ -245,9 +245,9 @@ class RegexBuilderTest { @Test fun testWhitespace() { - val regex = RegexBuilder() - .whitespace() - .buildRegex() + val regex = regex { + whitespace() + } assertEquals("\\s", regex.toString()) assertTrue(regex.containsMatchIn(" ")) @@ -284,9 +284,9 @@ class RegexBuilderTest { @Test fun testNonWhitespace() { - val regex = RegexBuilder() - .nonWhitespace() - .buildRegex() + val regex = regex { + nonWhitespace() + } assertEquals("\\S", regex.toString()) assertTrue(regex.containsMatchIn("a")) @@ -326,11 +326,11 @@ class RegexBuilderTest { @Test fun testPossibleWhitespace() { - val regex = RegexBuilder() - .nonWhitespace() - .possibleWhitespace() - .nonWhitespace() - .buildRegex() + val regex = regex { + nonWhitespace() + possibleWhitespace() + nonWhitespace() + } assertEquals("\\S\\s*\\S", regex.toString()) assertFalse(regex.containsMatchIn("1")) @@ -366,9 +366,9 @@ class RegexBuilderTest { @Test fun testSpace() { - val regex = RegexBuilder() - .space() - .buildRegex() + val regex = regex { + space() + } assertEquals(" ", regex.toString()) assertTrue(regex.containsMatchIn(" ")) @@ -405,9 +405,9 @@ class RegexBuilderTest { @Test fun testTab() { - val regex = RegexBuilder() - .tab() - .buildRegex() + val regex = regex { + tab() + } assertEquals("\\t", regex.toString()) assertFalse(regex.containsMatchIn(" ")) @@ -444,9 +444,9 @@ class RegexBuilderTest { @Test fun testLineFeed() { - val regex = RegexBuilder() - .lineFeed() - .buildRegex() + val regex = regex { + lineFeed() + } assertEquals("\\n", regex.toString()) assertFalse(regex.containsMatchIn(" ")) @@ -483,9 +483,9 @@ class RegexBuilderTest { @Test fun testCarriageReturn() { - val regex = RegexBuilder() - .carriageReturn() - .buildRegex() + val regex = regex { + carriageReturn() + } assertEquals("\\r", regex.toString()) assertFalse(regex.containsMatchIn(" ")) @@ -522,9 +522,9 @@ class RegexBuilderTest { @Test fun testDigit() { - val regex = RegexBuilder() - .digit() - .buildRegex() + val regex = regex { + digit() + } assertEquals("\\d", regex.toString()) assertTrue(regex.containsMatchIn("1")) @@ -560,9 +560,9 @@ class RegexBuilderTest { @Test fun testNonDigit() { - val regex = RegexBuilder() - .nonDigit() - .buildRegex() + val regex = regex { + nonDigit() + } assertEquals("\\D", regex.toString()) assertTrue(regex.containsMatchIn(" 1")) @@ -599,9 +599,9 @@ class RegexBuilderTest { @Test fun testLetter() { - val regex = RegexBuilder() - .letter() - .buildRegex() + val regex = regex { + letter() + } assertEquals("\\p{L}", regex.toString()) assertTrue(regex.containsMatchIn("a")) @@ -638,9 +638,9 @@ class RegexBuilderTest { @Test fun testNonLetter() { - val regex = RegexBuilder() - .nonLetter() - .buildRegex() + val regex = regex { + nonLetter() + } assertEquals("\\P{L}", regex.toString()) assertTrue(regex.containsMatchIn(" 1")) @@ -677,9 +677,9 @@ class RegexBuilderTest { @Test fun testUppercaseLetter() { - val regex = RegexBuilder() - .uppercaseLetter() - .buildRegex() + val regex = regex { + uppercaseLetter() + } assertEquals("\\p{Lu}", regex.toString()) assertTrue(regex.containsMatchIn("A")) @@ -716,9 +716,9 @@ class RegexBuilderTest { @Test fun testLowercaseLetter() { - val regex = RegexBuilder() - .lowercaseLetter() - .buildRegex() + val regex = regex { + lowercaseLetter() + } assertEquals("\\p{Ll}", regex.toString()) assertTrue(regex.containsMatchIn("a")) @@ -755,9 +755,9 @@ class RegexBuilderTest { @Test fun testLetterOrDigit() { - val regex = RegexBuilder() - .letterOrDigit() - .buildRegex() + val regex = regex { + letterOrDigit() + } assertEquals("[\\p{L}0-9]", regex.toString()) assertTrue(regex.containsMatchIn("A")) @@ -795,9 +795,9 @@ class RegexBuilderTest { @Test fun testNonLetterOrDigit() { - val regex = RegexBuilder() - .nonLetterOrDigit() - .buildRegex() + val regex = regex { + nonLetterOrDigit() + } assertEquals("[^\\p{L}0-9]", regex.toString()) assertFalse(regex.containsMatchIn("A")) @@ -835,9 +835,9 @@ class RegexBuilderTest { @Test fun testHexDigit() { - val regex = RegexBuilder() - .hexDigit() - .buildRegex() + val regex = regex { + hexDigit() + } assertEquals("[0-9A-Fa-f]", regex.toString()) assertTrue(regex.containsMatchIn("A")) @@ -875,9 +875,9 @@ class RegexBuilderTest { @Test fun testUppercaseHexDigit() { - val regex = RegexBuilder() - .uppercaseHexDigit() - .buildRegex() + val regex = regex { + uppercaseHexDigit() + } assertEquals("[0-9A-F]", regex.toString()) assertTrue(regex.containsMatchIn("A")) @@ -915,9 +915,9 @@ class RegexBuilderTest { @Test fun testLowercaseHexDigit() { - val regex = RegexBuilder() - .lowercaseHexDigit() - .buildRegex() + val regex = regex { + lowercaseHexDigit() + } assertEquals("[0-9a-f]", regex.toString()) assertFalse(regex.containsMatchIn("A")) @@ -955,9 +955,9 @@ class RegexBuilderTest { @Test fun testNonHexDigit() { - val regex = RegexBuilder() - .nonHexDigit() - .buildRegex() + val regex = regex { + nonHexDigit() + } assertEquals("[^0-9A-Fa-f]", regex.toString()) assertFalse(regex.containsMatchIn("A")) @@ -995,9 +995,9 @@ class RegexBuilderTest { @Test fun testWordCharacter() { - val regex = RegexBuilder() - .wordCharacter() - .buildRegex() + val regex = regex { + wordCharacter() + } assertEquals("[\\p{L}0-9_]", regex.toString()) assertTrue(regex.containsMatchIn("A")) @@ -1035,9 +1035,9 @@ class RegexBuilderTest { @Test fun testNonWordCharacter() { - val regex = RegexBuilder() - .nonWordCharacter() - .buildRegex() + val regex = regex { + nonWordCharacter() + } assertEquals("[^\\p{L}0-9_]", regex.toString()) assertFalse(regex.containsMatchIn("A")) @@ -1075,9 +1075,9 @@ class RegexBuilderTest { @Test fun testAnyCharacterFrom() { - val regex = RegexBuilder() - .anyCharacterFrom("cat") - .buildRegex() + val regex = regex { + anyCharacterFrom("cat") + } assertEquals("[cat]", regex.toString()) assertTrue(regex.containsMatchIn("cat")) @@ -1114,9 +1114,9 @@ class RegexBuilderTest { @Test fun testAnyCharacterFromWithCaretAtStart() { - val regex = RegexBuilder() - .anyCharacterFrom("^abc") - .buildRegex() + val regex = regex { + anyCharacterFrom("^abc") + } assertEquals("[\\^abc]", regex.toString()) assertTrue(regex.containsMatchIn("jazz")) @@ -1153,9 +1153,9 @@ class RegexBuilderTest { @Test fun testAnyCharacterFromWithHyphen() { - val regex = RegexBuilder() - .anyCharacterFrom("a-f") - .buildRegex() + val regex = regex { + anyCharacterFrom("a-f") + } assertEquals("[a\\-f]", regex.toString()) assertTrue(regex.containsMatchIn("a")) @@ -1166,9 +1166,9 @@ class RegexBuilderTest { @Test fun testAnyCharacterFromWithCaretNotAtStart() { - val regex = RegexBuilder() - .anyCharacterFrom("a^bc") - .buildRegex() + val regex = regex { + anyCharacterFrom("a^bc") + } assertEquals("[a^bc]", regex.toString()) assertTrue(regex.containsMatchIn("jazz")) @@ -1205,9 +1205,9 @@ class RegexBuilderTest { @Test fun testAnyCharacterExcept() { - val regex = RegexBuilder() - .anyCharacterExcept("cat") - .buildRegex() + val regex = regex { + anyCharacterExcept("cat") + } assertEquals("[^cat]", regex.toString()) assertFalse(regex.containsMatchIn("cat")) @@ -1245,9 +1245,9 @@ class RegexBuilderTest { @Test fun testAnyOf() { - val regex = RegexBuilder() - .anyOf(listOf("cat", "dog", "|")) - .buildRegex() + val regex = regex { + anyOf(listOf("cat", "dog", "|")) + } assertEquals("(?:cat|dog|\\|)", regex.toString()) assertFalse(regex.containsMatchIn("ca do")) @@ -1280,9 +1280,9 @@ class RegexBuilderTest { @Test fun testAnyOfWithQuantifier() { - val regex = RegexBuilder() - .anyOf(listOf("cat", "dog", "|"), Exactly(2)) - .buildRegex() + val regex = regex { + anyOf(listOf("cat", "dog", "|"), Exactly(2)) + } assertEquals("(?:cat|dog|\\|){2}", regex.toString()) assertTrue(regex.containsMatchIn("catdog")) @@ -1322,9 +1322,9 @@ class RegexBuilderTest { @Test fun testVarargAnyOf() { - val regex = RegexBuilder() - .anyOf("cat", "dog", "|") - .buildRegex() + val regex = regex { + anyOf("cat", "dog", "|") + } assertEquals("(?:cat|dog|\\|)", regex.toString()) assertFalse(regex.containsMatchIn("ca do")) @@ -1357,9 +1357,9 @@ class RegexBuilderTest { @Test fun testVarargAnyOfWithQuantifier() { - val regex = RegexBuilder() - .anyOf("cat", "dog", "|", quantifier = Exactly(2)) - .buildRegex() + val regex = regex { + anyOf("cat", "dog", "|", quantifier = Exactly(2)) + } assertEquals("(?:cat|dog|\\|){2}", regex.toString()) assertTrue(regex.containsMatchIn("catdog")) @@ -1399,13 +1399,13 @@ class RegexBuilderTest { @Test fun testAnyOfEmptyOrSingle() { - val anyOfEmptyRegex = RegexBuilder() - .anyOf(emptyList()) - .buildRegex() + val anyOfEmptyRegex = regex { + anyOf(emptyList()) + } - val anyOfSingleRegex = RegexBuilder() - .anyOf(listOf("cat")) - .buildRegex() + val anyOfSingleRegex = regex { + anyOf(listOf("cat")) + } assertEquals("", anyOfEmptyRegex.toString()) assertEquals("cat", anyOfSingleRegex.toString()) @@ -1413,10 +1413,10 @@ class RegexBuilderTest { @Test fun testStartOfString() { - val regex = RegexBuilder() - .startOfString() - .text("a") - .buildRegex() + val regex = regex { + startOfString() + text("a") + } assertEquals("^a", regex.toString()) assertTrue(regex.containsMatchIn("a")) @@ -1454,10 +1454,10 @@ class RegexBuilderTest { @Test fun testEndOfString() { - val regex = RegexBuilder() - .text("z") - .endOfString() - .buildRegex() + val regex = regex { + text("z") + endOfString() + } assertEquals("z$", regex.toString()) assertTrue(regex.containsMatchIn("z")) @@ -1495,10 +1495,10 @@ class RegexBuilderTest { @Test fun testWordBoundary() { - val regex = RegexBuilder() - .text("a") - .wordBoundary() - .buildRegex() + val regex = regex { + text("a") + wordBoundary() + } assertEquals("a\\b", regex.toString()) assertTrue(regex.containsMatchIn("a")) @@ -1538,13 +1538,13 @@ class RegexBuilderTest { @Test fun testSingleGroup() { - val regex = RegexBuilder() - .anyCharacter(ZeroOrMore) - .startGroup() - .letter() - .digit() - .endGroup() - .buildRegex() + val regex = regex { + anyCharacter(ZeroOrMore) + group { + letter() + digit() + } + } assertEquals(".*(\\p{L}\\d)", regex.toString()) @@ -1591,12 +1591,12 @@ class RegexBuilderTest { @Test fun testRepeatGroup() { - val regex = RegexBuilder() - .startGroup() - .letter() - .digit() - .endGroup() - .buildRegex() + val regex = regex { + group { + letter() + digit() + } + } assertEquals("(\\p{L}\\d)", regex.toString()) @@ -1636,13 +1636,13 @@ class RegexBuilderTest { @Test fun testNamedGroup() { - val regex = RegexBuilder() - .lowercaseLetter(OneOrMore) - .startNamedGroup("test123") - .digit(OneOrMore) - .endGroup() - .lowercaseLetter(OneOrMore) - .buildRegex() + val regex = regex { + lowercaseLetter(OneOrMore) + namedGroup("test123") { + digit(OneOrMore) + } + lowercaseLetter(OneOrMore) + } assertEquals("\\p{Ll}+(?\\d+)\\p{Ll}+", regex.toString()) @@ -1677,13 +1677,13 @@ class RegexBuilderTest { @Test fun testNonCapturingGroup() { - val regex = RegexBuilder() - .lowercaseLetter(OneOrMore) - .startNonCapturingGroup() - .digit(OneOrMore) - .endGroup() - .lowercaseLetter(OneOrMore) - .buildRegex() + val regex = regex { + lowercaseLetter(OneOrMore) + nonCapturingGroup { + digit(OneOrMore) + } + lowercaseLetter(OneOrMore) + } assertEquals("\\p{Ll}+(?:\\d+)\\p{Ll}+", regex.toString()) @@ -1717,15 +1717,15 @@ class RegexBuilderTest { @Test fun testMultipleGroups() { - val regex = RegexBuilder() - .startGroup() - .anyCharacter(ZeroOrMore) - .endGroup() - .startGroup() - .letter() - .digit() - .endGroup() - .buildRegex() + val regex = regex { + group { + anyCharacter(ZeroOrMore) + } + group { + letter() + digit() + } + } assertEquals("(.*)(\\p{L}\\d)", regex.toString()) @@ -1775,16 +1775,16 @@ class RegexBuilderTest { @Test fun testNestedGroups() { - val regex = RegexBuilder() - .anyCharacter() // Omit first character from groups - .startGroup() - .anyCharacter(ZeroOrMore) - .startGroup() - .letter() - .digit() - .endGroup() - .endGroup() - .buildRegex() + val regex = regex { + anyCharacter() // Omit first character from groups + group { + anyCharacter(ZeroOrMore) + group { + letter() + digit() + } + } + } assertEquals(".(.*(\\p{L}\\d))", regex.toString()) @@ -1834,11 +1834,11 @@ class RegexBuilderTest { @Test fun testZeroOrMore() { - val regex = RegexBuilder() - .letter() - .digit(ZeroOrMore) - .letter() - .buildRegex() + val regex = regex { + letter() + digit(ZeroOrMore) + letter() + } assertEquals("\\p{L}\\d*\\p{L}", regex.toString()) assertTrue(regex.containsMatchIn("ab")) @@ -1874,11 +1874,11 @@ class RegexBuilderTest { @Test fun testOneOrMore() { - val regex = RegexBuilder() - .letter() - .digit(OneOrMore) - .letter() - .buildRegex() + val regex = regex { + letter() + digit(OneOrMore) + letter() + } assertEquals("\\p{L}\\d+\\p{L}", regex.toString()) assertFalse(regex.containsMatchIn("ab")) @@ -1914,11 +1914,11 @@ class RegexBuilderTest { @Test fun testZeroOrOne() { - val regex = RegexBuilder() - .letter() - .digit(ZeroOrOne) - .letter() - .buildRegex() + val regex = regex { + letter() + digit(ZeroOrOne) + letter() + } assertEquals("\\p{L}\\d?\\p{L}", regex.toString()) assertTrue(regex.containsMatchIn("ab")) @@ -1954,11 +1954,11 @@ class RegexBuilderTest { @Test fun testExactlyNTimes() { - val regex = RegexBuilder() - .letter() - .digit(Exactly(3)) - .letter() - .buildRegex() + val regex = regex { + letter() + digit(Exactly(3)) + letter() + } assertEquals("\\p{L}\\d{3}\\p{L}", regex.toString()) assertFalse(regex.containsMatchIn("ab")) @@ -1997,11 +1997,11 @@ class RegexBuilderTest { @Test fun testAtLeastQuantifier() { - val regex = RegexBuilder() - .letter() - .digit(AtLeast(3)) - .letter() - .buildRegex() + val regex = regex { + letter() + digit(AtLeast(3)) + letter() + } assertEquals("\\p{L}\\d{3,}\\p{L}", regex.toString()) assertFalse(regex.containsMatchIn("ab")) @@ -2040,11 +2040,11 @@ class RegexBuilderTest { @Test fun testNoMoreThanQuantifier() { - val regex = RegexBuilder() - .letter() - .digit(NoMoreThan(3)) - .letter() - .buildRegex() + val regex = regex { + letter() + digit(NoMoreThan(3)) + letter() + } assertEquals("\\p{L}\\d{0,3}\\p{L}", regex.toString()) assertTrue(regex.containsMatchIn("ab")) @@ -2083,11 +2083,11 @@ class RegexBuilderTest { @Test fun testBetweenMinMaxTimes() { - val regex = RegexBuilder() - .letter() - .digit(Between(2, 4)) - .letter() - .buildRegex() + val regex = regex { + letter() + digit(Between(minimum = 2, maximum = 4)) + letter() + } assertEquals("\\p{L}\\d{2,4}\\p{L}", regex.toString()) assertFalse(regex.containsMatchIn("ab")) @@ -2126,11 +2126,11 @@ class RegexBuilderTest { @Test fun testOptionMultiLine() { - val regex = RegexBuilder() - .startOfString() - .text("find me!") - .endOfString() - .buildRegex(MULTILINE) + val regex = regex(MULTILINE) { + startOfString() + text("find me!") + endOfString() + } assertEquals("^find me!$", regex.toString()) assertTrue(regex.containsMatchIn("find me!")) @@ -2167,9 +2167,9 @@ class RegexBuilderTest { @Test fun testOptionIgnoreCase() { - val regex = RegexBuilder() - .anyCharacterFrom("cat") - .buildRegex(IGNORE_CASE) + val regex = regex(IGNORE_CASE) { + anyCharacterFrom("cat") + } assertEquals("[cat]", regex.toString()) assertTrue(regex.containsMatchIn("cat")) @@ -2207,15 +2207,15 @@ class RegexBuilderTest { @Test fun testEmailAddress() { // Very basic e-mail address checker! - val regex = RegexBuilder() - .startOfString() - .nonWhitespace(AtLeast(2)) - .text("@") - .nonWhitespace(AtLeast(2)) - .text(".") - .nonWhitespace(AtLeast(2)) - .endOfString() - .buildRegex() + val regex = regex { + startOfString() + nonWhitespace(AtLeast(2)) + text("@") + nonWhitespace(AtLeast(2)) + text(".") + nonWhitespace(AtLeast(2)) + endOfString() + } assertEquals("^\\S{2,}@\\S{2,}\\.\\S{2,}$", regex.toString()) assertTrue(regex.containsMatchIn("test.user@mainwave.co.uk")) @@ -2254,13 +2254,13 @@ class RegexBuilderTest { @Test fun testUrl() { // Very basic URL checker! - val regex = RegexBuilder() - .text("http") - .text("s", ZeroOrOne) - .text("://") - .nonWhitespace(OneOrMore) - .anyCharacterFrom("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/") // Valid last characters - .buildRegex() + val regex = regex { + text("http") + text("s", ZeroOrOne) + text("://") + nonWhitespace(OneOrMore) + anyCharacterFrom("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/") // Valid last characters + } assertEquals("http(?:s)?://\\S+[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/]", regex.toString()) assertTrue(regex.containsMatchIn("http://www.mainwave.co.uk")) @@ -2311,15 +2311,15 @@ class RegexBuilderTest { fun testIp4Address() { // Very basic IPv4 address checker! // (doesn't check values are in range, for example) - val regex = RegexBuilder() - .startOfString() - .startGroup() - .digit(Between(1, 3)) - .text(".") - .endGroup(Exactly(3)) - .digit(Between(1, 3)) - .endOfString() - .buildRegex() + val regex = regex { + startOfString() + group(Exactly(3)) { + digit(Between(1, 3)) + text(".") + } + digit(Between(1, 3)) + endOfString() + } assertEquals("^(\\d{1,3}\\.){3}\\d{1,3}$", regex.toString()) assertTrue(regex.containsMatchIn("10.1.1.100")) @@ -2356,34 +2356,11 @@ class RegexBuilderTest { assertFalse(regex.containsMatchIn(MAC_ADDRESS)) } - @Test(expected = RegexBuilderException::class) - fun testExceptionGroupMismatch1() { - RegexBuilder() - .endGroup() - .buildRegex() - } - - @Test(expected = RegexBuilderException::class) - fun testExceptionGroupMismatch2() { - RegexBuilder() - .startGroup() - .buildRegex() - } - - @Test(expected = RegexBuilderException::class) - fun testExceptionGroupMismatch3() { - RegexBuilder() - .startGroup() - .startGroup() - .endGroup() - .buildRegex() - } - @Test fun testZeroOrMoreButAsFewAsPossible() { - val regex = RegexBuilder() - .digit(ZeroOrMore.butAsFewAsPossible) - .buildRegex() + val regex = regex { + digit(ZeroOrMore.butAsFewAsPossible) + } assertEquals("\\d*?", regex.toString()) val nonGreedyMatch = regex.find("999") @@ -2415,9 +2392,9 @@ class RegexBuilderTest { @Test fun testOneOrMoreButAsFewAsPossible() { - val regex = RegexBuilder() - .digit(OneOrMore.butAsFewAsPossible) - .buildRegex() + val regex = regex { + digit(OneOrMore.butAsFewAsPossible) + } assertEquals("\\d+?", regex.toString()) val nonGreedyMatch = regex.find("999") @@ -2449,9 +2426,9 @@ class RegexBuilderTest { @Test fun testAtLeastButAsFewAsPossible() { - val regex = RegexBuilder() - .digit(AtLeast(1).butAsFewAsPossible) - .buildRegex() + val regex = regex { + digit(AtLeast(1).butAsFewAsPossible) + } assertEquals("\\d{1,}?", regex.toString()) val nonGreedyMatch = regex.find("999") @@ -2483,9 +2460,9 @@ class RegexBuilderTest { @Test fun testBetweenButAsFewAsPossible() { - val regex = RegexBuilder() - .digit(Between(2, 100).butAsFewAsPossible) - .buildRegex() + val regex = regex { + digit(Between(2, 100).butAsFewAsPossible) + } assertEquals("\\d{2,100}?", regex.toString()) val nonGreedyMatch = regex.find("999") @@ -2517,9 +2494,9 @@ class RegexBuilderTest { @Test fun testNoMoreThanButAsFewAsPossible() { - val regex = RegexBuilder() - .digit(NoMoreThan(2).butAsFewAsPossible) - .buildRegex() + val regex = regex { + digit(NoMoreThan(2).butAsFewAsPossible) + } assertEquals("\\d{0,2}?", regex.toString()) val nonGreedyMatch = regex.find("999") @@ -2551,9 +2528,9 @@ class RegexBuilderTest { @Test fun testZeroOrOneButAsFewAsPossible() { - val regex = RegexBuilder() - .digit(ZeroOrOne.butAsFewAsPossible) - .buildRegex() + val regex = regex { + digit(ZeroOrOne.butAsFewAsPossible) + } assertEquals("\\d??", regex.toString()) val nonGreedyMatch = regex.find("999")