Esempio n. 1
0
func Test_FileNode_Add(t *testing.T) {
	node0 := NewFileNode("foo/bar")
	node1 := NewFileNode("foo/baz")
	obj0 := types.MakeFuString(".c")
	obj1 := types.MakeStringList("a", "b")

	var err error
	var expect types.FuObject
	var actual types.FuObject

	// node + node = list of nodes
	expect = types.MakeFuList(node0, node1)
	actual, err = node0.Add(node1)
	assert.Nil(t, err)
	assert.True(t, expect.Equal(actual))

	// node + string = new node
	expect = NewFileNode("foo/bar.c")
	actual, err = node0.Add(obj0)
	assert.Nil(t, err)
	assert.True(t, expect.Equal(actual))

	// node + list = flattened list
	expect = types.MakeFuList(
		node0, types.MakeFuString("a"), types.MakeFuString("b"))
	actual, err = node0.Add(obj1)
	assert.Nil(t, err)
	assert.True(t, expect.Equal(actual))
}
Esempio n. 2
0
func Test_ListNode_Add(t *testing.T) {
	nodelist := newListNode()
	otherlist := types.MakeFuList()

	actual, err := nodelist.Add(otherlist)
	testutils.NoError(t, err)
	if _, ok := actual.(*ListNode); !ok {
		t.Fatalf("expected object of type *ListNode, but got %T", actual)
	}
	assert.Equal(t, 0, len(actual.List()))

	node0 := NewStubNode("bla")
	node1 := NewStubNode("pog")
	otherlist = types.MakeFuList(node0, node1)
	actual, err = nodelist.Add(otherlist)
	testutils.NoError(t, err)
	nodes := actual.(*ListNode).List()
	if len(nodes) != 2 {
		t.Errorf("expected ListNode with 2 nodes, but got %v", actual)
	} else {
		assert.Equal(t, []types.FuObject{node0, node1}, nodes)
	}

	otherlist = types.MakeStringList("foo")
	actual, err = nodelist.Add(otherlist)
	assert.Nil(t, actual)
	assert.Equal(t,
		"unsupported operation: cannot add list to ListNode "+
			"(second operand contains string)",
		err.Error())
}
Esempio n. 3
0
File: node.go Progetto: sbinet/fubsy
func defaultNodeAdd(self Node, other types.FuObject) (types.FuObject, error) {
	otherlist := other.List()
	values := make([]types.FuObject, 0, 1+len(otherlist))
	values = append(values, self)
	values = append(values, otherlist...)
	return types.MakeFuList(values...), nil
}
Esempio n. 4
0
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)
	}
}
Esempio n. 5
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)
}
Esempio n. 6
0
func ListNodeFromNodes(nodes []Node) *ListNode {
	names := make([]string, len(nodes))
	for i, node := range nodes {
		names[i] = node.Name()
	}
	name := strings.Join(names, ",")
	values := make([]types.FuObject, len(nodes))
	for i, node := range nodes {
		values[i] = node
	}
	return &ListNode{
		nodebase:     makenodebase(name),
		types.FuList: types.MakeFuList(values...),
	}
}
Esempio n. 7
0
func (self *Runtime) evaluateList(expr *dsl.ASTList) (types.FuObject, []error) {
	elements := expr.Elements()
	values := make([]types.FuObject, len(elements))
	var allerrs []error
	var errs []error
	for i, element := range elements {
		values[i], errs = self.evaluate(element)
		if len(errs) != 0 {
			allerrs = append(allerrs, errs...)
		}
	}
	if allerrs != nil {
		return nil, allerrs
	}
	return types.MakeFuList(values...), nil
}
Esempio n. 8
0
// Create a new ListNode. The members must all be Nodes; everything is
// declared as FuObject for convenience and to avoid repetitive
// coding, but you'll get a panic if you pass in any non-Node objects.
func newListNode(members ...types.FuObject) *ListNode {
	names := make([]string, len(members))
	for i, obj := range members {
		if node, ok := obj.(Node); ok {
			names[i] = node.Name()
		} else {
			panic(fmt.Sprintf("not a Node: %#v (type %T)", obj, obj))
		}
	}
	name := strings.Join(names, ",")
	node := &ListNode{
		nodebase:     makenodebase(name),
		types.FuList: types.MakeFuList(members...),
	}
	return node
}
Esempio n. 9
0
// Walk the filesystem for files matching this FinderNode's include
// patterns. Return the list of matching filenames as a FuList of
// FileNode.
func (self *FinderNode) ActionExpand(
	ns types.Namespace, ctx *types.ExpandContext) (
	types.FuObject, error) {

	// if case this node was not already expanded by
	// DAG.ExpandNodes(), do it now so variable references are
	// followed
	var err error
	err = self.NodeExpand(ns)
	if err != nil {
		return nil, err
	}
	filenames, err := self.FindFiles()
	if err != nil {
		return nil, err
	}
	var values []types.FuObject
	for _, filename := range filenames {
		values = append(values, types.MakeFuString(filename))
	}
	return types.MakeFuList(values...), nil
}
Esempio n. 10
0
func (self *ListNode) ActionExpand(
	ns types.Namespace, ctx *types.ExpandContext) (
	types.FuObject, error) {
	err := self.NodeExpand(ns)
	if err != nil {
		return nil, err
	}
	values := self.List()
	xvalues := make([]types.FuObject, 0, len(values))
	for _, obj := range values {
		// ok to panic here: already enforced in newListNode()
		node := obj.(Node)
		exp, err := node.ActionExpand(ns, ctx)
		if err != nil {
			return nil, err
		} else if exp == nil {
			xvalues = append(xvalues, node)
		} else {
			xvalues = append(xvalues, exp.List()...)
		}
	}
	return types.MakeFuList(xvalues...), nil
}