peer-at-code-parser-java/test/dev/peerat/parser/java/element/annotation/ClazzAnnotation.java

162 lines
5.2 KiB
Java

package dev.peerat.parser.java.element.annotation;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import java.util.Map.Entry;
import dev.peerat.parser.Token;
import dev.peerat.parser.TokenType;
import dev.peerat.parser.java.Annotation;
import dev.peerat.parser.java.Class;
import dev.peerat.parser.java.ClassBase;
import dev.peerat.parser.java.Enumeration;
import dev.peerat.parser.java.Interface;
import dev.peerat.parser.java.Value;
import dev.peerat.parser.java.element.BaseElementTests;
public class ClazzAnnotation extends BaseElementTests{
{
register(
"package be.jeffcheasey88;"
+ ""
+ "@Test "
+ "public static class Test{}",
(javafile) -> {
Class clazz = checkClass(javafile);
List<Annotation> annotations = clazz.getAnnotations();
assertNotNull(annotations);
assertEquals(1, annotations.size());
Annotation annotation = annotations.get(0);
assertEquals("Test", annotation.getName().getValue());
assertNull(annotation.getParameters());
});
register(
"package be.jeffcheasey88;"
+ ""
+ "@be.jeffcheasey88.Test "
+ "public static class Test{}",
(javafile) -> {
Class clazz = checkClass(javafile);
List<Annotation> annotations = clazz.getAnnotations();
assertNotNull(annotations);
assertEquals(1, annotations.size());
Annotation annotation = annotations.get(0);
assertEquals("be.jeffcheasey88.Test", annotation.getName().getValue());
assertNull(annotation.getParameters());
});
register(
"package be.jeffcheasey88;"
+ ""
+ "@Test() "
+ "public static class Test{}",
(javafile) -> {
Class clazz = checkClass(javafile);
List<Annotation> annotations = clazz.getAnnotations();
assertNotNull(annotations);
assertEquals(1, annotations.size());
Annotation annotation = annotations.get(0);
assertEquals("Test", annotation.getName().getValue());
assertNull(annotation.getParameters());
});
register(
"package be.jeffcheasey88;"
+ ""
+ "@Test(\"Hello\") "
+ "public static class Test{}",
(javafile) -> {
Class clazz = checkClass(javafile);
List<Annotation> annotations = clazz.getAnnotations();
assertNotNull(annotations);
assertEquals(1, annotations.size());
Annotation annotation = annotations.get(0);
assertEquals("Test", annotation.getName().getValue());
assertNotNull(annotation.getParameters());
Value value = annotation.getParameters().get(null);
assertNotNull(value);
Token token = value.getToken();
assertNotNull(token);
assertEquals(TokenType.STRING, token.getType());
assertEquals("\"Hello\"", token.getValue());
});
register(
"package be.jeffcheasey88;"
+ ""
+ "@Test(offset=8) "
+ "public static class Test{}",
(javafile) -> {
Class clazz = checkClass(javafile);
List<Annotation> annotations = clazz.getAnnotations();
assertNotNull(annotations);
assertEquals(1, annotations.size());
Annotation annotation = annotations.get(0);
assertEquals("Test", annotation.getName().getValue());
assertNotNull(annotation.getParameters());
assertEquals(1, annotation.getParameters().size());
Entry<Token, Value> entry = annotation.getParameters().entrySet().iterator().next();
assertEquals("offset", entry.getKey().getValue());
assertEquals("8", entry.getValue().getToken().getValue());
});
register(
"package be.jeffcheasey88;"
+ ""
+ "@Test(offset=8,concat=\",\") "
+ "public static class Test{}",
(javafile) -> {
Class clazz = checkClass(javafile);
List<Annotation> annotations = clazz.getAnnotations();
assertNotNull(annotations);
assertEquals(1, annotations.size());
Annotation annotation = annotations.get(0);
assertEquals("Test", annotation.getName().getValue());
assertNotNull(annotation.getParameters());
assertEquals(2, annotation.getParameters().size());
});
register(
"package be.jeffcheasey88;"
+ ""
+ "@Test "
+ "public static interface Test{}",
(javafile) -> {
ClassBase clazzb = javafile.getMainClass();
assertNotNull(clazzb);
assertTrue(clazzb instanceof Interface);
Interface clazz = (Interface)clazzb;
List<Annotation> annotations = clazz.getAnnotations();
assertNotNull(annotations);
assertEquals(1, annotations.size());
Annotation annotation = annotations.get(0);
assertEquals("Test", annotation.getName().getValue());
assertNull(annotation.getParameters());
});
register(
"package be.jeffcheasey88;"
+ ""
+ "@Test "
+ "public static enum Test{}",
(javafile) -> {
ClassBase clazzb = javafile.getMainClass();
assertNotNull(clazzb);
assertTrue(clazzb instanceof Enumeration);
Enumeration clazz = (Enumeration)clazzb;
List<Annotation> annotations = clazz.getAnnotations();
assertNotNull(annotations);
assertEquals(1, annotations.size());
Annotation annotation = annotations.get(0);
assertEquals("Test", annotation.getName().getValue());
assertNull(annotation.getParameters());
});
}
}