Skip to content

Commit

Permalink
feat(objectionary#1417): remove supplementary class and expose some F…
Browse files Browse the repository at this point in the history
…akeMaven methods
  • Loading branch information
volodya-lombrozo committed Nov 22, 2022
1 parent 297d4d7 commit 76576ea
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.eolang.maven.hash.ChNarrow;
import org.eolang.maven.hash.ChRemote;
import org.eolang.maven.testapi.FakeMaven;
import org.eolang.maven.testapi.TojoAttribute;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
Expand All @@ -52,6 +51,8 @@ final class ParseMojoTest {
void testSimpleParsing(@TempDir final Path temp) throws Exception {
final FakeMaven maven = new FakeMaven(temp);
maven.forProgram("+package f", "[args] > main", " (stdout \"Hello!\").print")
.withDefaults()
.withEoForeign()
.execute(ParseMojo.class);
MatcherAssert.assertThat(
new Home(maven.targetPath()).exists(
Expand Down Expand Up @@ -83,7 +84,9 @@ void testSimpleParsingCached(@TempDir final Path temp) throws Exception {
cache.resolve(ParseMojo.PARSED)
).save("foo.x.main", "xmir", () -> expected);
maven.forProgram("invalid content")
.with(TojoAttribute.HASH, hash)
.withTojoAttribute(AssembleMojo.ATTR_HASH, hash)
.withDefaults()
.withEoForeign()
.with("cache", cache)
.execute(ParseMojo.class);
MatcherAssert.assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public final class FakeMaven {
*/
public FakeMaven(final Path workspace) {
this.workspace = new Home(workspace);
this.params = this.defaultParams();
this.params = new HashMap<>();
this.attributes = new HashMap<>();
}

Expand All @@ -108,15 +108,44 @@ public FakeMaven forProgram(final String... program) throws IOException {
return this;
}

/**
* Set default the params for all mojos.
*
* @return The same maven instance.
*/
public FakeMaven withDefaults() {
this.params.put("targetDir", this.targetPath().toFile());
this.params.put("foreign", this.foreignPath().toFile());
this.params.put("foreignFormat", FakeMaven.FOREIGN_FORMAT);
return this;
}

/**
* Sets parameter for execution.
*
* @param key Parameter name
* @param param Parameter name
* @param value Parameter value
* @return The same maven instance.
*/
public FakeMaven with(final String key, final Object value) {
this.params.put(key, value);
public FakeMaven with(final String param, final Object value) {
this.params.put(param, value);
return this;
}

/**
* Creates eo-foreign.* file.
*
* @return The same maven instance.
*/
public FakeMaven withEoForeign() {
final Tojo tojo = Catalogs.INSTANCE.make(this.foreignPath())
.add(FakeMaven.PROGRAM_ID);
tojo.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_VERSION, "0.25.0")
.set(AssembleMojo.ATTR_EO, this.workspace.absolute(this.prog));
for (final Map.Entry<String, Object> entry : this.attributes.entrySet()) {
tojo.set(entry.getKey(), entry.getValue());
}
return this;
}

Expand All @@ -127,8 +156,8 @@ public FakeMaven with(final String key, final Object value) {
* @param value Attribute value.
* @return The same maven instance.
*/
public FakeMaven with(final TojoAttribute attribute, final Object value) {
this.attributes.put(attribute.toString(), value);
public FakeMaven withTojoAttribute(final String attribute, final Object value) {
this.attributes.put(attribute, value);
return this;
}

Expand All @@ -140,7 +169,6 @@ public FakeMaven with(final TojoAttribute attribute, final Object value) {
* @return Workspace after executing Mojo.
*/
public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) {
this.withEoForeign();
final Moja<T> moja = new Moja<>(mojo);
for (final Map.Entry<String, ?> entry : this.params.entrySet()) {
moja.with(entry.getKey(), entry.getValue());
Expand All @@ -164,32 +192,4 @@ public Path targetPath() {
public Path foreignPath() {
return this.workspace.absolute(FakeMaven.FOREIGN_PATH);
}

/**
* Creates eo-foreign.* file.
*/
private void withEoForeign() {
final Tojo tojo = Catalogs.INSTANCE.make(this.foreignPath())
.add(FakeMaven.PROGRAM_ID);
tojo.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_VERSION, "0.25.0")
.set(AssembleMojo.ATTR_EO, this.workspace.absolute(this.prog));
for (final Map.Entry<String, Object> entry : this.attributes.entrySet()) {
tojo.set(entry.getKey(), entry.getValue());
}
}

/**
* Creates the param map for all mojos.
* The params the same for each execution.
*
* @return Map of default mojo params.
*/
private Map<String, Object> defaultParams() {
final Map<String, Object> res = new HashMap<>();
res.put("targetDir", this.targetPath().toFile());
res.put("foreign", this.foreignPath().toFile());
res.put("foreignFormat", FakeMaven.FOREIGN_FORMAT);
return res;
}
}

This file was deleted.

0 comments on commit 76576ea

Please sign in to comment.