core tests: add custom string matchers

This commit is contained in:
Skylot
2014-06-28 15:38:50 +04:00
parent 37857e88ea
commit 0a241e3a9c
2 changed files with 47 additions and 0 deletions
@@ -0,0 +1,33 @@
package jadx.tests.utils;
import org.hamcrest.core.SubstringMatcher;
public class CountString extends SubstringMatcher {
private final int count;
public CountString(int count, String substring) {
super(substring);
this.count = count;
}
@Override
protected boolean evalSubstringOf(String string) {
return this.count == countStr(string, substring);
}
@Override
protected String relationship() {
return "containing " + count + " occurrence of";
}
private static int countStr(String string, String substring) {
int cnt = 0;
int idx = 0;
while ((idx = string.indexOf(substring, idx)) != -1) {
idx++;
cnt++;
}
return cnt;
}
}
@@ -0,0 +1,14 @@
package jadx.tests.utils;
import org.hamcrest.Matcher;
public class JadxMatchers {
public static Matcher<String> countString(int count, String substring) {
return new CountString(count, substring);
}
public static Matcher<String> containsOne(String substring) {
return new CountString(1, substring);
}
}