Skip to content

Commit 9f39818

Browse files
committed
add functional tests for defininig and using ES modules
1 parent f2cbc3f commit 9f39818

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ tasks.register('testWithGraalVMJDK', Test) {
291291
include "org/openstreetmap/josm/plugins/scripting/graalvm/esmodule/*.class"
292292
include "org/openstreetmap/josm/plugins/scripting/graalvm/*.class"
293293
exclude "org/openstreetmap/josm/plugins/scripting/graalvm/GraalVMNotPresentTest.class"
294+
include "org/openstreetmap/josm/plugins/scripting/esmodules/ESModuleUsageTest.class"
294295
testLogging {
295296
events TestLogEvent.FAILED, TestLogEvent.PASSED
296297
exceptionFormat = TestExceptionFormat.FULL
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.openstreetmap.josm.plugins.scripting.esmodules
2+
3+
import org.graalvm.polyglot.Context
4+
import org.graalvm.polyglot.HostAccess
5+
import org.graalvm.polyglot.Source
6+
import org.junit.jupiter.api.Test
7+
import org.openstreetmap.josm.plugins.scripting.JOSMFixtureBasedTest
8+
import org.openstreetmap.josm.plugins.scripting.graalvm.GraalVMFacade
9+
import org.openstreetmap.josm.plugins.scripting.graalvm.esmodule.ESModuleResolver
10+
import org.openstreetmap.josm.plugins.scripting.graalvm.esmodule.FileSystemESModuleRepository
11+
12+
import static org.junit.Assert.assertEquals
13+
14+
class ESModuleUsageTest extends JOSMFixtureBasedTest {
15+
16+
static private def buildSource(String source) {
17+
return Source.newBuilder("js", source, null)
18+
.mimeType("application/javascript+module")
19+
.build()
20+
}
21+
22+
@Test
23+
void "should successfully import and use a function provided by 'module-a' in the file system"() {
24+
final resolver = ESModuleResolver.instance
25+
final repo = new FileSystemESModuleRepository(new File(
26+
getProjectHome(),
27+
"src/test/resources/sample-modules/module-a"
28+
))
29+
resolver.setUserDefinedRepositories(List.of(repo))
30+
31+
final script= """
32+
import {add} from 'module-a'
33+
add(1,1)
34+
"""
35+
def context = Context.newBuilder("js")
36+
.allowHostAccess(HostAccess.ALL)
37+
.allowHostClassLookup(className -> true)
38+
.allowIO(true)
39+
.fileSystem(resolver)
40+
.build()
41+
GraalVMFacade.populateContext(context)
42+
final result = context.eval(buildSource(script))
43+
assertEquals(2, result.asInt())
44+
}
45+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!sample-modules.jar
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sample-modules.jar:
2+
jar cf sample-modules.jar \
3+
README.md \
4+
module-a
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
A collection of sample ECMAScript modules.
2+
3+
These ES modules are used as test cases. Some of them are very basic and export just a simple function provided in a single source file. Others have richer internal structure, internally import and use sub modules, or even import functions from other modules in this collection.
4+
5+
# How to use?
6+
7+
They are useful for two purposes:
8+
9+
1. as test case in **automatized functional tests**
10+
11+
Test scripts import them and use the exported functions. The main goal is to test whether `import` statements are correctly resolved.
12+
See [ESModuleUsageTest.groovy](../../../test/functional/groovy/org/openstreetmap/josm/plugins/scripting/esmodules/ESModuleTest.groovy) for an example.
13+
14+
2. as test case in **manual interactive tests**
15+
16+
To test the scripting plugin interactively in a running JOSM process the modules can be used as test modules.
17+
18+
In order to use them, you first have to configure an ES module repository in the scripting plugin preferences.
19+
1. Select menu item *Scripting -> Settings ...*
20+
2. Select the tab **ES Modules**
21+
3. Add a new repository pointing this directory
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Module 'module-a' exports a single function 'add'.
3+
*
4+
* Here's a sample script using the module
5+
* <pre>
6+
* import {add} from 'module-a'
7+
* import * as console from 'josm/scriptingconsole'
8+
* const b = add(1,1)
9+
* console.println(`result: ${b}`)
10+
* </pre>
11+
*/
12+
export function add(a, b) {
13+
return a + b
14+
}
1.43 KB
Binary file not shown.

0 commit comments

Comments
 (0)