Search in sources :

Example 6 with SoyFunction

use of com.google.template.soy.shared.restricted.SoyFunction in project closure-templates by google.

the class EvalVisitorTest method eval.

/**
 * Evaluates the given expression and returns the result.
 *
 * @param expression The expression to evaluate.
 * @return The expression result.
 * @throws Exception If there's an error.
 */
private SoyValue eval(String expression) throws Exception {
    PrintNode code = (PrintNode) SoyFileSetParserBuilder.forTemplateContents(// wrap in a function so we don't run into the 'can't print bools' error message
    untypedTemplateBodyForExpression("fakeFunction(" + expression + ")")).addSoyFunction(new SoyFunction() {

        @Override
        public String getName() {
            return "fakeFunction";
        }

        @Override
        public Set<Integer> getValidArgsSizes() {
            return ImmutableSet.of(1);
        }
    }).parse().fileSet().getChild(0).getChild(0).getChild(0);
    ExprNode expr = ((FunctionNode) code.getExpr().getChild(0)).getChild(0);
    EvalVisitor evalVisitor = new EvalVisitorFactoryImpl().create(TestingEnvironment.createForTest(testData, LOCALS), TEST_IJ_DATA, cssRenamingMap, xidRenamingMap, null, /* debugSoyTemplateInfo= */
    false);
    return evalVisitor.exec(expr);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) SoyFunction(com.google.template.soy.shared.restricted.SoyFunction) FunctionNode(com.google.template.soy.exprtree.FunctionNode) PrintNode(com.google.template.soy.soytree.PrintNode)

Example 7 with SoyFunction

use of com.google.template.soy.shared.restricted.SoyFunction in project closure-templates by google.

the class SharedModuleTest method testFunctionsSupportAllBackends.

@Test
public void testFunctionsSupportAllBackends() {
    for (SoyFunction function : injector.getInstance(new Key<Set<SoyFunction>>() {
    })) {
        if (!FUNCTIONS_WITH_SPECIAL_TYPE_HANDLING.contains(function.getName())) {
            assertThat(function).isInstanceOf(TypedSoyFunction.class);
        }
        assertThat(function).isInstanceOf(SoyJsSrcFunction.class);
        assertThat(function).isInstanceOf(SoyJavaFunction.class);
        assertThat(function).isInstanceOf(SoyJbcSrcFunction.class);
        assertThat(function).isInstanceOf(SoyPySrcFunction.class);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) SoyFunction(com.google.template.soy.shared.restricted.SoyFunction) TypedSoyFunction(com.google.template.soy.shared.restricted.TypedSoyFunction) Test(org.junit.Test)

Aggregations

SoyFunction (com.google.template.soy.shared.restricted.SoyFunction)7 ImmutableSet (com.google.common.collect.ImmutableSet)3 Set (java.util.Set)3 PrintNode (com.google.template.soy.soytree.PrintNode)2 Test (org.junit.Test)2 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 TypedSoyFunction (com.google.template.soy.shared.restricted.TypedSoyFunction)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1