func Test_nodify(t *testing.T) { sval1 := types.MakeFuString("hello.txt") sval2 := types.MakeFuString("foo.c") lval1 := types.MakeFuList(sval1, sval2) finder1 := dag.NewFinderNode("*.c", "*.h") finder2 := dag.NewFinderNode("**/*.java") rt := NewRuntime(build.BuildOptions{}, "", nil) nodes := rt.nodify(sval1) assert.Equal(t, 1, len(nodes)) assert.Equal(t, "hello.txt", nodes[0].(*dag.FileNode).Name()) nodes = rt.nodify(lval1) assert.Equal(t, 2, len(nodes)) assert.Equal(t, "hello.txt", nodes[0].(*dag.FileNode).Name()) assert.Equal(t, "foo.c", nodes[1].(*dag.FileNode).Name()) nodes = rt.nodify(finder1) assert.Equal(t, 1, len(nodes)) assert.Equal(t, "<*.c *.h>", nodes[0].(*dag.FinderNode).String()) findersum, err := finder1.Add(finder2) assert.Nil(t, err) nodes = rt.nodify(findersum) if len(nodes) == 2 { assert.Equal(t, "<*.c *.h>", nodes[0].(*dag.FinderNode).String()) assert.Equal(t, "<**/*.java>", nodes[1].(*dag.FinderNode).String()) } else { t.Errorf("expected nodify(%s) to return 2 nodes, but got %d: %v", findersum, len(nodes), nodes) } }
// evaluate simple expressions (no operators) func Test_evaluate_simple(t *testing.T) { // the expression "meep" evaluates to the string "meep" var expect types.FuObject snode := stringnode("meep") rt := minimalRuntime() ns := rt.Namespace() expect = types.MakeFuString("meep") assertEvaluateOK(t, rt, expect, snode) // the expression foo evaluates to the string "meep" if foo is set // to that string ns.Assign("foo", expect) nnode := dsl.NewASTName("foo") assertEvaluateOK(t, rt, expect, nnode) // ... and to an error if the variable is not defined location := dsl.NewStubLocation("hello, sailor") nnode = dsl.NewASTName("boo", location) assertEvaluateFail(t, rt, "hello, sailor: name not defined: 'boo'", nnode) // expression <*.c blah> evaluates to a FinderNode with two // include patterns patterns := []string{"*.c", "blah"} fnode := dsl.NewASTFileFinder(patterns) expect = dag.NewFinderNode("*.c", "blah") assertEvaluateOK(t, rt, expect, fnode) }
func (self *Runtime) evaluate( expr_ dsl.ASTExpression) ( result types.FuObject, errs []error) { switch expr := expr_.(type) { case *dsl.ASTString: result = types.MakeFuString(expr.Value()) case *dsl.ASTList: result, errs = self.evaluateList(expr) case *dsl.ASTName: result, errs = self.evaluateName(expr) case *dsl.ASTFileFinder: result = dag.NewFinderNode(expr.Patterns()...) case *dsl.ASTAdd: result, errs = self.evaluateAdd(expr) case *dsl.ASTFunctionCall: var callable types.FuCallable var args RuntimeArgs callable, args, errs = self.prepareCall(expr) if len(errs) == 0 { result, errs = self.evaluateCall(callable, args, nil) } case *dsl.ASTSelection: _, result, errs = self.evaluateLookup(expr) default: return nil, []error{unsupportedAST(expr_)} } for i, err := range errs { errs[i] = MakeLocationError(expr_, err) } return }