Skip to content

Commit

Permalink
add overload with assertion creator to asEntries
Browse files Browse the repository at this point in the history
  • Loading branch information
robstoll committed Jan 19, 2019
1 parent 14183c9 commit 95235a4
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 16 deletions.
Expand Up @@ -199,3 +199,15 @@ fun <K, V, T: Map<K, V>> Assert<T>.values(assertionCreator: Assert<Collection<V>
*/
fun <K, V> Assert<Map<K, V>>.asEntries(): Assert<Set<Map.Entry<K, V>>>
= AssertImpl.changeSubject(this) { subject.entries }

/**
* Turns `Assert<Map<K, V>>` into `Assert<Set<Map.Entry<K, V>>>` and makes the assertion that the assertions the given
* [assertionCreator] might create hold.
*
* The transformation as such is not reflected in reporting.
* Use `property(subject::entries)` if you want to show the transformation in reporting.
*
* @return The newly created [AssertionPlant] for the transformed subject.
*/
fun <K, V> Assert<Map<K, V>>.asEntries(assertionCreator: Assert<Set<Map.Entry<K, V>>>.() -> Unit): Assert<Set<Map.Entry<K, V>>>
= asEntries().addAssertionsCreatedBy(assertionCreator)
Expand Up @@ -5,6 +5,7 @@ import ch.tutteli.atrium.creating.Assert

object MapAsEntriesAssertionsSpec : ch.tutteli.atrium.spec.integration.MapAsEntriesAssertionsSpec(
AssertionVerbFactory,
Assert<Map<String, Int>>::asEntries.name,
"asEntries",
Assert<Map<String, Int>>::asEntries,
Assert<Map<String, Int>>::asEntries
)
Expand Up @@ -200,3 +200,15 @@ fun <K, V, T: Map<K, V>> Assert<T>.values(assertionCreator: Assert<Collection<V>
*/
fun <K, V> Assert<Map<K, V>>.asEntries(): Assert<Set<Map.Entry<K, V>>>
= AssertImpl.changeSubject(this) { subject.entries }

/**
* Turns `Assert<Map<K, V>>` into `Assert<Set<Map.Entry<K, V>>>` and makes the assertion that the assertions the given
* [assertionCreator] might create hold.
*
* The transformation as such is not reflected in reporting.
* Use `property(subject::entries)` if you want to show the transformation in reporting.
*
* @return The newly created [AssertionPlant] for the transformed subject.
*/
fun <K, V> Assert<Map<K, V>>.asEntries(assertionCreator: Assert<Set<Map.Entry<K, V>>>.() -> Unit): Assert<Set<Map.Entry<K, V>>>
= asEntries().addAssertionsCreatedBy(assertionCreator)
Expand Up @@ -5,6 +5,7 @@ import ch.tutteli.atrium.verbs.internal.AssertionVerbFactory

object MapAsEntriesAssertionsSpec : ch.tutteli.atrium.spec.integration.MapAsEntriesAssertionsSpec(
AssertionVerbFactory,
Assert<Map<String, Int>>::asEntries.name,
"asEntries",
Assert<Map<String, Int>>::asEntries,
Assert<Map<String, Int>>::asEntries
)
Expand Up @@ -230,3 +230,15 @@ infix fun <K, V, T: Map<K, V>> Assert<T>.values(assertionCreator: Assert<Collect
*/
fun <K, V> Assert<Map<K, V>>.asEntries(): Assert<Set<Map.Entry<K, V>>>
= AssertImpl.changeSubject(this) { subject.entries }

/**
* Turns `Assert<Map<K, V>>` into `Assert<Set<Map.Entry<K, V>>>` and makes the assertion that the assertions the given
* [assertionCreator] might create hold.
*
* The transformation as such is not reflected in reporting.
* Use `property(subject::entries)` if you want to show the transformation in reporting.
*
* @return The newly created [AssertionPlant] for the transformed subject.
*/
infix fun <K, V> Assert<Map<K, V>>.asEntries(assertionCreator: Assert<Set<Map.Entry<K, V>>>.() -> Unit): Assert<Set<Map.Entry<K, V>>>
= asEntries().addAssertionsCreatedBy(assertionCreator)
Expand Up @@ -3,8 +3,14 @@ package ch.tutteli.atrium.api.cc.infix.en_GB
import ch.tutteli.atrium.creating.Assert
import ch.tutteli.atrium.verbs.internal.AssertionVerbFactory

object MapAsEntriesAssertionsSpec : ch.tutteli.atrium.spec.integration.MapAsEntriesAssertionsSpec(
class MapAsEntriesAssertionsSpec : ch.tutteli.atrium.spec.integration.MapAsEntriesAssertionsSpec(
AssertionVerbFactory,
Assert<Map<String, Int>>::asEntries.name,
Assert<Map<String, Int>>::asEntries
)
"asEntries",
Companion::asEntries,
Companion::asEntriesWithCreator
) {
companion object {
fun asEntries(plant: Assert<Map<String, Int>>) = plant.asEntries()
fun asEntriesWithCreator(plant: Assert<Map<String, Int>>, assertionCreator: Assert<Set<Map.Entry<String, Int>>>.() -> Unit) = plant asEntries assertionCreator
}
}
13 changes: 6 additions & 7 deletions misc/atrium-bc-test/build.gradle
Expand Up @@ -301,17 +301,16 @@ createBcAndBbcTasksForApis('0.7.0',
'cc-de_CH', 'cc-en_GB', 'cc-infix-en_GB'
)

createFixSrcTask('cc-infix-en_GB', '0.7.0', 'CharSequenceAssertionsSpec.kt') { String content ->
return content.replaceAll(/"\$\{Assert<CharSequence>::notToBe.name}/, '"notToBe')
}

createFixSrcTask('cc-infix-en_GB', '0.7.0', 'BigDecimalAssertionsSpec.kt') { String content ->
return content.replaceAll(/Assert<BigDecimal>::toBe.name/, '"toBe"').replaceAll(/Assert<BigDecimal>::notToBe.name/, '"notToBe"')
}
def apis = ['cc-de_CH', 'cc-en_GB', 'cc-infix-en_GB']
apis.each {apiName ->
createFixSrcTask(apiName, '0.7.0', 'ArrayAsIterableAssertionsSpec.kt') { String content ->
return content.replaceAll(/Assert<Array<Int>>::asIterable.name/, '"asIterable"')
}
}
createFixSrcTask('cc-infix-en_GB', '0.7.0', 'BigDecimalAssertionsSpec.kt') { String content ->
return content.replaceAll(/Assert<BigDecimal>::toBe.name/, '"toBe"').replaceAll(/Assert<BigDecimal>::notToBe.name/, '"notToBe"')
}
createFixSrcTask('cc-infix-en_GB', '0.7.0', 'CharSequenceAssertionsSpec.kt') { String content ->
return content.replaceAll(/"\$\{Assert<CharSequence>::notToBe.name}/, '"notToBe')
}

Expand Up @@ -4,25 +4,30 @@ import ch.tutteli.atrium.api.cc.en_GB.*
import ch.tutteli.atrium.creating.Assert
import ch.tutteli.atrium.spec.AssertionVerbFactory
import ch.tutteli.atrium.spec.describeFun
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.SpecBody
import org.jetbrains.spek.api.include

abstract class MapAsEntriesAssertionsSpec(
verbs: AssertionVerbFactory,
asEntriesFunName: String,
asEntries: Assert<Map<String, Int>>.() -> Assert<Set<Map.Entry<String, Int>>>,
asEntriesWithCreator: Assert<Map<String, Int>>.(Assert<Set<Map.Entry<String, Int>>>.() -> Unit) -> Assert<Set<Map.Entry<String, Int>>>,
describePrefix: String = "[Atrium] "
) : IterablePredicateSpecBase(verbs, {
) : Spek({

val asEntriesWithCreatorFun = "$asEntriesFunName with Creator"
include(object : SubjectLessAssertionSpec<Map<String, Int>>("$describePrefix[$asEntriesFunName] ",
asEntriesFunName to mapToCreateAssertion { asEntries(this) }
asEntriesFunName to mapToCreateAssertion { asEntries(this) } ,
asEntriesWithCreatorFun to mapToCreateAssertion { asEntriesWithCreator(this){ contains("a" to 1)} }
) {})

val holdingSubject: Map<String, Int> = mapOf("a" to 1, "c" to 3, "e" to 5, "g" to 7)
val failingSubject: Map<String, Int> = mapOf("b" to 2, "d" to 4, "f" to 6, "h" to 8)

include(object : CheckingAssertionSpec<Map<String, Int>>(verbs, "$describePrefix[$asEntriesFunName] ",
checkingTriple(asEntriesFunName, { asEntries(this).contains { isKeyValue("g", 7) } }, holdingSubject, failingSubject)
checkingTriple(asEntriesFunName, { asEntries(this).contains { isKeyValue("g", 7) } }, holdingSubject, failingSubject),
checkingTriple(asEntriesWithCreatorFun, { asEntriesWithCreator(this){ contains { isKeyValue("g", 7) } } }, holdingSubject, failingSubject)
) {})

fun describeFun(vararg funName: String, body: SpecBody.() -> Unit) =
Expand Down

0 comments on commit 95235a4

Please sign in to comment.