Skip to content

Commit

Permalink
(objectionary#1104) - Store booleans as bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoss committed Aug 27, 2022
1 parent 991c825 commit 7f366f9
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 13 deletions.
10 changes: 10 additions & 0 deletions eo-maven-plugin/src/main/resources/org/eolang/maven/pre/data.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ SOFTWARE.
<xsl:text>, java.nio.charset.StandardCharsets.UTF_8</xsl:text>
<xsl:text>)</xsl:text>
</xsl:when>
<xsl:when test="@base='org.eolang.bool'">
<xsl:choose>
<xsl:when test="text() = '01'">
<xsl:text>Boolean.TRUE</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>Boolean.FALSE</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$array"/>
</xsl:otherwise>
Expand Down
20 changes: 15 additions & 5 deletions eo-maven-plugin/src/test/java/org/eolang/maven/GmiMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.hamcrest.Description;
import org.hamcrest.MatcherAssert;
import org.hamcrest.TypeSafeMatcher;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.xembly.Directives;
Expand All @@ -59,6 +61,7 @@ final class GmiMojoTest {

@ParameterizedTest
@MethodSource("yamlPacks")
@SuppressWarnings("PMD.JUnitTestContainsTooManyAsserts")
void testPacks(final String pack) throws Exception {
final Map<String, Object> map = new Yaml().load(
new TextOf(
Expand All @@ -72,17 +75,24 @@ void testPacks(final String pack) throws Exception {
new Xembler(new Directives(xembly)).domQuietly()
);
Logger.info(this, "Graph:\n%s", graph);
final Collection<Executable> assertions = new LinkedList<>();
for (final String loc : (Iterable<String>) map.get("locators")) {
MatcherAssert.assertThat(
loc, new GmiMojoTest.ExistsIn(graph)
assertions.add(
() -> MatcherAssert.assertThat(
loc,
new ExistsIn(graph)
)
);
}
for (final String xpath : (Iterable<String>) map.get("xpaths")) {
MatcherAssert.assertThat(
graph,
XhtmlMatchers.hasXPath(xpath)
assertions.add(
() -> MatcherAssert.assertThat(
graph,
XhtmlMatchers.hasXPath(xpath)
)
);
}
Assertions.assertAll(assertions);
}

@SuppressWarnings("PMD.UnusedPrivateMethod")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ xpaths:
- //v[@id='v1']/e[@to='v2' and @title='@']
- //v[@id='v1']/e[@to='v0' and @title='^']
locators:
- .simple .@ .α0 .α0 .Δ =true
- .simple .@ .α0 .α0 .Δ =01
- .simple .@ .α1 .Δ =42
- .simple .@ .α2 .hello .Δ =Jeff
eo: |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
xsls:
- /org/eolang/parser/add-default-package.xsl
- /org/eolang/maven/pre/classes.xsl
- /org/eolang/maven/pre/attrs.xsl
- /org/eolang/maven/pre/data.xsl
- /org/eolang/maven/pre/to-java.xsl
tests:
- /program/errors[count(*)=0]
- //java[contains(text(), 'Boolean.TRUE')]
- //java[contains(text(), 'Boolean.FALSE')]
eo: |
[] > t
TRUE > @
[] > f
FALSE > @
10 changes: 7 additions & 3 deletions eo-parser/src/main/java/org/eolang/parser/XeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,13 @@ public void enterData(final ProgramParser.DataContext ctx) {
base = "bytes";
data = text.replaceAll("\\s+", "").replace("-", " ").trim();
} else if (ctx.BOOL() != null) {
type = "bool";
type = "bytes";
base = "bool";
data = Boolean.toString(Boolean.parseBoolean(text));
if (Boolean.parseBoolean(text)) {
data = XeListener.bytesToHex((byte) 0x01);
} else {
data = XeListener.bytesToHex((byte) 0x00);
}
} else if (ctx.FLOAT() != null) {
type = "float";
base = "float";
Expand Down Expand Up @@ -495,7 +499,7 @@ private static String trimMargin(final String text, final int indent) {
* @param bytes Bytes.
* @return Hexadecimal value as string.
*/
private static String bytesToHex(final byte[] bytes) {
private static String bytesToHex(final byte... bytes) {
final StringJoiner str = new StringJoiner(" ");
for (final byte bty : bytes) {
str.add(String.format("%02X", bty));
Expand Down
12 changes: 11 additions & 1 deletion eo-parser/src/main/resources/org/eolang/parser/xmir-to-eo.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SOFTWARE.
supports only ASCII characters & text blocks.
Make it possible to handle any unicode character and
double-quoted strings.
@todo #1099:30m Add conversion from 'bytes' representation
@todo #1104:30m Add conversion from 'bytes' representation
back to 'int', 'double' & the rest of types. Then proceed
to with the parent todo.
-->
Expand Down Expand Up @@ -157,6 +157,16 @@ SOFTWARE.
<xsl:value-of select="$eol"/>
<xsl:text>"""</xsl:text>
</xsl:when>
<xsl:when test="@base='bool'">
<xsl:choose>
<xsl:when test="text() = '01'">
<xsl:text>TRUE</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>FALSE</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="replace(text(), ' ', '-')"/>
</xsl:otherwise>
Expand Down
31 changes: 28 additions & 3 deletions eo-parser/src/test/java/org/eolang/parser/SyntaxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void prasesNested() throws IOException {
}

@Test
void prasesDefinition() throws IOException {
void parsesDefinition() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Syntax syntax = new Syntax(
"test-it-5",
Expand All @@ -211,7 +211,7 @@ void prasesDefinition() throws IOException {
}

@Test
void prasesMethodCalls() throws IOException {
void parsesMethodCalls() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Syntax syntax = new Syntax(
"test-it-1",
Expand All @@ -225,7 +225,32 @@ void prasesMethodCalls() throws IOException {
"/program[@name='test-it-1']",
"/program/objects/o[@base='.add']",
"/program/objects/o/o[@data='int']",
"/program/objects/o/o[@data='bool']"
"/program/objects/o/o[@base='bool']"
)
);
}

@ParameterizedTest
@ValueSource(strings = {
"FALSE > false",
"TRUE > true"
})
void storesAsBytes(final String code) throws IOException {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final Syntax syntax = new Syntax(
"test-3",
new InputOf(code),
new OutputTo(output)
);
syntax.parse();
final XML xml = new XMLDocument(
new String(output.toByteArray(), StandardCharsets.UTF_8)
);
MatcherAssert.assertThat(
xml,
XhtmlMatchers.hasXPaths(
"/program/objects[count(o)=1]",
"/program/objects/o[@data='bytes']"
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ EA-EA-EA-EA > xs
"""
hello
""" > ys

TRUE > b1

FALSE > b2

0 comments on commit 7f366f9

Please sign in to comment.