Skip to content

Commit

Permalink
feat(objectionary#627): add unit tests for DirectivesAnnotationProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jul 5, 2024
1 parent c7098cb commit 01a7cfe
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/it/annotations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ The integration test verifies that the program can run successfully, and the jeo
If you only need to run this test, use the following command:

```shell
mvn clean integration-test invoker:run -Dinvoker.test=annotations -DskipTests
mvn clean integration-test -Dinvoker.test=annotations -DskipTests
```
3 changes: 1 addition & 2 deletions src/it/annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ SOFTWARE.
<plugin>
<groupId>org.eolang</groupId>
<artifactId>eo-maven-plugin</artifactId>
<version>0.38.1</version>
<version>0.38.4</version>
<executions>
<execution>
<id>convert-xmir-to-eo</id>
Expand All @@ -72,7 +72,6 @@ SOFTWARE.
<goal>xmir-to-phi</goal>
</goals>
<configuration>
<phiOptimize>false</phiOptimize>
<phiInputDir>${project.build.directory}/generated-sources/jeo-xmir</phiInputDir>
<phiOutputDir>${project.build.directory}/generated-sources/jeo-eo</phiOutputDir>
</configuration>
Expand Down
38 changes: 19 additions & 19 deletions src/it/spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ SOFTWARE.
<executions>
<execution>
<id>bytecode-to-eo</id>
<phase>process-classes</phase>
<phase>process-classes</phase>
<goals>
<goal>disassemble</goal>
</goals>
Expand All @@ -80,24 +80,24 @@ SOFTWARE.
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eolang</groupId>
<artifactId>eo-maven-plugin</artifactId>
<version>0.38.4</version>
<executions>
<execution>
<id>convert-xmir-to-phi</id>
<phase>process-classes</phase>
<goals>
<goal>xmir-to-phi</goal>
</goals>
<configuration>
<phiInputDir>${project.build.directory}/generated-sources/jeo-xmir</phiInputDir>
<phiOutputDir>${project.build.directory}/generated-sources/phi-jeo</phiOutputDir>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eolang</groupId>
<artifactId>eo-maven-plugin</artifactId>
<version>0.38.4</version>
<executions>
<execution>
<id>convert-xmir-to-phi</id>
<phase>process-classes</phase>
<goals>
<goal>xmir-to-phi</goal>
</goals>
<configuration>
<phiInputDir>${project.build.directory}/generated-sources/jeo-xmir</phiInputDir>
<phiOutputDir>${project.build.directory}/generated-sources/phi-jeo</phiOutputDir>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,28 @@ public XmlAnnotationProperty(final XmlNode node) {
* @return Bytecode annotation property.
*/
public BytecodeAnnotationProperty toBytecode() {
final List<XmlNode> all = this.node.children().collect(Collectors.toList());
return BytecodeAnnotationProperty.byType(
(String) new XmlOperand(all.get(0)).asObject(),
all.subList(1, all.size())
.stream()
.map(XmlOperand::new)
.map(XmlOperand::asObject)
.collect(Collectors.toList())
);
return BytecodeAnnotationProperty.byType(this.type(), this.params());
}
}

/**
* Type of the property.
* @return Type.
*/
public String type() {
return (String) new XmlOperand(
this.node.children().collect(Collectors.toList()).get(0)
).asObject();
}

/**
* Property parameters.
* @return Parameters.
*/
public List<Object> params() {
return this.node.children()
.skip(1)
.map(XmlOperand::new)
.map(XmlOperand::asObject)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
package org.eolang.jeo.representation.directives;

import com.jcabi.matchers.XhtmlMatchers;
import java.util.List;
import org.eolang.jeo.representation.DataType;
import org.eolang.jeo.representation.bytecode.BytecodeAnnotation;
import org.eolang.jeo.representation.xmir.XmlAnnotationProperty;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.objectweb.asm.Type;
import org.xembly.ImpossibleModificationException;
Expand Down Expand Up @@ -102,4 +107,136 @@ void createsAnnotationProperty() throws ImpossibleModificationException {
)
);
}

