Search in sources :

Example 61 with Script

use of groovy.lang.Script in project grails-core by grails.

the class DefaultUrlMappingEvaluatorTests method testNewMethod.

public void testNewMethod() throws Exception {
    GroovyShell shell = new GroovyShell();
    Binding binding = new Binding();
    Script script = shell.parse("mappings = {\n" + "    \"/$controller/$action?/$id?\" { \n" + "        constraints {\n" + "            id(matches:/\\d+/)\n" + "        }\n" + "    }\n" + "}\n");
    script.setBinding(binding);
    script.run();
    Closure closure = (Closure) binding.getVariable("mappings");
    List mappings = evaluator.evaluateMappings(closure);
    assertEquals(1, mappings.size());
    UrlMapping mapping = (UrlMapping) mappings.get(0);
    assertNull(mapping.getActionName());
    assertNull(mapping.getControllerName());
    assertEquals("(*)", mapping.getUrlData().getTokens()[0]);
    assertEquals("(*)?", mapping.getUrlData().getTokens()[1]);
    assertEquals("(*)?", mapping.getUrlData().getTokens()[2]);
    assertNotNull(mapping.getConstraints());
    assertTrue(makeSureMatchesConstraintExistsOnId(mapping));
    GrailsWebRequest r = GrailsWebMockUtil.bindMockWebRequest();
    UrlMappingInfo info = mapping.match("/mycontroller");
    info.configure(r);
    assertEquals("mycontroller", info.getControllerName());
    assertNull(mapping.match("/mycontroller").getActionName());
    assertNull(mapping.match("/mycontroller").getId());
    UrlMappingInfo info2 = mapping.match("/mycontroller/test");
    info2.configure(r);
    assertEquals("test", info2.getActionName());
    assertNull(mapping.match("/mycontroller/test").getId());
    assertEquals("234", mapping.match("/blog/test/234").getId());
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) List(java.util.List) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) GroovyShell(groovy.lang.GroovyShell) UrlMappingInfo(grails.web.mapping.UrlMappingInfo)

Example 62 with Script

use of groovy.lang.Script in project grails-core by grails.

the class DefaultUrlMappingEvaluatorTests method testNamedMappings.

public void testNamedMappings() throws Exception {
    GroovyShell shell = new GroovyShell();
    Binding binding = new Binding();
    Script script = shell.parse("mappings = {\n" + "name firstMapping: \"/first/one\" {\n" + "}\n" + "\"/second/one\" {" + "}\n" + "name thirdMapping: \"/third/one\" {\n" + "}\n" + "}");
    script.setBinding(binding);
    script.run();
    Closure closure = (Closure) binding.getVariable("mappings");
    List mappings = evaluator.evaluateMappings(closure);
    assertEquals(3, mappings.size());
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) Closure(groovy.lang.Closure) List(java.util.List) GroovyShell(groovy.lang.GroovyShell)

Example 63 with Script

use of groovy.lang.Script in project ratpack by ratpack.

the class RatpackDslScriptCapture method apply.

public RatpackDslClosures apply(Path file, String scriptContent) throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    ScriptEngine<Script> scriptEngine = new ScriptEngine<>(classLoader, compileStatic, Script.class);
    return RatpackDslClosures.capture(function, file, () -> {
        Script script = scriptEngine.create(file.getFileName().toString(), file, scriptContent);
        script.setBinding(new Binding(args));
        script.run();
    });
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) ScriptEngine(ratpack.groovy.script.internal.ScriptEngine)

Example 64 with Script

use of groovy.lang.Script in project groovy by apache.

the class GroovyTestSuite method loadTestSuite.

public void loadTestSuite() throws Exception {
    String fileName = System.getProperty("test", file);
    if (fileName == null) {
        throw new RuntimeException("No filename given in the 'test' system property so cannot run a Groovy unit test");
    }
    System.out.println("Compiling: " + fileName);
    Class type = compile(fileName);
    String[] args = {};
    if (!Test.class.isAssignableFrom(type) && Script.class.isAssignableFrom(type)) {
        // let's treat the script as a Test
        addTest(new ScriptTestAdapter(type, args));
    } else {
        addTestSuite(type);
    }
}
Also used : Script(groovy.lang.Script) ScriptTestAdapter(org.codehaus.groovy.runtime.ScriptTestAdapter)

Example 65 with Script

use of groovy.lang.Script in project intellij-community by JetBrains.

the class GroovyScriptMacro method runIt.

private static Object runIt(Expression[] params, ExpressionContext context) {
    try {
        Result result = params[0].calculateResult(context);
        if (result == null)
            return result;
        String text = result.toString();
        GroovyShell shell = new GroovyShell();
        File possibleFile = new File(text);
        Script script = possibleFile.exists() ? shell.parse(possibleFile) : shell.parse(text);
        Binding binding = new Binding();
        for (int i = 1; i < params.length; ++i) {
            Result paramResult = params[i].calculateResult(context);
            Object value = null;
            if (paramResult instanceof ListResult) {
                value = ContainerUtil.map2List(((ListResult) paramResult).getComponents(), result1 -> result1.toString());
            } else if (paramResult != null) {
                value = paramResult.toString();
            }
            binding.setVariable("_" + i, value);
        }
        binding.setVariable("_editor", context.getEditor());
        script.setBinding(binding);
        Object o = script.run();
        return o != null ? StringUtil.convertLineSeparators(o.toString()) : null;
    } catch (Exception e) {
        return new TextResult(StringUtil.convertLineSeparators(e.getLocalizedMessage()));
    }
}
Also used : Binding(groovy.lang.Binding) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.codeInsight.template(com.intellij.codeInsight.template) Set(java.util.Set) ContainerUtil(com.intellij.util.containers.ContainerUtil) Script(groovy.lang.Script) GroovyShell(groovy.lang.GroovyShell) File(java.io.File) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) Function(com.intellij.util.Function) Binding(groovy.lang.Binding) NotNull(org.jetbrains.annotations.NotNull) LinkedHashSet(java.util.LinkedHashSet) Script(groovy.lang.Script) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Aggregations

Script (groovy.lang.Script)123 Binding (groovy.lang.Binding)59 GroovyShell (groovy.lang.GroovyShell)23 IOException (java.io.IOException)21 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)13 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 File (java.io.File)12 Map (java.util.Map)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)8 Closure (groovy.lang.Closure)8 PrintWriter (java.io.PrintWriter)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)8 MissingMethodException (groovy.lang.MissingMethodException)7