Skip to content
Merged
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
36 changes: 27 additions & 9 deletions src/test/interpreter/VisitorUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class VisitorUnitTest {
@Mock private MineScriptParser.NotExprContext mockNotExprContext;
@Mock private MineScriptParser.AndContext mockAndContext;
@Mock private MineScriptParser.AddSubContext mockAddSubContext;
@Mock private MineScriptParser.OrContext mockOrContext;
@Mock private MineScriptParser.MultDivModContext mockMultDivModContext;

@ParameterizedTest
Expand Down Expand Up @@ -247,6 +248,7 @@ void visitNumberInvalidInputThrowsRuntimeException(){
Mockito.when(mockNumberContext.NUMBER()).thenReturn(new MockTerminalNode("abc"));
Assertions.assertThrows(RuntimeException.class, () -> spyVisitor.visitNumber(mockNumberContext));
}

@Test
void visitAndValidBoolsReturnsTrue(){
Mockito.when(mockAndContext.expression(0)).thenReturn(mockExpressionContext1);
Expand All @@ -257,24 +259,25 @@ void visitAndValidBoolsReturnsTrue(){
MSType result = spyVisitor.visitAnd(mockAndContext);
Assertions.assertTrue(((MSBool) result).getValue());
}
@Test
void visitAndBoolsTrueAndFalseReturnsFalse(){

@Test
void visitAndBoolsTrueAndFalseReturnsFalse(){
Mockito.when(mockAndContext.expression(0)).thenReturn(mockExpressionContext1);
Mockito.when(mockAndContext.expression(1)).thenReturn(mockExpressionContext2);
Mockito.when(spyVisitor.visit(mockExpressionContext1)).thenReturn(new MSBool(true));
Mockito.when(spyVisitor.visit(mockExpressionContext2)).thenReturn(new MSBool(false));

MSType result = spyVisitor.visitAnd(mockAndContext);
Assertions.assertFalse(((MSBool) result).getValue());
}
@Test
}
@Test
void visitAndFalseWithShortCircuitReturnsFalse() {
Mockito.when(mockAndContext.expression(0)).thenReturn(mockExpressionContext1);
Mockito.when(spyVisitor.visit(mockExpressionContext1)).thenReturn(new MSBool(false));
Mockito.when(mockAndContext.expression(0)).thenReturn(mockExpressionContext1);
Mockito.when(spyVisitor.visit(mockExpressionContext1)).thenReturn(new MSBool(false));

MSType result = spyVisitor.visitAnd(mockAndContext);
Assertions.assertFalse(((MSBool) result).getValue());
}
MSType result = spyVisitor.visitAnd(mockAndContext);
Assertions.assertFalse(((MSBool) result).getValue());
}

@ParameterizedTest
@CsvSource ({"1, 2, +", "2, 1, -", "0, 0, +", "-1, -2, +", "-2, -1, -"})
Expand All @@ -292,6 +295,21 @@ void visitAddSubValidInputReturnsNumber(int value1, int value2, String operator)
Assertions.assertEquals(value1 - value2, ((MSNumber) result).getValue());
}
}

@ParameterizedTest
@CsvSource({"false, false", "false, true", "true, false", "true, true"})
void visitOrValidInputReturnsOrValue(boolean value1, boolean value2) {
Mockito.when(mockOrContext.expression(0)).thenReturn(mockExpressionContext1);
Mockito.when(spyVisitor.visit(mockExpressionContext1)).thenReturn(new MSBool(value1));

if (!value1) {
Mockito.when(mockOrContext.expression(1)).thenReturn(mockExpressionContext2);
Mockito.when(spyVisitor.visit(mockExpressionContext2)).thenReturn(new MSBool(value2));
}

MSType result = spyVisitor.visitOr(mockOrContext);
Assertions.assertEquals(value1 || value2, ((MSBool) result).getValue());
}

@ParameterizedTest
@CsvSource ({"2,2,*", "2,2,/", "2,2,%"})
Expand Down