Skip to content

Commit

Permalink
#25 Implemented custom javascript loader function in spec parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ishubin committed Nov 2, 2014
1 parent 498fb78 commit 188d06f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public class GalenJsExecutor {

private Context context;
private ImporterTopLevel scope;
private ScriptExecutor scriptExecutor;
private JsFunctionLoad scriptExecutor;

public GalenJsExecutor() {
this.context = Context.enter();
this.scope = new ImporterTopLevel(context);

this.scriptExecutor = new ScriptExecutor();
this.scriptExecutor = new JsFunctionLoad();
scope.defineProperty("load", scriptExecutor, ScriptableObject.DONTENUM);
importAllMajorClasses();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@

import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

import net.mindengine.galen.utils.GalenUtils;
import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class ScriptExecutor extends BaseFunction {
public class JsFunctionLoad extends BaseFunction {

/**
*
Expand All @@ -36,7 +39,7 @@ public class ScriptExecutor extends BaseFunction {

private Set<String> loadedFiles = new HashSet<String>();

public ScriptExecutor() {
public JsFunctionLoad() {
}

@Override
Expand All @@ -62,28 +65,34 @@ public void load(String filePath, Context cx, Scriptable scope) {
if (!contextPathStack.isEmpty()) {
contextPath = contextPathStack.peek();
}


String fullPath = filePath;

try {
File file = new File(contextPath + File.separator + filePath);
String absolutePath = file.getAbsolutePath();

if (!loadedFiles.contains(absolutePath)) {

String parentPath = new File(absolutePath).getParent();
if (!filePath.startsWith("/")) {
fullPath = contextPath + File.separator + filePath;
}

if (!loadedFiles.contains(fullPath)) {

File file = new File(fullPath);
String parentPath = file.getParent();
if (parentPath != null) {
contextPathStack.push(file.getParent());
}

cx.evaluateReader(scope, new FileReader(file), file.getAbsolutePath(), 1, null);
loadedFiles.add(absolutePath);

InputStream is = GalenUtils.findFileOrResourceAsStream(fullPath);

cx.evaluateReader(scope, new InputStreamReader(is), file.getAbsolutePath(), 1, null);
loadedFiles.add(fullPath);

if (!contextPathStack.isEmpty()) {
contextPathStack.pop();
}
}
}
catch (Exception ex) {
throw new RuntimeException("Could not load script: " + filePath, ex);
throw new RuntimeException("Could not load script: " + fullPath, ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.mindengine.galen.parser;

import net.mindengine.galen.javascript.JsFunctionLoad;
import net.mindengine.galen.suite.reader.Context;
import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.ImporterTopLevel;
Expand Down Expand Up @@ -35,6 +36,8 @@ public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptab
return jsFunctions.count((String)args[0]);
}
}, ScriptableObject.DONTENUM);

scope.defineProperty("load", new JsFunctionLoad(), ScriptableObject.DONTENUM);
}

}
Expand Down
29 changes: 19 additions & 10 deletions src/test/java/net/mindengine/galen/tests/parser/VarsParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,32 @@
public class VarsParserTest {

private static final Properties EMPTY_PROPERTIES = new Properties();
private VarsParserJsFunctions jsFunctions = new VarsParserJsFunctions() {
@Override
public int count(String regex) {
if (regex.equals("testval1")){
return 12;
}
else return 15;
}
};


@Test(dataProvider="provideGoodSamples") public void shouldProcessTemplate_successfully(Integer number, Context context, String templateText, String expectedText) {
VarsParserJsFunctions jsFunctions = new VarsParserJsFunctions() {
@Override
public int count(String regex) {
if (regex.equals("testval1")){
return 12;
}
else return 15;
}
};
VarsParser template = new VarsParser(context, EMPTY_PROPERTIES, new VarsParserJsProcessor(context, jsFunctions));
String realText = template.parse(templateText);

assertThat(realText, is(expectedText));
}

@Test
public void shouldAllowTo_loadCustomJavascript() {
Context context = new Context();
VarsParser template = new VarsParser(context, EMPTY_PROPERTIES, new VarsParserJsProcessor(context, jsFunctions));
String realText = template.parse("${load('/specs/customFunction.js')} got it from js: ${customFunction('qwe', 'ert')}");

assertThat(realText, is(" got it from js: qwe-ert"));
}


@DataProvider public Object[][] provideGoodSamples() {
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/specs/customFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this.customFunction = function (a, b) {
return a + "-" + b;
};

0 comments on commit 188d06f

Please sign in to comment.