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) } }
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)) } }
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)) } }
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)) } }
func (c *C) Execute(data map[string]interface{}) (string, error) { script := data["script"].(string) return thack.New().Funcs(c.builtins()).Execute(script) }