Skip to content
This repository
Browse code

target to Match trait added

  • Loading branch information...
commit 366d99b2353129d3df45953a5aa643d54fd8c925 1 parent 61e8405
Daniel Seidler authored
7 src/main/kotlin/org/katchers/AnyMatcher.kt
@@ -22,16 +22,17 @@ import kotlin.test.*
22 22 * @since 2012/11/19
23 23 */
24 24
25   -trait Matcher {
  25 +trait Matcher<T> {
  26 + open val target : T
26 27 inline protected fun notSupported(): Unit = fail("Not Supported Condition ")
27 28 }
28 29
29   -open class AnyBeMatcher<T>(val target: T): Matcher {
  30 +open class AnyBeMatcher<T>(override val target: T): Matcher<T> {
30 31 inline fun equal(value: T) = if (value != target) fail(value, target)
31 32 inline fun any(values: List<T>) = if(!values.any { it == target }) fail("any of $values", target)
32 33 }
33 34
34   -open class AnyNotBeMatcher<T>(val target: T): Matcher {
  35 +open class AnyNotBeMatcher<T>(override val target: T): Matcher<T> {
35 36 inline fun equal(value: T) = if (value == target) fail(value, target)
36 37 inline fun any(values: List<T>) = if(values any { it == target }) fail("not any of $values", target)
37 38 }
4 src/main/kotlin/org/katchers/FunctionMatcher.kt
@@ -20,7 +20,7 @@ package org.katchers
20 20 * @since 2012/11/20
21 21 */
22 22
23   -class FunctionFailMatcher<R>(val target: () -> R): Matcher{
  23 +class FunctionFailMatcher<R>(override val target: () -> R): Matcher<() -> R>{
24 24 fun with(error: Throwable) {
25 25 var failed = true // assume it will fail
26 26 try {
@@ -33,7 +33,7 @@ class FunctionFailMatcher<R>(val target: () -> R): Matcher{
33 33 }
34 34 }
35 35 }
36   -class FunctionNotFailMatcher<R>(val target: () -> R): Matcher{
  36 +class FunctionNotFailMatcher<R>(override val target: () -> R): Matcher<() -> R>{
37 37 fun with(error: Throwable) {
38 38 try {
39 39 target()
10 src/main/kotlin/org/katchers/NumberMatcher.kt
@@ -22,8 +22,7 @@ package org.katchers
22 22
23 23
24 24 class NumberBeMatcher(target: jet.Number): AnyBeMatcher<Number>(target){
25   - {
26   - }
  25 +
27 26 inline fun gt(value: Number) = greaterThan(value)
28 27 inline fun gte(value: Number) = greaterOrEqualThan(value)
29 28 inline fun lt(value: Number) = lessThan(value)
@@ -50,6 +49,10 @@ class NumberBeMatcher(target: jet.Number): AnyBeMatcher<Number>(target){
50 49
51 50 fun lessOrEqualThan(value: Number) = if(target compare value > 0) fail("$target <= $value", "$target > $value")
52 51
  52 + fun inRange(val r: IntRange) {
  53 + if (target !in r) fail("$target should be in [${r.start},${r.end}]", "$target is not in [${r.start},${r.end}]")
  54 + }
  55 +
53 56 }
54 57
55 58 class NumberNotBeMatcher(target: Number): AnyNotBeMatcher<Number>(target){
@@ -79,6 +82,7 @@ class NumberNotBeMatcher(target: Number): AnyNotBeMatcher<Number>(target){
79 82 }
80 83 }
81 84 }
  85 +
82 86 fun Number.compare(that: Number): Int {
83 87 when(this){
84 88 is Int -> {
@@ -160,3 +164,5 @@ fun Number.compare(that: Number): Int {
160 164
161 165
162 166
  167 +
  168 +
16 src/main/kotlin/org/katchers/StringMatcher.kt
@@ -20,36 +20,36 @@ package org.katchers
20 20 * @since 2012/11/20
21 21 */
22 22
23   -class StringHaveMatcher(val target: String): Matcher{
  23 +class StringHaveMatcher(override val target: String): Matcher<String>{
24 24 inline fun length(expected: Int) = target.length() should be equal expected
25 25 }
26 26
27   -class StringNotHaveMatcher(val target: String): Matcher{
  27 +class StringNotHaveMatcher(override val target: String): Matcher<String>{
28 28 inline fun length(expected: Int) = target.length() should !be equal expected
29 29 }
30 30
31   -class StringStartMatcher(val target: String): Matcher{
  31 +class StringStartMatcher(override val target: String): Matcher<String>{
32 32 inline fun with(expected: String) = if(!target.startsWith(expected)) fail("$target should start with $expected", "$target starts with ${target.charAt(0)}")
33 33 }
34 34
35   -class StringNotStartMatcher(val target: String): Matcher{
  35 +class StringNotStartMatcher(override val target: String): Matcher<String>{
36 36 inline fun with(expected: String) = if(target.startsWith(expected)) fail("$target should not start with $expected", "$target starts with $expected")
37 37 }
38 38
39   -class StringEndMatcher(val target: String): Matcher{
  39 +class StringEndMatcher(override val target: String): Matcher<String>{
40 40 inline fun with(expected: String) = if(!target.endsWith(expected)) fail("$target should end with $expected", "$target ends with ${target.charAt(target.length - 1)}")
41 41 }
42 42
43   -class StringNotEndMatcher(val target: String): Matcher{
  43 +class StringNotEndMatcher(override val target: String): Matcher<String>{
44 44 inline fun with(expected: String) = if(target.endsWith(expected)) fail("$target should not end with $expected", "$target ends with $expected")
45 45 }
46 46
47   -class StringContainMatcher(val target: String): Matcher{
  47 +class StringContainMatcher(override val target: String): Matcher<String>{
48 48 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")
49 49 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) }}")
50 50 }
51 51
52   -class StringNotContainMatcher(val target: String): Matcher{
  52 +class StringNotContainMatcher(override val target: String): Matcher<String>{
53 53 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) }}")
54 54 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) }}")
55 55 }
1  src/test/kotlin/AllTest.kt
@@ -25,6 +25,7 @@ Suite.SuiteClasses(
25 25 javaClass<AnyMatchersTest>(),
26 26 javaClass<StringMatcherTest>(),
27 27 javaClass<ShouldTest>(),
  28 + javaClass<MustTest>(),
28 29 javaClass<FunctionMatcherTest>(),
29 30 javaClass<NumberMatcherTest>()
30 31 )

0 comments on commit 366d99b

Please sign in to comment.
Something went wrong with that request. Please try again.