Skip to content

lab complete #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>regex</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by thook on 10/7/15.
Expand All @@ -9,6 +11,7 @@ public class HamletParser {

private String hamletData;


public HamletParser(){
this.hamletData = loadFile();
}
Expand Down Expand Up @@ -36,4 +39,10 @@ public String getHamletData(){
return hamletData;
}

public String changeName(String pattern, String replacement, String input) {
Pattern hamletPattern = Pattern.compile(pattern);
Matcher matcher = hamletPattern.matcher(input);

return matcher.replaceAll(replacement);
}
}
22 changes: 22 additions & 0 deletions src/test/java/HamletParserTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

public class HamletParserTest {
Expand All @@ -15,17 +19,35 @@ public void setUp() {

@Test
public void testChangeHamletToLeon() {
String expected = hamletParser.changeName("(h|H)amlet", "Leon", this.hamletText);
Assert.assertFalse(expected.contains("(h|H)amlet"));
}

@Test
public void testChangeHoratioToTariq() {
String expected = hamletParser.changeName("(h|H)oratio", "Tariq", this.hamletText);
Assert.assertFalse(expected.contains("(h|H)oratio"));
}

@Test
public void testFindHoratio() {
String testHoratioStr = "My name is Horatio";
String horatioPattern = ("(h|H)oratio");

String expected = "My name is Tariq";
String actual = hamletParser.changeName(horatioPattern, "Tariq", testHoratioStr);

Assert.assertEquals(expected, actual);
}

@Test
public void testFindHamlet() {
String testHoratioStr = "My name is Hamlet, hamlet";
String hamletPattern = ("(h|H)amlet");

String expected = "My name is Leon, Leon";
String actual = hamletParser.changeName(hamletPattern, "Leon", testHoratioStr);

Assert.assertEquals(expected, actual);
}
}
Loading