Best Kotest code snippet using io.kotest.matchers.string.matchers.beUUID
matchers.kt
Source:matchers.kt
...239fun String.shouldBeUUID(240 version: UUIDVersion = ANY,241 considerNilValid: Boolean = true242): String {243 this should beUUID(version, considerNilValid)244 return this245}246/**247 * Asserts that this String is NOT a valid UUID248 *249 * Opposite of [shouldBeUUID]250 *251 * Verifies that this string is a NOT valid UUID as per RFC4122. Version depends on [version]. By default, all versions252 * (v1 through v5) are matched. A special attention is necessary for the NIL UUID (an UUID with all zeros),253 * which is considered a valid UUID. By default it's matched as valid.254 *255 * ```256 * "123e4567-e89b-12d3-a456-426655440000".shouldNotBeUUID(version = ANY) // Assertion fails257 * "123e4567e89b12d3a456426655440000".shouldNotBeUUID() // Assertion passes258 * "00000000-0000-0000-0000-000000000000".shouldNotBeUUID(considerNilValid = true) // Assertion fails259 *260 * ```261 *262 * @see [RFC4122] https://tools.ietf.org/html/rfc4122263 */264fun String.shouldNotBeUUID(265 version: UUIDVersion = ANY,266 considerNilValid: Boolean = true267): String {268 this shouldNot beUUID(version, considerNilValid)269 return this270}271/**272 * Matcher that verifies if a String is an UUID273 *274 *275 * Verifies that a string is a valid UUID as per RFC4122. Version depends on [version]. By default, all versions276 * (v1 through v5) are matched. A special attention is necessary for the NIL UUID (an UUID with all zeros),277 * which is considered a valid UUID. By default it's matched as valid.278 *279 *280 * @see [RFC4122] https://tools.ietf.org/html/rfc4122281 * @see shouldBeUUID282 * @see shouldNotBeUUID283 */284fun beUUID(285 version: UUIDVersion = ANY,286 considerNilValid: Boolean = true287) = object : Matcher<String> {288 override fun test(value: String) = MatcherResult(289 value.matches(version.uuidRegex) || (considerNilValid && value.isNilUUID()),290 { "String $value is not an UUID ($version), but should be" },291 { "String $value is an UUID ($version), but shouldn't be" })292 private fun String.isNilUUID() = this == "00000000-0000-0000-0000-000000000000"293}294fun String?.shouldBeInteger(radix: Int = 10): Int {295 contract {296 returns() implies (this@shouldBeInteger != null)297 }298 return when (this) {...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!