示例#1
0
func Test_PythonCallable_callPython(t *testing.T) {
	plugin, err := NewPythonPlugin()
	testutils.NoError(t, err)

	// Setup: define a Python function and make sure that Run() finds
	// it, so it can be added to a Fubsy namespace and used from Fubsy
	// code.
	pycode := `
def reverse_strings(*args):
    '''takes N strings, reverses each one, then returns the reversed
    strings concatenated into a single string with + between each
    one'''
    return '+'.join(''.join(reversed(arg)) for arg in args)`
	values, err := plugin.Run(pycode)
	testutils.NoError(t, err)
	value, ok := values.Lookup("reverse_strings")
	assert.True(t, ok)
	pycallable := value.(PythonCallable)
	assert.Equal(t, "reverse_strings", pycallable.Name())

	// Call the Python function with 3 strings.
	args := types.MakeStringList("foo", "blob", "pingpong").List()
	argsource := types.MakeBasicArgs(nil, args, nil)
	value, errs := pycallable.callPython(argsource)
	testutils.NoErrors(t, errs)

	// And test the returned value.
	expect := types.MakeFuString("oof+bolb+gnopgnip")
	assert.True(t, expect.Equal(value),
		"expected %s, but got %s", expect, value)
}
示例#2
0
func Test_PythonPlugin_Run(t *testing.T) {
	pp, err := NewPythonPlugin()
	testutils.NoError(t, err)
	values, err := pp.Run(`
import os
foo = ["abc", "def"]
bar = "!".join(foo)
def visible():
    pass
def _hidden():
    pass
pjoin = os.path.join
`)
	testutils.NoError(t, err)

	for _, name := range []string{"os", "foo", "bar", "_hidden", "basdf"} {
		value, ok := values.Lookup(name)
		assert.True(t, value == nil && !ok,
			"expected nothing for name '%s', but got: %v (%T)", name, value, value)
	}
	for _, name := range []string{"visible", "pjoin"} {
		value, ok := values.Lookup(name)
		callable := value.(PythonCallable)
		assert.True(t, ok && callable.Name() == name,
			"expected a PythonCallable for name '%s'", name)
	}

	values, err = pp.Run("foo = 1/0")
	assert.Equal(t, "inline Python plugin raised an exception", err.Error())
}
示例#3
0
func Test_PythonPlugin_builtins(t *testing.T) {
	cleanup := testutils.Chtemp()
	defer cleanup()

	// this isn't really the builtin Fubsy println() function: we can't
	// use it because it's in the runtime package, and we don't want
	// to because it has side-effects... but we're stuck with a
	// hardcoded set of builtin function names for now, so we have to
	// reuse one of them
	calls := []string{}
	fn_println := func(args types.ArgSource) (types.FuObject, []error) {
		s := args.Args()[0].ValueString()
		calls = append(calls, s)
		return nil, nil
	}
	builtins := StubBuiltinList{types.NewFixedFunction("println", 1, fn_println)}

	pp, err := LoadMetaPlugin("python2", builtins)
	testutils.NoError(t, err)

	values, err := pp.Run(`
fubsy.println("ding")
fubsy.println("dong")
`)
	_ = values
	expect := []string{"ding", "dong"}
	testutils.NoError(t, err)
	assert.Equal(t, expect, calls)
}
示例#4
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())
}
示例#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)
}