Skip to content

Commit

Permalink
Merge pull request #1 from cy6erGn0m/M11
Browse files Browse the repository at this point in the history
Migrate to M11
  • Loading branch information
danseid committed Nov 12, 2020
2 parents f6e38bb + eeae1f3 commit 2ec81e6
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 101 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -34,8 +34,8 @@
</snapshotRepository> </snapshotRepository>
</distributionManagement> </distributionManagement>
<properties> <properties>
<kotlin.version>0.4.297</kotlin.version> <kotlin.version>0.11.91</kotlin.version>
<junit.version>4.10</junit.version> <junit.version>4.12</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>


Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/org/katchers/AnyMatcher.kt
Expand Up @@ -24,25 +24,25 @@ import kotlin.test.*


trait Matcher<T> { trait Matcher<T> {
open val target: T open val target: T
inline protected fun notSupported(): Unit = fail("Not Supported Condition ") protected fun notSupported(): Unit = fail("Not Supported Condition ")
} }


open class AnyBeMatcher<T>(override val target: T): Matcher<T> { open class AnyBeMatcher<T>(override val target: T): Matcher<T> {
inline fun equal(value: T) = if (value != target) fail(value, target) 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) fun any(values: List<T>) = if(!values.any { it == target }) fail("any of $values", target)
} }


open class AnyNotBeMatcher<T>(override val target: T): Matcher<T> { open class AnyNotBeMatcher<T>(override val target: T): Matcher<T> {
inline fun equal(value: T) = if (value == target) fail(value, target) 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) 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> { open class AnyMatchMatcher<T>(override val target: T): Matcher<T> {
inline fun condition(match: T.() -> Boolean) = if(!target.match()) fail() fun condition(match: T.() -> Boolean) = if(!target.match()) fail()
} }


open class AnyNotMatchMatcher<T>(override val target: T): Matcher<T> { open class AnyNotMatchMatcher<T>(override val target: T): Matcher<T> {
inline fun condition(match: T.() -> Boolean) = if(target.match()) fail() fun condition(match: T.() -> Boolean) = if(target.match()) fail()
} }




Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/katchers/CollectionMatcher.kt
Expand Up @@ -30,9 +30,9 @@ class CollectionNotContainMatcher<T>(override val target: Collection<T>): Matche
} }


class CollectionHaveMatcher<T>(override val target: Collection<T>): Matcher<Collection<T>>{ class CollectionHaveMatcher<T>(override val target: Collection<T>): Matcher<Collection<T>>{
fun size(size: Int) = if(target.size != size) fail("$target should have size $size", "$target has size ${target.size}") fun size(size: Int) = if(target.size() != size) fail("$target should have size $size", "$target has size ${target.size()}")
} }


class CollectionNotHaveMatcher<T>(override val target: Collection<T>): Matcher<Collection<T>>{ class CollectionNotHaveMatcher<T>(override val target: Collection<T>): Matcher<Collection<T>>{
fun size(size: Int) = if(target.size == size) fail("$target should not have size $size", "$target has size ${target.size}") fun size(size: Int) = if(target.size() == size) fail("$target should not have size $size", "$target has size ${target.size()}")
} }
2 changes: 1 addition & 1 deletion src/main/kotlin/org/katchers/FunctionMatcher.kt
Expand Up @@ -43,5 +43,5 @@ class FunctionNotFailMatcher<T: () -> Any>(override val target: T): Matcher<T>{
} }
} }


private inline fun Throwable.getName() = this.javaClass.getName() private fun Throwable.getName() = this.javaClass.getName()
public val assertionError : AssertionError = AssertionError() public val assertionError : AssertionError = AssertionError()
4 changes: 2 additions & 2 deletions src/main/kotlin/org/katchers/HelperFunctions.kt
Expand Up @@ -23,11 +23,11 @@ import kotlin.test.*
* @since 2012/11/20 * @since 2012/11/20
*/ */


