Example #1
0
// evaluate more complex expressions
func Test_evaluate_complex(t *testing.T) {
	// a + b evaluates to various things, depending on the value
	// of those two variables
	addnode := dsl.NewASTAdd(
		dsl.NewASTName("a", dsl.NewStubLocation("loc1")),
		dsl.NewASTName("b", dsl.NewStubLocation("loc2")))

	// case 1: two strings just get concatenated
	rt := minimalRuntime()
	ns := rt.Namespace()
	ns.Assign("a", types.MakeFuString("foo"))
	ns.Assign("b", types.MakeFuString("bar"))
	expect := types.MakeFuString("foobar")
	assertEvaluateOK(t, rt, expect, addnode)

	// case 2: adding a function to a string fails
	ns.Assign("b", types.NewFixedFunction("b", 0, nil))
	assertEvaluateFail(t, rt,
		"loc1loc2: unsupported operation: cannot add function to string",
		addnode)

	// case 3: undefined name
	delete((*ns.(*types.ValueStack))[0].(types.ValueMap), "b")
	assertEvaluateFail(t, rt, "loc2: name not defined: 'b'", addnode)
}
Example #2
0
// evaluate simple expressions (no operators)
func Test_evaluate_simple(t *testing.T) {
	// the expression "meep" evaluates to the string "meep"
	var expect types.FuObject
	snode := stringnode("meep")
	rt := minimalRuntime()
	ns := rt.Namespace()
	expect = types.MakeFuString("meep")
	assertEvaluateOK(t, rt, expect, snode)

	// the expression foo evaluates to the string "meep" if foo is set
	// to that string
	ns.Assign("foo", expect)
	nnode := dsl.NewASTName("foo")
	assertEvaluateOK(t, rt, expect, nnode)

	// ... and to an error if the variable is not defined
	location := dsl.NewStubLocation("hello, sailor")
	nnode = dsl.NewASTName("boo", location)
	assertEvaluateFail(t, rt, "hello, sailor: name not defined: 'boo'", nnode)

	// expression <*.c blah> evaluates to a FinderNode with two
	// include patterns
	patterns := []string{"*.c", "blah"}
	fnode := dsl.NewASTFileFinder(patterns)
	expect = dag.NewFinderNode("*.c", "blah")
	assertEvaluateOK(t, rt, expect, fnode)
}
Example #3
0
func Test_LocationError(t *testing.T) {
	var loc dsl.Locatable
	loc = dsl.NewStubLocation("right here")
	err := errors.New("it hurts!")
	locerr := MakeLocationError(loc, err)
	assert.Equal(t, "right here: it hurts!", locerr.Error())

	// make sure it still works when LocationError has a Locatable
	// that wraps the real Location
	loc = dsl.NewStubLocatable(loc.(dsl.Location))
	locerr = MakeLocationError(loc, err)
	assert.Equal(t, "right here: it hurts!", locerr.Error())

	// and finally, don't crash when LocationError has a Locatable
	// that wraps a nil Location
	loc = dsl.NewStubLocatable(nil)
	locerr = MakeLocationError(loc, err)
	assert.Equal(t, "it hurts!", locerr.Error())
}