1
0
mirror of https://github.com/golang/go synced 2024-11-20 03:44:40 -07:00

printer_test: first cut

R=rsc
DELTA=169  (168 added, 0 deleted, 1 changed)
OCL=31319
CL=31321
This commit is contained in:
Robert Griesemer 2009-07-07 18:09:26 -07:00
parent 7c534e1bd2
commit d7f1f53e86
5 changed files with 170 additions and 1 deletions

View File

@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style # Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file. # license that can be found in the LICENSE file.
# DO NOT EDIT. Automatically generated by gobuild. # DO NOT EDIT. Automatically generated by gobuild.
# gobuild -m >Makefile # gobuild -m >Makefile
@ -20,7 +21,7 @@ test: packages
coverage: packages coverage: packages
gotest gotest
6cov -g `pwd` | grep -v '_test\.go:' 6cov -g $$(pwd) | grep -v '_test\.go:'
%.$O: %.go %.$O: %.go
$(GC) -I_obj $*.go $(GC) -I_obj $*.go

View File

@ -0,0 +1,25 @@
package main
import "fmt" // fmt
const c0 = 0 // zero
const (
c1 = iota; // c1
c2 // c2
)
type T struct {
a, b, c int // 3 fields
}
var x int // x
var ()
func f0() {
const pi = 3.14;
var s1 struct {}
var s2 struct {} = struct {}{};
x := pi
}

View File

@ -0,0 +1,3 @@
package main
type T struct

View File

@ -0,0 +1,26 @@
package main
import "fmt" // fmt
const c0 = 0; // zero
const (
c1 = iota; // c1
c2; // c2
)
type T struct {
a, b, c int // 3 fields
}
var x int; // x
var ()
func f0() {
const pi = 3.14; // pi
var s1 struct {}
var s2 struct {} = struct {}{};
x := pi;
}

View File

@ -0,0 +1,114 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package printer
import (
"bytes";
"io";
"go/ast";
"go/parser";
"go/printer";
"os";
"path";
"tabwriter";
"testing";
)
const (
tabwidth = 4;
padding = 1;
tabchar = ' ';
)
func lineString(text []byte, i int) string {
i0 := i;
for i < len(text) && text[i] != '\n' {
i++;
}
return string(text[i0 : i]);
}
func check(t *testing.T, source, golden string, exports bool) {
// get source
src, err := io.ReadFile(source);
if err != nil {
t.Error(err);
return;
}
// parse source
prog, err := parser.Parse(src, parser.ParseComments);
if err != nil {
t.Error(err);
return;
}
// filter exports if necessary
if exports {
ast.FilterExports(prog); // ignore result
}
// format source
var buf bytes.Buffer;
w := tabwriter.NewWriter(&buf, tabwidth, padding, tabchar, 0);
Fprint(w, prog, 0);
w.Flush();
res := buf.Data();
// get golden
gld, err := io.ReadFile(golden);
if err != nil {
t.Error(err);
return;
}
// compare lengths
if len(res) != len(gld) {
t.Errorf("len = %d, expected %d (= len(%s))", len(res), len(gld), golden);
}
// compare contents
for i, line, offs := 0, 1, 0; i < len(res) && i < len(gld); i++ {
ch := res[i];
if ch != gld[i] {
t.Errorf("%s:%d:%d: %s", source, line, i-offs+1, lineString(res, offs));
t.Errorf("%s:%d:%d: %s", golden, line, i-offs+1, lineString(gld, offs));
t.Error();
return;
}
if ch == '\n' {
line++;
offs = i+1;
}
}
}
const dataDir = "data";
type entry struct {
source, golden string;
exports bool;
}
// Use gofmt to create/update the respective golden files:
//
// gofmt source.go > golden.go
// gofmt -x source.go > golden.x
//
var data = []entry{
entry{ "source1.go", "golden1.go", false },
entry{ "source1.go", "golden1.x", true },
}
func Test(t *testing.T) {
for _, e := range data {
check(t, path.Join(dataDir, e.source), path.Join(dataDir, e.golden), e.exports);
}
}