Ruy Vilão suggestions: no point in declaring the variables for the match result, let's put them directly in the assert (to be fixed in both tutorial and code).
This commit is contained in:
@@ -13,19 +13,17 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PatternJava11UnitTest {
|
||||
|
||||
private static final String VALID_NAME = "Fabio Silva";
|
||||
private static final String INVALID_NAME = "Fabio Luis Silva";
|
||||
private static final List<String> NAMES_TO_VALIDATE = Arrays.asList(VALID_NAME, INVALID_NAME);
|
||||
private static final Pattern FIRST_LAST_NAME_PRE_COMPILED_PATTERN = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}");
|
||||
|
||||
@Test
|
||||
public void givenPreCompiledPattern_whenCallAsMatchPredicate_thenReturnMatchPredicateToMatchesThePatternInTheListElements() {
|
||||
Predicate<String> patternAsMatchPredicate = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.asMatchPredicate();
|
||||
List<String> validatedNames = NAMES_TO_VALIDATE.stream()
|
||||
List<String> namesToValidate = Arrays.asList("Fabio Silva", "Fabio Luis Silva");
|
||||
Pattern firstLastNamePreCompiledPattern = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}");
|
||||
|
||||
Predicate<String> patternAsMatchPredicate = firstLastNamePreCompiledPattern.asMatchPredicate();
|
||||
List<String> validatedNames = namesToValidate.stream()
|
||||
.filter(patternAsMatchPredicate)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertTrue(validatedNames.contains(VALID_NAME));
|
||||
assertFalse(validatedNames.contains(INVALID_NAME));
|
||||
assertTrue(validatedNames.contains("Fabio Silva"));
|
||||
assertFalse(validatedNames.contains("Fabio Luis Silva"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user