Browse files

target to Match trait added

  • Loading branch information...
1 parent 61e8405 commit 366d99b2353129d3df45953a5aa643d54fd8c925 @danseid committed Nov 23, 2012
View
7 src/main/kotlin/org/katchers/AnyMatcher.kt
@@ -22,16 +22,17 @@ import kotlin.test.*
* @since 2012/11/19
*/
-trait Matcher {
+trait Matcher<T> {
+ open val target : T
inline protected fun notSupported(): Unit = fail("Not Supported Condition ")
}
-open class AnyBeMatcher<T>(val target: T): Matcher {
+open class AnyBeMatcher<T>(override val target: T): Matcher<T> {
inline fun equal(value: T) = if (value != target) fail(value, target)
inline fun any(values: List<T>) = if(!values.any { it == target }) fail("any of $values", target)
}
-open class AnyNotBeMatcher<T>(val target: T): Matcher {
+open class AnyNotBeMatcher<T>(override val target: T): Matcher<T> {
inline fun equal(value: T) = if (value == target) fail(value, target)
inline fun any(values: List<T>) = if(values any { it == target }) fail("not any of $values", target)
}
View
4 src/main/kotlin/org/katchers/FunctionMatcher.kt
@@ -20,7 +20,7 @@ package org.katchers
* @since 2012/11/20
*/
-class FunctionFailMatcher<R>(val target: () -> R): Matcher{
+class FunctionFailMatcher<R>(override val target: () -> R): Matcher<() -> R>{
fun with(error: Throwable) {
var failed = true // assume it will fail
try {
@@ -33,7 +33,7 @@ class FunctionFailMatcher<R>(val target: () -> R): Matcher{
}
}
}
-class FunctionNotFailMatcher<R>(val target: () -> R): Matcher{
+class FunctionNotFailMatcher<R>(override val target: () -> R): Matcher<() -> R>{
fun with(error: Throwable) {
try {
target()
View
10 src/main/kotlin/org/katchers/NumberMatcher.kt
@@ -22,8 +22,7 @@ package org.katchers
class NumberBeMatcher(target: jet.Number): AnyBeMatcher<Number>(target){
- {
- }
+
inline fun gt(value: Number) = greaterThan(value)
inline fun gte(value: Number) = greaterOrEqualThan(value)
inline fun lt(value: Number) = lessThan(value)
@@ -50,6 +49,10 @@ class NumberBeMatcher(target: jet.Number): AnyBeMatcher<Number>(target){
fun lessOrEqualThan(value: Number) = if(target compare value > 0) fail("$target <= $value", "$target > $value")
+ fun inRange(val r: IntRange) {
+ if (target !in r) fail("$target should be in [${r.start},${r.end}]", "$target is not in [${r.start},${r.end}]")
+ }
+
}
class NumberNotBeMatcher(target: Number): AnyNotBeMatcher<Number>(target){
@@ -79,6 +82,7 @@ class NumberNotBeMatcher(target: Number): AnyNotBeMatcher<Number>(target){
}
}
}
+
fun Number.compare(that: Number): Int {
when(this){
is Int -> {
@@ -160,3 +164,5 @@ fun Number.compare(that: Number): Int {
+
+
View
16 src/main/kotlin/org/katchers/StringMatcher.kt
@@ -20,36 +20,36 @@ package org.katchers
* @since 2012/11/20
*/
-class StringHaveMatcher(val target: String): Matcher{
+class StringHaveMatcher(override val target: String): Matcher<String>{
inline fun length(expected: Int) = target.length() should be equal expected
}
-class StringNotHaveMatcher(val target: String): Matcher{
+class StringNotHaveMatcher(override val target: String): Matcher<String>{
inline fun length(expected: Int) = target.length() should !be equal expected
}
-class StringStartMatcher(val target: String): Matcher{
+class StringStartMatcher(override val target: String): Matcher<String>{
inline fun with(expected: String) = if(!target.startsWith(expected)) fail("$target should start with $expected", "$target starts with ${target.charAt(0)}")
}
-class StringNotStartMatcher(val target: String): Matcher{
+class StringNotStartMatcher(override val target: String): Matcher<String>{
inline fun with(expected: String) = if(target.startsWith(expected)) fail("$target should not start with $expected", "$target starts with $expected")
}
-class StringEndMatcher(val target: String): Matcher{
+class StringEndMatcher(override val target: String): Matcher<String>{
inline fun with(expected: String) = if(!target.endsWith(expected)) fail("$target should end with $expected", "$target ends with ${target.charAt(target.length - 1)}")
}
-class StringNotEndMatcher(val target: String): Matcher{
+class StringNotEndMatcher(override val target: String): Matcher<String>{
inline fun with(expected: String) = if(target.endsWith(expected)) fail("$target should not end with $expected", "$target ends with $expected")
}
-class StringContainMatcher(val target: String): Matcher{
+class StringContainMatcher(override val target: String): Matcher<String>{
inline fun any(values: List<String>) = if (!values.any { target.contains(it) }) fail("$target should contain any of $values", "$target did not contain any of $values")
inline fun all(values: List<String>) = if(!values.all { target.contains(it) }) fail("$target should contain all of $values", "$target did not contain ${values filter { !target.contains(it) }}")
}
-class StringNotContainMatcher(val target: String): Matcher{
+class StringNotContainMatcher(override val target: String): Matcher<String>{
inline fun any(values: List<String>) = if(values.any { target.contains(it) }) fail("$target should not contain any of $values", "$target contained ${values filter { target.contains(it) }}")
inline fun all(values: List<String>) = if (values.all { target.contains(it) }) fail("$target should not contain all of $values", "$target contained ${values filter { target.contains(it) }}")
}
View
1 src/test/kotlin/AllTest.kt
@@ -25,6 +25,7 @@ Suite.SuiteClasses(
javaClass<AnyMatchersTest>(),
javaClass<StringMatcherTest>(),
javaClass<ShouldTest>(),
+ javaClass<MustTest>(),
javaClass<FunctionMatcherTest>(),
javaClass<NumberMatcherTest>()
)

0 comments on commit 366d99b

Please sign in to comment.