Skip to content

Commit

Permalink
Refactor Verification/Assertion methods for Chains
Browse files Browse the repository at this point in the history
  • Loading branch information
bitPogo committed Apr 25, 2022
1 parent 9be81b1 commit 872aede
Show file tree
Hide file tree
Showing 42 changed files with 278 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ import tech.antibytes.kmock.example.contract.ExampleContract.SampleRemoteReposit
import tech.antibytes.kmock.example.contract.SampleDomainObjectMock
import tech.antibytes.kmock.example.contract.SampleLocalRepositoryMock
import tech.antibytes.kmock.example.contract.SampleRemoteRepositoryMock
import tech.antibytes.kmock.verification.NonFreezingAsserter
import tech.antibytes.kmock.verification.Asserter
import tech.antibytes.kmock.verification.hasBeenCalled
import tech.antibytes.kmock.verification.NonFreezingAsserter
import tech.antibytes.kmock.verification.assertOrder
import tech.antibytes.kmock.verification.hasBeenCalledWith
import tech.antibytes.kmock.verification.hasBeenCalledWithout
import tech.antibytes.kmock.verification.hasBeenStrictlyCalledWith
import tech.antibytes.kmock.verification.verify
import tech.antibytes.kmock.verification.verifyOrder
import tech.antibytes.kmock.verification.verifyStrictOrder
import tech.antibytes.kmock.verification.wasGotten
import tech.antibytes.kmock.verification.wasSet
import tech.antibytes.kmock.verification.wasSetTo
import tech.antibytes.util.test.coroutine.AsyncTestReturnValue
import tech.antibytes.util.test.coroutine.clearBlockingTest
Expand Down Expand Up @@ -105,7 +102,7 @@ class SampleControllerAutoSpyFactorySpec {
verify(exactly = 1) { remote._fetch.hasBeenStrictlyCalledWith(url) }
verify(exactly = 1) { local._store.hasBeenCalledWith() }

verifier.verifyStrictOrder {
verifier.assertOrder {
remote._fetch.hasBeenStrictlyCalledWith(url)
domainObject._id.wasGotten()
domainObject._id.wasSet()
Expand Down Expand Up @@ -154,7 +151,7 @@ class SampleControllerAutoSpyFactorySpec {
verify(exactly = 2) { local._fetch.hasBeenCalledWith() }
verify(exactly = 2) { remote._find.hasBeenStrictlyCalledWith(idOrg) }

verifier.verifyStrictOrder {
verifier.assertOrder {
local._contains.hasBeenStrictlyCalledWith(idOrg)
remote._find.hasBeenStrictlyCalledWith(idOrg)
domainObject._id.wasGotten()
Expand Down Expand Up @@ -201,7 +198,7 @@ class SampleControllerAutoSpyFactorySpec {
verify(exactly = 1) { local._fetch.hasBeenCalledWith() }
verify(exactly = 1) { remote._find.hasBeenStrictlyCalledWith(idOrg) }

verifier.verifyStrictOrder {
verifier.assertOrder {
local._contains.hasBeenStrictlyCalledWith(idOrg)
remote._find.hasBeenStrictlyCalledWith(idOrg)
domainObject._id.wasGotten()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ import tech.antibytes.kmock.example.contract.SampleDomainObjectMock
import tech.antibytes.kmock.example.contract.SampleLocalRepositoryMock
import tech.antibytes.kmock.example.contract.SampleRemoteRepositoryMock
import tech.antibytes.kmock.verification.Asserter
import tech.antibytes.kmock.verification.assertOrder
import tech.antibytes.kmock.verification.hasBeenCalledWith
import tech.antibytes.kmock.verification.hasBeenCalledWithout
import tech.antibytes.kmock.verification.hasBeenStrictlyCalledWith
import tech.antibytes.kmock.verification.verify
import tech.antibytes.kmock.verification.verifyOrder
import tech.antibytes.kmock.verification.verifyStrictOrder
import tech.antibytes.kmock.verification.wasGotten
import tech.antibytes.kmock.verification.wasSet
import tech.antibytes.kmock.verification.wasSetTo
import tech.antibytes.util.test.coroutine.AsyncTestReturnValue
import tech.antibytes.util.test.coroutine.clearBlockingTest
Expand Down Expand Up @@ -94,7 +92,7 @@ class SampleControllerAutoStubFactorySpec {
verify(exactly = 1) { remote._fetch.hasBeenStrictlyCalledWith(url) }
verify(exactly = 1) { local._store.hasBeenCalledWith(id[1]) }

verifier.verifyStrictOrder {
verifier.assertOrder {
remote._fetch.hasBeenStrictlyCalledWith(url)
domainObject._id.wasGotten()
domainObject._id.wasSet()
Expand Down Expand Up @@ -142,7 +140,7 @@ class SampleControllerAutoStubFactorySpec {
verify(exactly = 2) { local._fetch.hasBeenStrictlyCalledWith(id) }
verify(exactly = 2) { remote._find.hasBeenStrictlyCalledWith(idOrg) }

verifier.verifyStrictOrder {
verifier.assertOrder {
local._contains.hasBeenStrictlyCalledWith(idOrg)
remote._find.hasBeenStrictlyCalledWith(idOrg)
domainObject._id.wasGotten()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import tech.antibytes.kmock.example.contract.SampleDomainObjectMock
import tech.antibytes.kmock.example.contract.SampleLocalRepositoryMock
import tech.antibytes.kmock.example.contract.SampleRemoteRepositoryMock
import tech.antibytes.kmock.verification.Asserter
import tech.antibytes.kmock.verification.hasBeenCalled
import tech.antibytes.kmock.verification.assertOrder
import tech.antibytes.kmock.verification.hasBeenCalledWith
import tech.antibytes.kmock.verification.hasBeenCalledWithout
import tech.antibytes.kmock.verification.hasBeenStrictlyCalledWith
import tech.antibytes.kmock.verification.verify
import tech.antibytes.kmock.verification.verifyOrder
import tech.antibytes.kmock.verification.verifyStrictOrder
import tech.antibytes.kmock.verification.wasGotten
import tech.antibytes.kmock.verification.wasSet
import tech.antibytes.kmock.verification.wasSetTo
import tech.antibytes.util.test.coroutine.AsyncTestReturnValue
Expand Down Expand Up @@ -113,7 +111,7 @@ class SampleControllerAutoStubRelaxedSpec {
verify(exactly = 1) { remote._fetch.hasBeenStrictlyCalledWith(url) }
verify(exactly = 1) { local._store.hasBeenCalledWith(id[1]) }

verifier.verifyStrictOrder {
verifier.assertOrder {
remote._fetch.hasBeenStrictlyCalledWith(url)
domainObject._id.wasGotten()
domainObject._id.wasSet()
Expand Down Expand Up @@ -161,7 +159,7 @@ class SampleControllerAutoStubRelaxedSpec {
verify(exactly = 2) { local._fetch.hasBeenStrictlyCalledWith(id) }
verify(exactly = 2) { remote._find.hasBeenStrictlyCalledWith(idOrg) }

verifier.verifyStrictOrder {
verifier.assertOrder {
local._contains.hasBeenStrictlyCalledWith(idOrg)
remote._find.hasBeenStrictlyCalledWith(idOrg)
domainObject._id.wasGotten()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ import tech.antibytes.kmock.example.contract.SampleDomainObjectMock
import tech.antibytes.kmock.example.contract.SampleLocalRepositoryMock
import tech.antibytes.kmock.example.contract.SampleRemoteRepositoryMock
import tech.antibytes.kmock.verification.Asserter
import tech.antibytes.kmock.verification.assertOrder
import tech.antibytes.kmock.verification.hasBeenCalledWith
import tech.antibytes.kmock.verification.hasBeenCalledWithout
import tech.antibytes.kmock.verification.hasBeenStrictlyCalledWith
import tech.antibytes.kmock.verification.verify
import tech.antibytes.kmock.verification.verifyOrder
import tech.antibytes.kmock.verification.verifyStrictOrder
import tech.antibytes.kmock.verification.wasGotten
import tech.antibytes.kmock.verification.wasSet
import tech.antibytes.kmock.verification.wasSetTo
import tech.antibytes.util.test.coroutine.AsyncTestReturnValue
import tech.antibytes.util.test.coroutine.clearBlockingTest
Expand Down Expand Up @@ -96,7 +94,7 @@ class SampleControllerAutoStubSpec {
verify(exactly = 1) { remote._fetch.hasBeenStrictlyCalledWith(url) }
verify(exactly = 1) { local._store.hasBeenStrictlyCalledWith(id[1], number) }

verifier.verifyStrictOrder {
verifier.assertOrder {
remote._fetch.hasBeenStrictlyCalledWith(url)
domainObject._id.wasGotten()
domainObject._id.wasSet()
Expand Down Expand Up @@ -145,7 +143,7 @@ class SampleControllerAutoStubSpec {
verify(exactly = 2) { local._fetch.hasBeenStrictlyCalledWith(id) }
verify(exactly = 2) { remote._find.hasBeenStrictlyCalledWith(idOrg) }

verifier.verifyStrictOrder {
verifier.assertOrder {
local._contains.hasBeenStrictlyCalledWith(idOrg)
remote._find.hasBeenStrictlyCalledWith(idOrg)
domainObject._id.wasGotten()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ import tech.antibytes.kmock.example.contract.SampleDomainObjectMock
import tech.antibytes.kmock.example.contract.SampleLocalRepositoryMock
import tech.antibytes.kmock.example.contract.SampleRemoteRepositoryMock
import tech.antibytes.kmock.verification.Asserter
import tech.antibytes.kmock.verification.assertOrder
import tech.antibytes.kmock.verification.hasBeenCalledWith
import tech.antibytes.kmock.verification.hasBeenCalledWithout
import tech.antibytes.kmock.verification.hasBeenStrictlyCalledWith
import tech.antibytes.kmock.verification.verify
import tech.antibytes.kmock.verification.verifyOrder
import tech.antibytes.kmock.verification.verifyStrictOrder
import tech.antibytes.kmock.verification.wasGotten
import tech.antibytes.kmock.verification.wasSet
import tech.antibytes.kmock.verification.wasSetTo
import tech.antibytes.util.test.coroutine.AsyncTestReturnValue
import tech.antibytes.util.test.coroutine.clearBlockingTest
Expand Down Expand Up @@ -94,7 +92,7 @@ class SampleControllerAutoStubFactoryJsSpec {
verify(exactly = 1) { remote._fetch.hasBeenStrictlyCalledWith(url) }
verify(exactly = 1) { local._store.hasBeenCalledWith(id[1]) }

verifier.verifyStrictOrder {
verifier.assertOrder {
remote._fetch.hasBeenStrictlyCalledWith(url)
domainObject._id.wasGotten()
domainObject._id.wasSet()
Expand Down Expand Up @@ -141,7 +139,7 @@ class SampleControllerAutoStubFactoryJsSpec {
verify(exactly = 2) { local._fetch.hasBeenStrictlyCalledWith(id) }
verify(exactly = 2) { remote._find.hasBeenStrictlyCalledWith(idOrg) }

verifier.verifyStrictOrder {
verifier.assertOrder {
local._contains.hasBeenStrictlyCalledWith(idOrg)
remote._find.hasBeenStrictlyCalledWith(idOrg)
domainObject._id.wasGotten()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ internal object KMockGenerics : GenericResolver {

override fun mapDeclaredGenerics(
generics: Map<String, List<KSTypeReference>>,
resolver: TypeParameterResolver
typeResolver: TypeParameterResolver
): List<TypeVariableName> = generics.map { (type, bounds) ->
TypeVariableName(
type,
bounds = bounds.map { ksReference -> ksReference.resolve().toTypeName(resolver) }
bounds = bounds.map { ksReference -> ksReference.resolve().toTypeName(typeResolver) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private fun resolveReceiver(

KMockGenerics.mapDeclaredGenerics(
generics = generics!!,
resolver = resolver
typeResolver = resolver
).first()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2022 Matthias Geisler (bitPogo) / All rights reserved.
*
* Use of this source code is governed by Apache v2.0
*/

package tech.antibytes.kmock.util

import kotlin.reflect.KClass

actual fun extractKClassName(clazz: KClass<*>): String? = clazz.qualifiedName
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2022 Matthias Geisler (bitPogo) / All rights reserved.
*
* Use of this source code is governed by Apache v2.0
*/

package tech.antibytes.kmock.util

import org.junit.Test
import tech.antibytes.util.test.mustBe

actual class ClassNameExtractorSpec {
@Test
actual fun `Given extractKClassName is called it returns the of the class name`() {
extractKClassName(Any::class) mustBe Any::class.qualifiedName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ object KMockContract {
/**
*
*/
internal interface Assert : AssertionInsurance, AssertionContext
interface Assert : AssertionInsurance, AssertionContext

/**
* AssertionChain in order to verify over multiple Handles.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2022 Matthias Geisler (bitPogo) / All rights reserved.
*
* Use of this source code is governed by Apache v2.0
*/

package tech.antibytes.kmock.util

import kotlin.reflect.KClass

expect fun extractKClassName(clazz: KClass<*>): String?
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import tech.antibytes.kmock.KMockContract.GetOrSet
import tech.antibytes.kmock.verification.constraints.ArgumentConstraintWrapper.wrapNegatedValue
import tech.antibytes.kmock.verification.constraints.ArgumentConstraintWrapper.wrapValue


@Suppress("UNUSED_PARAMETER")
private fun noopClosure(argument: Any?, matcherIndex: Int): Unit = Unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ package tech.antibytes.kmock.verification

import kotlinx.atomicfu.atomic
import tech.antibytes.kmock.KMockContract
import tech.antibytes.kmock.KMockContract.Assert
import tech.antibytes.kmock.KMockContract.AssertionChain
import tech.antibytes.kmock.KMockContract.Assertions
import tech.antibytes.kmock.KMockContract.CALL_NOT_FOUND
import tech.antibytes.kmock.KMockContract.Proxy
import tech.antibytes.kmock.KMockContract.Reference
import tech.antibytes.kmock.KMockContract.CALL_NOT_FOUND
import tech.antibytes.kmock.KMockContract.STRICT_CALL_NOT_MATCH
import tech.antibytes.kmock.KMockContract.STRICT_MISSING_EXPECTATION
import tech.antibytes.kmock.KMockContract.AssertionChain
import tech.antibytes.kmock.KMockContract.Assert
import tech.antibytes.kmock.util.format

internal class AssertionChain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package tech.antibytes.kmock.verification

import tech.antibytes.kmock.KMockContract
import tech.antibytes.kmock.KMockContract.Proxy
import tech.antibytes.kmock.KMockContract.FunProxy
import tech.antibytes.kmock.KMockContract.HAD_BEEN_CALLED_NO_MATCHER
import tech.antibytes.kmock.KMockContract.ILLEGAL_VALUE
Expand All @@ -17,6 +16,7 @@ import tech.antibytes.kmock.KMockContract.MISSING_INVOCATION
import tech.antibytes.kmock.KMockContract.NON_VOID_FUNCTION
import tech.antibytes.kmock.KMockContract.NOT_GET
import tech.antibytes.kmock.KMockContract.NOT_SET
import tech.antibytes.kmock.KMockContract.Proxy
import tech.antibytes.kmock.KMockContract.VOID_FUNCTION
import tech.antibytes.kmock.util.format

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
package tech.antibytes.kmock.verification

import tech.antibytes.kmock.KMockContract
import tech.antibytes.kmock.KMockContract.Assert
import tech.antibytes.kmock.KMockContract.Asserter
import tech.antibytes.kmock.KMockContract.AssertionContext
import tech.antibytes.kmock.KMockContract.AssertionInsurance
import tech.antibytes.kmock.KMockContract.Expectation
import tech.antibytes.kmock.KMockContract.NOT_CALLED
import tech.antibytes.kmock.KMockContract.TOO_LESS_CALLS
import tech.antibytes.kmock.KMockContract.TOO_MANY_CALLS
import tech.antibytes.kmock.KMockContract.AssertionInsurance
import tech.antibytes.kmock.KMockContract.Asserter
import tech.antibytes.kmock.util.format

private fun determineAtLeastMessage(actual: Int, expected: Int): String {
Expand Down Expand Up @@ -77,61 +79,40 @@ fun verify(
}
}

private fun <T> Asserter.verifyChain(
scope: AssertionInsurance.() -> Any?,
fun <T> runAssertion(
chain: T,
) where T : AssertionInsurance, T : KMockContract.AssertionChain {
TODO()

scope: Assert.() -> Any
) where T : Assert, T : KMockContract.AssertionChain {
scope(chain)

chain.ensureAllReferencesAreEvaluated()
}

/**
* Verifies a chain of VerificationHandles. Each Handle must be in strict order of the referenced Proxy invocation
* and all invocations must be present.
* Asserts a chain of Expectations. Each Expectations must be in strict order of the referenced Proxy invocations
* and all invocations must be present.
* @param scope chain of Expectation Methods.
* @throws AssertionError if given criteria are not met.
* @see AssertionContext
* @author Matthias Geisler
*/
fun Asserter.assertOrder(scope: Assert.() -> Any) = runAssertion(AssertionChain(references), scope)

/**
* Alias of assertOrder.
* @param scope chain of Expectation Methods.
* @throws AssertionError if given criteria are not met.
* @see hasBeenCalled
* @see hasBeenCalledWithVoid
* @see hasBeenCalledWith
* @see hasBeenStrictlyCalledWith
* @see hasBeenCalledWithout
* @see wasGotten
* @see wasSet
* @see wasSetTo
* @see assertOrder
* @author Matthias Geisler
*/
fun Asserter.verifyStrictOrder(
scope: AssertionInsurance.() -> Any,
) {
verifyChain(
scope = scope,
chain = AssertionChain(references),
)
}
fun Asserter.verifyStrictOrder(scope: Assert.() -> Any) = assertOrder(scope)

/**
* Verifies a chain VerificationHandles. Each Handle must be in order but gaps are allowed.
* Verifies a chain of Expectations. Each Expectations must be in order but gaps are allowed.
* Also the chain does not need to be exhaustive.
* @param scope chain of Expectation Methods.
* @throws AssertionError if given criteria are not met.
* @see hasBeenCalled
* @see hasBeenCalledWithVoid
* @see hasBeenCalledWith
* @see hasBeenStrictlyCalledWith
* @see hasBeenCalledWithout
* @see wasGotten
* @see wasSet
* @see wasSetTo
* @see AssertionContext
* @author Matthias Geisler
*/
fun Asserter.verifyOrder(
scope: AssertionInsurance.() -> Any
) {
verifyChain(
scope = scope,
chain = VerificationChain(references),
)
}
fun Asserter.verifyOrder(scope: AssertionInsurance.() -> Any) = runAssertion(VerificationChain(references), scope)
Loading

0 comments on commit 872aede

Please sign in to comment.