inline fun <T>fail(expected: T, got: T): Unit { fun <T>fail(expected: T, got: T): Unit {
fail(""" fail("""
expected: <$expected> expected: <$expected>
got: <$got> got: <$got>
""") """)
} }


inline fun <T>of(vararg values: T): List<T> = values.toList() fun <T>of(vararg values: T): List<T> = values.toList()
20 changes: 10 additions & 10 deletions src/main/kotlin/org/katchers/MapContainMatcher.kt
Expand Up @@ -21,23 +21,23 @@ package org.katchers
*/ */


class MapContainMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{ class MapContainMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{
private inline fun item(key: K, value: V) = if(!(value == target.get(key))) fail("$target contains ($key=$value)", "$target doesn't contain ($key=$value)") private fun item(key: K, value: V) = if(value != target.get(key)) fail("$target contains ($key=$value)", "$target doesn't contain ($key=$value)")
inline fun item(el: Pair<K,V>) = item(el.first, el.second) fun item(el: Pair<K,V>) = item(el.first, el.second)
inline fun key(key: K) = if(!target.containsKey(key)) fail("$target contains key $key", "$target doesn't contain key $key") fun key(key: K) = if(!target.containsKey(key)) fail("$target contains key $key", "$target doesn't contain key $key")
inline fun value(value: V) = if(!target.containsValue(value)) fail("$target contains value $value", "$target doesn't contain value $value") fun value(value: V) = if(!target.containsValue(value)) fail("$target contains value $value", "$target doesn't contain value $value")
} }


class MapNotContainMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{ class MapNotContainMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{
private inline fun item(key: K, value: V) = if(value == target.get(key)) fail("$target should not contain ($key=$value)", "$target contains ($key=$value)") private fun item(key: K, value: V) = if(value == target.get(key)) fail("$target should not contain ($key=$value)", "$target contains ($key=$value)")
inline fun item(el: Pair<K,V>) = item(el.first, el.second) fun item(el: Pair<K,V>) = item(el.first, el.second)
inline fun key(key: K) = if(target.containsKey(key)) fail("$target should not contain key $key", "$target contains key $key") fun key(key: K) = if(target.containsKey(key)) fail("$target should not contain key $key", "$target contains key $key")
inline fun value(value: V) = if(target.containsValue(value)) fail("$target should not contain value $value", "$target contains value $value") fun value(value: V) = if(target.containsValue(value)) fail("$target should not contain value $value", "$target contains value $value")
} }


class MapHaveMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{ class MapHaveMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{
fun size(size: Int) = if(target.size != size) fail("$target should have size $size", "$target has size ${target.size}") fun size(size: Int) = if(target.size() != size) fail("$target should have size $size", "$target has size ${target.size()}")
} }


class MapNotHaveMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{ class MapNotHaveMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{
fun size(size: Int) = if(target.size == size) fail("$target should not have size $size", "$target has size ${target.size}") fun size(size: Int) = if(target.size() == size) fail("$target should not have size $size", "$target has size ${target.size()}")
} }
44 changes: 22 additions & 22 deletions src/main/kotlin/org/katchers/Must.kt
Expand Up @@ -21,38 +21,38 @@ package org.katchers
*/ */


// Any must // Any must
public fun <T>T.must(val verb: BE): AnyBeMatcher<T> = AnyBeMatcher(this) public fun <T>T.must(verb: BE): AnyBeMatcher<T> = AnyBeMatcher(this)
public fun <T>T.must(val verb: NOTBE): AnyNotBeMatcher<T> = AnyNotBeMatcher(this) public fun <T>T.must(verb: NOTBE): AnyNotBeMatcher<T> = AnyNotBeMatcher(this)
public fun <T>T.must(val verb: MATCH): AnyMatchMatcher<T> = AnyMatchMatcher(this) public fun <T>T.must(verb: MATCH): AnyMatchMatcher<T> = AnyMatchMatcher(this)
public fun <T>T.must(val verb: NOTMATCH): AnyNotMatchMatcher<T> = AnyNotMatchMatcher(this); public fun <T>T.must(verb: NOTMATCH): AnyNotMatchMatcher<T> = AnyNotMatchMatcher(this);


