PAN-10 Corrected many styling errors which required little refactoring.

This commit is contained in:
BrittanyBi
2019-03-04 21:23:45 -07:00
parent dbce550625
commit 6f5bbab5ac
3 changed files with 79 additions and 66 deletions
@@ -9,15 +9,21 @@ public class Node
Node parent;
int depth;
public Node(final String s, final Node p)
public Node(final String statement, final Node parent)
{
statement = s + "\n";
this.statement = statement + "\n";
left = null;
right = null;
center = null;
parent = p;
if(p != null) depth = p.depth + 1;
else depth = 0;
this.parent = parent;
if(parent != null)
{
depth = parent.depth + 1;
}
else
{
depth = 0;
}
}
@Override
@@ -15,7 +15,7 @@ class ParserService {
private final TheoremController theoremController;
private final NotationController notationController;
private final ProofController proofController;
Node root;
private Node root;
@Autowired
public ParserService(final DefinitionController definitionController, final TheoremController theoremController,
@@ -26,24 +26,24 @@ class ParserService {
this.proofController = proofController;
}
public void driveParsingProcess(final String userInput)
public boolean parseUserInput(final String userInput)
{
final Node tree;
final ArrayList<String> statements;
try {
final Node tree = parseRawInput(userInput);
final ArrayList<String> statements = retrieveStatements(tree);
tree = parseRawInput(userInput);
statements = retrieveStatements(tree);
retrieveDefinitions(statements);
return true;
} catch(final Exception e) {
return false;
}
}
public Node parseRawInput(String input)
{
// convert to the same case for easier processing
input = input.toLowerCase();
root = new Node(input, null);
// special case: nothing is submitted
if(input.equals(""))
{
return root;
@@ -56,7 +56,7 @@ class ParserService {
private void recurse(final Node current)
{
int iContain;
int wordBeginsHere;
if(current.statement.contains("let"))
{
@@ -64,15 +64,15 @@ class ParserService {
current);
if(current.statement.contains("if")){
iContain = current.statement.indexOf("if");
wordBeginsHere = current.statement.indexOf("if");
} else if(current.statement.contains("then")){
iContain = current.statement.indexOf("then");
wordBeginsHere = current.statement.indexOf("then");
} else {
iContain = current.statement.length();
wordBeginsHere = current.statement.length();
}
current.left.center = new Node(current.statement.substring(current.statement.indexOf("let")+3,
iContain),
current.left.center = new Node(current.statement.substring(current.statement.indexOf("let")+"let".length(),
wordBeginsHere),
current.left);
recurse(current.left.center);
}
@@ -82,10 +82,10 @@ class ParserService {
{
current.center = new Node("if",
current);
iContain = (current.statement.contains("then") ? current.statement.indexOf("then") : current.statement.length());
wordBeginsHere = (current.statement.contains("then") ? current.statement.indexOf("then") : current.statement.length());
current.center.center = new Node(current.statement.substring(current.statement.indexOf("if")+2,
iContain),
current.center.center = new Node(current.statement.substring(current.statement.indexOf("if")+"if".length(),
wordBeginsHere),
current.center);
recurse(current.center.center);
}
@@ -95,7 +95,7 @@ class ParserService {
{
current.right = new Node("then",
current);
current.right.center = new Node(current.statement.substring(current.statement.indexOf("then")+4),
current.right.center = new Node(current.statement.substring(current.statement.indexOf("then")+"then".length()),
current.right);
recurse(current.right.center);
}
@@ -127,9 +127,4 @@ class ParserService {
return statementList;
}
public void retrieveDefinitions(final ArrayList<String> list)
{
// stub
}
}
@@ -3,12 +3,13 @@ package edu.msudenver.tsp.services.parser;
import edu.msudenver.tsp.persistence.controller.DefinitionController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -16,28 +17,35 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ParserServiceTest {
@Mock
DefinitionController definitionControllerMock = mock(DefinitionController.class);
private final DefinitionController definitionControllerMock = mock(DefinitionController.class);
private final ParserService mockParserService = mock(ParserService.class);
@Mock
ParserService mps = mock(ParserService.class);
ParserService ps = new ParserService(definitionControllerMock, null,
@InjectMocks
private final ParserService parserService = new ParserService(definitionControllerMock, null,
null, null);
@Test
public void testEmptyStringEqualsEmptyString() {
assertEquals("0: \n", ps.parseRawInput("").toString());
final String expected = "0: \n";
final String actual = parserService.parseRawInput("").toString();
assertEquals(expected, actual);
}
@Test
public void testUselessStringEqualsUselessString() {
assertEquals("0: cat\n", ps.parseRawInput("cat").toString());
final String expected = "0: cat\n";
final String actual = parserService.parseRawInput("cat").toString();
assertEquals(expected, actual);
}
@Test
public void testSingleIfReturnsIfPlusEmptyString() {
assertEquals("0: if\n... 1: if\n... 2: \n\n", ps.parseRawInput("if").toString());
final String expected = "0: if\n... 1: if\n... 2: \n\n";
final String actual = parserService.parseRawInput("if").toString();
assertEquals(expected, actual);
}
@Test
@@ -48,9 +56,9 @@ public class ParserServiceTest {
"... 1: then\n" +
"... 2: x^2 is even\n\n";
final String testCase = "if x is even then x^2 is even";
final String actual = parserService.parseRawInput("if x is even then x^2 is even").toString();
assertEquals(expected, ps.parseRawInput(testCase).toString());
assertEquals(expected, actual);
}
@Test
@@ -61,9 +69,9 @@ public class ParserServiceTest {
"... 1: then\n" +
"... 2: x^2 is even.\n\n";
final String testCase = "Let x be even. Then x^2 is even.";
final String actual = parserService.parseRawInput("Let x be even. Then x^2 is even.").toString();
assertEquals(expected, ps.parseRawInput(testCase).toString());
assertEquals(expected, actual);
}
@Test
@@ -76,20 +84,20 @@ public class ParserServiceTest {
"... 1: then\n" +
"... 2: c.\n\n";
final String testCase = "Let a. If b, then c.";
final String actual = parserService.parseRawInput("Let a. If b, then c.").toString();
assertEquals(expected, ps.parseRawInput(testCase).toString());
assertEquals(expected, actual);
}
@Test
public void testLetAlone() {
public void testLetStatementWithoutAnyIfOrThenStatements() {
final String expected = "0: let a be equal to b.\n" +
"... 1: let\n" +
"... 2: a be equal to b.\n\n";
final String testCase = "Let a be equal to b.";
final String actual = parserService.parseRawInput("Let a be equal to b.").toString();
assertEquals(expected, ps.parseRawInput(testCase).toString());
assertEquals(expected, actual);
}
@Test
@@ -97,34 +105,38 @@ public class ParserServiceTest {
final ArrayList<String> expectedList = new ArrayList<>();
expectedList.add("");
when(mps.parseRawInput(anyString())).thenReturn(new Node("", null));
when(mockParserService.parseRawInput(anyString())).thenReturn(new Node("", null));
final ArrayList<String> actualList = parserService.retrieveStatements(mockParserService.parseRawInput(""));
assertEquals(expectedList, ps.retrieveStatements(mps.parseRawInput("")));
assertEquals(expectedList, actualList);
}
@Test
public void testBaseCaseReturnsXIsEven() {
final ArrayList<String> expectation = new ArrayList<>();
expectation.add("x is even");
expectation.add("x^2 is even");
final ArrayList<String> expectedList = new ArrayList<>();
expectedList.add("x is even");
expectedList.add("x^2 is even");
ps.root = new Node("if x is even then x^2 is even", null);
ps.root.center = new Node("if", ps.root);
ps.root.center.center = new Node(" x is even ", ps.root.center);
ps.root.right = new Node("then", ps.root);
ps.root.right.center = new Node(" x^2 is even", ps.root.right);
when(mps.parseRawInput(anyString())).thenReturn(ps.root);
final Node testNode = new Node("if x is even then x^2 is even", null);
testNode.center = new Node("if", testNode);
testNode.center.center = new Node(" x is even ", testNode.center);
testNode.right = new Node("then", testNode);
testNode.right.center = new Node(" x^2 is even", testNode.right);
assertEquals(expectation, ps.retrieveStatements(mps.parseRawInput("baseCase")));
when(mockParserService.parseRawInput(anyString())).thenReturn(testNode);
final ArrayList<String> actualList = parserService.retrieveStatements(mockParserService.parseRawInput("baseCase"));
assertEquals(expectedList, actualList);
}
@Test
public void testDriveParsingProcess() {
mps.root = new Node("", null);
final ArrayList<String> testdummy = new ArrayList<>();
when(mps.parseRawInput(anyString())).thenReturn(mps.root);
when(mps.retrieveStatements(mps.root)).thenReturn(testdummy);
public void testDriveParseUserInput() {
final Node testNode = new Node("", null);
when(mockParserService.parseRawInput(anyString())).thenReturn(testNode);
when(mockParserService.retrieveStatements(testNode)).thenReturn(new ArrayList<>());
ps.driveParsingProcess("");
final boolean successfulTestDrive = parserService.parseUserInput("");
assertTrue(successfulTestDrive);
}
}