Skip to content

Commit

Permalink
Add a failing test for evaluating enso_project from java
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Jun 7, 2024
1 parent aa24564 commit f50e6e1
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
import static org.hamcrest.Matchers.notNullValue;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import org.enso.common.LanguageInfo;
import org.enso.interpreter.util.ScalaConversions;
import org.enso.pkg.QualifiedName;
import org.enso.polyglot.PolyglotContext;
import org.enso.polyglot.RuntimeOptions;
import org.enso.test.utils.ContextUtils;
import org.enso.test.utils.ProjectUtils;
import org.enso.test.utils.SourceModule;
import org.graalvm.polyglot.Source;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -83,4 +89,37 @@ public void ensoProjectWorksInTwoProjects() throws IOException {
assertThat(res.asString(), is("Proj2"));
});
}

@Test
public void ensoProjectCanBeCalledFromJava() throws IOException {
var mainMod =
new SourceModule(
QualifiedName.fromString("Main"),
"""
from Standard.Base import all
main =
42
""");
var projDir = temporaryFolder.newFolder().toPath();
ProjectUtils.createProject("Proj", Set.of(mainMod), projDir);
var mainModFile = projDir.resolve("src").resolve("Main.enso");
assertThat(mainModFile.toFile().exists(), is(true));
try (var ctx =
ContextUtils.defaultContextBuilder()
.option(RuntimeOptions.PROJECT_ROOT, projDir.toAbsolutePath().toString())
.build()) {
var mainSrc = Source.newBuilder(LanguageInfo.ID, mainModFile.toFile()).build();
// First eval the source so that everything is compiled.
ctx.eval(mainSrc);
var polyCtx = new PolyglotContext(ctx);
var mod = polyCtx.getTopScope().getModule("Standard.Base.Meta.Enso_Project");
var assocType = mod.getAssociatedType();
var ensoProjMethod = mod.getMethod(assocType, "enso_project").get();
var projDescr = ensoProjMethod.execute(ScalaConversions.seq(List.of(assocType)));
assertThat(projDescr.hasMembers(), is(true));
assertThat(projDescr.getMetaObject().getMetaSimpleName(), is("Project_Description"));
assertThat(projDescr.hasMember("name"), is(true));
assertThat(projDescr.invokeMember("name").asString(), is("Proj"));
}
}
}

0 comments on commit f50e6e1

Please sign in to comment.