Skip to content

Commit

Permalink
Added new hasItem(<T>, atIndex: Int) matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Pirringer committed Dec 2, 2020
1 parent c91adb0 commit e7d3186
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 2.2.2
-------------
_2 Dec 2020_

- Added new hasItem(<T>, atIndex:Int) matcher


Version 2.2.1
------------
_15 Apr 2019_
Expand Down
12 changes: 12 additions & 0 deletions Hamcrest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
24D9341E19A23DC200A411BD /* SequenceMatcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24D9341D19A23DC200A411BD /* SequenceMatcherTests.swift */; };
24E64D3B19A29E6100090B1D /* MatcherMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E64D3A19A29E6100090B1D /* MatcherMocks.swift */; };
24F95B241B80EBD0004A0F4C /* ErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24F95B231B80EBD0004A0F4C /* ErrorTests.swift */; };
A7D357EB2577A8EF00AAE489 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7D357E82577A8EE00AAE489 /* Package.swift */; };
A7D357EC2577A8EF00AAE489 /* CHANGELOG.md in Resources */ = {isa = PBXBuildFile; fileRef = A7D357E92577A8EE00AAE489 /* CHANGELOG.md */; };
A7D357ED2577A8EF00AAE489 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = A7D357EA2577A8EF00AAE489 /* README.md */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -114,6 +117,9 @@
24D9341D19A23DC200A411BD /* SequenceMatcherTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SequenceMatcherTests.swift; sourceTree = "<group>"; };
24E64D3A19A29E6100090B1D /* MatcherMocks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MatcherMocks.swift; sourceTree = "<group>"; };
24F95B231B80EBD0004A0F4C /* ErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorTests.swift; sourceTree = "<group>"; };
A7D357E82577A8EE00AAE489 /* Package.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
A7D357E92577A8EE00AAE489 /* CHANGELOG.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
A7D357EA2577A8EF00AAE489 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -164,6 +170,9 @@
2496DAE719922407008C270E = {
isa = PBXGroup;
children = (
A7D357E92577A8EE00AAE489 /* CHANGELOG.md */,
A7D357E82577A8EE00AAE489 /* Package.swift */,
A7D357EA2577A8EF00AAE489 /* README.md */,
2496DAF319922407008C270E /* Hamcrest */,
2496DAFD19922407008C270E /* HamcrestTests */,
242D160019B7A3E30099EA62 /* Frameworks */,
Expand Down Expand Up @@ -403,6 +412,8 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A7D357ED2577A8EF00AAE489 /* README.md in Resources */,
A7D357EC2577A8EF00AAE489 /* CHANGELOG.md in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -500,6 +511,7 @@
2496DB0B19922429008C270E /* Hamcrest.swift in Sources */,
2428C4BE19A3E3E100475822 /* ArithmeticMatchers.swift in Sources */,
2428C4BA19A3E1F300475822 /* StringMatchers.swift in Sources */,
A7D357EB2577A8EF00AAE489 /* Package.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
9 changes: 9 additions & 0 deletions Hamcrest.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Hamcrest/README.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ assertThat(array, containsInAnyOrder(equalTo("bar"), equalTo("foo")))
assertThat(array, hasItem(equalTo("foo")))
assertThat(array, hasItem(equalTo("baz"))) // mismatch

assertThat(array, hasItem(equalTo("foo", atIndex: 0)))
assertThat(array, hasItem(equalTo("foo", atIndex: 1))) // mismatch

assertThat(array, hasItems("foo", "bar"))
assertThat(array, hasItems(equalTo("foo"), equalTo("baz"))) // mismatch

Expand Down
3 changes: 2 additions & 1 deletion Hamcrest/README.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx' display-mode='rendered' last-migration='0810'>
<playground version='5.0' target-platform='macos' display-mode='rendered' buildActiveScheme='true' last-migration='0810'>
<timeline fileName='timeline.xctimeline'/>
</playground>
19 changes: 19 additions & 0 deletions Hamcrest/SequenceMatchers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ public func hasItem<T: Equatable, S: Sequence>(_ expectedValue: T) -> Matcher<S>
return hasItem(equalToWithoutDescription(expectedValue))
}

private func hasItem<T, S: Sequence>(matcher: Matcher<T>, values: S, atIndex: Int) -> Bool where S.Iterator.Element == T {
for (index, value) in values.enumerated() {
if matcher.matches(value).boolValue && index == atIndex {
return true
}
}
return false
}

public func hasItem<T, S: Sequence>(_ matcher: Matcher<T>, atIndex index: Int) -> Matcher<S> where S.Iterator.Element == T {
Matcher("a sequence containing \(matcher.description) at index \(index)") { (values: S) -> Bool in
hasItem(matcher: matcher, values: values, atIndex: index)
}
}

public func hasItem<T: Equatable, S: Sequence>(_ expectedValue: T, atIndex index: Int) -> Matcher<S> where S.Iterator.Element == T {
hasItem(equalToWithoutDescription(expectedValue), atIndex: index)
}

private func hasItems<T, S: Sequence>(_ matchers: [Matcher<T>]) -> Matcher<S> where S.Iterator.Element == T {
return Matcher("a sequence containing \(joinMatcherDescriptions(matchers))") { (values: S) -> MatchResult in
var missingItems = [] as [Matcher<T>]
Expand Down
12 changes: 12 additions & 0 deletions HamcrestTests/SequenceMatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,16 @@ class SequenceMatcherTests: BaseTestCase {
"a sequence containing in any order [equal to item3, equal to wrong, equal to item1]",
mismatchDescription: "mismatch: GOT: \"item2\", EXPECTED: equal to wrong")
}

func testHasItemAtIndex() {
assertMatch(sequence, hasItem("item1", atIndex: 0))
assertMatch(sequence, hasItem("item2", atIndex: 1))
assertMatch(sequence, hasItem("item3", atIndex: 2))
}

func testHasItemEqualAtIndex() {
assertMatch(sequence, hasItem(equalTo("item1"), atIndex: 0))
assertMatch(sequence, hasItem(equalTo("item2"), atIndex: 1))
assertMatch(sequence, hasItem(equalTo("item3"), atIndex: 2))
}
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ assertThat(array, hasItem(equalTo("foo"))) // ✓
assertThat(array, hasItem(equalTo("baz")))
// GOT: [foo, bar], EXPECTED: a sequence containing equal to baz

assertThat(array, hasItem("foo", atIndex: 0))) //
assertThat(array, hasItem("foo", atIndex: 1))) // GOT: ["foo", "bar"], EXPECTED: a sequence containing "foo" at index 1"

assertThat(array, hasItem(equalTo("foo"), atIndex: 0))) //
assertThat(array, hasItem(equalTo("foo"), atIndex: 1))) // GOT: ["foo", "bar"], EXPECTED: a sequence containing "foo" at index 1"


assertThat(array, hasItems("foo", "bar")) //
assertThat(array, hasItems(equalTo("foo"), equalTo("baz")))
// GOT: [foo, bar] (missing item equal to baz),
Expand Down

0 comments on commit e7d3186

Please sign in to comment.