summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/nicksnyder/go-i18n/i18n/translation/template_test.go
blob: 73a9234048355437fa36cc87e97db785697c6774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package translation

import (
	"bytes"
	"fmt"
	//"launchpad.net/goyaml"
	"testing"
	gotemplate "text/template"
)

func TestNilTemplate(t *testing.T) {
	expected := "hello"
	tmpl := &template{
		tmpl: nil,
		src:  expected,
	}
	if actual := tmpl.Execute(nil); actual != expected {
		t.Errorf("Execute(nil) returned %s; expected %s", actual, expected)
	}
}

func TestMarshalText(t *testing.T) {
	tmpl := &template{
		tmpl: gotemplate.Must(gotemplate.New("id").Parse("this is a {{.foo}} template")),
		src:  "boom",
	}
	expectedBuf := []byte(tmpl.src)
	if buf, err := tmpl.MarshalText(); !bytes.Equal(buf, expectedBuf) || err != nil {
		t.Errorf("MarshalText() returned %#v, %#v; expected %#v, nil", buf, err, expectedBuf)
	}
}

func TestUnmarshalText(t *testing.T) {
	tmpl := &template{}
	tmpl.UnmarshalText([]byte("hello {{.World}}"))
	result := tmpl.Execute(map[string]string{
		"World": "world!",
	})
	expected := "hello world!"
	if result != expected {
		t.Errorf("expected %#v; got %#v", expected, result)
	}
}

/*
func TestYAMLMarshal(t *testing.T) {
	src := "hello {{.World}}"
	tmpl, err := newTemplate(src)
	if err != nil {
		t.Fatal(err)
	}
	buf, err := goyaml.Marshal(tmpl)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(buf, []byte(src)) {
		t.Fatalf(`expected "%s"; got "%s"`, src, buf)
	}
}

func TestYAMLUnmarshal(t *testing.T) {
	buf := []byte(`Tmpl: "hello"`)

	var out struct {
		Tmpl *template
	}
	var foo map[string]string
	if err := goyaml.Unmarshal(buf, &foo); err != nil {
		t.Fatal(err)
	}
	if out.Tmpl == nil {
		t.Fatalf("out.Tmpl was nil")
	}
	if out.Tmpl.tmpl == nil {
		t.Fatalf("out.Tmpl.tmpl was nil")
	}
	if expected := "hello {{.World}}"; out.Tmpl.src != expected {
		t.Fatalf("expected %s; got %s", expected, out.Tmpl.src)
	}
}

func TestGetYAML(t *testing.T) {
	src := "hello"
	tmpl := &template{
		tmpl: nil,
		src:  src,
	}
	if tag, value := tmpl.GetYAML(); tag != "" || value != src {
		t.Errorf("GetYAML() returned (%#v, %#v); expected (%#v, %#v)", tag, value, "", src)
	}
}

func TestSetYAML(t *testing.T) {
	tmpl := &template{}
	tmpl.SetYAML("tagDoesntMatter", "hello {{.World}}")
	result := tmpl.Execute(map[string]string{
		"World": "world!",
	})
	expected := "hello world!"
	if result != expected {
		t.Errorf("expected %#v; got %#v", expected, result)
	}
}
*/

func BenchmarkExecuteNilTemplate(b *testing.B) {
	template := &template{src: "hello world"}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		template.Execute(nil)
	}
}

func BenchmarkExecuteHelloWorldTemplate(b *testing.B) {
	template, err := newTemplate("hello world")
	if err != nil {
		b.Fatal(err)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		template.Execute(nil)
	}
}

// Executing a simple template like this is ~6x slower than Sprintf
// but it is still only a few microseconds which should be sufficiently fast.
// The benefit is that we have nice semantic tags in the translation.
func BenchmarkExecuteHelloNameTemplate(b *testing.B) {
	template, err := newTemplate("hello {{.Name}}")
	if err != nil {
		b.Fatal(err)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		template.Execute(map[string]string{
			"Name": "Nick",
		})
	}
}

func BenchmarkSprintf(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		fmt.Sprintf("hello %s", "nick")
	}
}