Skip to content

Commit

Permalink
Added contains and doesn't contain at index matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen Mols committed Nov 3, 2016
1 parent 8cc4588 commit 4eb9368
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public void customMatchers() throws Exception {
## Features
```java
verify(mock).someMethod(listContains(expectedObject));
verify(mock).someMethod(listContains(expectedObject, index));
verify(mock).someMethod(listDoesNotContain(wrongObject));
verify(mock).someMethod(listDoesNotContain(wrongObject, index));
verify(mock).someMethod(listOfSize(3));

verify(mock).someMethod(setContains(expectedObject));
Expand All @@ -52,7 +54,7 @@ verify(mock).someMethod(setContains(expectedObject));
```
2) Add a dependency on the library:
```groovy
testCompile 'com.github.JeroenMols:MockitoCollectionMatchers:0.0.1'
testCompile 'com.github.JeroenMols:MockitoCollectionMatchers:0.0.2'
```

## TODO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package com.jeroenmols.mockitocollectionmatchers;

import com.jeroenmols.mockitocollectionmatchers.list.ListContains;
import com.jeroenmols.mockitocollectionmatchers.list.ListDoesNotContain;
import com.jeroenmols.mockitocollectionmatchers.list.ListOfSize;
import com.jeroenmols.mockitocollectionmatchers.list.*;

import java.util.List;

Expand All @@ -34,10 +32,18 @@ public static <K> List listContains(K... object) {
return argThat(new ListContains<K>(object));
}

public static <K> List listContains(K object, int index) {
return argThat(new ListContainsAtIndex<K>(object, index));
}

public static <K> List listDoesNotContain(K... object) {
return argThat(new ListDoesNotContain<K>(object));
}

public static <K> List listDoesNotContain(K object, int index) {
return argThat(new ListDoesNotContainAtIndex<K>(object, index));
}

public static List listOfSize(int size) {
return argThat(new ListOfSize(size));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2016 Jeroen Mols
*
* 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 com.jeroenmols.mockitocollectionmatchers.list;

import org.mockito.ArgumentMatcher;

import java.util.List;

/**
* @author Jeroen Mols on 11/09/16.
*/
public class ListContainsAtIndex<T> implements ArgumentMatcher<List> {

private final T object;
private final int index;

public ListContainsAtIndex(T object, int index) {
this.object = object;
this.index = index;
}

public boolean matches(List list) {
return object == list.get(index);
}

public String toString() {
return "[list doesn't contain object at index]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2016 Jeroen Mols
*
* 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 com.jeroenmols.mockitocollectionmatchers.list;

import org.mockito.ArgumentMatcher;

import java.util.List;

/**
* @author Jeroen Mols on 11/09/16.
*/
public class ListDoesNotContainAtIndex<T> implements ArgumentMatcher<List> {

private final T object;
private final int index;

public ListDoesNotContainAtIndex(T object, int index) {
this.object = object;
this.index = index;
}

public boolean matches(List list) {
return object != list.get(index);
}

public String toString() {
//printed in verification errors
return "[list does contain object at index]";
}
}

0 comments on commit 4eb9368

Please sign in to comment.