// String must have | start | end | contain // String must have | start | end | contain
public fun String.must(val verb: HAVE): StringHaveMatcher = StringHaveMatcher(this) public fun String.must(verb: HAVE): StringHaveMatcher = StringHaveMatcher(this)
public fun String.must(val verb: NOTHAVE): StringNotHaveMatcher = StringNotHaveMatcher(this) public fun String.must(verb: NOTHAVE): StringNotHaveMatcher = StringNotHaveMatcher(this)
public fun String.must(val verb: START): StringStartMatcher = StringStartMatcher(this) public fun String.must(verb: START): StringStartMatcher = StringStartMatcher(this)
public fun String.must(val verb: NOTSTART): StringNotStartMatcher = StringNotStartMatcher(this) public fun String.must(verb: NOTSTART): StringNotStartMatcher = StringNotStartMatcher(this)
public fun String.must(val verb: END): StringEndMatcher = StringEndMatcher(this) public fun String.must(verb: END): StringEndMatcher = StringEndMatcher(this)
public fun String.must(val verb: NOTEND): StringNotEndMatcher = StringNotEndMatcher(this) public fun String.must(verb: NOTEND): StringNotEndMatcher = StringNotEndMatcher(this)
public fun String.must(val verb: CONTAIN): StringContainMatcher = StringContainMatcher(this) public fun String.must(verb: CONTAIN): StringContainMatcher = StringContainMatcher(this)
public fun String.must(val verb: NOTCONTAIN): StringNotContainMatcher = StringNotContainMatcher(this) public fun String.must(verb: NOTCONTAIN): StringNotContainMatcher = StringNotContainMatcher(this)


// Number must be // Number must be
public inline fun Number.must(val verb: BE): NumberBeMatcher = NumberBeMatcher(this) public fun Number.must(verb: BE): NumberBeMatcher = NumberBeMatcher(this)
public inline fun Number.must(val verb: NOTBE): NumberNotBeMatcher = NumberNotBeMatcher(this) public fun Number.must(verb: NOTBE): NumberNotBeMatcher = NumberNotBeMatcher(this)




//() -> Unit must fail //() -> Unit must fail
public fun <T: () -> Any>T.must(verb: NOTFAIL): FunctionNotFailMatcher<T> = FunctionNotFailMatcher(this) public fun <T: () -> Any>T.must(verb: NOTFAIL): FunctionNotFailMatcher<T> = FunctionNotFailMatcher(this)
public fun <T: () -> Any>T.must(verb: FAIL): FunctionFailMatcher<T> = FunctionFailMatcher(this) public fun <T: () -> Any>T.must(verb: FAIL): FunctionFailMatcher<T> = FunctionFailMatcher(this)


//Collection //Collection
public fun <T>Collection<T>.must(verb: CONTAIN): CollectionContainMatcher<T> = CollectionContainMatcher<T>(this) public fun <T>Collection<T>.must(verb: CONTAIN): CollectionContainMatcher<T> = CollectionContainMatcher(this)
public fun <T>Collection<T>.must(verb: NOTCONTAIN): CollectionNotContainMatcher<T> = CollectionNotContainMatcher<T>(this) public fun <T>Collection<T>.must(verb: NOTCONTAIN): CollectionNotContainMatcher<T> = CollectionNotContainMatcher(this)
public fun <T>Collection<T>.must(verb: HAVE): CollectionHaveMatcher<T> = CollectionHaveMatcher<T>(this) public fun <T>Collection<T>.must(verb: HAVE): CollectionHaveMatcher<T> = CollectionHaveMatcher(this)
public fun <T>Collection<T>.must(verb: NOTHAVE): CollectionNotHaveMatcher<T> = CollectionNotHaveMatcher<T>(this) public fun <T>Collection<T>.must(verb: NOTHAVE): CollectionNotHaveMatcher<T> = CollectionNotHaveMatcher(this)


