PAN-64 Completed and refactored unit tests for ParserService.

This commit is contained in:
BrittanyBi
2019-04-14 20:28:11 -06:00
parent de5a69d619
commit 5de86dc498
@@ -9,20 +9,210 @@ 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 {
@Mock private ParserService mockParserService;
@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;
when(parserService.parseRawInput("")).thenReturn(new Node("", null));
actual = parserService.parseRawInput("").toString();
assertEquals(expected, actual);
}
@Test
public void testParseRawInputAndRecurse_EmptyStringEqualsEmptyString() {
final String expected = "0: \n";
final String actual = parserService.parseRawInput("").toString();
@@ -30,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();
@@ -38,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();
@@ -46,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" +
@@ -59,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" +
@@ -72,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" +
@@ -87,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";
@@ -98,7 +288,7 @@ public class ParserServiceTest {
}
@Test
public void testEmptyStringReturnsEmptyList() {
public void testRetrieveStatements_EmptyStringReturnsEmptyList() {
final List<String> expectedList = new ArrayList<>();
expectedList.add("");
@@ -109,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");