github.com/neugram/ng@v0.0.0-20180309130942-d472ff93d872/format/format_test.go (about) 1 package format_test 2 3 import ( 4 "testing" 5 6 "neugram.io/ng/format" 7 "neugram.io/ng/parser" 8 "neugram.io/ng/syntax/stmt" 9 ) 10 11 var roundTripExprs = []string{ 12 "$$ sleep 1 && X=V Y=U env | grep X= & echo first || false; echo last $$", 13 "$$ (echo a && echo b); echo c $$", 14 `$$ 15 echo one 16 echo two 17 $$`, 18 19 // TODO: spacing around return statement 20 "func(x int, y bool) ([]byte, error) {return nil, nil}", 21 "func() error {return nil}", 22 "func() (err error) {return nil}", 23 "func(x int, y bool) (b []byte, err error) {return nil, nil}", 24 25 "x[:y]", 26 "x[y:z:t]", 27 "new(int)", 28 } 29 30 var roundTripStmts = []string{ 31 "import ()", 32 `import stdpath "path"`, 33 `import ( 34 stdpath "path" 35 "path/filepath" 36 )`, 37 "type Ints []int", 38 39 `methodik foo struct { 40 S string 41 } {}`, 42 43 `methodik foo struct { 44 I int 45 } { 46 func (f) F() int {return f.I} 47 }`, 48 } 49 50 func TestRoundTrip(t *testing.T) { 51 var srcs []string 52 for _, src := range roundTripExprs { 53 srcs = append(srcs, "("+src+")") 54 } 55 srcs = append(srcs, roundTripStmts...) 56 57 for _, src := range srcs { 58 s, err := parser.ParseStmt([]byte(src)) 59 if err != nil { 60 t.Errorf("ParseStmt(%q): error: %v", src, err) 61 continue 62 } 63 if s == nil { 64 t.Errorf("ParseStmt(%q): nil stmt", src) 65 continue 66 } 67 got := format.Stmt(s) 68 if got != src { 69 t.Errorf("bad ouput: Expr(%q)=%q", src, got) 70 } 71 } 72 } 73 74 var typeTests = []string{ 75 `string`, 76 `uintptr`, 77 `[]interface{}`, 78 `map[int64]map[string]int`, 79 `struct { 80 Field0 int 81 Field1More <-chan struct{} 82 Field2 []byte 83 Filed3 struct { 84 Inner1 int 85 Inner2More interface{} 86 } 87 }`, 88 `func()`, 89 `***int`, 90 `func(func(int) bool, func() (int, error)) func() (bool, error)`, 91 `interface { 92 M0(int, int) (int, int) 93 M1(struct{}) 94 M2(*int) error 95 }`, 96 `struct{}`, 97 } 98 99 func TestTypes(t *testing.T) { 100 for _, src := range typeTests { 101 s, err := parser.ParseStmt([]byte("type x " + src)) 102 if err != nil { 103 t.Errorf("ParseStmt(%q): error: %v", src, err) 104 continue 105 } 106 if s == nil { 107 t.Errorf("ParseStmt(%q): nil stmt", src) 108 continue 109 } 110 typ := s.(*stmt.TypeDecl).Type.Type 111 got := format.Type(typ) 112 if got != src { 113 t.Errorf("bad ouput: Type(%q)=%q", src, got) 114 } 115 } 116 }