| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- package fasttemplate
- import (
- "bytes"
- "errors"
- "io"
- "testing"
- )
- func TestEmptyTemplate(t *testing.T) {
- tpl := New("", "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "bar", "aaa": "bbb"})
- if s != "" {
- t.Fatalf("unexpected string returned %q. Expected empty string", s)
- }
- }
- func TestEmptyTagStart(t *testing.T) {
- expectPanic(t, func() { NewTemplate("foobar", "", "]") })
- }
- func TestEmptyTagEnd(t *testing.T) {
- expectPanic(t, func() { NewTemplate("foobar", "[", "") })
- }
- func TestNoTags(t *testing.T) {
- template := "foobar"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "bar", "aaa": "bbb"})
- if s != template {
- t.Fatalf("unexpected template value %q. Expected %q", s, template)
- }
- }
- func TestEmptyTagName(t *testing.T) {
- template := "foo[]bar"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"": "111", "aaa": "bbb"})
- result := "foo111bar"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestOnlyTag(t *testing.T) {
- template := "[foo]"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
- result := "111"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestStartWithTag(t *testing.T) {
- template := "[foo]barbaz"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
- result := "111barbaz"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestEndWithTag(t *testing.T) {
- template := "foobar[foo]"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
- result := "foobar111"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestTemplateReset(t *testing.T) {
- template := "foo{bar}baz"
- tpl := New(template, "{", "}")
- s := tpl.ExecuteString(map[string]interface{}{"bar": "111"})
- result := "foo111baz"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- template = "[xxxyyyzz"
- if err := tpl.Reset(template, "[", "]"); err == nil {
- t.Fatalf("expecting error for unclosed tag on %q", template)
- }
- template = "[xxx]yyy[zz]"
- if err := tpl.Reset(template, "[", "]"); err != nil {
- t.Fatalf("unexpected error: %s", err)
- }
- s = tpl.ExecuteString(map[string]interface{}{"xxx": "11", "zz": "2222"})
- result = "11yyy2222"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestDuplicateTags(t *testing.T) {
- template := "[foo]bar[foo][foo]baz"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
- result := "111bar111111baz"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestMultipleTags(t *testing.T) {
- template := "foo[foo]aa[aaa]ccc"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
- result := "foo111aabbbccc"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestLongDelimiter(t *testing.T) {
- template := "foo{{{foo}}}bar"
- tpl := New(template, "{{{", "}}}")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
- result := "foo111bar"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestIdenticalDelimiter(t *testing.T) {
- template := "foo@foo@foo@aaa@"
- tpl := New(template, "@", "@")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
- result := "foo111foobbb"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestDlimitersWithDistinctSize(t *testing.T) {
- template := "foo<?phpaaa?>bar<?phpzzz?>"
- tpl := New(template, "<?php", "?>")
- s := tpl.ExecuteString(map[string]interface{}{"zzz": "111", "aaa": "bbb"})
- result := "foobbbbar111"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestEmptyValue(t *testing.T) {
- template := "foobar[foo]"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"foo": "", "aaa": "bbb"})
- result := "foobar"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestNoValue(t *testing.T) {
- template := "foobar[foo]x[aaa]"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{"aaa": "bbb"})
- result := "foobarxbbb"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestNoEndDelimiter(t *testing.T) {
- template := "foobar[foo"
- _, err := NewTemplate(template, "[", "]")
- if err == nil {
- t.Fatalf("expected non-nil error. got nil")
- }
- expectPanic(t, func() { New(template, "[", "]") })
- }
- func TestUnsupportedValue(t *testing.T) {
- template := "foobar[foo]"
- tpl := New(template, "[", "]")
- expectPanic(t, func() {
- tpl.ExecuteString(map[string]interface{}{"foo": 123, "aaa": "bbb"})
- })
- }
- func TestMixedValues(t *testing.T) {
- template := "foo[foo]bar[bar]baz[baz]"
- tpl := New(template, "[", "]")
- s := tpl.ExecuteString(map[string]interface{}{
- "foo": "111",
- "bar": []byte("bbb"),
- "baz": TagFunc(func(w io.Writer, tag string) (int, error) { return w.Write([]byte(tag)) }),
- })
- result := "foo111barbbbbazbaz"
- if s != result {
- t.Fatalf("unexpected template value %q. Expected %q", s, result)
- }
- }
- func TestExecuteFunc(t *testing.T) {
- testExecuteFunc(t, "", "")
- testExecuteFunc(t, "a", "a")
- testExecuteFunc(t, "abc", "abc")
- testExecuteFunc(t, "{foo}", "xxxx")
- testExecuteFunc(t, "a{foo}", "axxxx")
- testExecuteFunc(t, "{foo}a", "xxxxa")
- testExecuteFunc(t, "a{foo}bc", "axxxxbc")
- testExecuteFunc(t, "{foo}{foo}", "xxxxxxxx")
- testExecuteFunc(t, "{foo}bar{foo}", "xxxxbarxxxx")
- // unclosed tag
- testExecuteFunc(t, "{unclosed", "{unclosed")
- testExecuteFunc(t, "{{unclosed", "{{unclosed")
- testExecuteFunc(t, "{un{closed", "{un{closed")
- // test unknown tag
- testExecuteFunc(t, "{unknown}", "zz")
- testExecuteFunc(t, "{foo}q{unexpected}{missing}bar{foo}", "xxxxqzzzzbarxxxx")
- }
- func testExecuteFunc(t *testing.T, template, expectedOutput string) {
- var bb bytes.Buffer
- ExecuteFunc(template, "{", "}", &bb, func(w io.Writer, tag string) (int, error) {
- if tag == "foo" {
- return w.Write([]byte("xxxx"))
- }
- return w.Write([]byte("zz"))
- })
- output := string(bb.Bytes())
- if output != expectedOutput {
- t.Fatalf("unexpected output for template=%q: %q. Expected %q", template, output, expectedOutput)
- }
- }
- func TestExecute(t *testing.T) {
- testExecute(t, "", "")
- testExecute(t, "a", "a")
- testExecute(t, "abc", "abc")
- testExecute(t, "{foo}", "xxxx")
- testExecute(t, "a{foo}", "axxxx")
- testExecute(t, "{foo}a", "xxxxa")
- testExecute(t, "a{foo}bc", "axxxxbc")
- testExecute(t, "{foo}{foo}", "xxxxxxxx")
- testExecute(t, "{foo}bar{foo}", "xxxxbarxxxx")
- // unclosed tag
- testExecute(t, "{unclosed", "{unclosed")
- testExecute(t, "{{unclosed", "{{unclosed")
- testExecute(t, "{un{closed", "{un{closed")
- // test unknown tag
- testExecute(t, "{unknown}", "")
- testExecute(t, "{foo}q{unexpected}{missing}bar{foo}", "xxxxqbarxxxx")
- }
- func testExecute(t *testing.T, template, expectedOutput string) {
- var bb bytes.Buffer
- Execute(template, "{", "}", &bb, map[string]interface{}{"foo": "xxxx"})
- output := string(bb.Bytes())
- if output != expectedOutput {
- t.Fatalf("unexpected output for template=%q: %q. Expected %q", template, output, expectedOutput)
- }
- }
- func TestExecuteString(t *testing.T) {
- testExecuteString(t, "", "")
- testExecuteString(t, "a", "a")
- testExecuteString(t, "abc", "abc")
- testExecuteString(t, "{foo}", "xxxx")
- testExecuteString(t, "a{foo}", "axxxx")
- testExecuteString(t, "{foo}a", "xxxxa")
- testExecuteString(t, "a{foo}bc", "axxxxbc")
- testExecuteString(t, "{foo}{foo}", "xxxxxxxx")
- testExecuteString(t, "{foo}bar{foo}", "xxxxbarxxxx")
- // unclosed tag
- testExecuteString(t, "{unclosed", "{unclosed")
- testExecuteString(t, "{{unclosed", "{{unclosed")
- testExecuteString(t, "{un{closed", "{un{closed")
- // test unknown tag
- testExecuteString(t, "{unknown}", "")
- testExecuteString(t, "{foo}q{unexpected}{missing}bar{foo}", "xxxxqbarxxxx")
- }
- func testExecuteString(t *testing.T, template, expectedOutput string) {
- output := ExecuteString(template, "{", "}", map[string]interface{}{"foo": "xxxx"})
- if output != expectedOutput {
- t.Fatalf("unexpected output for template=%q: %q. Expected %q", template, output, expectedOutput)
- }
- }
- func expectPanic(t *testing.T, f func()) {
- defer func() {
- if r := recover(); r == nil {
- t.Fatalf("missing panic")
- }
- }()
- f()
- }
- func TestExecuteFuncStringWithErr(t *testing.T) {
- var expectErr = errors.New("test111")
- result, err := ExecuteFuncStringWithErr(`{a} is {b}'s best friend`, "{", "}", func(w io.Writer, tag string) (int, error) {
- if tag == "a" {
- return w.Write([]byte("Alice"))
- }
- return 0, expectErr
- })
- if err != expectErr {
- t.Fatalf("error must be the same as the error returned from f, expect: %s, actual: %s", expectErr, err)
- }
- if result != "" {
- t.Fatalf("result should be an empty string if error occurred")
- }
- result, err = ExecuteFuncStringWithErr(`{a} is {b}'s best friend`, "{", "}", func(w io.Writer, tag string) (int, error) {
- if tag == "a" {
- return w.Write([]byte("Alice"))
- }
- return w.Write([]byte("Bob"))
- })
- if err != nil {
- t.Fatalf("should success but found err: %s", err)
- }
- if result != "Alice is Bob's best friend" {
- t.Fatalf("expect: %s, but: %s", "Alice is Bob's best friend", result)
- }
- }
- func TestTpl_ExecuteFuncStringWithErr(t *testing.T) {
- var expectErr = errors.New("test111")
- tpl, err := NewTemplate(`{a} is {b}'s best friend`, "{", "}")
- if err != nil {
- t.Fatalf("should success, but found err: %s", err)
- }
- result, err := tpl.ExecuteFuncStringWithErr(func(w io.Writer, tag string) (int, error) {
- if tag == "a" {
- return w.Write([]byte("Alice"))
- }
- return 0, expectErr
- })
- if err != expectErr {
- t.Fatalf("error must be the same as the error returned from f, expect: %s, actual: %s", expectErr, err)
- }
- if result != "" {
- t.Fatalf("result should be an empty string if error occurred")
- }
- result, err = tpl.ExecuteFuncStringWithErr(func(w io.Writer, tag string) (int, error) {
- if tag == "a" {
- return w.Write([]byte("Alice"))
- }
- return w.Write([]byte("Bob"))
- })
- if err != nil {
- t.Fatalf("should success but found err: %s", err)
- }
- if result != "Alice is Bob's best friend" {
- t.Fatalf("expect: %s, but: %s", "Alice is Bob's best friend", result)
- }
- }
|