OSDN Git Service

libgo: update to weekly.2011-10-25
[pf3gnuchains/gcc-fork.git] / libgo / go / go / ast / print_test.go
1 // Copyright 2011 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.
4
5 package ast
6
7 import (
8         "bytes"
9         "strings"
10         "testing"
11 )
12
13 var tests = []struct {
14         x interface{} // x is printed as s
15         s string
16 }{
17         // basic types
18         {nil, "0  nil"},
19         {true, "0  true"},
20         {42, "0  42"},
21         {3.14, "0  3.14"},
22         {1 + 2.718i, "0  (1+2.718i)"},
23         {"foobar", "0  \"foobar\""},
24
25         // maps
26         {map[string]int{"a": 1},
27                 `0  map[string] int (len = 1) {
28                 1  .  "a": 1
29                 2  }`},
30
31         // pointers
32         {new(int), "0  *0"},
33
34         // slices
35         {[]int{1, 2, 3},
36                 `0  []int (len = 3) {
37                 1  .  0: 1
38                 2  .  1: 2
39                 3  .  2: 3
40                 4  }`},
41
42         // structs
43         {struct{ X, Y int }{42, 991},
44                 `0  struct { X int; Y int } {
45                 1  .  X: 42
46                 2  .  Y: 991
47                 3  }`},
48 }
49
50 // Split s into lines, trim whitespace from all lines, and return
51 // the concatenated non-empty lines.
52 func trim(s string) string {
53         lines := strings.Split(s, "\n")
54         i := 0
55         for _, line := range lines {
56                 line = strings.TrimSpace(line)
57                 if line != "" {
58                         lines[i] = line
59                         i++
60                 }
61         }
62         return strings.Join(lines[0:i], "\n")
63 }
64
65 func TestPrint(t *testing.T) {
66         var buf bytes.Buffer
67         for _, test := range tests {
68                 buf.Reset()
69                 if _, err := Fprint(&buf, nil, test.x, nil); err != nil {
70                         t.Errorf("Fprint failed: %s", err)
71                 }
72                 if s, ts := trim(buf.String()), trim(test.s); s != ts {
73                         t.Errorf("got:\n%s\nexpected:\n%s\n", s, ts)
74                 }
75         }
76 }