From d4aad2d9663c4940eedbe3f00befd8ae725f3b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sat, 7 Feb 2026 20:47:17 +0100 Subject: [PATCH] feat(suite): Introduce Ignored.If and OnlyIf for conditional ignoring tests --- suite/src/commonMain/kotlin/config/Ignored.kt | 84 ++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/suite/src/commonMain/kotlin/config/Ignored.kt b/suite/src/commonMain/kotlin/config/Ignored.kt index 2d91739..9cfdd27 100644 --- a/suite/src/commonMain/kotlin/config/Ignored.kt +++ b/suite/src/commonMain/kotlin/config/Ignored.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2025, OpenSavvy and contributors. + * Copyright (c) 2023-2026, OpenSavvy and contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,7 +34,89 @@ package opensavvy.prepared.suite.config * // … * } * ``` + * + * @see Ignored.If Conditionally ignore a test or suite. */ object Ignored : TestConfig.Element, TestConfig.Key.Unique { override val key get() = this + + /** + * Marks this test or an entire suite as disabled if [predicate] returns `true`. + * + * If [predicate] returns `false`, does nothing. + * + * ### Example + * + * Mark a suite as disabled: + * + * ```kotlin + * suite("Suite name", Ignored.If { true }) { + * // … + * } + * ``` + * + * Mark a test as disabled: + * + * ```kotlin + * test("Some kind of test", Ignored.If { true }) { + * // … + * } + * ``` + * + * ### Use-case + * + * Conceptually, marking a test as ignored: + * ```kotlin + * test("Some kind of test", Ignored.If { someCondition }) { + * // … + * } + * ``` + * is similar to not declaring the test at all: + * ```kotlin + * if (!someCondition) { + * test("Some kind of test") { + * // … + * } + * } + * ``` + * + * However, some test runners (e.g., TestBalloon) crash when a suite is declared that contains no tests at all. + * + * @see Ignored Always ignore a test or suite. + * @see OnlyIf Opposite. + */ + @Suppress("FunctionName") // Looks like an 'object' on purpose: Ignored, Ignored.If {} + inline fun If(predicate: () -> Boolean): TestConfig { + return if (predicate()) Ignored else TestConfig.Empty + } } + +/** + * Executes this test or ensuite suite only if [predicate] returns `true`. + * + * Conceptually, this is the opposite of [Ignored.If]. + * + * ### Example + * + * Only execute a suite if a JVM property is enabled: + * + * ```kotlin + * suite("Suite name", OnlyIf { System.getProperty("foo") != null }) { + * // … + * } + * ``` + * + * Only execute a test if a JVM property is enabled: + * + * ```kotlin + * test("Some kind of test", OnlyIf { System.getProperty("foo") != null }) { + * // … + * } + * ``` + * + * @see Ignored Ignore a test or suite. + * @see Ignored.If Conditionally ignore a test or suite. + */ +@Suppress("FunctionName") // Similar to Ignored.If {} +inline fun OnlyIf(predicate: () -> Boolean): TestConfig = + Ignored.If { !predicate() } -- 2.51.2