Skip to content

Commit 4894ed3

Browse files
sunshinejrikesyo
authored andcommitted
Add beWithin operator with tests
1 parent 1c30c1d commit 4894ed3

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Nimble.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@
314314
7B5358BE1C38479700A23FAA /* SatisfyAnyOf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5358BD1C38479700A23FAA /* SatisfyAnyOf.swift */; };
315315
7B5358BF1C38479700A23FAA /* SatisfyAnyOf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5358BD1C38479700A23FAA /* SatisfyAnyOf.swift */; };
316316
7B5358C01C38479700A23FAA /* SatisfyAnyOf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5358BD1C38479700A23FAA /* SatisfyAnyOf.swift */; };
317+
857D1849253610A900D8693A /* BeWithin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 857D1848253610A900D8693A /* BeWithin.swift */; };
318+
857D184B253610B200D8693A /* BeWithin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 857D1848253610A900D8693A /* BeWithin.swift */; };
319+
857D184C253610B300D8693A /* BeWithin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 857D1848253610A900D8693A /* BeWithin.swift */; };
320+
857D184F2536124400D8693A /* BeWithinTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 857D184D2536123F00D8693A /* BeWithinTest.swift */; };
321+
857D18502536124400D8693A /* BeWithinTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 857D184D2536123F00D8693A /* BeWithinTest.swift */; };
322+
857D18512536124500D8693A /* BeWithinTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 857D184D2536123F00D8693A /* BeWithinTest.swift */; };
317323
898F28B025D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */; };
318324
898F28B125D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */; };
319325
898F28B225D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */; };
@@ -612,6 +618,8 @@
612618
7B5358B91C3846C900A23FAA /* SatisfyAnyOfTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SatisfyAnyOfTest.swift; sourceTree = "<group>"; };
613619
7B5358BD1C38479700A23FAA /* SatisfyAnyOf.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SatisfyAnyOf.swift; sourceTree = "<group>"; };
614620
7B5358C11C39155600A23FAA /* ObjCSatisfyAnyOfTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjCSatisfyAnyOfTest.m; sourceTree = "<group>"; };
621+
857D1848253610A900D8693A /* BeWithin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BeWithin.swift; sourceTree = "<group>"; };
622+
857D184D2536123F00D8693A /* BeWithinTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BeWithinTest.swift; sourceTree = "<group>"; };
615623
898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlwaysFailMatcher.swift; sourceTree = "<group>"; };
616624
8DF1C3F61C94FC75004B2D36 /* ObjcStringersTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjcStringersTest.m; sourceTree = "<group>"; };
617625
964CFEFC1C4FF48900513336 /* ThrowAssertion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThrowAssertion.swift; sourceTree = "<group>"; };
@@ -823,6 +831,7 @@
823831
1F925EEE195C136500ED456B /* BeLogicalTest.swift */,
824832
1F925EF8195C175000ED456B /* BeNilTest.swift */,
825833
1F91DD2C1C74BF36002C309F /* BeVoidTest.swift */,
834+
857D184D2536123F00D8693A /* BeWithinTest.swift */,
826835
7B13BA091DD360DE00C9098C /* ContainElementSatisfyingTest.swift */,
827836
1F925F01195C189500ED456B /* ContainTest.swift */,
828837
B20058C420E92CE400C1264D /* ElementsEqualTest.swift */,
@@ -875,6 +884,7 @@
875884
1FD8CD171968AB07008ED995 /* BeLogical.swift */,
876885
1FD8CD181968AB07008ED995 /* BeNil.swift */,
877886
1F91DD301C74BF61002C309F /* BeVoid.swift */,
887+
857D1848253610A900D8693A /* BeWithin.swift */,
878888
1FD8CD1A1968AB07008ED995 /* Contain.swift */,
879889
7B13BA051DD360AA00C9098C /* ContainElementSatisfying.swift */,
880890
B20058C020E92C7500C1264D /* ElementsEqual.swift */,
@@ -1356,6 +1366,7 @@
13561366
62FB326323B78BF90047BED9 /* BeginWithPrefix.swift in Sources */,
13571367
1FD8CD5A1968AB07008ED995 /* Equal.swift in Sources */,
13581368
CDF5C57C2647B89B0036532C /* Equal+Tuple.swift in Sources */,
1369+
857D184C253610B300D8693A /* BeWithin.swift in Sources */,
13591370
1FD8CD4C1968AB07008ED995 /* BeLessThan.swift in Sources */,
13601371
1FD8CD461968AB07008ED995 /* BeGreaterThan.swift in Sources */,
13611372
F8A1BE2F1CB3710900031679 /* XCTestObservationCenter+Register.m in Sources */,
@@ -1448,6 +1459,7 @@
14481459
1F4A568B1A3B3407009E1637 /* ObjCBeTrueTest.m in Sources */,
14491460
DDEFAEB41A93CBE6005CA37A /* ObjCAllPassTest.m in Sources */,
14501461
1F4A567F1A3B333F009E1637 /* ObjCBeLessThanTest.m in Sources */,
1462+
857D18502536124400D8693A /* BeWithinTest.swift in Sources */,
14511463
1F925EE6195C121200ED456B /* AsynchronousTest.swift in Sources */,
14521464
1F0648CC19639F5A001F9C46 /* ObjectWithLazyProperty.swift in Sources */,
14531465
1F4A56851A3B33A0009E1637 /* ObjCBeTruthyTest.m in Sources */,
@@ -1508,6 +1520,7 @@
15081520
1F5DF1711BDCA0F500C3A531 /* DSL+Wait.swift in Sources */,
15091521
1F1871D61CA89EEF00A34BF2 /* DSL.m in Sources */,
15101522
1F5DF17D1BDCA0F500C3A531 /* BeGreaterThanOrEqualTo.swift in Sources */,
1523+
857D184B253610B200D8693A /* BeWithin.swift in Sources */,
15111524
AE7ADE471C80BF8000B94CD3 /* MatchError.swift in Sources */,
15121525
1FC494AC1C29CBA40010975C /* NimbleEnvironment.swift in Sources */,
15131526
1F5DF18E1BDCA0F500C3A531 /* Stringers.swift in Sources */,
@@ -1588,6 +1601,7 @@
15881601
1F5DF19D1BDCA10200C3A531 /* BeGreaterThanOrEqualToTest.swift in Sources */,
15891602
A8A3B6F7207329DE00E25A08 /* SatisfyAllOfTest.swift in Sources */,
15901603
1F5DF1A41BDCA10200C3A531 /* BeNilTest.swift in Sources */,
1604+
857D18512536124500D8693A /* BeWithinTest.swift in Sources */,
15911605
7A6AB2C41E7F547E00A2F694 /* ToSucceedTest.swift in Sources */,
15921606
CD79C9A71D2CC848004B6F9A /* ObjCBeGreaterThanTest.m in Sources */,
15931607
CD79C9A51D2CC848004B6F9A /* ObjCBeginWithTest.m in Sources */,
@@ -1641,6 +1655,7 @@
16411655
62FB326223B78BF90047BED9 /* BeginWithPrefix.swift in Sources */,
16421656
1FD8CD5B1968AB07008ED995 /* Equal.swift in Sources */,
16431657
CDF5C57B2647B89B0036532C /* Equal+Tuple.swift in Sources */,
1658+
857D1849253610A900D8693A /* BeWithin.swift in Sources */,
16441659
1FD8CD4D1968AB07008ED995 /* BeLessThan.swift in Sources */,
16451660
1FD8CD471968AB07008ED995 /* BeGreaterThan.swift in Sources */,
16461661
F8A1BE301CB3710900031679 /* XCTestObservationCenter+Register.m in Sources */,
@@ -1733,6 +1748,7 @@
17331748
1F4A568C1A3B3407009E1637 /* ObjCBeTrueTest.m in Sources */,
17341749
DDEFAEB51A93CBE6005CA37A /* ObjCAllPassTest.m in Sources */,
17351750
1F4A56801A3B333F009E1637 /* ObjCBeLessThanTest.m in Sources */,
1751+
857D184F2536124400D8693A /* BeWithinTest.swift in Sources */,
17361752
1F925EE7195C121200ED456B /* AsynchronousTest.swift in Sources */,
17371753
1F0648CD19639F5A001F9C46 /* ObjectWithLazyProperty.swift in Sources */,
17381754
1F4A56861A3B33A0009E1637 /* ObjCBeTruthyTest.m in Sources */,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// A Nimble matcher that succeeds when the actual value is within given range.
2+
public func beWithin<T: Comparable>(_ range: Range<T>) -> Predicate<T> {
3+
let errorMessage = "be within range <(\(range.lowerBound)..<\(range.upperBound))>"
4+
return Predicate.simple(errorMessage) { actualExpression in
5+
if let actual = try actualExpression.evaluate() {
6+
return PredicateStatus(bool: range.contains(actual))
7+
}
8+
return .fail
9+
}
10+
}
11+
12+
/// A Nimble matcher that succeeds when the actual value is within given range.
13+
public func beWithin<T: Comparable>(_ range: ClosedRange<T>) -> Predicate<T> {
14+
let errorMessage = "be within range <(\(range.lowerBound)...\(range.upperBound))>"
15+
return Predicate.simple(errorMessage) { actualExpression in
16+
if let actual = try actualExpression.evaluate() {
17+
return PredicateStatus(bool: range.contains(actual))
18+
}
19+
return .fail
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Foundation
2+
import XCTest
3+
import Nimble
4+
5+
final class BeWithinTest: XCTestCase {
6+
func testBeWithin() {
7+
expect(0.1).to(beWithin(0.1...1.1))
8+
expect(5).to(beWithin(3...5))
9+
expect(-3).to(beWithin(-7...5))
10+
11+
expect(0.3).toNot(beWithin(0.31...0.99))
12+
expect(2).toNot(beWithin(0..<2))
13+
expect(-7.1).toNot(beWithin(-14.3..<(-7.2)))
14+
15+
failsWithErrorMessage("expected to be within range <(0.1...1.1)>, got <0>") {
16+
expect(0).to(beWithin(0.1...1.1))
17+
}
18+
19+
failsWithErrorMessage("expected to be within range <(0..<2)>, got <2>") {
20+
expect(2).to(beWithin(0..<2))
21+
}
22+
23+
failsWithErrorMessage("expected to not be within range <(0.31...0.99)>, got <0.31>") {
24+
expect(0.31).toNot(beWithin(0.31...0.99))
25+
}
26+
27+
failsWithErrorMessage("expected to not be within range <(0.0..<2.1)>, got <2>") {
28+
expect(2).toNot(beWithin(0..<2.1))
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)