github.com/goplus/gop@v1.2.6/printer/bugfix_test.go (about) 1 package printer_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 goast "go/ast" 8 goprinter "go/printer" 9 gotoken "go/token" 10 11 "github.com/goplus/gop/ast" 12 "github.com/goplus/gop/printer" 13 "github.com/goplus/gop/token" 14 ) 15 16 func goIdents(name string) []*goast.Ident { 17 return []*goast.Ident{{Name: name}} 18 } 19 20 func TestGoFormat(t *testing.T) { 21 const ( 22 mode = goprinter.UseSpaces | goprinter.TabIndent 23 ) 24 config := goprinter.Config{Mode: mode, Indent: 0, Tabwidth: 8} 25 decl := &goast.GenDecl{Tok: gotoken.VAR, Lparen: 1, Rparen: 1} 26 decl.Specs = []goast.Spec{ 27 &goast.ValueSpec{Names: goIdents("foo"), Type: goast.NewIdent("int")}, 28 &goast.ValueSpec{Names: goIdents("bar"), Type: goast.NewIdent("string")}, 29 } 30 b := bytes.NewBuffer(nil) 31 fset := gotoken.NewFileSet() 32 config.Fprint(b, fset, decl) 33 const codeExp = `var ( 34 foo int 35 bar string 36 )` 37 if code := b.String(); code != codeExp { 38 t.Fatal("config.Fprint:", code, codeExp) 39 } 40 } 41 42 func gopIdents(name string) []*ast.Ident { 43 return []*ast.Ident{{Name: name}} 44 } 45 46 func TestGopFormat(t *testing.T) { 47 const ( 48 mode = printer.UseSpaces | printer.TabIndent 49 ) 50 config := printer.Config{Mode: mode, Indent: 0, Tabwidth: 8} 51 decl := &ast.GenDecl{Tok: token.VAR, Lparen: 1, Rparen: 1} 52 decl.Specs = []ast.Spec{ 53 &ast.ValueSpec{Names: gopIdents("foo"), Type: ast.NewIdent("int")}, 54 &ast.ValueSpec{Names: gopIdents("bar"), Type: ast.NewIdent("string")}, 55 } 56 b := bytes.NewBuffer(nil) 57 fset := token.NewFileSet() 58 config.Fprint(b, fset, decl) 59 const codeExp = `var ( 60 foo int 61 bar string 62 )` 63 if code := b.String(); code != codeExp { 64 t.Fatal("config.Fprint:", code, codeExp) 65 } 66 }