From c3f7088d22907152787fb391dd681a8438ab55af Mon Sep 17 00:00:00 2001 From: sabotack Date: Tue, 16 May 2023 12:14:41 +0200 Subject: [PATCH] feat: added visitAssign unit tests --- src/test/interpreter/VisitorUnitTest.java | 61 +++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/test/interpreter/VisitorUnitTest.java b/src/test/interpreter/VisitorUnitTest.java index d285165..39a1fa1 100644 --- a/src/test/interpreter/VisitorUnitTest.java +++ b/src/test/interpreter/VisitorUnitTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mock; import org.mockito.Mockito; @@ -51,6 +52,66 @@ void visitAssignStoresCorrectNumber(int value) { Assertions.assertNull(result); } + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void visitAssignStoresCorrectBoolean(boolean value) { + // Mock functions ID(), expression(), and visit() + Mockito.when(mockAssignContext.ID()).thenReturn(new MockTerminalNode("varName")); + Mockito.when(mockAssignContext.expression()).thenReturn(mockExpressionContext); + Mockito.when(spyVisitor.visit(mockExpressionContext)).thenReturn(new MSBool(value)); + + // Call the visitAssign method on the spy + MSType result = spyVisitor.visitAssign(mockAssignContext); + + // Initialize mock value and assert that no exceptions are thrown when value is retrieved + AtomicReference mockValue = new AtomicReference<>(); + Assertions.assertDoesNotThrow(() -> mockValue.set(symbolTable.retrieveSymbolValue(symbolTable.retrieveSymbol("varName")))); + + // Assert that the symbol value is equal to the initial assign value, and assert that result is null + Assertions.assertEquals(value, ((MSBool) mockValue.get()).getValue()); + Assertions.assertNull(result); + } + + @ParameterizedTest + @EnumSource(MSRelDir.Direction.class) + void visitAssignStoresCorrectRelDir(MSRelDir.Direction value) { + // Mock functions ID(), expression(), and visit() + Mockito.when(mockAssignContext.ID()).thenReturn(new MockTerminalNode("varName")); + Mockito.when(mockAssignContext.expression()).thenReturn(mockExpressionContext); + Mockito.when(spyVisitor.visit(mockExpressionContext)).thenReturn(new MSRelDir(value.toString().toLowerCase())); + + // Call the visitAssign method on the spy + MSType result = spyVisitor.visitAssign(mockAssignContext); + + // Initialize mock value and assert that no exceptions are thrown when value is retrieved + AtomicReference mockValue = new AtomicReference<>(); + Assertions.assertDoesNotThrow(() -> mockValue.set(symbolTable.retrieveSymbolValue(symbolTable.retrieveSymbol("varName")))); + + // Assert that the symbol value is equal to the initial assign value, and assert that result is null + Assertions.assertEquals(value, ((MSRelDir) mockValue.get()).getValue()); + Assertions.assertNull(result); + } + + @ParameterizedTest + @EnumSource(MSAbsDir.Direction.class) + void visitAssignStoresCorrectAbsDir(MSAbsDir.Direction value) { + // Mock functions ID(), expression(), and visit() + Mockito.when(mockAssignContext.ID()).thenReturn(new MockTerminalNode("varName")); + Mockito.when(mockAssignContext.expression()).thenReturn(mockExpressionContext); + Mockito.when(spyVisitor.visit(mockExpressionContext)).thenReturn(new MSAbsDir(value.toString().toLowerCase())); + + // Call the visitAssign method on the spy + MSType result = spyVisitor.visitAssign(mockAssignContext); + + // Initialize mock value and assert that no exceptions are thrown when value is retrieved + AtomicReference mockValue = new AtomicReference<>(); + Assertions.assertDoesNotThrow(() -> mockValue.set(symbolTable.retrieveSymbolValue(symbolTable.retrieveSymbol("varName")))); + + // Assert that the symbol value is equal to the initial assign value, and assert that result is null + Assertions.assertEquals(value, ((MSAbsDir) mockValue.get()).getValue()); + Assertions.assertNull(result); + } + @Test void visitNotExprValidBoolReturnsNegatedBool() { Mockito.when(mockNotExprContext.expression()).thenReturn(mockExpressionContext);