1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 var fset = token.NewFileSet()
15 func parse(t *testing.T, form string, fmap FormatterMap) Format {
16 f, err := Parse(fset, "", []byte(form), fmap)
18 t.Errorf("Parse(%s): %v", form, err)
24 func verify(t *testing.T, f Format, expected string, args ...interface{}) {
26 return // allow other tests to run
28 result := f.Sprint(args...)
29 if result != expected {
31 "result : `%s`\nexpected: `%s`\n\n",
36 func formatter(s *State, value interface{}, rule_name string) bool {
39 fmt.Fprintf(s, "%d %d %d", s.Pos().Line, s.LinePos().Column, s.Pos().Column)
45 if value.(int)&1 == 0 {
46 fmt.Fprint(s, "even ")
54 s.Write([]byte("testing.T"))
61 func TestCustomFormatters(t *testing.T) {
62 fmap0 := FormatterMap{"/": formatter}
63 fmap1 := FormatterMap{"int": formatter, "blank": formatter, "nil": formatter}
64 fmap2 := FormatterMap{"testing.T": formatter}
66 f := parse(t, `int=`, fmap0)
67 verify(t, f, ``, 1, 2, 3)
69 f = parse(t, `int="#"`, nil)
70 verify(t, f, `###`, 1, 2, 3)
72 f = parse(t, `int="#";string="%s"`, fmap0)
73 verify(t, f, "#1 0 1#1 0 7#1 0 13\n2 0 0foo2 0 8\n", 1, 2, 3, "\n", "foo", "\n")
75 f = parse(t, ``, fmap1)
76 verify(t, f, `even odd even odd `, 0, 1, 2, 3)
78 f = parse(t, `/ =@:blank; float64="#"`, fmap1)
79 verify(t, f, `# # #`, 0.0, 1.0, 2.0)
81 f = parse(t, `float64=@:nil`, fmap1)
82 verify(t, f, ``, 0.0, 1.0, 2.0)
84 f = parse(t, `testing "testing"; ptr=*`, fmap2)
85 verify(t, f, `testing.T`, t)
87 // TODO needs more tests
90 // ----------------------------------------------------------------------------
91 // Formatting of basic and simple composite types
93 func check(t *testing.T, form, expected string, args ...interface{}) {
94 f := parse(t, form, nil)
96 return // allow other tests to run
98 result := f.Sprint(args...)
99 if result != expected {
101 "format : %s\nresult : `%s`\nexpected: `%s`\n\n",
102 form, result, expected)
106 func TestBasicTypes(t *testing.T) {
108 check(t, `bool=":%v"`, `:true:false`, true, false)
109 check(t, `int="%b %d %o 0x%x"`, `101010 42 52 0x2a`, 42)
111 check(t, `int="%"`, `%`, 42)
112 check(t, `int="%%"`, `%`, 42)
113 check(t, `int="**%%**"`, `**%**`, 42)
114 check(t, `int="%%%%%%"`, `%%%`, 42)
115 check(t, `int="%%%d%%"`, `%42%`, 42)
119 check(t, `int ="%d"`, is, i)
120 check(t, `int8 ="%d"`, is, int8(i))
121 check(t, `int16="%d"`, is, int16(i))
122 check(t, `int32="%d"`, is, int32(i))
123 check(t, `int64="%d"`, is, int64(i))
127 check(t, `uint ="%d"`, us, uint(u))
128 check(t, `uint8 ="%d"`, us, uint8(u))
129 check(t, `uint16="%d"`, us, uint16(u))
130 check(t, `uint32="%d"`, us, uint32(u))
131 check(t, `uint64="%d"`, us, uint64(u))
134 const fs = `3.141592`
135 check(t, `float64="%g"`, fs, f)
136 check(t, `float32="%g"`, fs, float32(f))
137 check(t, `float64="%g"`, fs, float64(f))
140 func TestArrayTypes(t *testing.T) {
142 check(t, `array="array";`, `array`, a0)
144 a1 := [...]int{1, 2, 3}
145 check(t, `array="array";`, `array`, a1)
146 check(t, `array={*}; int="%d";`, `123`, a1)
147 check(t, `array={* / ", "}; int="%d";`, `1, 2, 3`, a1)
148 check(t, `array={* / *}; int="%d";`, `12233`, a1)
150 a2 := []interface{}{42, "foo", 3.14}
151 check(t, `array={* / ", "}; interface=*; string="bar"; default="%v";`, `42, bar, 3.14`, a2)
154 func TestChanTypes(t *testing.T) {
156 check(t, `chan="chan"`, `chan`, c0)
159 go func() { c1 <- 42 }()
160 check(t, `chan="chan"`, `chan`, c1)
161 // check(t, `chan=*`, `42`, c1); // reflection support for chans incomplete
164 func TestFuncTypes(t *testing.T) {
166 check(t, `func="func"`, `func`, f0)
168 f1 := func() int { return 42 }
169 check(t, `func="func"`, `func`, f1)
170 // check(t, `func=*`, `42`, f1); // reflection support for funcs incomplete
173 func TestMapTypes(t *testing.T) {
174 var m0 map[string]int
175 check(t, `map="map"`, `map`, m0)
177 m1 := map[string]int{}
178 check(t, `map="map"`, `map`, m1)
179 // check(t, `map=*`, ``, m1); // reflection support for maps incomplete
182 func TestPointerTypes(t *testing.T) {
184 check(t, `ptr="ptr"`, `ptr`, p0)
185 check(t, `ptr=*`, ``, p0)
186 check(t, `ptr=*|"nil"`, `nil`, p0)
190 check(t, `ptr="ptr"`, `ptr`, p1)
191 check(t, `ptr=*; int="%d"`, `99991`, p1)
194 func TestDefaultRule(t *testing.T) {
195 check(t, `default="%v"`, `42foo3.14`, 42, "foo", 3.14)
196 check(t, `default="%v"; int="%x"`, `abcdef`, 10, 11, 12, 13, 14, 15)
197 check(t, `default="%v"; int="%x"`, `ab**ef`, 10, 11, "**", 14, 15)
198 check(t, `default="%x"; int=@:default`, `abcdef`, 10, 11, 12, 13, 14, 15)
201 func TestGlobalSeparatorRule(t *testing.T) {
202 check(t, `int="%d"; / ="-"`, `1-2-3-4`, 1, 2, 3, 4)
203 check(t, `int="%x%x"; / ="*"`, `aa*aa`, 10, 10)
206 // ----------------------------------------------------------------------------
207 // Formatting of a struct
213 const F1 = `datafmt "datafmt";` +
215 `datafmt.T1 = "<" a ">";`
217 func TestStruct1(t *testing.T) { check(t, F1, "<42>", T1{42}) }
219 // ----------------------------------------------------------------------------
220 // Formatting of a struct with an optional field (ptr)
230 `datafmt.T2 = s ["-" p "-"];`
235 `datafmt.T2 = s ("-" p "-" | "empty");`
237 func TestStruct2(t *testing.T) {
238 check(t, F2a, "foo", T2{"foo", nil})
239 check(t, F2a, "bar-<17>-", T2{"bar", &T1{17}})
240 check(t, F2b, "fooempty", T2{"foo", nil})
243 // ----------------------------------------------------------------------------
244 // Formatting of a struct with a repetitive field (slice)
251 const F3a = `datafmt "datafmt";` +
254 `datafmt.T3 = s {" " a a / ","};`
256 const F3b = `datafmt "datafmt";` +
262 `datafmt.T3 = s [a:empty ": " {a / "-"}]`
264 func TestStruct3(t *testing.T) {
265 check(t, F3a, "foo", T3{"foo", nil})
266 check(t, F3a, "foo 00, 11, 22", T3{"foo", []int{0, 1, 2}})
267 check(t, F3b, "bar", T3{"bar", nil})
268 check(t, F3b, "bal: 2-3-5", T3{"bal", []int{2, 3, 5}})
271 // ----------------------------------------------------------------------------
272 // Formatting of a struct with alternative field
279 const F4a = `datafmt "datafmt";` +
285 `datafmt.T4 = "<" (x:empty x | "-") ">" `
287 const F4b = `datafmt "datafmt";` +
293 `datafmt.T4 = "<" (a:empty {a / ", "} | "-") ">" `
295 func TestStruct4(t *testing.T) {
297 check(t, F4a, "<->", T4{nil, nil})
298 check(t, F4a, "<7>", T4{&x, nil})
299 check(t, F4b, "<->", T4{nil, nil})
300 check(t, F4b, "<2, 3, 7>", T4{nil, []int{2, 3, 7}})
303 // ----------------------------------------------------------------------------
304 // Formatting a struct (documentation example)
311 const FPoint = `datafmt "datafmt";` +
314 `string = "---%s---";` +
315 `datafmt.Point = name "{" x ", " y:hexInt "}";`
317 func TestStructPoint(t *testing.T) {
318 p := Point{"foo", 3, 15}
319 check(t, FPoint, "---foo---{3, 0xf}", p)
322 // ----------------------------------------------------------------------------
323 // Formatting a slice (documentation example)
325 const FSlice = `int = "%b";` +
326 `array = { * / ", " }`
328 func TestSlice(t *testing.T) { check(t, FSlice, "10, 11, 101, 111", []int{2, 3, 5, 7}) }
330 // TODO add more tests