@Test
void createsAnnotationPlainProperty() throws ImpossibleModificationException {
final String name = "hello";
final int value = 1;
final XmlAnnotationProperty property = new XmlAnnotationProperty(
new XmlNode(
new Xembler(DirectivesAnnotationProperty.plain(name, value)).xml()
)
);
final List<Object> params = property.params();
MatcherAssert.assertThat(
"Incorrect annotation property type",
property.type(),
Matchers.equalTo("PLAIN")
);
MatcherAssert.assertThat(
"Incorrect annotation property name",
params.get(0),
Matchers.equalTo(name)
);
MatcherAssert.assertThat(
"Incorrect annotation property value",
params.get(1),
Matchers.equalTo(value)
);
}

@Test
void createsAnnotationEnumProperty() throws ImpossibleModificationException {
final String name = "name";
final String descriptor = "Lorg/eolang/jeo/representation/DataType";
final String value = "BOOL";
final XmlAnnotationProperty property = new XmlAnnotationProperty(
new XmlNode(
new Xembler(DirectivesAnnotationProperty.enump(name, descriptor, value)).xml()
)
);
final List<Object> params = property.params();
MatcherAssert.assertThat(
"Incorrect annotation property type",
property.type(),
Matchers.equalTo("ENUM")
);
MatcherAssert.assertThat(
"Incorrect annotation property name",
params.get(0),
Matchers.equalTo(name)
);
MatcherAssert.assertThat(
"Incorrect annotation property descriptor",
params.get(1),
Matchers.equalTo(descriptor)
);
MatcherAssert.assertThat(
"Incorrect annotation property value",
params.get(2),
Matchers.equalTo(value)
);
}

@Test
void createsAnnotationArrayProperty() throws ImpossibleModificationException {
final String name = "name";
final String descriptor = "java/lang/Override";
final boolean visible = true;
final XmlAnnotationProperty property = new XmlAnnotationProperty(
new XmlNode(
new Xembler(
DirectivesAnnotationProperty.array(
name,
new DirectivesAnnotation(descriptor, visible)
)
).xml()
)
);
final List<Object> params = property.params();
MatcherAssert.assertThat(
"Incorrect annotation property type",
property.type(),
Matchers.equalTo("ARRAY")
);
MatcherAssert.assertThat(
"Incorrect annotation property name",
params.get(0),
Matchers.equalTo(name)
);
MatcherAssert.assertThat(
"Incorrect annotation property child",
(BytecodeAnnotation) params.get(1),
Matchers.equalTo(new BytecodeAnnotation(descriptor, visible))
);
}

@Test
void createsAnnotationAnnotationProperty() throws ImpossibleModificationException {
final String name = "name";
final String descriptor = "java/lang/Override";
final boolean visible = true;
final XmlAnnotationProperty property = new XmlAnnotationProperty(
new XmlNode(
new Xembler(
DirectivesAnnotationProperty.annotation(
name,
descriptor,
new DirectivesAnnotation(descriptor, visible)
)
).xml()
)
);
final List<Object> params = property.params();
MatcherAssert.assertThat(
"Incorrect annotation property type",
property.type(),
Matchers.equalTo("ANNOTATION")
);
MatcherAssert.assertThat(
"Incorrect annotation property name",
params.get(0),
Matchers.equalTo(name)
);
MatcherAssert.assertThat(
"Incorrect annotation property descriptor",
params.get(1),
Matchers.equalTo(descriptor)
);
MatcherAssert.assertThat(
"Incorrect annotation property child",
(BytecodeAnnotation) params.get(2),
Matchers.equalTo(new BytecodeAnnotation(descriptor, visible))
);
}
}

0 comments on commit 01a7cfe

Please sign in to comment.