Explorar el Código

all tests pass and names are replaced

mpierse hace 6 años
padre
commit
ae2e6e01ff
Se han modificado 2 ficheros con 47 adiciones y 26 borrados
  1. 20
    7
      src/main/java/ReplaceNames.java
  2. 27
    19
      src/test/java/HamletParserTest.java

+ 20
- 7
src/main/java/ReplaceNames.java Ver fichero

@@ -1,19 +1,32 @@
1
+import java.util.regex.Matcher;
1 2
 import java.util.regex.Pattern;
2 3
 
3 4
 public class ReplaceNames {
4 5
 
5
-    Pattern pattern;
6
+   Pattern pattern;
6 7
 
7
-    public boolean isHoratio(String word) {
8
-        return pattern.matches("([Hh][Oo][Rr][Aa][Tt][Ii][Oo])\\H*", word);
8
+
9
+    public boolean findHamlet(String word) {
10
+       Pattern hamletPattern = pattern.compile("([Hh][Aa][Mm][Ll][Ee][Tt])\\H*");
11
+       Matcher hamletMatcher = hamletPattern.matcher(word);
12
+       return hamletMatcher.find();
9 13
     }
10 14
 
11
-    public boolean isHamlet(String word) {
12
-        return pattern.matches("([Hh][Aa][Mm][Ll][Ee][Tt])\\H*", word);
15
+    public boolean findHoratio(String word) {
16
+        Pattern horatioPattern = pattern.compile("([Hh][Oo][Rr][Aa][Tt][Ii][Oo])\\H*");
17
+        Matcher horatioMatch= horatioPattern.matcher(word);
18
+        return horatioMatch.find();
13 19
     }
14 20
 
15
-    public int findHoratio(String word) {
16
-        return 0;
21
+    public String replaceHoratio(String string) {
22
+        Pattern horatioPattern = pattern.compile("([Hh][Oo][Rr][Aa][Tt][Ii][Oo])\\H*");
23
+        Matcher horatioMatch= horatioPattern.matcher(string);
24
+       return horatioMatch.replaceAll("Tariq");
17 25
     }
18 26
 
27
+    public String replaceHamlet(String string) {
28
+            Pattern hamletPattern = pattern.compile("([Hh][Aa][Mm][Ll][Ee][Tt])\\H*");
29
+            Matcher hamletMatcher = hamletPattern.matcher(string);
30
+          return  hamletMatcher.replaceAll("Leon");
31
+    }
19 32
 }

+ 27
- 19
src/test/java/HamletParserTest.java Ver fichero

@@ -2,61 +2,69 @@ import org.junit.Assert;
2 2
 import org.junit.Before;
3 3
 import org.junit.Test;
4 4
 
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+
5 8
 import static org.junit.Assert.*;
6 9
 
7 10
 public class HamletParserTest {
8 11
     private String hamletText;
9 12
     private HamletParser hamletParser;
10 13
     private ReplaceNames replaceNames;
14
+    private Pattern pattern;
15
+
11 16
 
12 17
     @Before
13 18
     public void setUp() {
14 19
         this.hamletParser = new HamletParser();
15 20
         this.hamletText = hamletParser.getHamletData();
16 21
         this.replaceNames = new ReplaceNames();
22
+
17 23
     }
18 24
 
19 25
     @Test
20 26
     public void testChangeHamletToLeon() {
27
+        Pattern leonPattern = pattern.compile("[Leon]");
28
+        Matcher leonMatcher = leonPattern.matcher(hamletText);
29
+        hamletText = replaceNames.replaceHamlet(hamletText);
30
+        Assert.assertTrue(leonMatcher.find());
21 31
     }
22 32
 
23 33
     @Test
24 34
     public void testChangeHoratioToTariq() {
35
+        Pattern leonPattern = pattern.compile("[Tariq]");
36
+        Matcher tariqMatcher = leonPattern.matcher(hamletText);
37
+        hamletText = replaceNames.replaceHoratio(hamletText);
38
+        Assert.assertTrue(tariqMatcher.find());
25 39
     }
26 40
 
27
-    @Test
28
-    public void testIsHoratioTrue(){
29
-        boolean actual = replaceNames.isHoratio("hORatIo's");
30
-        Assert.assertTrue(actual);
31
-    }
32 41
 
33
-    @Test
34
-    public void testIsHoratioFalse(){
35
-        boolean actual = replaceNames.isHoratio("notHoratio");
36
-        Assert.assertFalse(actual);
37
-    }
38 42
 
39 43
     @Test
40 44
     public void testFindHoratio() {
41
-        String test = "Horatio, horatio test test HORATIO, hORatIo";
42
-        int actual = replaceNames.findHoratio(test);
43
-        int expected = 4;
44
-        Assert.assertEquals(expected, actual);
45
+        boolean actual = replaceNames.findHoratio(hamletText);
46
+        Assert.assertTrue(actual);
45 47
     }
46 48
 
49
+
47 50
     @Test
48
-    public void testIsHamletTrue(){
49
-        boolean actual = replaceNames.isHamlet("HamLet's");
51
+    public void testFindHamlet() {
52
+        boolean actual =replaceNames.findHamlet(hamletText);
50 53
         Assert.assertTrue(actual);
51 54
     }
52 55
 
53 56
     @Test
54
-    public void testIsHamletFalse(){
55
-        boolean actual = replaceNames.isHamlet("notHamLet");
57
+    public void testReplaceHoratio() {
58
+        hamletText = replaceNames.replaceHoratio(hamletText);
59
+        boolean actual = replaceNames.findHoratio(hamletText);
56 60
         Assert.assertFalse(actual);
57 61
     }
58 62
 
63
+
59 64
     @Test
60
-    public void testFindHamlet() {
65
+    public void testReplaceHamlet() {
66
+       hamletText = replaceNames.replaceHamlet(hamletText);
67
+        boolean actual =replaceNames.findHamlet(hamletText);
68
+        Assert.assertFalse(actual);
61 69
     }
62 70
 }