-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserTest.java
More file actions
82 lines (72 loc) · 3.78 KB
/
Copy pathParserTest.java
File metadata and controls
82 lines (72 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package de.uni_passau.fim.se2.sa.readability.utils;
import com.github.javaparser.ParseException;
import com.github.javaparser.ast.body.BodyDeclaration;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
class ParserTest {
@Test
void testParseJavaSnippet_validMethod() {
String validMethodSnippet = "public void myMethod() { int x = 10; }";
try {
BodyDeclaration<?> result = Parser.parseJavaSnippet(validMethodSnippet);
assertNotNull(result, "Parsed result should not be null for a valid method snippet.");
assertTrue(result.isMethodDeclaration(), "Result should be a MethodDeclaration.");
// You can add more assertions here, e.g., check method name, return type, etc.
} catch (ParseException e) {
fail("Parsing a valid method snippet should not throw a ParseException: " + e.getMessage());
}
}
@Test
void testParseJavaSnippet_validField() {
String validFieldSnippet = "private String name = \"test\";";
try {
BodyDeclaration<?> result = Parser.parseJavaSnippet(validFieldSnippet);
assertNotNull(result, "Parsed result should not be null for a valid field snippet.");
assertTrue(result.isFieldDeclaration(), "Result should be a FieldDeclaration.");
} catch (ParseException e) {
fail("Parsing a valid field snippet should not throw a ParseException: " + e.getMessage());
}
}
@Test
void testParseJavaSnippet_validConstructor() {
String validConstructorSnippet = "public MyClass() { this.x = 0; }";
try {
BodyDeclaration<?> result = Parser.parseJavaSnippet(validConstructorSnippet);
assertNotNull(result, "Parsed result should not be null for a valid constructor snippet.");
assertTrue(result.isConstructorDeclaration(), "Result should be a ConstructorDeclaration.");
} catch (ParseException e) {
fail("Parsing a valid constructor snippet should not throw a ParseException: " + e.getMessage());
}
}
@Test
void testParseJavaSnippet_invalidSnippetSyntaxError() {
String invalidSnippet = "public void myMethod() { int x = ; }"; // Syntax error
// Expect a ParseException to be thrown for invalid syntax
assertThrows(ParseException.class, () -> Parser.parseJavaSnippet(invalidSnippet),
"Parsing an invalid snippet should throw a ParseException.");
}
@Test
void testParseJavaSnippet_emptyString() {
String emptySnippet = "";
// JavaParser might handle empty strings differently depending on ParseStart,
// but for CLASS_BODY, it's likely to fail.
assertThrows(ParseException.class, () -> Parser.parseJavaSnippet(emptySnippet),
"Parsing an empty string should throw a ParseException.");
}
@Test
void testParseJavaSnippet_incompleteSnippet() {
String incompleteSnippet = "public int calculate(int a"; // Missing closing parenthesis and body
assertThrows(ParseException.class, () -> Parser.parseJavaSnippet(incompleteSnippet),
"Parsing an incomplete snippet should throw a ParseException.");
}
@Test
void testParseJavaSnippet_onlyComment() {
String commentSnippet = "// This is a comment only";
// An empty class body after removing comments is often considered unparsable for CLASS_BODY
assertThrows(ParseException.class, () -> Parser.parseJavaSnippet(commentSnippet),
"Parsing only comments should throw a ParseException for a class body context.");
}
}