@@ -15,13 +15,41 @@ public void whenMatchesTenDigitsNumber_thenCorrect() {
1515 Matcher matcher = pattern .matcher ("2055550125" );
1616 assertTrue (matcher .matches ());
1717 }
18+
19+ @ Test
20+ public void whenMOreThanTenDigits_thenNotCorrect () {
21+ Pattern pattern = Pattern .compile ("^\\ d{10}$" );
22+ Matcher matcher = pattern .matcher ("20555501251" );
23+ assertFalse (matcher .matches ());
24+ }
1825
1926 @ Test
2027 public void whenMatchesTenDigitsNumberWhitespacesDotHyphen_thenCorrect () {
2128 Pattern pattern = Pattern .compile ("^(\\ d{3}[- .]?){2}\\ d{4}$" );
2229 Matcher matcher = pattern .matcher ("202 555 0125" );
2330 assertTrue (matcher .matches ());
2431 }
32+
33+ @ Test
34+ public void whenIncludesBracket_thenNotCorrect () {
35+ Pattern pattern = Pattern .compile ("^(\\ d{3}[- .]?){2}\\ d{4}$" );
36+ Matcher matcher = pattern .matcher ("202]555 0125" );
37+ assertFalse (matcher .matches ());
38+ }
39+
40+ @ Test
41+ public void whenNotStartsWithBatchesOfThreeDigits_thenNotCorrect () {
42+ Pattern pattern = Pattern .compile ("^(\\ d{3}[- .]?){2}\\ d{4}$" );
43+ Matcher matcher = pattern .matcher ("2021 555 0125" );
44+ assertFalse (matcher .matches ());
45+ }
46+
47+ @ Test
48+ public void whenLastPartWithNoFourDigits_thenNotCorrect () {
49+ Pattern pattern = Pattern .compile ("^(\\ d{3}[- .]?){2}\\ d{4}$" );
50+ Matcher matcher = pattern .matcher ("202 555 012" );
51+ assertFalse (matcher .matches ());
52+ }
2553
2654 @ Test
2755 public void whenMatchesTenDigitsNumberParenthesis_thenCorrect () {
@@ -30,13 +58,41 @@ public void whenMatchesTenDigitsNumberParenthesis_thenCorrect() {
3058 assertTrue (matcher .matches ());
3159 }
3260
61+ @ Test
62+ public void whenJustOpeningParenthesis_thenNotCorrect () {
63+ Pattern pattern = Pattern .compile ("^((\\ (\\ d{3}\\ ))|\\ d{3})[- .]?\\ d{3}[- .]?\\ d{4}$" );
64+ Matcher matcher = pattern .matcher ("(202 555-0125" );
65+ assertFalse (matcher .matches ());
66+ }
67+
68+ @ Test
69+ public void whenJustClosingParenthesis_thenNotCorrect () {
70+ Pattern pattern = Pattern .compile ("^((\\ (\\ d{3}\\ ))|\\ d{3})[- .]?\\ d{3}[- .]?\\ d{4}$" );
71+ Matcher matcher = pattern .matcher ("202) 555-0125" );
72+ assertFalse (matcher .matches ());
73+ }
74+
3375 @ Test
3476 public void whenMatchesTenDigitsNumberPrefix_thenCorrect () {
3577 Pattern pattern = Pattern .compile ("^(\\ +\\ d{1,3}( )?)?((\\ (\\ d{3}\\ ))|\\ d{3})[- .]?\\ d{3}[- .]?\\ d{4}$" );
3678 Matcher matcher = pattern .matcher ("+111 (202) 555-0125" );
3779 assertTrue (matcher .matches ());
3880 }
3981
82+ @ Test
83+ public void whenIncorrectPrefix_thenNotCorrect () {
84+ Pattern pattern = Pattern .compile ("^(\\ +\\ d{1,3}( )?)?((\\ (\\ d{3}\\ ))|\\ d{3})[- .]?\\ d{3}[- .]?\\ d{4}$" );
85+ Matcher matcher = pattern .matcher ("-111 (202) 555-0125" );
86+ assertFalse (matcher .matches ());
87+ }
88+
89+ @ Test
90+ public void whenTooLongPrefix_thenNotCorrect () {
91+ Pattern pattern = Pattern .compile ("^(\\ +\\ d{1,3}( )?)?((\\ (\\ d{3}\\ ))|\\ d{3})[- .]?\\ d{3}[- .]?\\ d{4}$" );
92+ Matcher matcher = pattern .matcher ("+1111 (202) 555-0125" );
93+ assertFalse (matcher .matches ());
94+ }
95+
4096 @ Test
4197 public void whenMatchesPhoneNumber_thenCorrect () {
4298 String patterns
@@ -53,4 +109,21 @@ public void whenMatchesPhoneNumber_thenCorrect() {
53109 assertTrue (matcher .matches ());
54110 }
55111 }
112+
113+ @ Test
114+ public void whenNotMatchesPhoneNumber_thenNotCorrect () {
115+ String patterns
116+ = "^(\\ +\\ d{1,3}( )?)?((\\ (\\ d{3}\\ ))|\\ d{3})[- .]?\\ d{3}[- .]?\\ d{4}$"
117+ + "|^(\\ +\\ d{1,3}( )?)?(\\ d{3}[ ]?){2}\\ d{3}$"
118+ + "|^(\\ +\\ d{1,3}( )?)?(\\ d{3}[ ]?)(\\ d{2}[ ]?){2}\\ d{2}$" ;
119+
120+ String [] invalidPhoneNumbers
121+ = {"20555501251" ,"202]555 0125" , "2021 555 012" , "(202 555-0125" , "202) 555-0125" , "-111 (202) 555-0125" , "+1111 (202) 555-0125" , "636 85 789" , "636 85 67 893" };
122+
123+ Pattern pattern = Pattern .compile (patterns );
124+ for (String phoneNumber : invalidPhoneNumbers ) {
125+ Matcher matcher = pattern .matcher (phoneNumber );
126+ assertFalse (matcher .matches ());
127+ }
128+ }
56129}
0 commit comments