示例#1
0
func Test_FinderNode_Add(t *testing.T) {
	finder1 := NewFinderNode("*.c")
	finder2 := NewFinderNode("*.h")
	str1 := types.MakeFuString("foo.txt")
	str2 := types.MakeFuString("bar.txt")
	file1 := NewFileNode("foo.txt")
	file2 := NewFileNode("bar.txt")
	list := types.MakeFuList(str1, str2)
	stub := types.NewStubObject("bla", nil)

	sum, err := finder1.Add(finder2)
	testutils.NoError(t, err)
	assert.Equal(t, []types.FuObject{finder1, finder2}, sum.List())

	sum, err = finder1.Add(file1)
	testutils.NoError(t, err)
	assert.Equal(t, []types.FuObject{finder1, file1}, sum.List())

	sum, err = finder1.Add(str1)
	testutils.NoError(t, err)
	assert.Equal(t, []types.FuObject{finder1, file1}, sum.List())

	sum, err = finder1.Add(list)
	testutils.NoError(t, err)
	assert.Equal(t, []types.FuObject{finder1, file1, file2}, sum.List())

	list = types.MakeFuList(file2, file1)
	sum, err = finder1.Add(list)
	testutils.NoError(t, err)
	assert.Equal(t, []types.FuObject{finder1, file2, file1}, sum.List())

	sum, err = finder1.Add(stub)
	assert.Equal(t,
		"unsupported operation: cannot add stub to FinderNode", err.Error())
	assert.Nil(t, sum)

	list = types.MakeFuList(str1, stub)
	sum, err = finder1.Add(list)
	assert.Equal(t,
		"unsupported operation: cannot add list to FinderNode "+
			"(second operand contains stub)",
		err.Error())
	assert.Nil(t, sum)
}
示例#2
0
func Test_evaluateCall_no_expand(t *testing.T) {
	calls := 0
	fn_foo := func(argsource types.ArgSource) (types.FuObject, []error) {
		calls++
		return types.MakeFuString("arg: " + argsource.Args()[0].ValueString()), nil
	}
	foo := types.NewFixedFunction("foo", 1, fn_foo)
	rt := minimalRuntime()
	args := RuntimeArgs{runtime: rt}

	// call bar() with an arg that needs to be expanded to test that
	// expansion does *not* happen -- evaluateCall() doesn't know
	// which phase it's in, so it has to rely on someone else to
	// ActionExpand() each value in the build phase
	args.SetArgs([]types.FuObject{types.MakeFuString(">$src<")})
	result, errs := rt.evaluateCall(foo, args, nil)
	assert.Equal(t, 1, calls)
	assert.Equal(t, types.MakeFuString("arg: >$src<"), result)
	if len(errs) != 0 {
		t.Errorf("expected no errors, but got: %v", errs)
	}

	// now make a value that expands to three values
	expansion := types.MakeStringList("a", "b", "c")
	var val types.FuObject = types.NewStubObject("val", expansion)
	valexp, _ := val.ActionExpand(nil, nil)
	assert.Equal(t, expansion, valexp) // this actually tests StubObject

	// call foo() with that expandable value, and make sure it is
	// really called with the unexpanded value
	args.SetArgs([]types.FuObject{val})
	result, errs = rt.evaluateCall(foo, args, nil)
	assert.Equal(t, 2, calls)
	assert.Equal(t, types.MakeFuString("arg: val"), result)
	if len(errs) != 0 {
		t.Errorf("expected no errors, but got: %v", errs)
	}
}