You are on page 1of 14

Hamcrest

Matchers
Agenda
jUnit.Assert.assertThat
PatternMatcher.matchesPattern;
Patterns.anyCharacter;
Patterns.anyCharacterIn;
Patterns.anyCharacterInCategory;
Patterns.anyCharacterNotIn;
Patterns.anyCharacterNotInCategory;
Patterns.capture;
Patterns.caseInsensitive;
Patterns.caseSensitive;
Patterns.either;
Patterns.exactly;
Patterns.from;
Patterns.listOf;
Patterns.oneOrMore;
Patterns.optional;
Patterns.separatedBy;
Patterns.sequence;
Patterns.text;
Patterns.valueOf;
Patterns.whitespace;
Patterns.zeroOrMore;
Assert That
assertEquals(Esperado, algoCalculado() );
A lgica que algoCalculado() produza um resultado Esperado
assertThat(Dados, processaDados() );
Diferentemente de assertEquals, os Dados sero submetidos para
processaDados(), que os recebero como um parmetro de entrada.
Pattern matcher
Cria um Macher
O matcher
@Test
public void matchesPlainText() {
PatternMatcher matcher = new PatternMatcher(text("text"));
assertThat("text", matchesPattern(matcher));
assertThat("xxxtextxxx", not(matchesPattern(matcher)));
assertThat("tex", not(matchesPattern(matcher)));
assertThat("blah", not(matchesPattern(matcher)));
}
Regex
Filtros e expresses de texto
@Test
public void matchesPlainTextContainingSpecialRegexCharacters() {
assertThat("*star*", matchesPattern(
new PatternMatcher(text("*star*"))));

assertThat("-", matchesPattern(
new PatternMatcher(text("-"))));
}
Sequncia
@Test
public void matchesSequenceOfText() {
PatternMatcher sequencia = new PatternMatcher(
sequence("Ola", " ", "mundo"));
assertThat("Ola mundo", matchesPattern(sequencia));
}
Either - Qualquer
@Test
public void matchesAlternatives() {
PatternMatcher matcher = new PatternMatcher(
either(text("isto"), text("aquilo")));
assertThat("isto", matchesPattern(matcher));
assertThat("aquilo", matchesPattern(matcher));
assertThat("isto aquilo", not(matchesPattern(matcher)));
}
Opcional
@Test
public void matchesOptionalPattern() {
PatternMatcher matcher = new PatternMatcher(
sequence(text("isto"), optional(text(" aquilo"))));
assertThat("isto", matchesPattern(matcher));
assertThat("isto aquilo", matchesPattern(matcher));
assertThat(" aquilo", not(matchesPattern(matcher)));
}
Zero ou mais
@Test
public void matchesRepetitionZeroOrMoreTimes() {
PatternMatcher matcher = new PatternMatcher(
zeroOrMore(text("x")));
assertThat("", matchesPattern(matcher));
assertThat("x", matchesPattern(matcher));
assertThat("xxx", matchesPattern(matcher));
assertThat(" xx", not(matchesPattern(matcher)));
assertThat("x x", not(matchesPattern(matcher)));
assertThat("xx ", not(matchesPattern(matcher)));
}
Um ou mais
@Test
public void matchesRepetitionOneOrMoreTimes() {
PatternMatcher matcher = new PatternMatcher(
oneOrMore(text("x")));
assertThat("", not(matchesPattern(matcher)));
assertThat("x", matchesPattern(matcher));
assertThat("xxx", matchesPattern(matcher));
assertThat(" xx", not(matchesPattern(matcher)));
assertThat("x x", not(matchesPattern(matcher)));
assertThat("xx ", not(matchesPattern(matcher)));
}
Parse - get
@Test public void testCapturesMatchedGroups() throws Exception {
PatternMatcher matcher = new PatternMatcher(
sequence(capture("xs", oneOrMore(text("x"))), capture("ys", oneOrMore(text("y")))));
Parse parse;
parse = matcher.parse("xxxyyy");
assertEquals("xxx", parse.get("xs"));
assertEquals("yyy", parse.get("ys"));
parse = matcher.parse("xxyyyyyy");
assertEquals("xx", parse.get("xs"));
assertEquals("yyyyyy", parse.get("ys"));
}
email
@Test public void testCanDefinePatternsInTermsOfExistingPatterns() {
PatternMatcher emailAddressMatcher = new PatternMatcher(
sequence(capture("user", oneOrMore(anyCharacter())), "@",
capture("host", oneOrMore(anyCharacter()))));
PatternMatcher mailToURLMatcher = new PatternMatcher(
sequence(capture("scheme", text("mailto")), ":",
capture("email", emailAddressMatcher)));
assertThat("mailto:npryce@users.sf.net",
matchesPattern(mailToURLMatcher));
}
Outros
PatternMatcher matcher = new PatternMatcher(sequence("a", whitespace(), "z"));
assertThat("az", matchesPattern(matcher));
PatternMatcher matcher = new PatternMatcher(listOf("x"));
assertThat("x,x,x,x,x", matchesPattern(matcher));
PatternMatcher matcher = new PatternMatcher(sequence("a", whitespace(), "z"));
assertThat("az", matchesPattern(matcher));
Referncias
Hancrest -
https://code.google.com/p/hamcrest-text-patterns/

You might also like