Skip to content

Commit

Permalink
New should spec #6
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Apr 15, 2016
1 parent fff1aca commit 7078a02
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/io/kotlintest/KTestJUnitRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class KTestJUnitRunner(testClass: Class<TestBase>) : Runner() {
}

private fun descriptionForSuite(suite: TestSuite): Description? {
val desc = Description.createSuiteDescription(suite.name, counter.andIncrement)
val desc = Description.createSuiteDescription(suite.name.replace(".", " "), counter.andIncrement)
suite.suites.forEach { suite ->
desc.addChild(descriptionForSuite(suite))
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/kotlin/io/kotlintest/specs/ShouldSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.kotlintest.specs

import io.kotlintest.TestBase
import io.kotlintest.TestCase
import io.kotlintest.TestSuite

abstract class ShouldSpec : TestBase() {

var current = root

operator fun String.invoke(init: () -> Unit): Unit {
val suite = TestSuite.empty(this)
current.suites.add(suite)
val temp = current
current = suite
init()
current = temp
}

fun should(name: String, test: () -> Unit): Unit {
current.cases.add(TestCase(name, test))
}
}
4 changes: 1 addition & 3 deletions src/main/kotlin/io/kotlintest/testcase.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.kotlintest

import java.util.*

data class TestSuite(val name: String, val suites: MutableList<TestSuite>, val cases: MutableList<io.kotlintest.TestCase>) {
companion object {
fun empty(name: String) = TestSuite(name, ArrayList<TestSuite>(), ArrayList<io.kotlintest.TestCase>())
fun empty(name: String) = TestSuite(name, mutableListOf<TestSuite>(), mutableListOf<io.kotlintest.TestCase>())
}
}

Expand Down
45 changes: 45 additions & 0 deletions src/test/kotlin/io/kotlintest/ShouldSpecTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.kotlintest

import io.kotlintest.specs.ShouldSpec

class ShouldSpecTest : ShouldSpec() {

init {

// should allow nested
"List" {
"pop" {
should("should remove the last element from stack") {
val stack = ListStack<String>()
stack.push("hello")
stack.push("world")
stack.size() shouldBe 2
stack.pop() shouldBe "world"
stack.size() shouldBe 1
}
}
}

// should allow nested
"List.pop" {
should("should remove the last element from stack") {
val stack = ListStack<String>()
stack.push("hello")
stack.push("world")
stack.size() shouldBe 2
stack.pop() shouldBe "world"
stack.size() shouldBe 1
}
}

// and un-nested
should("should leave the stack unmodified") {
val stack = ListStack<String>()
stack.push("hello")
stack.push("world")
stack.size() shouldBe 2
stack.peek() shouldBe "world"
stack.size() shouldBe 2
}
}
}

0 comments on commit 7078a02

Please sign in to comment.