NiraParikh 6 vuotta sitten
vanhempi
commit
4f52654c1d
2 muutettua tiedostoa jossa 35 lisäystä ja 26 poistoa
  1. 29
    0
      src/main/java/HamletParser.java
  2. 6
    26
      src/test/java/HamletParserTest.java

+ 29
- 0
src/main/java/HamletParser.java Näytä tiedosto

@@ -1,6 +1,8 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3 3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4 6
 
5 7
 /**
6 8
  * Created by thook on 10/7/15.
@@ -28,6 +30,7 @@ public class HamletParser {
28 30
         }catch(IOException e){
29 31
             e.printStackTrace();
30 32
         }
33
+        System.out.println(result.toString());
31 34
 
32 35
         return result.toString();
33 36
     }
@@ -36,4 +39,30 @@ public class HamletParser {
36 39
         return hamletData;
37 40
     }
38 41
 
42
+    public void replaceHamlet(){
43
+        Pattern p = Pattern.compile("Hamlet",Pattern.CASE_INSENSITIVE);
44
+        Matcher m = p.matcher(hamletData);
45
+        hamletData = m.replaceAll("Leon");
46
+    }
47
+
48
+    public void replaceHoratio(){
49
+        Pattern p = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
50
+        Matcher m = p.matcher(hamletData);
51
+        hamletData = m.replaceAll("Tariq");
52
+    }
53
+
54
+    public boolean findHamlet(){
55
+        Pattern p = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
56
+        Matcher m = p.matcher(hamletData);
57
+        return m.find();
58
+    }
59
+
60
+    public boolean findHoratio(){
61
+        Pattern p = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
62
+        Matcher m = p.matcher(hamletData);
63
+        return m.find();
64
+    }
65
+
66
+
67
+
39 68
 }

+ 6
- 26
src/test/java/HamletParserTest.java Näytä tiedosto

@@ -20,47 +20,27 @@ public class HamletParserTest {
20 20
 
21 21
     @Test
22 22
     public void testChangeHamletToLeon() {
23
-        Pattern pattern = Pattern.compile("Hamlet");
24
-        Matcher m = pattern.matcher(hamletText);
25
-        String actual = m.replaceAll("Leon");
26
-
27
-        String expected = hamletText.toString().replaceAll("Hamlet","Leon");
28
-
29
-        Assert.assertEquals(expected,actual);
23
+       hamletParser.replaceHamlet();
24
+        Assert.assertFalse(hamletParser.findHamlet());
30 25
     }
31 26
 
32 27
     @Test
33 28
     public void testChangeHoratioToTariq() {
34
-      Pattern pattern = Pattern.compile("Horatio");
35
-        Matcher cher = pattern.matcher(hamletText);
36
-        String actual = cher.replaceAll("Tariq");
37
-
38
-        String expected = hamletText.toString().replaceAll("Horatio","Tariq");
39
-
40
-        Assert.assertEquals(expected,actual);
29
+      hamletParser.replaceHoratio();
30
+      Assert.assertFalse(hamletParser.findHoratio());
41 31
     }
42 32
 //
43 33
     @Test
44 34
     public void testFindHoratio() {
45 35
 
46
-        String regex = "((Horatio) (.+?))";
47
-        Pattern pattern=Pattern.compile(regex );
48
-        Matcher mat =pattern.matcher(hamletText);
49
-
50
-        Boolean expected = mat.find();
51
-
52
-        Assert.assertTrue(expected);
36
+        Assert.assertTrue(hamletParser.findHoratio());
53 37
 
54 38
     }
55 39
 
56 40
     @Test
57 41
     public void testFindHamlet() {
58
-        String regex = "((Hamlet) (.+?))";
59
-        Pattern pattern = Pattern.compile(regex);
60
-        Matcher m = pattern.matcher(hamletText);
61 42
 
62
-        Boolean expected = m.find();
63
-        Assert.assertTrue(expected);
43
+        Assert.assertTrue(hamletParser.findHamlet());
64 44
 
65 45
     }
66 46
 }