Skip to content

Commit

Permalink
Merge pull request #1821 from robstoll/feature/extractors-for-SQLExce…
Browse files Browse the repository at this point in the history
…ption

Feature/extractors for sql exception
  • Loading branch information
robstoll authored Aug 23, 2024
2 parents a06cdcb + 879bb7c commit 80ea307
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
7 changes: 7 additions & 0 deletions apis/fluent/atrium-api-fluent/api/main/atrium-api-fluent.api
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ch.tutteli.atrium.api.fluent.en_GB

import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.creating.FeatureExpect
import java.sql.SQLException

/**
* Creates an [Expect] for the result of calling `getErrorCode()` on the subject of `this` expectation,
* so that further fluent calls are assertions about it.
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.SQLExceptionFeaturesSamples.errorCodeFeature
*
* @since 1.3.0
*/
val <T : SQLException> Expect<T>.errorCode: FeatureExpect<T, Int>
get() = feature("errorCode", SQLException::getErrorCode)

/**
* Expects that the result of calling `getErrorCode()` on the subject of `this` expectation
* holds all assertions the given [assertionCreator] creates for it and
* returns an [Expect] for the current subject of `this` expectation.
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.SQLExceptionFeaturesSamples.sqlStateFeature
*
* @since 1.3.0
*/
fun <T : SQLException> Expect<T>.errorCode(assertionCreator: Expect<Int>.() -> Unit) =
feature("errorCode", SQLException::getErrorCode, assertionCreator)

/**
* Creates an [Expect] for the result of calling `getSQLState()` on the subject of `this` expectation,
* so that further fluent calls are assertions about it.
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.SQLExceptionFeaturesSamples.errorCode
*
* @since 1.3.0
*/
val <T : SQLException> Expect<T>.sqlState: FeatureExpect<T, String>
get() = feature("sqlState", SQLException::getSQLState)

/**
* Expects that the result of calling `getSQLState()` on the subject of `this` expectation
* holds all assertions the given [assertionCreator] creates for it and
* returns an [Expect] for the current subject of `this` expectation.
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.SQLExceptionFeaturesSamples.sqlState
*
* @since 1.3.0
*/
fun <T : SQLException> Expect<T>.sqlState(assertionCreator: Expect<String>.() -> Unit) =
feature("sqlState", SQLException::getSQLState, assertionCreator)
Original file line number Diff line number Diff line change
@@ -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
// `.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
}
}
}
}

0 comments on commit 80ea307

Please sign in to comment.