Beispiel #1
0
func Test_prepareCall(t *testing.T) {
	// this is never going to be called, so it's OK that it's nil
	var fn_dummy func(argsource types.ArgSource) (types.FuObject, []error)
	var dummy1, dummy2 types.FuCallable
	dummy1 = types.NewFixedFunction("dummy1", 0, fn_dummy)
	dummy2 = types.NewFixedFunction("dummy1", 1, fn_dummy)

	rt := minimalRuntime()
	ns := rt.Namespace()
	ns.Assign("dummy1", dummy1)
	ns.Assign("dummy2", dummy2)
	ns.Assign("x", types.MakeFuString("whee!"))

	noargs := []dsl.ASTExpression{}
	onearg := []dsl.ASTExpression{dsl.NewASTString("\"meep\"")}

	var astcall *dsl.ASTFunctionCall
	var callable types.FuCallable
	var args RuntimeArgs
	var errs []error

	// correct (no args) call to dummy1()
	astcall = dsl.NewASTFunctionCall(dsl.NewASTName("dummy1"), noargs)
	callable, args, errs = rt.prepareCall(astcall)
	assert.Equal(t, 0, len(errs))
	assert.Equal(t, dummy1, callable)
	assert.Equal(t, []types.FuObject{}, args.Args())

	// and to dummy2()
	astcall = dsl.NewASTFunctionCall(dsl.NewASTName("dummy2"), onearg)
	callable, args, errs = rt.prepareCall(astcall)
	assert.Equal(t, 0, len(errs))
	assert.Equal(t, dummy2, callable)
	assert.Equal(t, []types.FuObject{types.MakeFuString("meep")}, args.Args())

	// attempt to call dummy2() incorrectly (1 arg, but it's an undefined name)
	astcall = dsl.NewASTFunctionCall(
		dsl.NewASTName("dummy2"),
		[]dsl.ASTExpression{dsl.NewASTName("bogus")})
	callable, _, errs = rt.prepareCall(astcall)
	assert.Equal(t, 1, len(errs))
	assert.Equal(t, "name not defined: 'bogus'", errs[0].Error())

	// attempt to call non-existent function
	astcall = dsl.NewASTFunctionCall(dsl.NewASTName("bogus"), noargs)
	callable, _, errs = rt.prepareCall(astcall)
	assert.Nil(t, callable)
	assert.Equal(t, 1, len(errs))
	assert.Equal(t, "name not defined: 'bogus'", errs[0].Error())

	// attempt to call something that is not a function
	astcall = dsl.NewASTFunctionCall(dsl.NewASTName("x"), noargs)
	callable, _, errs = rt.prepareCall(astcall)
	assert.Nil(t, callable)
	assert.Equal(t, 1, len(errs))
	assert.Equal(t, "not a function or method: 'x'", errs[0].Error())
}
Beispiel #2
0
func Test_SequenceAction_create(t *testing.T) {
	rt := &Runtime{}
	action := NewSequenceAction()
	assert.Equal(t, 0, len(action.subactions))

	// Execute() on an empty SequenceAction does nothing, silently
	assert.Nil(t, action.Execute(rt))

	// action 1 is a bare string: "ls -lR foo/bar"
	cmd := dsl.NewASTString("\"ls -lR foo/bar\"")
	action.AddCommand(cmd)

	// action 2: a = "foo"
	assign := dsl.NewASTAssignment(
		"a",
		dsl.NewASTString("foo"))
	action.AddAssignment(assign)

	// action 3: remove("*.o")
	fcall := dsl.NewASTFunctionCall(
		dsl.NewASTString("remove"),
		[]dsl.ASTExpression{dsl.NewASTString("\"*.c\"")})
	action.AddFunctionCall(fcall)

	assert.Equal(t, 3, len(action.subactions))
	assert.Equal(t,
		"ls -lR foo/bar",
		action.subactions[0].(*CommandAction).raw.ValueString())
}
Beispiel #3
0
func Test_evaluateCall_method(t *testing.T) {
	// construct AST for "a.b.c(x)"
	astargs := []dsl.ASTExpression{dsl.NewASTName("x")}
	astcall := dsl.NewASTFunctionCall(
		dsl.NewASTSelection(
			dsl.NewASTSelection(dsl.NewASTName("a"), "b"), "c"),
		astargs)

	// make sure a is an object with attributes, and b is one of them
	// (N.B. having FileNodes be attributes of one another is weird
	// and would never happen in a real Fubsy script, but it's a
	// convenient way to setup this method call)
	aobj := dag.NewFileNode("a.txt")
	bobj := dag.NewFileNode("b.txt")
	aobj.ValueMap = types.NewValueMap()
	aobj.Assign("b", bobj)

	// make sure a.b.c is a method
	calls := make([]string, 0) // list of function names
	var meth_c types.FuCode
	meth_c = func(argsource types.ArgSource) (types.FuObject, []error) {
		args := argsource.Args()
		if len(args) != 1 {
			panic("c() called with wrong number of args")
		}
		calls = append(calls, "c")
		robj := argsource.Receiver()
		return nil, []error{
			fmt.Errorf("c failed: receiver: %s %v, arg: %s %v",
				robj.Typename(), robj, args[0].Typename(), args[0])}
	}
	bobj.ValueMap = types.NewValueMap()
	bobj.Assign("c", types.NewFixedFunction("c", 1, meth_c))

	rt := minimalRuntime()
	ns := rt.Namespace()
	ns.Assign("a", aobj)
	ns.Assign("x", types.MakeFuString("hello"))

	// what the hell, let's test the precall feature too
	var precalledCallable types.FuCallable
	var precalledArgs types.ArgSource
	precall := func(callable types.FuCallable, argsource types.ArgSource) {
		precalledCallable = callable
		precalledArgs = argsource
	}

	callable, args, errs := rt.prepareCall(astcall)
	assert.Equal(t, "c", callable.(*types.FuFunction).Name())
	assert.True(t, args.Receiver() == bobj)
	assert.Equal(t, 0, len(errs))

	result, errs := rt.evaluateCall(callable, args, precall)
	assert.Equal(t, precalledCallable, callable)
	assert.Equal(t, precalledArgs.Args(), types.MakeStringList("hello").List())
	assert.Nil(t, result)
	if len(errs) == 1 {
		assert.Equal(t,
			"c failed: receiver: FileNode \"b.txt\", arg: string \"hello\"",
			errs[0].Error())
	} else {
		t.Errorf("expected exactly 1 error, but got: %v", errs)
	}
}