33import static junit .framework .TestCase .assertEquals ;
44import static org .junit .Assert .assertThat ;
55import static org .hamcrest .CoreMatchers .*;
6+ import java .util .regex .Matcher ;
7+ import java .util .regex .Pattern ;
68
79import org .junit .Test ;
810
@@ -11,57 +13,59 @@ public class EscapingCharsTest {
1113 public void givenRegexWithDot_whenMatchingStr_thenMatches () {
1214 String strInput = "foof" ;
1315 String strRegex = "foo." ;
14- EscapingChars e = new EscapingChars ();
15-
16- assertEquals (true , e .isMatching (strInput , strRegex ));
16+
17+ assertEquals (true , strInput .matches (strRegex ));
1718 }
18-
19+
1920 @ Test
2021 public void givenRegexWithDotEsc_whenMatchingStr_thenNotMatching () {
2122 String strInput = "foof" ;
2223 String strRegex = "foo\\ ." ;
23- EscapingChars e = new EscapingChars ();
24-
25- assertEquals (false , e .isMatching (strInput , strRegex ));
24+
25+ assertEquals (false , strInput .matches (strRegex ));
2626 }
27-
27+
2828 @ Test
2929 public void givenRegexWithPipeEscaped_whenSplitStr_thenSplits () {
3030 String strInput = "foo|bar|hello|world" ;
3131 String strRegex = "\\ Q|\\ E" ;
32- EscapingChars e = new EscapingChars ();
33-
34- assertEquals (4 , e .splitAndCountWords (strInput , strRegex ));
32+
33+ assertEquals (4 , strInput .split (strRegex ).length );
3534 }
36-
35+
3736 @ Test
3837 public void givenRegexWithPipeEscQuoteMeth_whenSplitStr_thenSplits () {
3938 String strInput = "foo|bar|hello|world" ;
4039 String strRegex = "|" ;
41- EscapingChars e = new EscapingChars ();
42-
43- assertEquals (4 , e .splitAndCountWordsUsingQuoteMethod (strInput , strRegex ));
40+
41+ assertEquals (4 ,strInput .split (Pattern .quote (strRegex )).length );
4442 }
45-
43+
4644 @ Test
4745 public void givenRegexWithDollar_whenReplacing_thenNotReplace () {
48- String strInput = "I gave $50 to my brother.He bought candy for $35. Now he has $15 left." ;
46+ String strInput = "I gave $50 to my brother."
47+ + "He bought candy for $35. Now he has $15 left." ;
4948 String strRegex = "$" ;
50- String strReplacement = "�" ;
51- String output = "I gave �50 to my brother.He bought candy for �35. Now he has �15 left." ;
52- EscapingChars e = new EscapingChars ();
53-
54- assertThat (output , not (equalTo (e .changeCurrencySymbol (strInput , strRegex , strReplacement ))));
49+ String strReplacement = "£" ;
50+ String output = "I gave £50 to my brother."
51+ + "He bought candy for £35. Now he has £15 left." ;
52+ Pattern p = Pattern .compile (strRegex );
53+ Matcher m = p .matcher (strInput );
54+
55+ assertThat (output , not (equalTo (m .replaceAll (strReplacement ))));
5556 }
56-
57+
5758 @ Test
5859 public void givenRegexWithDollarEsc_whenReplacing_thenReplace () {
59- String strInput = "I gave $50 to my brother. He bought candy for $35. Now he has $15 left." ;
60+ String strInput = "I gave $50 to my brother."
61+ + "He bought candy for $35. Now he has $15 left." ;
6062 String strRegex = "\\ $" ;
61- String strReplacement = "�" ;
62- String output = "I gave �50 to my brother. He bought candy for �35. Now he has �15 left." ;
63- EscapingChars e = new EscapingChars ();
64-
65- assertEquals (output , e .changeCurrencySymbol (strInput , strRegex , strReplacement ));
63+ String strReplacement = "£" ;
64+ String output = "I gave £50 to my brother."
65+ + "He bought candy for £35. Now he has £15 left." ;
66+ Pattern p = Pattern .compile (strRegex );
67+ Matcher m = p .matcher (strInput );
68+
69+ assertEquals (output ,m .replaceAll (strReplacement ));
6670 }
6771}
0 commit comments