//Map //Map
public fun <K,V>Map<K,V>.must(verb: CONTAIN): MapContainMatcher<K,V> = MapContainMatcher<K,V>(this) public fun <K,V>Map<K,V>.must(verb: CONTAIN): MapContainMatcher<K,V> = MapContainMatcher(this)
public fun <K,V>Map<K,V>.must(verb: NOTCONTAIN): MapNotContainMatcher<K,V> = MapNotContainMatcher<K,V>(this) public fun <K,V>Map<K,V>.must(verb: NOTCONTAIN): MapNotContainMatcher<K,V> = MapNotContainMatcher(this)
public fun <K,V>Map<K,V>.must(verb: HAVE): MapHaveMatcher<K,V> = MapHaveMatcher<K,V>(this) public fun <K,V>Map<K,V>.must(verb: HAVE): MapHaveMatcher<K,V> = MapHaveMatcher(this)
public fun <K,V>Map<K,V>.must(verb: NOTHAVE): MapNotHaveMatcher<K,V> = MapNotHaveMatcher<K,V>(this) public fun <K,V>Map<K,V>.must(verb: NOTHAVE): MapNotHaveMatcher<K,V> = MapNotHaveMatcher(this)
37 changes: 18 additions & 19 deletions src/main/kotlin/org/katchers/NumberMatcher.kt
Expand Up @@ -21,60 +21,59 @@ package org.katchers
*/ */




