LEFT | RIGHT |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package format | 5 package format |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "go/parser" | 9 "go/parser" |
10 "go/token" | 10 "go/token" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 t.Fatal(err) | 42 t.Fatal(err) |
43 } | 43 } |
44 | 44 |
45 fset := token.NewFileSet() | 45 fset := token.NewFileSet() |
46 file, err := parser.ParseFile(fset, testfile, src, parser.ParseComments) | 46 file, err := parser.ParseFile(fset, testfile, src, parser.ParseComments) |
47 if err != nil { | 47 if err != nil { |
48 t.Fatal(err) | 48 t.Fatal(err) |
49 } | 49 } |
50 | 50 |
51 var buf bytes.Buffer | 51 var buf bytes.Buffer |
| 52 |
52 if err = Node(&buf, fset, file); err != nil { | 53 if err = Node(&buf, fset, file); err != nil { |
53 » » t.Fatal("AST failed:", err) | 54 » » t.Fatal("Node failed:", err) |
54 } | 55 } |
55 | 56 |
56 diff(t, buf.Bytes(), src) | 57 diff(t, buf.Bytes(), src) |
57 } | 58 } |
58 | 59 |
59 func TestSource(t *testing.T) { | 60 func TestSource(t *testing.T) { |
60 src, err := ioutil.ReadFile(testfile) | 61 src, err := ioutil.ReadFile(testfile) |
61 if err != nil { | 62 if err != nil { |
62 t.Fatal(err) | 63 t.Fatal(err) |
63 } | 64 } |
64 | 65 |
65 res, err := Source(src) | 66 res, err := Source(src) |
66 if err != nil { | 67 if err != nil { |
67 » » t.Fatal("Bytes failed:", err) | 68 » » t.Fatal("Source failed:", err) |
68 } | 69 } |
69 | 70 |
70 diff(t, res, src) | 71 diff(t, res, src) |
71 } | 72 } |
72 | 73 |
73 // Test cases that are expected to fail are marked by the prefix "ERROR". | 74 // Test cases that are expected to fail are marked by the prefix "ERROR". |
74 var tests = []string{ | 75 var tests = []string{ |
75 // declaration lists | 76 // declaration lists |
76 `import "go/format"`, | 77 `import "go/format"`, |
77 "var x int", | 78 "var x int", |
78 "var x int\n\ntype T struct{}", | 79 "var x int\n\ntype T struct{}", |
79 | 80 |
80 // statement lists | 81 // statement lists |
81 "x := 0", | 82 "x := 0", |
82 "f(a, b, c)\nvar x int = f(1, 2, 3)", | 83 "f(a, b, c)\nvar x int = f(1, 2, 3)", |
83 | 84 |
84 // indentation, leading and trailing space | 85 // indentation, leading and trailing space |
85 "\tx := 0\n\tgo f()", | 86 "\tx := 0\n\tgo f()", |
86 "\tx := 0\n\tgo f()\n\n\n", | 87 "\tx := 0\n\tgo f()\n\n\n", |
87 "\n\t\t\n\n\tx := 0\n\tgo f()\n\n\n", | 88 "\n\t\t\n\n\tx := 0\n\tgo f()\n\n\n", |
88 "\n\t\t\n\n\t\t\tx := 0\n\t\t\tgo f()\n\n\n", | 89 "\n\t\t\n\n\t\t\tx := 0\n\t\t\tgo f()\n\n\n", |
| 90 "\n\t\t\n\n\t\t\tx := 0\n\t\t\tconst s = `\nfoo\n`\n\n\n", // no indenta
tion inside raw strings |
89 | 91 |
90 // erroneous programs | 92 // erroneous programs |
91 "ERRORvar x", | 93 "ERRORvar x", |
92 "ERROR1 + 2 +", | 94 "ERROR1 + 2 +", |
93 "ERRORx := 0", | 95 "ERRORx := 0", |
94 } | 96 } |
95 | 97 |
96 func String(s string) (string, error) { | 98 func String(s string) (string, error) { |
97 res, err := Source([]byte(s)) | 99 res, err := Source([]byte(s)) |
98 if err != nil { | 100 if err != nil { |
(...skipping 15 matching lines...) Expand all Loading... |
114 // test expected to succeed | 116 // test expected to succeed |
115 res, err := String(src) | 117 res, err := String(src) |
116 if err != nil { | 118 if err != nil { |
117 t.Errorf("formatting failed (%s):\n%q", err, src
) | 119 t.Errorf("formatting failed (%s):\n%q", err, src
) |
118 } else if res != src { | 120 } else if res != src { |
119 t.Errorf("formatting incorrect:\nsource: %q\nres
ult: %q", src, res) | 121 t.Errorf("formatting incorrect:\nsource: %q\nres
ult: %q", src, res) |
120 } | 122 } |
121 } | 123 } |
122 } | 124 } |
123 } | 125 } |
LEFT | RIGHT |