Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactoring

  • Loading branch information...
commit ed4a919de4c686326cbe4f04207f6a2e13b46b6d 1 parent 414ef23
@danseid authored
View
23 pom.xml
@@ -8,8 +8,31 @@
<groupId>org.katchers</groupId>
<artifactId>katchers</artifactId>
<version>0.1</version>
+ <packaging>jar</packaging>
+ <name>Katchers</name>
+ <description>library of matchers for building test expressions in Kotlin</description>
+ <url>https://github.com/danseid/katchers</url>
+ <licenses>
+ <license>
+ <name>The Apache Software License, Version 2.0</name>
+ <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
+
+ <scm>
+ <url>https://github.com/danseid/katchers.git</url>
+ </scm>
+ <distributionManagement>
+ <snapshotRepository>
+ <id>danseid-cloudbees</id>
+ <name>My CloudBees Repo</name>
+ <url>http://repository-danseid.forge.cloudbees.com/snapshot/</url>
+ <uniqueVersion>false</uniqueVersion>
+ </snapshotRepository>
+ </distributionManagement>
<properties>
<kotlin.version>0.4.68</kotlin.version>
<junit.version>4.10</junit.version>
View
10 src/main/kotlin/org/katchers/AnyMatcher.kt
@@ -23,7 +23,7 @@ import kotlin.test.*
*/
trait Matcher<T> {
- open val target : T
+ open val target: T
inline protected fun notSupported(): Unit = fail("Not Supported Condition ")
}
@@ -37,5 +37,13 @@ open class AnyNotBeMatcher<T>(override val target: T): Matcher<T> {
inline fun any(values: List<T>) = if(values any { it == target }) fail("not any of $values", target)
}
+open class AnyMatchMatcher<T>(override val target: T): Matcher<T> {
+ inline fun condition(match: T.() -> Boolean) = if(!target.match()) fail()
+}
+
+open class AnyNotMatchMatcher<T>(override val target: T): Matcher<T> {
+ inline fun condition(match: T.() -> Boolean) = if(target.match()) fail()
+}
+
View
4 src/main/kotlin/org/katchers/FunctionMatcher.kt
@@ -20,7 +20,7 @@ package org.katchers
* @since 2012/11/20
*/
-class FunctionFailMatcher<R>(override val target: () -> R): Matcher<() -> R>{
+class FunctionFailMatcher<T: () -> Any>(override val target: T): Matcher<T>{
fun with(error: Throwable) {
var failed = true // assume it will fail
try {
@@ -33,7 +33,7 @@ class FunctionFailMatcher<R>(override val target: () -> R): Matcher<() -> R>{
}
}
}
-class FunctionNotFailMatcher<R>(override val target: () -> R): Matcher<() -> R>{
+class FunctionNotFailMatcher<T: () -> Any>(override val target: T): Matcher<T>{
fun with(error: Throwable) {
try {
target()
View
2  src/main/kotlin/org/katchers/HelperFunctions.kt
@@ -30,4 +30,6 @@ inline fun <T>fail(expected: T, got: T): Unit {
""")
}
+inline fun fail() = fail
+
inline fun <T>of(vararg values: T): List<T> = values.toList()
View
4 src/main/kotlin/org/katchers/Must.kt
@@ -40,5 +40,5 @@ public inline fun Number.must(val verb: NOTBE): NumberNotBeMatcher = NumberNotBe
//() -> Unit must fail
-public fun <R>jet.Function0<R>.must(verb: FAIL): FunctionFailMatcher<R> = FunctionFailMatcher(this)
-public fun <R>jet.Function0<R>.must(verb: NOTFAIL): FunctionNotFailMatcher<R> = FunctionNotFailMatcher(this)
+public fun <T: () -> Any>T.must(verb: FAIL): FunctionFailMatcher<T> = FunctionFailMatcher(this)
+public fun <T: () -> Any>T.must(verb: NOTFAIL): FunctionNotFailMatcher<T> = FunctionNotFailMatcher(this)
View
2  src/main/kotlin/org/katchers/NumberMatcher.kt
@@ -101,7 +101,7 @@ fun Number.compare(that: Number): Int {
}
return when(this){ // casting toDouble because of http://youtrack.jetbrains.com/issue/KT-3078
- is Int -> compare(this.toDouble(), that)
+ is Int -> compare(this.toDouble(), that)
is Double -> compare(this, that)
is Long -> compare(this.toDouble(), that)
is Float -> compare(this.toDouble(), that)
View
9 src/main/kotlin/org/katchers/Should.kt
@@ -21,9 +21,12 @@ package org.katchers
* Should Matcher Functions
*/
-// Any should be
+// Any should
public fun <T>T.should(val verb: BE): AnyBeMatcher<T> = AnyBeMatcher(this)
public fun <T>T.should(val verb: NOTBE): AnyNotBeMatcher<T> = AnyNotBeMatcher(this)
+public fun <T>T.should(val verb: MATCH): AnyMatchMatcher<T> = AnyMatchMatcher(this)
+public fun <T>T.should(val verb: NOTMATCH): AnyNotMatchMatcher<T> = AnyNotMatchMatcher(this);
+public fun <T>T.should(val cond: T.() -> Boolean): Unit = this.should(match).condition(cond)
// String should have | start | end | contain
public fun String.should(val verb: HAVE): StringHaveMatcher = StringHaveMatcher(this)
@@ -41,5 +44,5 @@ public inline fun Number.should(val verb: NOTBE): NumberNotBeMatcher = NumberNot
//() -> Unit should fail
-public fun <R>jet.Function0<R>.should(verb: FAIL): FunctionFailMatcher<R> = FunctionFailMatcher(this)
-public fun <R>jet.Function0<R>.should(verb: NOTFAIL): FunctionNotFailMatcher<R> = FunctionNotFailMatcher(this)
+public fun <T: () -> Any>T.should(verb: NOTFAIL): FunctionNotFailMatcher<T> = FunctionNotFailMatcher(this)
+public fun <T: () -> Any>T.should(verb: FAIL): FunctionFailMatcher<T> = FunctionFailMatcher(this)
View
10 src/main/kotlin/org/katchers/Verb.kt
@@ -68,6 +68,14 @@ public class NOTFAIL: Verb {
fun not() = fail
}
+public class FAILFUN: Verb {
+ fun not() = notfailfun
+}
+
+public class NOTFAILFUN: Verb {
+ fun not() = failfun
+}
+
/* VALUES */
val be = BE()
val notbe = NOTBE()
@@ -83,3 +91,5 @@ val match = MATCH()
val notmatch = NOTMATCH()
val fail = FAIL()
val notfail = NOTFAIL()
+val failfun = FAILFUN()
+val notfailfun = NOTFAILFUN()
View
21 src/test/kotlin/AnyMatchersTest.kt
@@ -14,7 +14,8 @@
* limitations under the License.
*/
import org.junit.Test as test
-import org.katchers.*
+import org.katchers.*
+
/**
* @author Daniel Seidler
* @since 2012/11/21
@@ -48,4 +49,22 @@ public class AnyMatchersTest {
{ "d" should !be any of("a", "b", "c") } should !fail with AssertionError();
{ "a" should !be any of("a", "b", "c") } should fail with AssertionError()
}
+
+ test fun condition() {
+ object book {
+ val title = "Title"
+ val author = "Author"
+ }
+
+ book should match condition { title == "Title" && author == "Author" }
+ book should match condition { title == "" || author == "Author" }
+ book should match condition { title == "Title" || author == "" }
+ book should !match condition { title == "T" && author == "Author" }
+
+ { book should match condition { title == "Titl" && author == "Author" } } should fail with assertionError
+ { book should !match condition { title == "Title" && author == "Author" } } should fail with assertionError
+
+ 1 should match condition {this > 0 && this < 2}
+ 1 should !match condition {this < 1 || this > 1}
+ }
}
View
4 src/test/kotlin/FunctionMatcherTest.kt
@@ -52,8 +52,4 @@ public class FunctionMatcherTest {
if(!assertErrorThrown) fail("AssertionError", "none")
}
}
-
- test fun tf() {
- {(): Unit -> println({(x: Int, y: Int) : String -> "$x + $y" }(1, 2)) }
- }
}
View
14 src/test/kotlin/MustTest.kt
@@ -41,11 +41,11 @@ public class MustTest {
assertTrue(String().must(!have) is StringNotHaveMatcher)
}
- test fun mustBeFunctionMatcher() {
- assertTrue({}.must(fail) is FunctionFailMatcher<Unit>)
- assertTrue({}.must(!fail) is FunctionNotFailMatcher<Unit>)
- assertTrue({"string"}.must(fail) is FunctionFailMatcher<String>)
- assertTrue({"string"}.must(!fail) is FunctionNotFailMatcher<String>)
-
- }
+// test fun mustBeFunctionMatcher() {
+// assertTrue({}.must(fail) is FunctionFailMatcher<Unit>)
+// assertTrue({}.must(!fail) is FunctionNotFailMatcher<Unit>)
+// assertTrue({"string"}.must(fail) is FunctionFailMatcher<String>)
+// assertTrue({"string"}.must(!fail) is FunctionNotFailMatcher<String>)
+//
+// }
}
View
3  src/test/kotlin/NumbersTest.kt
@@ -38,18 +38,15 @@ public class NumbersTest {
val oneList: List<Number> = arrayList<Number>(oneD, oneF, oneI, oneL, oneS, oneB)
val onePointOneList: List<Number> = arrayList<Number>(onePointOneD, onePointOneF)
for(n1 in oneList) for(n2 in oneList) {
- println("$n1, $n2")
(n1 compare n2) should be equal 0
}
for(n1 in oneList) for(n2 in onePointOneList) {
- println("$n1(${n1.javaClass}), $n2(${n2.javaClass}) = ${n1 compare n2}")
(n1 compare n2) should be equal -1
}
for(n1 in onePointOneList) for(n2 in oneList) {
- println("$n1(${n1.javaClass}), $n2(${n2.javaClass}) = ${n1 compare n2}")
(n1 compare n2) should be equal 1
}
}
View
12 src/test/kotlin/ShouldTest.kt
@@ -40,10 +40,14 @@ public class ShouldTest {
}
test fun shouldBeFunctionMatcher() {
- assertTrue({}.should(fail) is FunctionFailMatcher<Unit>)
- assertTrue({}.should(!fail) is FunctionNotFailMatcher<Unit>)
- assertTrue({"string"}.should(fail) is FunctionFailMatcher<String>)
- assertTrue({"string"}.should(!fail) is FunctionNotFailMatcher<String>)
+ {} should !fail with AssertionError();
+ {1 > 0} should !fail with AssertionError();
+ {1 / 0} should fail()
+ try {
+ {throw NullPointerException()} should !fail with NullPointerException()
+ } catch (e: Throwable) {
+ assertTrue(e is AssertionError)
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.