0% found this document useful (0 votes)
147 views14 pages

Hamcrest PDF

The document discusses Hamcrest matchers, which are used to assert that values match expected patterns. It provides examples of using various matchers like: - text, sequence, either, optional, zeroOrMore, oneOrMore to match plain text patterns - capture to extract matched groups - defining patterns in terms of other patterns, as shown in the email address matching example - other matchers like listOf, whitespace, caseInsensitive The examples show how to create PatternMatcher objects from patterns and assert that strings match or don't match those patterns using matchesPattern assertions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views14 pages

Hamcrest PDF

The document discusses Hamcrest matchers, which are used to assert that values match expected patterns. It provides examples of using various matchers like: - text, sequence, either, optional, zeroOrMore, oneOrMore to match plain text patterns - capture to extract matched groups - defining patterns in terms of other patterns, as shown in the email address matching example - other matchers like listOf, whitespace, caseInsensitive The examples show how to create PatternMatcher objects from patterns and assert that strings match or don't match those patterns using matchesPattern assertions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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:[email protected]",
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