Example #1
0
func TestBasic(t *testing.T) {
	f := map[string]interface{}{
		"testFunc": func() string {
			return "Hello."
		},
	}
	script := "testFunc"
	s, err := thack.New().Funcs(f).Execute(script)
	if err != nil {
		t.Fatal(err)
	}
	if s != "Hello." {
		t.Fatal(s)
	}
}
Example #2
0
func TestEmptyLines(t *testing.T) {
	f := map[string]interface{}{
		"testFunc": func() error {
			return nil
		},
	}
	script := "testFunc\ntestFunc"
	s, err := thack.New().Funcs(f).Execute(script)
	if err != nil {
		t.Fatal(err)
	}
	if s != "" {
		t.Fatal(s, len(s))
	}
}
Example #3
0
func TestComments(t *testing.T) {
	f := map[string]interface{}{
		"testFunc": func() string {
			return "Hello."
		},
	}
	script := "testFunc\n#Comment stuff\ntestFunc"
	s, err := thack.New().Funcs(f).Execute(script)
	if err != nil {
		t.Fatal(err)
	}
	if s != "Hello.\nHello." {
		t.Fatal(s, len(s))
	}
}
Example #4
0
func TestError(t *testing.T) {
	f := map[string]interface{}{
		"testE": func() error {
			return fmt.Errorf("Test error.")
		},
		"t1": func() string {
			return "Hello."
		},
	}
	script := "$x := testE\n$x\nt1"
	s, err := thack.New().Funcs(f).Execute(script)
	if err == nil {
		t.Fatal("Error was not returned to Execute.")
	}
	if s != "" {
		t.Fatal(s, len(s))
	}
}
Example #5
0
func (c *C) Execute(data map[string]interface{}) (string, error) {
	script := data["script"].(string)
	return thack.New().Funcs(c.builtins()).Execute(script)
}