Permalink
Browse files

NumberMatchers added

  • Loading branch information...
1 parent d784f7b commit 82cd143ebd7a123ffd7232795e466028096dca96 @danseid committed Nov 21, 2012
View
162 src/main/kotlin/org/katchers/NumberMatcher.kt
@@ -0,0 +1,162 @@
+/*
+ * Copyright 2012 Daniel Seidler
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.katchers
+
+/**
+ * @author Daniel Seidler
+ * @since 2012/11/21
+ */
+
+
+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)
+ inline fun lte(value: Number) = lessOrEqualThan(value)
+
+ fun greaterThan(value: jet.Number) {
+
+ when {
+ target == value -> fail("$target > $value", "$target == $value")
+ target compare value < 0 -> fail("$target > $value", "$target < $value")
+ else -> return
+ }
+ }
+
+ fun greaterOrEqualThan(value: Number) = if(target compare value < 0) fail("$target >= $value", "$target < $value")
+
+ fun lessThan(value: Number) {
+ when {
+ target == value -> fail("$target < $value", "$target == $value")
+ target compare value > 0 -> fail("$target < $value", "$target > $value")
+ else -> return
+ }
+ }
+
+ fun lessOrEqualThan(value: Number) = if(target compare value > 0) fail("$target <= $value", "$target > $value")
+
+}
+
+class NumberNotBeMatcher(target: Number): AnyNotBeMatcher<Number>(target){
+
+ inline fun gt(value: Number) = greaterThan(value)
+ inline fun gte(value: Number) = greaterOrEqualThan(value)
+ inline fun lt(value: Number) = lessThan(value)
+ inline fun lte(value: Number) = lessOrEqualThan(value)
+
+ fun greaterThan(value: Number) = if(target compare value > 0) fail("!($target > $value)", "$target > $value")
+
+ fun greaterOrEqualThan(value: Number) {
+ when {
+ target == value -> fail("!($target >= $value)", "$target == $value")
+ target compare value > 0 -> fail("!($target >= $value)", "$target > $value")
+ else -> return
+ }
+ }
+
+ fun lessThan(value: Number) = if(target compare value < 0) fail("!($target < $value)", "$target < $value")
+
+ fun lessOrEqualThan(value: Number) {
+ when {
+ target == value -> fail("!($target <= $value)", "$target == $value")
+ target compare value < 0 -> fail("!($target <= $value)", "$target < $value")
+ else -> return
+ }
+ }
+}
+fun Number.compare(that: Number): Int {
+ when(this){
+ is Int -> {
+ val it = this as Int
+ when(that){
+ is Double -> return (it.compareTo(that as Double))
+ is Float -> return (it.compareTo(that as Float))
+ is Long -> return (it.compareTo(that as Long))
+ is Int -> return (it.compareTo(that as Int))
+ is Short -> return (it.compareTo(that as Short))
+ is Byte -> return (it.compareTo(that as Byte))
+ else -> throw ClassCastException()
+ }
+ }
+ is Double -> {
+ val it = this as Double
+ when(that){
+ is Double -> return (it.compareTo(that as Double))
+ is Float -> return (it.compareTo(that as Float))
+ is Long -> return (it.compareTo(that as Long))
+ is Int -> return (it.compareTo(that as Int))
+ is Short -> return (it.compareTo(that as Short))
+ is Byte -> return (it.compareTo(that as Byte))
+ else -> throw ClassCastException()
+ }
+ }
+ is Long -> {
+ val it = this as Long
+ when(that){
+ is Double -> return (it.compareTo(that as Double))
+ is Float -> return (it.compareTo(that as Float))
+ is Long -> return (it.compareTo(that as Long))
+ is Int -> return (it.compareTo(that as Int))
+ is Short -> return (it.compareTo(that as Short))
+ is Byte -> return (it.compareTo(that as Byte))
+ else -> throw ClassCastException()
+ }
+ }
+ is Float -> {
+ val it = this as Float
+ when(that){
+ is Double -> return (it.compareTo(that as Double))
+ is Float -> return (it.compareTo(that as Float))
+ is Long -> return (it.compareTo(that as Long))
+ is Int -> return (it.compareTo(that as Int))
+ is Short -> return (it.compareTo(that as Short))
+ is Byte -> return (it.compareTo(that as Byte))
+ else -> throw ClassCastException()
+ }
+ }
+ is Short -> {
+ val it = this as Short
+ when(that){
+ is Double -> return (it.compareTo(that as Double))
+ is Float -> return (it.compareTo(that as Float))
+ is Long -> return (it.compareTo(that as Long))
+ is Int -> return (it.compareTo(that as Int))
+ is Short -> return (it.compareTo(that as Short))
+ is Byte -> return (it.compareTo(that as Byte))
+ else -> throw ClassCastException()
+ }
+ }
+ is Byte -> {
+ val it = this as Byte
+ when(that){
+ is Double -> return (it.compareTo(that as Double))
+ is Float -> return (it.compareTo(that as Float))
+ is Long -> return (it.compareTo(that as Long))
+ is Int -> return (it.compareTo(that as Int))
+ is Short -> return (it.compareTo(that as Short))
+ is Byte -> return (it.compareTo(that as Byte))
+ else -> throw ClassCastException()
+ }
+ }
+ else -> throw ClassCastException()
+ }
+}
+
+
+
+
View
7 src/main/kotlin/org/katchers/Should.kt
@@ -35,9 +35,10 @@ public fun String.should(val verb: NOTEND): StringNotEndMatcher = StringNotEndMa
public fun String.should(val verb: CONTAIN): StringContainMatcher = StringContainMatcher(this)
public fun String.should(val verb: NOTCONTAIN): StringNotContainMatcher = StringNotContainMatcher(this)
-// Int should be
-public inline fun Int.should(val verb: BE): IntBeMatcher = IntBeMatcher(this)
-public inline fun Int.should(val verb: NOTBE): IntNotBeMatcher = IntNotBeMatcher(this)
+// Number should be
+public inline fun Number.should(val verb: BE): NumberBeMatcher = NumberBeMatcher(this)
+public inline fun Number.should(val verb: NOTBE): NumberNotBeMatcher = NumberNotBeMatcher(this)
+
//() -> Unit should fail
public fun <R>jet.Function0<R>.should(verb: FAIL): FunctionFailMatcher<R> = FunctionFailMatcher(this)
View
2 src/test/kotlin/AllTest.kt
@@ -26,6 +26,6 @@ Suite.SuiteClasses(
javaClass<StringMatcherTest>(),
javaClass<ShouldTest>(),
javaClass<FunctionMatcherTest>(),
- javaClass<IntMatcherTest>()
+ javaClass<NumberMatcherTest>()
)
public class AllTest {}
View
82 src/test/kotlin/NumberMatcherTest.kt
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2012 Daniel Seidler
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import org.junit.Test as test
+import org.katchers.*
+
+/**
+ * @author Daniel Seidler
+ * @since 2012/11/21
+ */
+public class NumberMatcherTest {
+
+ test fun shouldBeGreaterThan() {
+ 1 should be gt 0
+ 1 should !be gt 2
+ { 1 should be gt 0 } should !fail with assertionError;
+ { 1 should be gt 1 } should fail with assertionError;
+ { 1 should !be gt 2 } should !fail with assertionError;
+
+ 1.0 should !be gt 1
+ 1 should be gt 0.9
+ 0.9 should !be gt 1
+
+
+
+ }
+ test fun shouldBeGreaterOrEqualThan() {
+ 1 should be gte 0
+ 1 should be gte 1
+ 1 should !be gte 2
+
+ { 1 should be gte 0 } should !fail with assertionError;
+ { 1 should be gte 1 } should !fail with assertionError;
+ { 1 should be gte 2 } should fail with assertionError;
+
+ { 1 should !be gte 1 } should fail with assertionError;
+ { 1 should !be gte 0 } should fail with assertionError;
+ { 1 should !be gte 2 } should !fail with assertionError;
+ }
+
+ test fun shouldBeLessThan() {
+ 1 should be lt 2
+ 1 should !be lt 1
+ 1 should !be lt 0
+
+ { 1 should be lt 2 } should !fail with assertionError;
+ { 1 should !be lt 1 } should !fail with assertionError;
+ { 1 should !be lt 0 } should !fail with assertionError;
+
+ { 1 should !be lt 2 } should fail with assertionError;
+ { 1 should be lt 1 } should fail with assertionError;
+ { 1 should be lt 0 } should fail with assertionError;
+
+
+ }
+ test fun shouldBeLessOrEqualThan() {
+ 1 should !be lte 0
+ 1 should be lte 1
+ 1 should be lte 2
+
+ { 1 should !be lte 0 } should !fail with assertionError;
+ { 1 should be lte 1 } should !fail with assertionError;
+ { 1 should be lte 2 } should !fail with assertionError;
+
+ { 1 should !be lte 2 } should fail with assertionError;
+ { 1 should !be lte 1 } should fail with assertionError;
+ { 1 should be lte 0 } should fail with assertionError;
+ }
+
+}

0 comments on commit 82cd143

Please sign in to comment.