Exemplo n.º 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)
}
Exemplo n.º 2
0
func TestParse_valid_sequence(t *testing.T) {
	tmpdir, cleanup := testutils.Mktemp()
	defer cleanup()

	// sequence of top-level children
	script := `
main {
"boo"
}
plugin foo {{{
o'malley & friends
}}}
blob {
 "meep"
 }`
	fn := testutils.Mkfile(tmpdir, "valid_2.fubsy", script)
	ast, errs := Parse(fn)
	testutils.NoErrors(t, errs)

	expect := &ASTRoot{children: []ASTNode{
		&ASTPhase{
			name:     "main",
			children: []ASTNode{&ASTString{value: "boo"}}},
		&ASTInline{
			lang: "foo", content: "o'malley & friends"},
		&ASTPhase{
			name:     "blob",
			children: []ASTNode{&ASTString{value: "meep"}}},
	}}
	assertASTEqual(t, expect, ast)
}
Exemplo n.º 3
0
func Test_evaluate_list(t *testing.T) {
	rt := minimalRuntime()
	rt.stack.Assign("blop", types.MakeFuString("bar"))
	elements := []dsl.ASTExpression{
		dsl.NewASTString("\"foo\""),
		dsl.NewASTName("blop"),
		dsl.NewASTString("\"baz\""),
	}
	astlist := dsl.NewASTList(elements)

	actual, errs := rt.evaluate(astlist)
	testutils.NoErrors(t, errs)
	expect := types.MakeStringList("foo", "bar", "baz")
	assert.Equal(t, expect, actual)
}