added some tests
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
Bruder John 2024-06-20 12:45:54 +02:00
parent e552bd5ada
commit 2ff720b042
4 changed files with 254 additions and 4 deletions

View File

@ -11,10 +11,7 @@ import semantic.exeptions.TypeMismatchException;
import java.io.File;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
public class SemanticTest {
@ -23,6 +20,80 @@ public class SemanticTest {
SemanticAnalyzer.clearAnalyzier();
}
@Test
public void alreadyDeclaredMethod() {
//Arrange
ObjectMapper objectMapper = new ObjectMapper();
ProgramNode programNode = null;
try{
programNode = objectMapper.readValue(new File("src/test/resources/semantic/alreadyDeclearedMethod.json"), ProgramNode.class);
} catch (Exception e) {
e.printStackTrace();
}
//Act
ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
//Assert
assertEquals(1, SemanticAnalyzer.errors.size());
assertEquals(true, SemanticAnalyzer.errors.get(0) instanceof AlreadyDeclearedException);
assertEquals(null, typedAst);
}
@Test
public void duplicatedParamInMethod() {
//Arrange
ObjectMapper objectMapper = new ObjectMapper();
ProgramNode programNode = null;
try{
programNode = objectMapper.readValue(new File("src/test/resources/semantic/duplicatedParamInMethod.json"), ProgramNode.class);
} catch (Exception e) {
e.printStackTrace();
}
//Act
ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
//Assert
assertEquals(1, SemanticAnalyzer.errors.size());
assertInstanceOf(AlreadyDeclearedException.class, SemanticAnalyzer.errors.getFirst());
assertNull(typedAst);
}
@Test
public void correctMethodOverloading() {
//Arrange
ObjectMapper objectMapper = new ObjectMapper();
ProgramNode programNode = null;
try{
programNode = objectMapper.readValue(new File("src/test/resources/semantic/correctMethodOverloading.json"), ProgramNode.class);
} catch (Exception e) {
e.printStackTrace();
}
//Act
ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
//Assert
assertEquals(0, SemanticAnalyzer.errors.size());
assertNotEquals(null, typedAst);
}
@Test
public void alreadyDeclaredLocalFieldVar() {

View File

@ -0,0 +1,64 @@
{
"classes": [
{
"identifier": "testClass1",
"accessType": {
"enumAccessTypeNode": "PUBLIC"
},
"members": [
{
"@type": "Method",
"visibility": {
"enumAccessTypeNode": "PUBLIC"
},
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "testMethod",
"parameters": {
"parameters": [
{
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "param1"
}
]
},
"statements": [
]
},
{
"@type": "Method",
"visibility": {
"enumAccessTypeNode": "PUBLIC"
},
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "testMethod",
"parameters": {
"parameters": [
{
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "param1"
}
]
},
"statements": [
]
}
],
"hasConstructor": false
}
]
}

View File

@ -0,0 +1,70 @@
{
"classes": [
{
"identifier": "testClass1",
"accessType": {
"enumAccessTypeNode": "PUBLIC"
},
"members": [
{
"@type": "Method",
"visibility": {
"enumAccessTypeNode": "PUBLIC"
},
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "testMethod",
"parameters": {
"parameters": [
{
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "param1"
}
]
},
"statements": [
]
},
{
"@type": "Method",
"visibility": {
"enumAccessTypeNode": "PUBLIC"
},
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "testMethod",
"parameters": {
"parameters": [
{
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "param1"
},
{
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "param2"
}
]
},
"statements": [
]
}
],
"hasConstructor": false
}
]
}

View File

@ -0,0 +1,45 @@
{
"classes": [
{
"identifier": "testClass1",
"accessType": {
"enumAccessTypeNode": "PUBLIC"
},
"members": [
{
"@type": "Method",
"visibility": {
"enumAccessTypeNode": "PUBLIC"
},
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "testMethod",
"parameters": {
"parameters": [
{
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "param1"
},
{
"type": {
"@type": "Base",
"enumType": "INT"
},
"identifier": "param1"
}
]
},
"statements": [
]
}
],
"hasConstructor": false
}
]
}