Merge branch 'master' of https://github.com/atusa17/ptp into PAN-53
This commit is contained in:
@@ -39,10 +39,8 @@ class ParserService {
|
||||
return root;
|
||||
}
|
||||
|
||||
private void recurse(final Node current)
|
||||
public void recurse(final Node current)
|
||||
{
|
||||
int startIndex;
|
||||
int endIndex;
|
||||
final String statement;
|
||||
|
||||
if (current != null) {
|
||||
@@ -51,10 +49,28 @@ class ParserService {
|
||||
return;
|
||||
}
|
||||
|
||||
String nextStatement;
|
||||
|
||||
if(statement.contains("let"))
|
||||
{
|
||||
separateByLet(current, statement);
|
||||
}
|
||||
|
||||
if(statement.contains("if"))
|
||||
{
|
||||
separateByIf(current, statement);
|
||||
}
|
||||
|
||||
|
||||
if(current.getStatement().contains("then"))
|
||||
{
|
||||
separateByThen(current, statement);
|
||||
}
|
||||
}
|
||||
|
||||
public void separateByLet(final Node current, final String statement){
|
||||
final int startIndex;
|
||||
final int endIndex;
|
||||
final String nextStatement;
|
||||
|
||||
current.setLeft(new Node("let", current));
|
||||
|
||||
startIndex = statement.indexOf("let")+"let".length();
|
||||
@@ -73,9 +89,11 @@ class ParserService {
|
||||
recurse(current.getLeft().getCenter());
|
||||
}
|
||||
|
||||
public void separateByIf(final Node current, final String statement){
|
||||
final int startIndex;
|
||||
final int endIndex;
|
||||
final String nextStatement;
|
||||
|
||||
if(statement.contains("if"))
|
||||
{
|
||||
current.setCenter(new Node("if", current));
|
||||
|
||||
startIndex = statement.indexOf("if")+"if".length();
|
||||
@@ -86,9 +104,10 @@ class ParserService {
|
||||
recurse(current.getCenter().getCenter());
|
||||
}
|
||||
|
||||
public void separateByThen(final Node current, final String statement){
|
||||
final int startIndex;
|
||||
final String nextStatement;
|
||||
|
||||
if(current.getStatement().contains("then"))
|
||||
{
|
||||
current.setRight(new Node("then", current));
|
||||
|
||||
startIndex = statement.indexOf("then")+"then".length();
|
||||
@@ -97,14 +116,13 @@ class ParserService {
|
||||
current.getRight().setCenter(new Node(nextStatement, current.getRight()));
|
||||
recurse(current.getRight().getCenter());
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> retrieveStatements(final Node parsedTree)
|
||||
{
|
||||
return populateStatementList(parsedTree, new ArrayList<>());
|
||||
}
|
||||
|
||||
private ArrayList<String> populateStatementList(final Node node, final ArrayList<String> statementList)
|
||||
public ArrayList<String> populateStatementList(final Node node, final ArrayList<String> statementList)
|
||||
{
|
||||
if(node == null)
|
||||
{
|
||||
|
||||
@@ -9,10 +9,9 @@ import org.mockito.runners.MockitoJUnitRunner;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ParserServiceTest {
|
||||
@@ -21,7 +20,199 @@ public class ParserServiceTest {
|
||||
@InjectMocks private ParserService parserService;
|
||||
|
||||
@Test
|
||||
public void testEmptyStringEqualsEmptyString() {
|
||||
public void testParseUserInput_noErrors() {
|
||||
final String userInput = "string";
|
||||
final Node root = new Node(userInput, null);
|
||||
|
||||
final ParserService parse = mock(ParserService.class);
|
||||
when(parse.parseRawInput(anyString())).thenReturn(root);
|
||||
when(parse.retrieveStatements(root)).thenReturn(new ArrayList<>());
|
||||
when(parse.parseUserInput(userInput)).thenCallRealMethod();
|
||||
|
||||
assertTrue(parserService.parseUserInput(userInput));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseUserInput_thrownError() {
|
||||
final String userInput = "string";
|
||||
final Node root = new Node(userInput, null);
|
||||
final Exception error = new NullPointerException();
|
||||
|
||||
final ParserService parse = mock(ParserService.class);
|
||||
when(parse.parseRawInput(anyString())).thenReturn(root);
|
||||
when(parse.retrieveStatements(root)).thenThrow(error);
|
||||
when(parse.parseUserInput(userInput)).thenCallRealMethod();
|
||||
|
||||
assertFalse(parse.parseUserInput(userInput));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecurse_NullNode() {
|
||||
parserService.recurse(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecurse_StatementContainsLet() {
|
||||
final String statement = "let";
|
||||
final Node test = new Node(statement, null);
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
doNothing().when(spy).separateByLet(test, statement);
|
||||
|
||||
spy.recurse(test);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecurse_StatementContainsIf() {
|
||||
final String statement = "if";
|
||||
final Node test = new Node(statement, null);
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
doNothing().when(spy).separateByIf(test, statement);
|
||||
|
||||
spy.recurse(test);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecurse_StatementContainsThen() {
|
||||
final String statement = "then";
|
||||
final Node test = new Node(statement, null);
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
doNothing().when(spy).separateByThen(test, statement);
|
||||
|
||||
spy.recurse(test);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeparateByLet_StatementContainsOnlyLet() {
|
||||
final String statement = "let x be even";
|
||||
final Node test = new Node(statement, null);
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
doNothing().when(spy).recurse(anyObject());
|
||||
spy.separateByLet(test, statement);
|
||||
|
||||
final String keywordLet = test.getLeft().getStatement();
|
||||
assertEquals("let\n", keywordLet);
|
||||
|
||||
final String restOfIt = test.getLeft().getCenter().getStatement();
|
||||
assertEquals(" x be even\n", restOfIt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeparateByLet_StatementContainsLetIf() {
|
||||
final String statement = "let x be even. if x is prime then x is 2.";
|
||||
final Node test = new Node(statement, null);
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
doNothing().when(spy).recurse(anyObject());
|
||||
spy.separateByLet(test, statement);
|
||||
|
||||
final String keywordLet = test.getLeft().getStatement();
|
||||
assertEquals("let\n", keywordLet);
|
||||
|
||||
final String restOfIt = test.getLeft().getCenter().getStatement();
|
||||
assertEquals(" x be even. \n", restOfIt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeparateByLet_StatementContainsLetThen() {
|
||||
final String statement = "let x be an even prime. then x is 2.";
|
||||
final Node test = new Node(statement, null);
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
doNothing().when(spy).recurse(anyObject());
|
||||
spy.separateByLet(test, statement);
|
||||
|
||||
final String keywordLet = test.getLeft().getStatement();
|
||||
assertEquals("let\n", keywordLet);
|
||||
|
||||
final String restOfIt = test.getLeft().getCenter().getStatement();
|
||||
assertEquals(" x be an even prime. \n", restOfIt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeparateByIf_StatementContainsOnlyIf() {
|
||||
final String statement = "if x is even";
|
||||
final Node test = new Node(statement, null);
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
doNothing().when(spy).recurse(anyObject());
|
||||
spy.separateByIf(test, statement);
|
||||
|
||||
final String keywordIf = test.getCenter().getStatement();
|
||||
assertEquals("if\n", keywordIf);
|
||||
|
||||
final String restOfIt = test.getCenter().getCenter().getStatement();
|
||||
assertEquals(" x is even\n", restOfIt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeparateByIf_StatementContainsThen() {
|
||||
final String statement = "if x is even then x^2 is even";
|
||||
final Node test = new Node(statement, null);
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
doNothing().when(spy).recurse(anyObject());
|
||||
spy.separateByIf(test, statement);
|
||||
|
||||
final String keywordIf = test.getCenter().getStatement();
|
||||
assertEquals("if\n", keywordIf);
|
||||
|
||||
final String restOfIt = test.getCenter().getCenter().getStatement();
|
||||
assertEquals(" x is even \n", restOfIt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeparateByThen_StatementContainsOnlyThen() {
|
||||
final String statement = "then x^2 is even";
|
||||
final Node test = new Node(statement, null);
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
doNothing().when(spy).recurse(anyObject());
|
||||
spy.separateByThen(test, statement);
|
||||
|
||||
final String keywordThen = test.getRight().getStatement();
|
||||
assertEquals("then\n", keywordThen);
|
||||
|
||||
final String restOfIt = test.getRight().getCenter().getStatement();
|
||||
assertEquals(" x^2 is even\n", restOfIt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPopulateStatementList_NodeIsNull(){
|
||||
final ArrayList<String> list = new ArrayList<>();
|
||||
final ArrayList<String> result = (ArrayList)parserService.populateStatementList(null, list);
|
||||
|
||||
assertEquals(list, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPopulateStatementList_NodeExists(){
|
||||
final ArrayList<String> list = new ArrayList<>();
|
||||
final String statement = "this is a statement ";
|
||||
final Node input = new Node(statement, null);
|
||||
|
||||
final ArrayList<String> result = (ArrayList)parserService.populateStatementList(input, list);
|
||||
|
||||
assertTrue(result.contains("this is a statement"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseRawInput_EmptyString() {
|
||||
final String expected = "0: \n";
|
||||
final String actual;
|
||||
|
||||
final ParserService spy = spy(new ParserService());
|
||||
when(spy.parseRawInput("")).thenReturn(new Node("", null));
|
||||
actual = spy.parseRawInput("").toString();
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseRawInputAndRecurse_EmptyStringEqualsEmptyString() {
|
||||
final String expected = "0: \n";
|
||||
final String actual = parserService.parseRawInput("").toString();
|
||||
|
||||
@@ -29,7 +220,7 @@ public class ParserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUselessStringEqualsUselessString() {
|
||||
public void testParseRawInput_UselessStringEqualsUselessString() {
|
||||
final String expected = "0: cat\n";
|
||||
final String actual = parserService.parseRawInput("cat").toString();
|
||||
|
||||
@@ -37,7 +228,7 @@ public class ParserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleIfReturnsIfPlusEmptyString() {
|
||||
public void testParseRawInput_SingleIfReturnsIfPlusEmptyString() {
|
||||
final String expected = "0: if\n... 1: if\n... 2: \n\n";
|
||||
final String actual = parserService.parseRawInput("if").toString();
|
||||
|
||||
@@ -45,7 +236,7 @@ public class ParserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseCaseIfXIsEvenThenXSquaredIsEven() {
|
||||
public void testParseRawInput_BaseCaseIfXIsEvenThenXSquaredIsEven() {
|
||||
final String expected = "0: if x is even then x^2 is even\n" +
|
||||
"... 1: if\n" +
|
||||
"... 2: x is even \n" +
|
||||
@@ -58,7 +249,7 @@ public class ParserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLetXBeEvenThenXSquaredIsEven() {
|
||||
public void testParseRawInput_LetXBeEvenThenXSquaredIsEven() {
|
||||
final String expected = "0: let x be even. then x^2 is even.\n" +
|
||||
"... 1: let\n" +
|
||||
"... 2: x be even. \n" +
|
||||
@@ -71,7 +262,7 @@ public class ParserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLetIfThen() {
|
||||
public void testParseRawInput_LetIfThen() {
|
||||
final String expected = "0: let a. if b, then c.\n" +
|
||||
"... 1: let\n" +
|
||||
"... 2: a. \n" +
|
||||
@@ -86,7 +277,7 @@ public class ParserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLetStatementWithoutAnyIfOrThenStatements() {
|
||||
public void testParseRawInput_LetStatementWithoutAnyIfOrThenStatements() {
|
||||
final String expected = "0: let a be equal to b.\n" +
|
||||
"... 1: let\n" +
|
||||
"... 2: a be equal to b.\n\n";
|
||||
@@ -97,7 +288,7 @@ public class ParserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyStringReturnsEmptyList() {
|
||||
public void testRetrieveStatements_EmptyStringReturnsEmptyList() {
|
||||
final List<String> expectedList = new ArrayList<>();
|
||||
expectedList.add("");
|
||||
|
||||
@@ -108,7 +299,7 @@ public class ParserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseCaseReturnsXIsEven() {
|
||||
public void testRetrieveStatements_BaseCaseReturnsXIsEven() {
|
||||
final List<String> expectedList = new ArrayList<>();
|
||||
expectedList.add("x is even");
|
||||
expectedList.add("x^2 is even");
|
||||
|
||||
Reference in New Issue
Block a user