github.com/sandwich-go/boost@v1.3.29/misc/goformat/goformat_test.go (about) 1 package goformat 2 3 import ( 4 . "github.com/smartystreets/goconvey/convey" 5 "go/token" 6 "testing" 7 ) 8 9 func TestParseMainFragment(t *testing.T) { 10 Convey("parse main fragment", t, func() { 11 for _, test := range []struct { 12 src string 13 adjust, err bool 14 }{ 15 { 16 src: ` 17 func a() error {return nil} 18 `, adjust: true, 19 }, 20 { 21 src: ` 22 func main() error {return nil} 23 `, adjust: true, 24 }, 25 { 26 src: ` 27 func main() {} 28 `, 29 }, 30 { 31 src: ` 32 err := db.Connect() 33 `, err: true, 34 }, 35 } { 36 _, adjust, err := parseMainFragment(token.NewFileSet(), "", []byte(test.src), 0) 37 if test.err { 38 So(err, ShouldNotBeNil) 39 } else { 40 So(err, ShouldBeNil) 41 } 42 if test.adjust { 43 So(adjust, ShouldNotBeNil) 44 } else { 45 So(adjust, ShouldBeNil) 46 } 47 } 48 }) 49 } 50 51 func TestParseDeclarationFragment(t *testing.T) { 52 Convey("parse declaration fragment", t, func() { 53 for _, test := range []struct { 54 src string 55 adjust, err bool 56 }{ 57 { 58 src: ` 59 func a() error {return nil} 60 `, err: true, 61 }, 62 { 63 src: ` 64 func main() {} 65 `, err: true, 66 }, 67 { 68 src: ` 69 err := db.Connect() 70 `, adjust: true, 71 }, 72 } { 73 _, adjust, err := parseDeclarationFragment(token.NewFileSet(), "", []byte(test.src), 0) 74 if test.err { 75 So(err, ShouldNotBeNil) 76 } else { 77 So(err, ShouldBeNil) 78 } 79 if test.adjust { 80 So(adjust, ShouldNotBeNil) 81 } else { 82 So(adjust, ShouldBeNil) 83 } 84 } 85 }) 86 } 87 88 func TestParse(t *testing.T) { 89 Convey("parse", t, func() { 90 for _, test := range []struct { 91 src string 92 fragment bool 93 adjust, err bool 94 }{ 95 { 96 src: ` 97 package main 98 99 func main() {} 100 `, 101 }, 102 { 103 src: ` 104 package a 105 106 func b() error { return nil } 107 `, 108 }, 109 { 110 src: ` 111 func b() error { return nil } 112 `, err: true, 113 }, 114 { 115 src: ` 116 func a() error {return nil} 117 `, fragment: true, adjust: true, 118 }, 119 { 120 src: ` 121 func main() error {return nil} 122 `, fragment: true, adjust: true, 123 }, 124 { 125 src: ` 126 func main() {} 127 `, fragment: true, 128 }, 129 { 130 src: ` 131 err := db.Connect() 132 `, fragment: true, adjust: true, 133 }, 134 } { 135 _, adjust, err := parse(token.NewFileSet(), "", []byte(test.src), NewOptions(WithFragment(test.fragment))) 136 if test.err { 137 So(err, ShouldNotBeNil) 138 } else { 139 So(err, ShouldBeNil) 140 } 141 if test.adjust { 142 So(adjust, ShouldNotBeNil) 143 } else { 144 So(adjust, ShouldBeNil) 145 } 146 } 147 }) 148 } 149 150 func TestProcess(t *testing.T) { 151 Convey("process", t, func() { 152 for _, test := range []struct { 153 fileName string 154 code string 155 err bool 156 expected string 157 fragment bool 158 }{ 159 { 160 fileName: "testgo", 161 expected: `package test_file 162 163 func a() { return } 164 `, 165 }, 166 { 167 code: "func a( ) {return}", 168 fragment: true, 169 expected: `func a() { return }`, 170 }, 171 { 172 code: "func a( ) {return}", 173 err: true, 174 }, 175 } { 176 var err error 177 var out []byte 178 if len(test.fileName) > 0 { 179 out, err = ProcessFile(test.fileName, WithFragment(test.fragment)) 180 } else { 181 out, err = ProcessCode([]byte(test.code), WithFragment(test.fragment)) 182 } 183 if test.err { 184 So(err, ShouldNotBeNil) 185 } else { 186 So(err, ShouldBeNil) 187 So(out, ShouldResemble, []byte(test.expected)) 188 } 189 } 190 }) 191 }