Skip to content

Commit

Permalink
feat(#81): add one more puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Sep 20, 2023
1 parent ea048aa commit af87ffd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 30 deletions.
20 changes: 12 additions & 8 deletions src/test/java/it/EoSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@

/**
* EO source.
*
* @since 0.1
*/
@SuppressWarnings("JTCOP.RuleCorrectTestName")
final class EoSource {

/**
Expand All @@ -54,38 +57,39 @@ final class EoSource {
* @return XMIR.
*/
XML parse() {
final ResourceOf eo = new ResourceOf(this.resource);
final XMLOutput output = new XMLOutput();
new Syntax("scenario", eo, output);
final ResourceOf eolang = new ResourceOf(this.resource);
final XmlOutput output = new XmlOutput();
new Syntax("scenario", eolang, output);
return output.xml();
}

/**
* XML output.
*
* @since 0.1.0
*/
private static class XMLOutput implements Output {
private static final class XmlOutput implements Output {

/**
* Output stream.
*/
final ByteArrayOutputStream baos;
private final ByteArrayOutputStream baos;

/**
* Constructor.
*/
private XMLOutput() {
private XmlOutput() {
this(new ByteArrayOutputStream());
}

/**
* Constructor.
* @param baos Output stream.
*/
private XMLOutput(final ByteArrayOutputStream baos) {
private XmlOutput(final ByteArrayOutputStream baos) {
this.baos = baos;
}


@Override
public OutputStream stream() {
return this.baos;
Expand Down
1 change: 1 addition & 0 deletions src/test/java/it/JavaSourceClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @since 0.1
*/
@SuppressWarnings("JTCOP.RuleCorrectTestName")
final class JavaSourceClass {

/**
Expand Down
56 changes: 34 additions & 22 deletions src/test/java/it/JavaToEoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eolang.jeo.representation.BytecodeRepresentation;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -41,43 +42,53 @@
* Integration test that checks that the Java code is transpiled to EO code correctly.
*
* @since 0.1
* @todo #81:30min Enable the test in JavaToEoTest.
* The test is disabled because we didn't implemented correct transpilation of the Java code
* to EO code. When it is done, the test should be enabled and should pass.
* The test should check that the Java code is transpiled to EO code correctly.
* Remove that puzzle when the test is enabled.
*/
final class JavaToEoTest {

private final static String EO_RESOURCES = "transpilation/eo";
/**
* Path to the EO resources.
*/
private static final String EO_RESOURCES = "transpilation/eo";

@ParameterizedTest(name = "{index} => {0}")
@MethodSource("expectedAndActual")
@MethodSource("arguments")
@Disabled("The test is disabled because it is not ready yet, see the puzzle description above")
void compilesJavaAndTranspilesBytecodeToEo(final Resource resource) {
final String eo = resource.eo();
final String eolang = resource.eolang();
final String java = resource.java();
MatcherAssert.assertThat(
String.format(
"The transpiled EO code is not as expected, we compared the next files:%n%s%n%s",
eo,
eolang,
java
),
new BytecodeRepresentation(new JavaSourceClass(java).compile()).toEO(),
Matchers.equalTo(new EoSource(eo).parse())
Matchers.equalTo(new EoSource(eolang).parse())
);
}

/**
* Provides expected and actual resources for the test.
* @return Stream of arguments.
*/
private static Stream<Arguments> expectedAndActual() {
@SuppressWarnings("PMD.UnusedPrivateMethod")
private static Stream<Arguments> arguments() {
try {
return JavaToEoTest.resources(JavaToEoTest.EO_RESOURCES)
.stream()
.map(Arguments::of);
} catch (IOException ex) {
} catch (final IOException exception) {
throw new IllegalStateException(
String.format(
"Can't retrieve resources for the test from %s folder",
JavaToEoTest.EO_RESOURCES
),
ex
exception
);
}
}
Expand All @@ -88,28 +99,29 @@ private static Stream<Arguments> expectedAndActual() {
* @return List of resources.
* @throws IOException If something goes wrong.
*/
private static List<Resource> resources(String path) throws IOException {
List<Resource> res = new ArrayList<>(0);
private static List<Resource> resources(final String path) throws IOException {
final List<Resource> res = new ArrayList<>(0);
try (
BufferedReader br = new BufferedReader(
new InputStreamReader(
JavaSourceClass.class.getClassLoader().getResourceAsStream(path),
Thread.currentThread().getContextClassLoader().getResourceAsStream(path),
StandardCharsets.UTF_8
)
)
) {
String resource;
while ((resource = br.readLine()) != null) {
if (!resource.endsWith(".eo")) {
res.addAll(JavaToEoTest.resources(String.format("%s/%s", path, resource)));
} else {
String resource = br.readLine();
while (resource != null) {
if (resource.endsWith(".eo")) {
res.add(
new Resource(
path.substring(JavaToEoTest.EO_RESOURCES.length()),
resource.substring(0, resource.length() - 3)
)
);
} else {
res.addAll(JavaToEoTest.resources(String.format("%s/%s", path, resource)));
}
resource = br.readLine();
}
}
return res;
Expand Down Expand Up @@ -142,17 +154,17 @@ private Resource(final String subpath, final String filename) {
this.filename = filename;
}

private String eo() {
@Override
public String toString() {
return String.format("%s.eo and %1$s.java", this.filename);
}

private String eolang() {
return String.format("transpilation/eo%s/%s.eo", this.subpath, this.filename);
}

private String java() {
return String.format("transpilation/java%s/%s.java", this.subpath, this.filename);
}

@Override
public String toString() {
return String.format("%s.eo and %1$s.java", this.filename);
}
}
}
27 changes: 27 additions & 0 deletions src/test/java/it/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Integration tests.
*/
package it;

2 comments on commit af87ffd

@0pdd
Copy link

@0pdd 0pdd commented on af87ffd Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 81-8b72dde1 discovered in src/test/java/it/JavaSourceClass.java) and submitted as #83. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on af87ffd Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 81-9d03e679 discovered in src/test/java/it/JavaToEoTest.java) and submitted as #84. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.