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.
24 var update
= flag
.Bool("update", false, "update golden files")
27 func lineString(text
[]byte, i
int) string {
29 for i
< len(text
) && text
[i
] != '\n' {
32 return string(text
[i0
:i
])
39 export checkMode
= 1 << iota
44 func check(t
*testing
.T
, source
, golden
string, mode checkMode
) {
46 prog
, err
:= parser
.ParseFile(source
, nil, parser
.ParseComments
)
52 // filter exports if necessary
54 ast
.FileExports(prog
) // ignore result
55 prog
.Comments
= nil // don't print comments that are not in AST
58 // determine printer configuration
59 cfg
:= Config
{Tabwidth
: tabwidth
}
60 if mode
&rawFormat
!= 0 {
66 if _
, err
:= cfg
.Fprint(&buf
, prog
); err
!= nil {
71 // update golden files if necessary
73 if err
:= ioutil
.WriteFile(golden
, res
, 0644); err
!= nil {
80 gld
, err
:= ioutil
.ReadFile(golden
)
87 if len(res
) != len(gld
) {
88 t
.Errorf("len = %d, expected %d (= len(%s))", len(res
), len(gld
), golden
)
92 for i
, line
, offs
:= 0, 1, 0; i
< len(res
) && i
< len(gld
); i
++ {
95 t
.Errorf("%s:%d:%d: %s", source
, line
, i
-offs
+1, lineString(res
, offs
))
96 t
.Errorf("%s:%d:%d: %s", golden
, line
, i
-offs
+1, lineString(gld
, offs
))
109 source
, golden
string
113 // Use gotest -update to create/update the respective golden files.
115 {"empty.input", "empty.golden", 0},
116 {"comments.input", "comments.golden", 0},
117 {"comments.input", "comments.x", export
},
118 {"linebreaks.input", "linebreaks.golden", 0},
119 {"expressions.input", "expressions.golden", 0},
120 {"expressions.input", "expressions.raw", rawFormat
},
121 {"declarations.input", "declarations.golden", 0},
122 {"statements.input", "statements.golden", 0},
126 func Test(t
*testing
.T
) {
127 for _
, e
:= range data
{
128 source
:= path
.Join(dataDir
, e
.source
)
129 golden
:= path
.Join(dataDir
, e
.golden
)
130 check(t
, source
, golden
, e
.mode
)
131 // TODO(gri) check that golden is idempotent
132 //check(t, golden, golden, e.mode);