Skip to content

Commit 1a7aa3b

Browse files
committed
refactor: result and exc will be saved in statement
1 parent 8c6de29 commit 1a7aa3b

File tree

6 files changed

+79
-29
lines changed

6 files changed

+79
-29
lines changed

randunit-android/src/main/java/com/williamfzc/randunit/exceptions/RUInstanceException.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717

1818
package com.williamfzc.randunit.exceptions
1919

20-
class RUInstanceException(reason: String) : RUException(reason)
20+
class RUInstanceException(reason: String) : RUException(reason)

randunit/src/main/kotlin/com/williamfzc/randunit/env/AbstractTestEnv.kt

+8-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import com.williamfzc.randunit.env.strategy.AbstractStrategy
2020
import com.williamfzc.randunit.models.MockModel
2121
import com.williamfzc.randunit.models.StatementModel
2222

23-
// env was designed for running inside something like TestCase/TestSuite
24-
// which can be used by different runner
25-
// it manages:
26-
// - statement model (s)
27-
// - about how to gen runnable statement from statement model
23+
/*
24+
package `env` contains multiple parts
25+
- env: used by junit directly, managing everything, making them work together
26+
- strategy: statement model -> statement s
27+
- statement: a ready-to-run statement, which contains some real values
28+
- sandbox: actual statement runner, invoking and suppressing some specific errors provided by rules
29+
- rules: check if this exception should be thrown
30+
*/
2831
abstract class AbstractTestEnv @JvmOverloads constructor(var envConfig: EnvConfig = EnvConfig()) {
2932
var mockModel = MockModel(envConfig.mockConfig)
3033
private val strategy: AbstractStrategy by lazy {

randunit/src/main/kotlin/com/williamfzc/randunit/env/NormalTestEnv.kt

+8-18
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package com.williamfzc.randunit.env
1717

18-
import com.williamfzc.randunit.exceptions.RUTypeException
19-
import java.lang.reflect.Method
2018
import java.util.logging.Logger
2119

2220
open class NormalTestEnv @JvmOverloads constructor(envConfig: EnvConfig = EnvConfig()) :
@@ -30,23 +28,15 @@ open class NormalTestEnv @JvmOverloads constructor(envConfig: EnvConfig = EnvCon
3028
override fun runStatement(statement: Statement) {
3129
logger.info("running: ${statement.model.getDesc()}")
3230

33-
val returnValueOfInvoke = synchronized(statement) {
34-
try {
35-
statement.invoke()
36-
} finally {
37-
statement.model.callerOperation.tearDown(statement.caller)
38-
}
39-
}
31+
synchronized(statement) {
32+
statement.invoke()
33+
statement.model.callerOperation.tearDown(statement.caller)
4034

41-
returnValueOfInvoke?.let { v ->
42-
verifyReturnValue(v, statement.method)
43-
}
44-
}
35+
// handled by sandbox
36+
if (statement.isErrorHappened())
37+
throw statement.throwExc!!
4538

46-
private fun verifyReturnValue(v: Any, method: Method) {
47-
val retType = v.javaClass
48-
val shouldBe = method.returnType.javaClass
49-
if (shouldBe.isAssignableFrom(retType))
50-
throw RUTypeException("return type $retType != $shouldBe")
39+
statement.verifyResult()
40+
}
5141
}
5242
}

randunit/src/main/kotlin/com/williamfzc/randunit/env/Statement.kt

+17-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.williamfzc.randunit.env
1717

18+
import com.williamfzc.randunit.exceptions.RUVerifyException
1819
import com.williamfzc.randunit.models.StatementModel
1920
import java.lang.reflect.Method
2021
import java.util.logging.Logger
@@ -25,14 +26,27 @@ data class Statement(
2526
val method: Method,
2627
val caller: Any,
2728
val parameters: Collection<Any?>,
28-
val model: StatementModel
29+
val model: StatementModel,
30+
val verifyResult: (Statement) -> String?
2931
) {
3032
companion object {
3133
private val logger = Logger.getGlobal()
3234
}
3335

36+
var executedResult: Any? = null
37+
var throwExc: Throwable? = null
38+
3439
fun invoke(): Any? {
35-
method.isAccessible = true
36-
return method.invoke(caller, *parameters.toTypedArray())
40+
try {
41+
method.isAccessible = true
42+
executedResult = method.invoke(caller, *parameters.toTypedArray())
43+
} catch (e: Throwable) {
44+
throwExc = e
45+
}
46+
return executedResult
3747
}
48+
49+
fun isErrorHappened() = (null != throwExc)
50+
51+
fun verifyResult() = verifyResult(this)?.let { throw RUVerifyException(it) }
3852
}

randunit/src/main/kotlin/com/williamfzc/randunit/env/strategy/NormalStrategy.kt

+25-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,25 @@ import com.williamfzc.randunit.models.StatementModel
2222
import java.lang.Exception
2323
import java.util.logging.Logger
2424

25+
fun verifyFunc(s: Statement): String? {
26+
s.executedResult?.let {
27+
val retType = it.javaClass
28+
val shouldBe = s.method.returnType.javaClass
29+
if (shouldBe.isAssignableFrom(retType))
30+
return "return type $retType != $shouldBe"
31+
}
32+
33+
// can not verify
34+
return null
35+
}
36+
2537
object NormalStrategy : AbstractStrategy() {
2638
private val logger = Logger.getGlobal()
2739

28-
override fun genStatements(statementModel: StatementModel, mockModel: MockModel): Iterable<Statement> {
40+
override fun genStatements(
41+
statementModel: StatementModel,
42+
mockModel: MockModel
43+
): Iterable<Statement> {
2944
// at least it will not cause any crashes because of mock
3045
var caller = generateCaller(statementModel)
3146
caller ?: run {
@@ -51,7 +66,15 @@ object NormalStrategy : AbstractStrategy() {
5166
}
5267

5368
// by default, gen only one statement
54-
return setOf(Statement(statementModel.method, caller!!, parameters, statementModel))
69+
return setOf(
70+
Statement(
71+
statementModel.method,
72+
caller!!,
73+
parameters,
74+
statementModel,
75+
::verifyFunc
76+
)
77+
)
5578
}
5679

5780
private fun generateCaller(statementModel: StatementModel): Any? {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2021 williamfzc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.williamfzc.randunit.exceptions
19+
20+
class RUVerifyException(reason: String) : RUException(reason)

0 commit comments

Comments
 (0)