Skip to content

Commit

Permalink
feat(objectionary#1417): Test API sketch.
Browse files Browse the repository at this point in the history
feat(objectionary#1417): add FakeMaven class

feat(objectionary#1417): simplify API code

feat(objectionary#1417): add package-info.java
  • Loading branch information
volodya-lombrozo committed Nov 17, 2022
1 parent 508b035 commit dcca5ec
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*
* @since 0.29
*/
final class Catalogs {
public final class Catalogs {

/**
* Singleton.
Expand Down
2 changes: 1 addition & 1 deletion eo-maven-plugin/src/main/java/org/eolang/maven/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public Bytes load(final Path path) throws IOException {
* @param path Cwd-relative path to file
* @return Absolute path
*/
private Path absolute(final Path path) {
public Path absolute(final Path path) {
return this.cwd.resolve(path);
}
}
4 changes: 2 additions & 2 deletions eo-maven-plugin/src/main/java/org/eolang/maven/Moja.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* @param <T> Type of mojo
* @since 0.1
*/
final class Moja<T extends AbstractMojo> {
public final class Moja<T extends AbstractMojo> {

/**
* The type of mojo.
Expand All @@ -60,7 +60,7 @@ final class Moja<T extends AbstractMojo> {
*
* @param tpe The type
*/
Moja(final Class<T> tpe) {
public Moja(final Class<T> tpe) {
this.type = tpe;
this.attrs = new HashMap<>(0);
}
Expand Down
34 changes: 6 additions & 28 deletions eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.cactoos.text.UncheckedText;
import org.eolang.maven.hash.ChNarrow;
import org.eolang.maven.hash.ChRemote;
import org.eolang.maven.testapi.MwFake;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
Expand All @@ -48,35 +49,12 @@ final class ParseMojoTest {

@Test
void testSimpleParsing(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/x/main.eo");
final Path target = temp.resolve("target");
new Home(temp).save(
"+package f\n\n[args] > main\n (stdout \"Hello!\").print\n",
temp.relativize(src)
);
final Path foreign = temp.resolve("eo-foreign.csv");
Catalogs.INSTANCE.make(foreign)
.add("foo.x.main")
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_EO, src.toString());
new Moja<>(ParseMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("cache", temp.resolve("cache/parsed"))
.with("foreignFormat", "csv")
.execute();
MatcherAssert.assertThat(
new Home(target).exists(
Paths.get(
String.format("%s/foo/x/main.%s", ParseMojo.DIR, TranspileMojo.EXT)
)
),
Matchers.is(true)
);
MatcherAssert.assertThat(
new TjSmart(
Catalogs.INSTANCE.make(foreign)
).getById("foo.x.main").exists("xmir"),
new MwFake(temp)
.program("+package f\n\n[args] > main\n (stdout \"Hello!\").print\n")
.execute(ParseMojo.class)
.result()
.xmirCompiled(),
Matchers.is(true)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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.
*/
package org.eolang.maven.testapi;

import com.yegor256.tojos.TjSmart;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.eolang.maven.Catalogs;
import org.eolang.maven.Home;
import org.eolang.maven.ParseMojo;
import org.eolang.maven.TranspileMojo;

/**
* Maven compilation result.
* @since 0.28.12
*/
public final class CompilationResult {
/**
* Compiled eo program id.
*/
private final String id;

/**
* Target folder with compilation results.
*/
private final Home target;

/**
* Tojo for eo-foreign.* file.
*/
private final TjSmart foreign;

/**
* The main constructor.
*
* @param id Compiled eo program id
* @param target Target folder with compilation results.
* @param foreign Path to eo-foreign.* file
*/
public CompilationResult(
final String id,
final Home target,
final Path foreign
) {
this.target = target;
this.foreign = new TjSmart(
Catalogs.INSTANCE.make(foreign)
);
this.id = id;
}

/**
* Checks if eo program was successfully parsed to xmir file.
* eo-foreign tojo also has an entry about it.
*
* @return True if eo program parsed to xmir successfully.
*/
public boolean xmirCompiled() {
return this.target.exists(
Paths.get(
String.format(
"%s/%s.%s",
ParseMojo.DIR,
this.id.replace(".", "/"),
TranspileMojo.EXT
)
)
) && this.foreign.getById(this.id).exists("xmir");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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.
*/
package org.eolang.maven.testapi;

import java.io.IOException;
import org.apache.maven.plugin.AbstractMojo;

/**
* Maven testing workspace.
* @since 0.28.12
*/
public interface MavenWorkspace {

/**
* Adds eo program to a workspace.
* @param program Program as a raw string.
* @return Workspace with eo program.
* @throws IOException If can't save eo program in workspace.
*/
MavenWorkspace program(String program) throws IOException;

/**
* Executes Mojo in the workspace.
*
* @param mojo Mojo to execute.
* @param <T> Template for descendants of Mojo.
* @return Workspace after executing Mojo.
*/
<T extends AbstractMojo> MavenWorkspace execute(Class<T> mojo);

/**
* Builds and returns compilation results after all manipulations.
*
* @return Compilation result.
*/
CompilationResult result();
}
135 changes: 135 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/testapi/MwFake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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.
*/
package org.eolang.maven.testapi;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.maven.plugin.AbstractMojo;
import org.eolang.maven.AssembleMojo;
import org.eolang.maven.Catalogs;
import org.eolang.maven.Home;
import org.eolang.maven.Moja;

/**
* Fake maven workspace that executes Mojos in order to test
* their behaviour and results.
* @since 0.28.12
*/
public final class MwFake implements MavenWorkspace {

/**
* Default eo program id.
*/
private static final String PROGRAM_ID = "foo.x.main";

/**
* Default eo program path.
*/
private static final String PROGRAM_PATH = "foo/x/main.eo";

/**
* Default eo-foreign.csv file format.
*/
private static final String FOREIGN_FORMAT = "csv";

/**
* Default eo-foreign.csv file path.
*/
private static final Path FOREIGN_PATH = Paths.get("eo-foreign.csv");

/**
* Test workspace where we place all programs, files, compilation results, etc.
*/
private final Home workspace;

/**
* Path to a program in workspace.
*/
private Path prog;

/**
* The main constructor.
*
* @param workspace Test temporary directory.
*/
public MwFake(final Path workspace) {
this.workspace = new Home(workspace);
}

@Override
public MavenWorkspace program(final String program) throws IOException {
final Path path = Paths.get(MwFake.PROGRAM_PATH);
this.workspace.save(program, path);
this.prog = path;
return this;
}

@Override
public <T extends AbstractMojo> MavenWorkspace execute(final Class<T> mojo) {
this.withEoForeign();
new Moja<>(mojo)
.with("targetDir", this.targetPath().toFile())
.with("foreign", this.foreignPath().toFile())
.with("foreignFormat", MwFake.FOREIGN_FORMAT)
.execute();
return this;
}

@Override
public CompilationResult result() {
return new CompilationResult(
MwFake.PROGRAM_ID,
new Home(this.targetPath()),
this.foreignPath()
);
}

/**
* Creates eo-foreign.* file. In the future it is going to be a method for `MavenWorkspace`
* interface.
*/
private void withEoForeign() {
Catalogs.INSTANCE.make(this.foreignPath())
.add(MwFake.PROGRAM_ID)
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_EO, this.workspace.absolute(this.prog));
}

/**
* Path to 'eo-foreign.csv' or 'eo-foreign.json' file after all changes.
* @return Path to eo-foreign.* file.
*/
private Path foreignPath() {
return this.workspace.absolute(MwFake.FOREIGN_PATH);
}

/**
* Path to compilation target directory.
* @return Path to target dir.
*/
private Path targetPath() {
return this.workspace.absolute(Paths.get("target"));
}
}
Loading

0 comments on commit dcca5ec

Please sign in to comment.