-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1821 from robstoll/feature/extractors-for-SQLExce…
…ption Feature/extractors for sql exception
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...nt/src/jvmMain/kotlin/ch/tutteli/atrium/api/fluent/en_GB/sqlExceptionFeatureExtractors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
79 changes: 79 additions & 0 deletions
79
.../kotlin/ch/tutteli/atrium/api/fluent/en_GB/samples/SQLExceptionFeatureExtractorSamples.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |