1
1
Fork 0

Generated Unit Test examples

This commit is contained in:
Svetlana Isakova 2020-04-13 23:45:21 +02:00
parent c843e1e6a4
commit 37d5f75e6d
13 changed files with 181 additions and 2 deletions

View File

@ -2,6 +2,8 @@ type: theory
files:
- name: src/DefiningExceptions.kt
visible: true
- name: src/Stacktrace.kt
visible: true
- name: src/Handlers.kt
visible: true
- name: src/Hierarchy.kt
@ -16,5 +18,3 @@ files:
visible: true
- name: src/CaptureImplementation.kt
visible: true
- name: src/Stacktrace.kt
visible: true

View File

@ -0,0 +1,13 @@
// UnitTesting/Learner.kt
package learner
enum class Language {
EN, FR, DE
}
data class Learner(
val id: Int,
val name: String,
val surname: String,
val lang: Language
)

View File

@ -0,0 +1,24 @@
// UnitTesting/NoFramework.kt
package unittesting
import kotlin.test.assertEquals
import kotlin.test.assertTrue
fun foo() = 42
fun allIsGood() = true
fun testFoo() {
assertEquals(
expected = 42,
actual = foo(),
message = "Wrong answer")
}
fun testAllIsGood() {
assertTrue(allIsGood(), "Wrong answer")
}
fun main() {
testFoo()
testAllIsGood()
}

View File

@ -0,0 +1,15 @@
// UnitTesting/SampleTest.kt
package unittesting
import kotlin.test.*
class SampleTest {
@Test
fun testFoo() {
expect(42, "Wrong answer") { foo() }
}
@Test
fun testAllIsGood() {
assertTrue(allIsGood(), "Wrong answer")
}
}

View File

@ -0,0 +1,29 @@
// UnitTesting/StateMachine.kt
package statemachine
import statemachine.State.*
enum class State { ON, OFF, PAUSED }
class StateMachine {
var state: State = OFF
private set
private fun updateState(
current: State, new: State
) {
if (state == current) {
state = new
}
}
fun start() = updateState(OFF, ON)
fun pause() = updateState(ON, PAUSED)
fun resume() = updateState(PAUSED, ON)
fun finish() {
state = OFF
}
}

View File

@ -0,0 +1,31 @@
// UnitTesting/TestStateMachine.kt
package statemachine
import kotlin.test.*
class TestStateMachine {
private lateinit var sm: StateMachine // [1]
@BeforeTest
fun initStateMachine() {
sm = StateMachine() // [2]
}
@Test
fun `test start`() {
sm.start()
assertEquals(State.ON, sm.state)
}
@Test
fun `test pause and resume`() {
sm.start()
sm.pause()
assertEquals(State.PAUSED, sm.state)
sm.resume()
assertEquals(State.ON, sm.state)
sm.pause()
assertEquals(State.PAUSED, sm.state)
}
// ...
}

View File

@ -0,0 +1,24 @@
// UnitTesting/TestingLearner.kt
package learner
import learner.Language.*
import kotlin.test.Test
fun createLearner(
id: Int,
lang: Language = EN, // [1]
name: String = "Test Name $id",
surname: String = "Test Surname $id"
) = Learner(id, name, surname, lang)
class TestingLearner {
@Test
fun testLearners() {
// TODO replace with member reference
// (1..9).map(::createLearner)
val learners = (1..9).map {
createLearner(it)
}
val learner = createLearner(10, FR)
// use learners in tests
}
}

View File

@ -0,0 +1,11 @@
// UnitTesting/UsingExpect.kt
package unittesting
import kotlin.test.*
fun testFoo2() = expect(42, "Wrong answer") {
foo()
}
fun main() {
testFoo2()
}

View File

@ -0,0 +1,16 @@
type: theory
files:
- name: src/Learner.kt
visible: true
- name: src/TestingLearner.kt
visible: true
- name: src/StateMachine.kt
visible: true
- name: src/UsingExpect.kt
visible: true
- name: src/NoFramework.kt
visible: true
- name: src/TestStateMachine.kt
visible: true
- name: src/SampleTest.kt
visible: true

View File

@ -0,0 +1,3 @@
## Unit Testing
Examples accompanying the atom.

View File

@ -0,0 +1,2 @@
content:
- Examples

View File

@ -4,3 +4,4 @@ content:
- The Nothing Type
- Resource Cleanup
- Logging
- Unit Testing

View File

@ -2121,6 +2121,16 @@ public class TestAllExamples extends AbstractTestExamples {
testExample("../AtomicKotlinCourse/Preventing Failure/Logging/Examples/src/SimpleLoggingStrategy.kt", logging.SimpleLoggingStrategyKt::main);
}
@Test
public void testNoFramework() {
testExample("../AtomicKotlinCourse/Preventing Failure/Unit Testing/Examples/src/NoFramework.kt", unittesting.NoFrameworkKt::main);
}
@Test
public void testUsingExpect() {
testExample("../AtomicKotlinCourse/Preventing Failure/Unit Testing/Examples/src/UsingExpect.kt", unittesting.UsingExpectKt::main);
}
@Test
public void testRegularLambda() {
testExample("../AtomicKotlinCourse/Power Tools/Lambda with Receiver/Examples/src/RegularLambda.kt", regularlambda.RegularLambdaKt::main);