diff --git a/apis/fluent/atrium-api-fluent/api/main/atrium-api-fluent.api b/apis/fluent/atrium-api-fluent/api/main/atrium-api-fluent.api index 197702e69b..bad036054c 100644 --- a/apis/fluent/atrium-api-fluent/api/main/atrium-api-fluent.api +++ b/apis/fluent/atrium-api-fluent/api/main/atrium-api-fluent.api @@ -518,6 +518,13 @@ public final class ch/tutteli/atrium/api/fluent/en_GB/SequenceSubjectChangersKt public static final fun asList (Lch/tutteli/atrium/creating/Expect;Lkotlin/jvm/functions/Function1;)Lch/tutteli/atrium/creating/Expect; } +public final class ch/tutteli/atrium/api/fluent/en_GB/SqlExceptionFeatureExtractorsKt { + public static final fun errorCode (Lch/tutteli/atrium/creating/Expect;Lkotlin/jvm/functions/Function1;)Lch/tutteli/atrium/creating/Expect; + public static final fun getErrorCode (Lch/tutteli/atrium/creating/Expect;)Lch/tutteli/atrium/creating/FeatureExpect; + public static final fun getSqlState (Lch/tutteli/atrium/creating/Expect;)Lch/tutteli/atrium/creating/FeatureExpect; + public static final fun sqlState (Lch/tutteli/atrium/creating/Expect;Lkotlin/jvm/functions/Function1;)Lch/tutteli/atrium/creating/Expect; +} + public final class ch/tutteli/atrium/api/fluent/en_GB/ThirdPartyExpectationsKt { public static final fun toHoldThirdPartyExpectation (Lch/tutteli/atrium/creating/Expect;Ljava/lang/String;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Lch/tutteli/atrium/creating/Expect; } diff --git a/apis/fluent/atrium-api-fluent/src/jvmMain/java/module-info.java b/apis/fluent/atrium-api-fluent/src/jvmMain/java/module-info.java index 35fa46e0f3..c76a373cf9 100644 --- a/apis/fluent/atrium-api-fluent/src/jvmMain/java/module-info.java +++ b/apis/fluent/atrium-api-fluent/src/jvmMain/java/module-info.java @@ -3,6 +3,7 @@ requires ch.tutteli.atrium.logic; requires kotlin.stdlib; requires ch.tutteli.kbox; + requires java.sql; exports ch.tutteli.atrium.api.fluent.en_GB; } diff --git a/apis/fluent/atrium-api-fluent/src/jvmMain/kotlin/ch/tutteli/atrium/api/fluent/en_GB/sqlExceptionFeatureExtractors.kt b/apis/fluent/atrium-api-fluent/src/jvmMain/kotlin/ch/tutteli/atrium/api/fluent/en_GB/sqlExceptionFeatureExtractors.kt new file mode 100644 index 0000000000..da2c3214ed --- /dev/null +++ b/apis/fluent/atrium-api-fluent/src/jvmMain/kotlin/ch/tutteli/atrium/api/fluent/en_GB/sqlExceptionFeatureExtractors.kt @@ -0,0 +1,41 @@ +package ch.tutteli.atrium.api.fluent.en_GB + +import ch.tutteli.atrium.creating.Expect +import ch.tutteli.atrium.creating.FeatureExpect +import java.sql.SQLException + +/** + * Extracts the [errorCode][SQLException.getErrorCode] property from the subject of the assertion. + * + * @since 1.3.0 + * @sample ch.tutteli.atrium.api.fluent.en_GB.samples.SQLExceptionFeaturesSamples.errorCodeFeature + */ +val Expect.errorCode: FeatureExpect + get() = feature("errorCode", SQLException::getErrorCode) + +/** + * Extracts the [errorCode][SQLException.getErrorCode] property from the subject of the assertion and makes it the new subject. + * + * @since 1.3.0 + * @sample ch.tutteli.atrium.api.fluent.en_GB.samples.SQLExceptionFeaturesSamples.sqlStateFeature + */ +fun Expect.errorCode(assertionCreator: Expect.() -> Unit) = + feature("errorCode", SQLException::getErrorCode, assertionCreator) + +/** + * Extracts the [sqlState][SQLException.getSQLState] property from the subject of the assertion. + * + * @since 1.3.0 + * @sample ch.tutteli.atrium.api.fluent.en_GB.samples.SQLExceptionFeaturesSamples.errorCode + */ +val Expect.sqlState: FeatureExpect + get() = feature("sqlState", SQLException::getSQLState) + +/** + * Extracts the [sqlState][SQLException.getSQLState] property from the subject of the assertion and makes it the new subject. + * + * @since 1.3.0 + * @sample ch.tutteli.atrium.api.fluent.en_GB.samples.SQLExceptionFeaturesSamples.sqlState + */ +fun Expect.sqlState(assertionCreator: Expect.() -> Unit) = + feature("sqlState", SQLException::getSQLState, assertionCreator) diff --git a/apis/fluent/atrium-api-fluent/src/jvmTest/kotlin/ch/tutteli/atrium/api/fluent/en_GB/samples/SQLExceptionFeatureExtractorSamples.kt b/apis/fluent/atrium-api-fluent/src/jvmTest/kotlin/ch/tutteli/atrium/api/fluent/en_GB/samples/SQLExceptionFeatureExtractorSamples.kt new file mode 100644 index 0000000000..658dc042cb --- /dev/null +++ b/apis/fluent/atrium-api-fluent/src/jvmTest/kotlin/ch/tutteli/atrium/api/fluent/en_GB/samples/SQLExceptionFeatureExtractorSamples.kt @@ -0,0 +1,79 @@ +package ch.tutteli.atrium.api.fluent.en_GB.samples + +import ch.tutteli.atrium.api.fluent.en_GB.* +import ch.tutteli.atrium.api.verbs.expect +import java.sql.SQLException +import kotlin.test.Test + +class SQLExceptionFeaturesSamples { + + @Test + fun errorCodeFeature() { + val exception = SQLException("Test exception", "42000", 1001) + + expect(exception) + .errorCode // subject is now of type Int + .toEqual(1001) + + fails { + expect(exception) + .errorCode // subject is now of type Int + .toEqual(9999) // fails + .toBeGreaterThan(1000) // not evaluated/reported because `toEqual` already fails + // use `.errorCode { ... }` if you want all assertions evaluated + } + } + + @Test + fun errorCode() { + val exception = SQLException("Test exception", "42000", 1001) + + expect(exception).errorCode { // subject within this expectation-group is of type Int + toBeGreaterThan(1000) + toEqual(1001) + } // subject here is back to type SQLException + + fails { + expect(exception).errorCode { + toEqual(9999) // fails + toBeGreaterThan(1000) // still evaluated even though `toEqual` already fails + } + } + } + + @Test + fun sqlStateFeature() { + val exception = SQLException("Test exception", "42000", 1001) + + expect(exception) + .sqlState // subject is now of type String + .toEqual("42000") + + fails { + expect(exception) + .sqlState // subject is now of type String + .toEqual("00000") // fails + .toContain("42") // not evaluated/reported because `toEqual` already fails + // use `.sqlState { ... }` if you want all assertions evaluated + } + } + + + + @Test + fun sqlState() { + val exception = SQLException("Test exception", "42000", 1001) + + expect(exception).sqlState { // subject within this expectation-group is of type String + toEqual("42000") + toContain("42") + } // subject here is back to type SQLException + + fails { + expect(exception).sqlState { + toEqual("00000") // fails + toContain("42") // still evaluated even though `toEqual` already fails + } + } + } +}