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