github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/go/format/format_test.go (about) 1 // Copyright 2012 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 format 6 7 import ( 8 "bytes" 9 "go/parser" 10 "go/token" 11 "io/ioutil" 12 "strings" 13 "testing" 14 ) 15 16 const testfile = "format_test.go" 17 18 func diff(t *testing.T, dst, src []byte) { 19 line := 1 20 offs := 0 // line offset 21 for i := 0; i < len(dst) && i < len(src); i++ { 22 d := dst[i] 23 s := src[i] 24 if d != s { 25 t.Errorf("dst:%d: %s\n", line, dst[offs:i+1]) 26 t.Errorf("src:%d: %s\n", line, src[offs:i+1]) 27 return 28 } 29 if s == '\n' { 30 line++ 31 offs = i + 1 32 } 33 } 34 if len(dst) != len(src) { 35 t.Errorf("len(dst) = %d, len(src) = %d\nsrc = %q", len(dst), len(src), src) 36 } 37 } 38 39 func TestNode(t *testing.T) { 40 src, err := ioutil.ReadFile(testfile) 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 fset := token.NewFileSet() 46 file, err := parser.ParseFile(fset, testfile, src, parser.ParseComments) 47 if err != nil { 48 t.Fatal(err) 49 } 50 51 var buf bytes.Buffer 52 53 if err = Node(&buf, fset, file); err != nil { 54 t.Fatal("Node failed:", err) 55 } 56 57 diff(t, buf.Bytes(), src) 58 } 59 60 func TestSource(t *testing.T) { 61 src, err := ioutil.ReadFile(testfile) 62 if err != nil { 63 t.Fatal(err) 64 } 65 66 res, err := Source(src) 67 if err != nil { 68 t.Fatal("Source failed:", err) 69 } 70 71 diff(t, res, src) 72 } 73 74 // Test cases that are expected to fail are marked by the prefix "ERROR". 75 // The formatted result must look the same as the input for successful tests. 76 var tests = []string{ 77 // declaration lists 78 `import "go/format"`, 79 "var x int", 80 "var x int\n\ntype T struct{}", 81 82 // statement lists 83 "x := 0", 84 "f(a, b, c)\nvar x int = f(1, 2, 3)", 85 86 // indentation, leading and trailing space 87 "\tx := 0\n\tgo f()", 88 "\tx := 0\n\tgo f()\n\n\n", 89 "\n\t\t\n\n\tx := 0\n\tgo f()\n\n\n", 90 "\n\t\t\n\n\t\t\tx := 0\n\t\t\tgo f()\n\n\n", 91 "\n\t\t\n\n\t\t\tx := 0\n\t\t\tconst s = `\nfoo\n`\n\n\n", // no indentation added inside raw strings 92 "\n\t\t\n\n\t\t\tx := 0\n\t\t\tconst s = `\n\t\tfoo\n`\n\n\n", // no indentation removed inside raw strings 93 94 // comments 95 "/* Comment */", 96 "\t/* Comment */ ", 97 "\n/* Comment */ ", 98 "i := 5 /* Comment */", // issue #5551 99 "\ta()\n//line :1", // issue #11276 100 "\t//xxx\n\ta()\n//line :2", // issue #11276 101 "\ta() //line :1\n\tb()\n", // issue #11276 102 "x := 0\n//line :1\n//line :2", // issue #11276 103 104 // whitespace 105 "", // issue #11275 106 " ", // issue #11275 107 "\t", // issue #11275 108 "\t\t", // issue #11275 109 "\n", // issue #11275 110 "\n\n", // issue #11275 111 "\t\n", // issue #11275 112 113 // erroneous programs 114 "ERROR1 + 2 +", 115 "ERRORx := 0", 116 } 117 118 func String(s string) (string, error) { 119 res, err := Source([]byte(s)) 120 if err != nil { 121 return "", err 122 } 123 return string(res), nil 124 } 125 126 func TestPartial(t *testing.T) { 127 for _, src := range tests { 128 if strings.HasPrefix(src, "ERROR") { 129 // test expected to fail 130 src = src[5:] // remove ERROR prefix 131 res, err := String(src) 132 if err == nil && res == src { 133 t.Errorf("formatting succeeded but was expected to fail:\n%q", src) 134 } 135 } else { 136 // test expected to succeed 137 res, err := String(src) 138 if err != nil { 139 t.Errorf("formatting failed (%s):\n%q", err, src) 140 } else if res != src { 141 t.Errorf("formatting incorrect:\nsource: %q\nresult: %q", src, res) 142 } 143 } 144 } 145 }