Browse Source

fixed error in one test

Tommy Rogers 6 years ago
parent
commit
4c1ddaf3c3
2 changed files with 3 additions and 11 deletions
  1. 2
    2
      src/main/java/HamletParser.java
  2. 1
    9
      src/test/java/HamletParserTest.java

+ 2
- 2
src/main/java/HamletParser.java View File

39
     }
39
     }
40
 
40
 
41
     public String changeHamletToLeon(String search, String replace){
41
     public String changeHamletToLeon(String search, String replace){
42
-        Pattern patt = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
42
+        Pattern patt = Pattern.compile("\\b" +search + "\\b", Pattern.CASE_INSENSITIVE);
43
         Matcher m = patt.matcher(hamletData);
43
         Matcher m = patt.matcher(hamletData);
44
         return m.replaceAll(replace);
44
         return m.replaceAll(replace);
45
     }
45
     }
46
 
46
 
47
     public int findWord(CharSequence hamletData ,String search) {
47
     public int findWord(CharSequence hamletData ,String search) {
48
         int count = 0;
48
         int count = 0;
49
-        Pattern patt = Pattern.compile("^" + search + "", Pattern.CASE_INSENSITIVE);
49
+        Pattern patt = Pattern.compile("\\b" +search + "\\b", Pattern.CASE_INSENSITIVE);
50
         Matcher m = patt.matcher(hamletData);
50
         Matcher m = patt.matcher(hamletData);
51
      while (m.find())
51
      while (m.find())
52
             count++;
52
             count++;

+ 1
- 9
src/test/java/HamletParserTest.java View File

18
         String leon = hamletParser.changeHamletToLeon("Hamlet", "Leon");
18
         String leon = hamletParser.changeHamletToLeon("Hamlet", "Leon");
19
 
19
 
20
         int expected = hamletParser.findWord(hamletText , "Hamlet");
20
         int expected = hamletParser.findWord(hamletText , "Hamlet");
21
-        int actual = hamletParser.findWord(leon , "Hamlet");
21
+        int actual = hamletParser.findWord(leon , "Leon");
22
 
22
 
23
         System.out.println(leon);
23
         System.out.println(leon);
24
-       // System.out.println(hamletText);
25
-
26
-
27
 
24
 
28
         assertEquals(expected, actual);
25
         assertEquals(expected, actual);
29
     }
26
     }
36
         int actual = hamletParser.findWord(leon , "Tariq");
33
         int actual = hamletParser.findWord(leon , "Tariq");
37
 
34
 
38
         System.out.println(leon);
35
         System.out.println(leon);
39
-        // System.out.println(hamletText);
40
-
41
-
42
 
36
 
43
         assertEquals(expected, actual);
37
         assertEquals(expected, actual);
44
     }
38
     }
47
     public void testFindHoratio() {
41
     public void testFindHoratio() {
48
         int expected = 158;
42
         int expected = 158;
49
         int actual = hamletParser.findWord(hamletText,"Horatio");
43
         int actual = hamletParser.findWord(hamletText,"Horatio");
50
-
51
         assertEquals(expected, actual);
44
         assertEquals(expected, actual);
52
     }
45
     }
53
 
46
 
55
     public void testFindHamlet() {
48
     public void testFindHamlet() {
56
         int expected = 472;
49
         int expected = 472;
57
         int actual = hamletParser.findWord(hamletText,"Hamlet");
50
         int actual = hamletParser.findWord(hamletText,"Hamlet");
58
-
59
         assertEquals(expected, actual);
51
         assertEquals(expected, actual);
60
     }
52
     }
61
 }
53
 }