class NumberBeMatcher(target: jet.Number): AnyBeMatcher<Number>(target){ class NumberBeMatcher(target: Number): AnyBeMatcher<Number>(target){
inline fun gt(value: Number) = greaterThan(value) fun gt(value: Number) = greaterThan(value)
inline fun gte(value: Number) = greaterOrEqualThan(value) fun gte(value: Number) = greaterOrEqualThan(value)
inline fun lt(value: Number) = lessThan(value) fun lt(value: Number) = lessThan(value)
inline fun lte(value: Number) = lessOrEqualThan(value) fun lte(value: Number) = lessOrEqualThan(value)


inline fun greaterThan(value: jet.Number) { fun greaterThan(value: Number) {
when { when {
target == value -> fail("$target > $value", "$target == $value") target == value -> fail("$target > $value", "$target == $value")
target compare value < 0 -> fail("$target > $value", "$target < $value") target compare value < 0 -> fail("$target > $value", "$target < $value")
else -> return else -> return
} }
} }


inline fun greaterOrEqualThan(value: Number) = if(target compare value < 0) fail("$target >= $value", "$target < $value") fun greaterOrEqualThan(value: Number) = if(target compare value < 0) fail("$target >= $value", "$target < $value")


inline fun lessThan(value: Number) { fun lessThan(value: Number) {
when { when {
target == value -> fail("$target < $value", "$target == $value") target == value -> fail("$target < $value", "$target == $value")
target compare value > 0 -> fail("$target < $value", "$target > $value") target compare value > 0 -> fail("$target < $value", "$target > $value")
else -> return else -> return
} }
} }


inline fun lessOrEqualThan(value: Number) = if(target compare value > 0) fail("$target <= $value", "$target > $value") fun lessOrEqualThan(value: Number) = if(target compare value > 0) fail("$target <= $value", "$target > $value")


inline fun inRange(val r: IntRange) { fun inRange(r: IntRange) {
if (target !in r) fail("$target should be in [${r.start},${r.end}]", "$target is not in [${r.start},${r.end}]") 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){ class NumberNotBeMatcher(target: Number): AnyNotBeMatcher<Number>(target){


inline fun gt(value: Number) = greaterThan(value) fun gt(value: Number) = greaterThan(value)
inline fun gte(value: Number) = greaterOrEqualThan(value) fun gte(value: Number) = greaterOrEqualThan(value)
inline fun lt(value: Number) = lessThan(value) fun lt(value: Number) = lessThan(value)
inline fun lte(value: Number) = lessOrEqualThan(value) fun lte(value: Number) = lessOrEqualThan(value)


inline fun greaterThan(value: Number) = if(target compare value > 0) { fun greaterThan(value: Number) = if(target compare value > 0)
fail("!($target > $value)", "$target > $value") fail("!($target > $value)", "$target > $value")
}


inline fun greaterOrEqualThan(value: Number) { fun greaterOrEqualThan(value: Number) {
when { when {
target == value -> fail("!($target >= $value)", "$target == $value") target == value -> fail("!($target >= $value)", "$target == $value")
target compare value > 0 -> fail("!($target >= $value)", "$target > $value") target compare value > 0 -> fail("!($target >= $value)", "$target > $value")
else -> return else -> return
} }
} }


inline fun lessThan(value: Number) = if(target compare value < 0) fail("!($target < $value)", "$target < $value") fun lessThan(value: Number) = if(target compare value < 0) fail("!($target < $value)", "$target < $value")


inline fun lessOrEqualThan(value: Number) { fun lessOrEqualThan(value: Number) {
when { when {
target == value -> fail("!($target <= $value)", "$target == $value") target == value -> fail("!($target <= $value)", "$target == $value")
target compare value < 0 -> fail("!($target <= $value)", "$target < $value") target compare value < 0 -> fail("!($target <= $value)", "$target < $value")
Expand Down
44 changes: 22 additions & 22 deletions src/main/kotlin/org/katchers/Should.kt
Expand Up @@ -24,41 +24,41 @@ import kotlin.test.*
*/ */


// Any should // Any should
public fun <T>T.should(val verb: BE): AnyBeMatcher<T> = AnyBeMatcher(this) public fun <T>T.should(verb: BE): AnyBeMatcher<T> = AnyBeMatcher(this)
public fun <T>T.should(val verb: NOTBE): AnyNotBeMatcher<T> = AnyNotBeMatcher(this) public fun <T>T.should(verb: NOTBE): AnyNotBeMatcher<T> = AnyNotBeMatcher(this)
public fun <T>T.should(val verb: MATCH): AnyMatchMatcher<T> = AnyMatchMatcher(this) public fun <T>T.should(verb: MATCH): AnyMatchMatcher<T> = AnyMatchMatcher(this)
public fun <T>T.should(val verb: NOTMATCH): AnyNotMatchMatcher<T> = AnyNotMatchMatcher(this); public fun <T>T.should(verb: NOTMATCH): AnyNotMatchMatcher<T> = AnyNotMatchMatcher(this);


// String should have | start | end | contain // String should have | start | end | contain
public fun String.should(val verb: HAVE): StringHaveMatcher = StringHaveMatcher(this) public fun String.should(verb: HAVE): StringHaveMatcher = StringHaveMatcher(this)
public fun String.should(val verb: NOTHAVE): StringNotHaveMatcher = StringNotHaveMatcher(this) public fun String.should(verb: NOTHAVE): StringNotHaveMatcher = StringNotHaveMatcher(this)
public fun String.should(val verb: START): StringStartMatcher = StringStartMatcher(this) public fun String.should(verb: START): StringStartMatcher = StringStartMatcher(this)
public fun String.should(val verb: NOTSTART): StringNotStartMatcher = StringNotStartMatcher(this) public fun String.should(verb: NOTSTART): StringNotStartMatcher = StringNotStartMatcher(this)
public fun String.should(val verb: END): StringEndMatcher = StringEndMatcher(this) public fun String.should(verb: END): StringEndMatcher = StringEndMatcher(this)
public fun String.should(val verb: NOTEND): StringNotEndMatcher = StringNotEndMatcher(this) public fun String.should(verb: NOTEND): StringNotEndMatcher = StringNotEndMatcher(this)
public fun String.should(val verb: CONTAIN): StringContainMatcher = StringContainMatcher(this) public fun String.should(verb: CONTAIN): StringContainMatcher = StringContainMatcher(this)
public fun String.should(val verb: NOTCONTAIN): StringNotContainMatcher = StringNotContainMatcher(this) public fun String.should(verb: NOTCONTAIN): StringNotContainMatcher = StringNotContainMatcher(this)


// Number should be // Number should be
public inline fun Number.should(val verb: BE): NumberBeMatcher = NumberBeMatcher(this) public fun Number.should(verb: BE): NumberBeMatcher = NumberBeMatcher(this)
public inline fun Number.should(val verb: NOTBE): NumberNotBeMatcher = NumberNotBeMatcher(this) public fun Number.should(verb: NOTBE): NumberNotBeMatcher = NumberNotBeMatcher(this)




//() -> Unit should fail //() -> Unit should fail
public fun <T: () -> Any>T.should(verb: NOTFAIL): FunctionNotFailMatcher<T> = 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) public fun <T: () -> Any>T.should(verb: FAIL): FunctionFailMatcher<T> = FunctionFailMatcher(this)


//Collection //Collection
public fun <T>Collection<T>.should(verb: CONTAIN): CollectionContainMatcher<T> = CollectionContainMatcher<T>(this) public fun <T>Collection<T>.should(verb: CONTAIN): CollectionContainMatcher<T> = CollectionContainMatcher(this)
public fun <T>Collection<T>.should(verb: NOTCONTAIN): CollectionNotContainMatcher<T> = CollectionNotContainMatcher<T>(this) public fun <T>Collection<T>.should(verb: NOTCONTAIN): CollectionNotContainMatcher<T> = CollectionNotContainMatcher(this)
public fun <T>Collection<T>.should(verb: HAVE): CollectionHaveMatcher<T> = CollectionHaveMatcher<T>(this) public fun <T>Collection<T>.should(verb: HAVE): CollectionHaveMatcher<T> = CollectionHaveMatcher(this)
public fun <T>Collection<T>.should(verb: NOTHAVE): CollectionNotHaveMatcher<T> = CollectionNotHaveMatcher<T>(this) public fun <T>Collection<T>.should(verb: NOTHAVE): CollectionNotHaveMatcher<T> = CollectionNotHaveMatcher(this)


//Map //Map
public fun <K,V>Map<K,V>.should(verb: CONTAIN): MapContainMatcher<K,V> = MapContainMatcher<K,V>(this) public fun <K,V>Map<K,V>.should(verb: CONTAIN): MapContainMatcher<K,V> = MapContainMatcher(this)
public fun <K,V>Map<K,V>.should(verb: NOTCONTAIN): MapNotContainMatcher<K,V> = MapNotContainMatcher<K,V>(this) public fun <K,V>Map<K,V>.should(verb: NOTCONTAIN): MapNotContainMatcher<K,V> = MapNotContainMatcher(this)
public fun <K,V>Map<K,V>.should(verb: HAVE): MapHaveMatcher<K,V> = MapHaveMatcher<K,V>(this) public fun <K,V>Map<K,V>.should(verb: HAVE): MapHaveMatcher<K,V> = MapHaveMatcher(this)
public fun <K,V>Map<K,V>.should(verb: NOTHAVE): MapNotHaveMatcher<K,V> = MapNotHaveMatcher<K,V>(this) public fun <K,V>Map<K,V>.should(verb: NOTHAVE): MapNotHaveMatcher<K,V> = MapNotHaveMatcher(this)






Expand Down

0 comments on commit 2ec81e6

Please sign in to comment.