forked from mockito/mockito
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
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/org/mockito/internal/matchers/VarargMatcherImpl.java
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,32 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.internal.matchers; | ||
|
||
import org.mockito.ArgumentMatcher; | ||
|
||
/** | ||
* This class is used to verify with a vararg matcher | ||
*/ | ||
public class VarargMatcherImpl<T> implements VarargMatcher, ArgumentMatcher<T> { | ||
private T[] values; | ||
private int iterator; | ||
public VarargMatcherImpl(T... value) { | ||
this.values = value; | ||
} | ||
|
||
@Override | ||
public boolean matches(T argument){ | ||
iterator %= values.length; | ||
return values[iterator++].equals(argument); | ||
} | ||
|
||
public String toString() { | ||
String name = values.getClass().getComponentType().getSimpleName(); | ||
String str = "vararg<"+name+">("; | ||
for(int i = 0 ; i < values.length; i++) | ||
str += values[i] + (i < values.length - 1 ? ", " : ")"); | ||
return str; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/org/mockito/internal/matchers/VarargMatcherImplTest.java
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,43 @@ | ||
/* | ||
* Copyright (c) 2017 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.internal.matchers; | ||
|
||
import org.junit.Test; | ||
import org.mockito.ArgumentMatcher; | ||
import org.mockitousage.IMethods; | ||
import org.mockitoutil.TestBase; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* This test is used to use vararg matcher instead of arrays | ||
*/ | ||
public class VarargMatcherImplTest extends TestBase { | ||
|
||
@Test | ||
public void testMatcher() throws Exception { | ||
ArgumentMatcher<String> matcher = new VarargMatcherImpl<String>("1", "2", "3"); | ||
assertTrue(matcher.matches("1")); | ||
assertTrue(matcher.matches("2")); | ||
assertTrue(matcher.matches("3")); | ||
} | ||
|
||
@Test | ||
public void testMatcherString() throws Exception { | ||
ArgumentMatcher<String> matcher = new VarargMatcherImpl<String>("1", "2", "3"); | ||
assertTrue(matcher.toString().equals("vararg<String>(1, 2, 3)")); | ||
} | ||
|
||
@Test | ||
public void should_verify_varargs_as_array() throws Exception { | ||
IMethods mock = mock(IMethods.class); | ||
mock.mixedVarargs("1", "2", "3"); | ||
verify(mock).mixedVarargs(any(), argThat(new VarargMatcherImpl<String>("2", "3"))); | ||
} | ||
} |