Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
230 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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/22 | ||
* NumberRangesMatcher use the "in" syntax of Kotlin | ||
*/ | ||
|
||
fun range(r: IntRange) = NumberIntRange(r) | ||
fun range(r: LongRange) = NumberLongRange(r) | ||
fun range(r: FloatRange) = NumberFloatRange(r) | ||
fun range(r: DoubleRange) = NumberDoubleRange(r) | ||
|
||
trait NumberRange { | ||
open val start: Number; | ||
open val end: Number; | ||
fun inRange(n: Number): Boolean = n.compare(start) >= 0 && n.compare(end) <= 0 | ||
fun contains(n: NumberBeMatcher): Boolean { | ||
if(!inRange(n.target)) { | ||
fail("${n.target} should be in ($start, $end)", "${n.target} is not in ($start, $end)") | ||
return false | ||
} | ||
else return true | ||
} | ||
fun contains(n: NumberNotBeMatcher): Boolean { | ||
if(inRange(n.target)) { | ||
fail("${n.target} should not be in ($start, $end)", "${n.target} is in ($start, $end)") | ||
return false | ||
} | ||
else return true | ||
} | ||
} | ||
|
||
class NumberIntRange(val r: IntRange): NumberRange{ | ||
override val start = r.start; | ||
override val end = r.end; | ||
} | ||
|
||
class NumberLongRange(val r: LongRange): NumberRange{ | ||
override val start = r.start; | ||
override val end = r.end; | ||
} | ||
|
||
class NumberFloatRange(val r: FloatRange): NumberRange{ | ||
override val start = r.start; | ||
override val end = r.end; | ||
} | ||
|
||
class NumberDoubleRange(val r: DoubleRange): NumberRange{ | ||
override val start = r.start; | ||
override val end = r.end; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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/23 | ||
*/ | ||
public class NumberRangesMatcherTest { | ||
|
||
test fun shouldBeInRange() { | ||
1 should be in range(1..2) | ||
1 should be in range(1.0..2.0) | ||
1.1 should be in range(1..2) | ||
1.1 should be in range(1.0..2.0) | ||
1 should be in range(0.9..1.1); | ||
{1 should be in range(1.1..1.2)} should fail with assertionError; | ||
{1.09 should be in range(1.1..1.2)} should fail with assertionError | ||
{1.201 should be in range(1.1..1.2)} should fail with assertionError | ||
} | ||
|
||
test fun shouldNotBeInRange() { | ||
1 should !be in range(1.1..2.0) | ||
1.0 should !be in range(1.1..2.0) | ||
1.99 should !be in range(2..3) | ||
1.99 should !be in range(1.991..1.992) | ||
1 should !be in range(0.9..0.99); | ||
{1.001 should !be in range(1..2)} should fail with assertionError; | ||
{1.11 should !be in range(1.10..1.12)} should fail with assertionError | ||
{1.1999 should !be in range(1.1..1.2)} should fail with assertionError | ||
|
||
} | ||
} |
Oops, something went wrong.