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:
Fabio Silva 2019-12-08 15:57:34 -03:00
parent 61ef5289a5
commit 9f78f62732
2 changed files with 15 additions and 22 deletions

View File

@ -13,19 +13,17 @@ import static org.junit.Assert.assertTrue;
public class PatternJava11UnitTest { 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 @Test
public void givenPreCompiledPattern_whenCallAsMatchPredicate_thenReturnMatchPredicateToMatchesThePatternInTheListElements() { public void givenPreCompiledPattern_whenCallAsMatchPredicate_thenReturnMatchPredicateToMatchesThePatternInTheListElements() {
Predicate<String> patternAsMatchPredicate = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.asMatchPredicate(); List<String> namesToValidate = Arrays.asList("Fabio Silva", "Fabio Luis Silva");
List<String> validatedNames = NAMES_TO_VALIDATE.stream() Pattern firstLastNamePreCompiledPattern = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}");
Predicate<String> patternAsMatchPredicate = firstLastNamePreCompiledPattern.asMatchPredicate();
List<String> validatedNames = namesToValidate.stream()
.filter(patternAsMatchPredicate) .filter(patternAsMatchPredicate)
.collect(Collectors.toList()); .collect(Collectors.toList());
assertTrue(validatedNames.contains(VALID_NAME)); assertTrue(validatedNames.contains("Fabio Silva"));
assertFalse(validatedNames.contains(INVALID_NAME)); assertFalse(validatedNames.contains("Fabio Luis Silva"));
} }
} }

View File

@ -15,17 +15,12 @@ import static org.junit.Assert.*;
public class PatternUnitTest { public class PatternUnitTest {
private static final Pattern FIRST_LAST_NAME_PRE_COMPILED_PATTERN = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}"); private static final Pattern FIRST_LAST_NAME_PRE_COMPILED_PATTERN = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}");
private static final String VALID_NAME = "Fabio Silva";
private static final String INVALID_NAME = "Mr. Silva";
private static final List<String> NAMES_TO_VALIDATE = Arrays.asList(VALID_NAME, INVALID_NAME);
private static final Pattern SPLIT_PRE_COMPILED_PATTERN = Pattern.compile("__"); private static final Pattern SPLIT_PRE_COMPILED_PATTERN = Pattern.compile("__");
private static final String TEXT_TO_SPLIT = "My_Name__is__Fabio_Silva";
@Test @Test
public void givenPreCompiledPattern_whenCallMatcher_thenReturnAMatcherToMatches() { public void givenPreCompiledPattern_whenCallMatcher_thenReturnAMatcherToMatches() {
Matcher matcherName1 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher(VALID_NAME); Matcher matcherName1 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher("Fabio Silva");
Matcher matcherName2 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher(INVALID_NAME); Matcher matcherName2 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher("Mr. Silva");
boolean matchesName1 = matcherName1.matches(); boolean matchesName1 = matcherName1.matches();
boolean matchesName2 = matcherName2.matches(); boolean matchesName2 = matcherName2.matches();
@ -36,19 +31,20 @@ public class PatternUnitTest {
@Test @Test
public void givenPreCompiledPattern_whenCallAsPredicate_thenReturnPredicateToFindThePatternInTheListElements() { public void givenPreCompiledPattern_whenCallAsPredicate_thenReturnPredicateToFindThePatternInTheListElements() {
List<String> namesToValidate = Arrays.asList("Fabio Silva", "Mr. Silva");
Predicate<String> patternsAsPredicate = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.asPredicate(); Predicate<String> patternsAsPredicate = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.asPredicate();
List<String> validNames = NAMES_TO_VALIDATE.stream() List<String> validNames = namesToValidate.stream()
.filter(patternsAsPredicate) .filter(patternsAsPredicate)
.collect(Collectors.toList()); .collect(Collectors.toList());
assertEquals(1,validNames.size()); assertEquals(1, validNames.size());
assertTrue(validNames.contains(VALID_NAME)); assertTrue(validNames.contains("Fabio Silva"));
} }
@Test @Test
public void givenPreCompiledPattern_whenCallSplit_thenReturnArrayWithValuesSplitByThePattern() { public void givenPreCompiledPattern_whenCallSplit_thenReturnArrayWithValuesSplitByThePattern() {
String[] textSplit = SPLIT_PRE_COMPILED_PATTERN.split(TEXT_TO_SPLIT); String[] textSplit = SPLIT_PRE_COMPILED_PATTERN.split("My_Name__is__Fabio_Silva");
assertEquals("My_Name", textSplit[0]); assertEquals("My_Name", textSplit[0]);
assertEquals("is", textSplit[1]); assertEquals("is", textSplit[1]);
@ -57,12 +53,11 @@ public class PatternUnitTest {
@Test @Test
public void givenPreCompiledPattern_whenCallSplitAsStream_thenReturnArrayWithValuesSplitByThePattern() { public void givenPreCompiledPattern_whenCallSplitAsStream_thenReturnArrayWithValuesSplitByThePattern() {
Stream<String> textSplitAsStream = SPLIT_PRE_COMPILED_PATTERN.splitAsStream(TEXT_TO_SPLIT); Stream<String> textSplitAsStream = SPLIT_PRE_COMPILED_PATTERN.splitAsStream("My_Name__is__Fabio_Silva");
String[] textSplit = textSplitAsStream.toArray(String[]::new); String[] textSplit = textSplitAsStream.toArray(String[]::new);
assertEquals("My_Name", textSplit[0]); assertEquals("My_Name", textSplit[0]);
assertEquals("is", textSplit[1]); assertEquals("is", textSplit[1]);
assertEquals("Fabio_Silva", textSplit[2]); assertEquals("Fabio_Silva", textSplit[2]);
} }
} }