github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/blueprint/parser/printer_test.go (about) 1 // Copyright 2014 Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package parser 16 17 import ( 18 "bytes" 19 "testing" 20 ) 21 22 var validPrinterTestCases = []struct { 23 input string 24 output string 25 }{ 26 { 27 input: ` 28 foo {} 29 `, 30 output: ` 31 foo {} 32 `, 33 }, 34 { 35 input: ` 36 foo{name= "abc",num= 4,} 37 `, 38 output: ` 39 foo { 40 name: "abc", 41 num: 4, 42 } 43 `, 44 }, 45 { 46 input: ` 47 foo { 48 stuff: ["asdf", "jkl;", "qwert", 49 "uiop", "bnm,"] 50 } 51 `, 52 output: ` 53 foo { 54 stuff: [ 55 "asdf", 56 "bnm,", 57 "jkl;", 58 "qwert", 59 "uiop", 60 ], 61 } 62 `, 63 }, 64 { 65 input: ` 66 var = "asdf" 67 foo { 68 stuff: ["asdf"] + var, 69 }`, 70 output: ` 71 var = "asdf" 72 foo { 73 stuff: ["asdf"] + var, 74 } 75 `, 76 }, 77 { 78 input: ` 79 var = "asdf" 80 foo { 81 stuff: [ 82 "asdf" 83 ] + var, 84 }`, 85 output: ` 86 var = "asdf" 87 foo { 88 stuff: [ 89 "asdf", 90 ] + var, 91 } 92 `, 93 }, 94 { 95 input: ` 96 var = "asdf" 97 foo { 98 stuff: ["asdf"] + var + ["qwert"], 99 }`, 100 output: ` 101 var = "asdf" 102 foo { 103 stuff: ["asdf"] + var + ["qwert"], 104 } 105 `, 106 }, 107 { 108 input: ` 109 foo { 110 stuff: { 111 isGood: true, 112 name: "bar", 113 num: 4, 114 } 115 } 116 `, 117 output: ` 118 foo { 119 stuff: { 120 isGood: true, 121 name: "bar", 122 num: 4, 123 }, 124 } 125 `, 126 }, 127 { 128 input: ` 129 // comment1 130 foo { 131 // comment2 132 isGood: true, // comment3 133 } 134 `, 135 output: ` 136 // comment1 137 foo { 138 // comment2 139 isGood: true, // comment3 140 } 141 `, 142 }, 143 { 144 input: ` 145 foo { 146 name: "abc", 147 num: 4, 148 } 149 150 bar { 151 name: "def", 152 num: 5, 153 } 154 `, 155 output: ` 156 foo { 157 name: "abc", 158 num: 4, 159 } 160 161 bar { 162 name: "def", 163 num: 5, 164 } 165 `, 166 }, 167 { 168 input: ` 169 foo = "stuff" 170 bar = foo 171 baz = foo + bar 172 baz += foo 173 `, 174 output: ` 175 foo = "stuff" 176 bar = foo 177 baz = foo + bar 178 baz += foo 179 `, 180 }, 181 { 182 input: ` 183 foo = 100 184 bar = foo 185 baz = foo + bar 186 baz += foo 187 `, 188 output: ` 189 foo = 100 190 bar = foo 191 baz = foo + bar 192 baz += foo 193 `, 194 }, 195 { 196 input: ` 197 //test 198 test /* test */ { 199 srcs: [ 200 /*"bootstrap/bootstrap.go", 201 "bootstrap/cleanup.go",*/ 202 "bootstrap/command.go", 203 "bootstrap/doc.go", //doc.go 204 "bootstrap/config.go", //config.go 205 ], 206 deps: ["libabc"], 207 incs: [] 208 } //test 209 //test 210 test2 { 211 } 212 213 214 //test3 215 `, 216 output: ` 217 //test 218 test /* test */ { 219 srcs: [ 220 /*"bootstrap/bootstrap.go", 221 "bootstrap/cleanup.go",*/ 222 "bootstrap/command.go", 223 "bootstrap/config.go", //config.go 224 "bootstrap/doc.go", //doc.go 225 ], 226 deps: ["libabc"], 227 incs: [], 228 } //test 229 //test 230 231 test2 { 232 } 233 234 //test3 235 `, 236 }, 237 { 238 input: ` 239 // test 240 module // test 241 242 { 243 srcs 244 : [ 245 "src1.c", 246 "src2.c", 247 ], 248 //test 249 } 250 //test2 251 `, 252 output: ` 253 // test 254 module { // test 255 256 srcs: [ 257 "src1.c", 258 "src2.c", 259 ], 260 //test 261 } 262 263 //test2 264 `, 265 }, 266 { 267 input: ` 268 /*test { 269 test: true, 270 }*/ 271 272 test { 273 /*test: true,*/ 274 } 275 276 // This 277 /* Is *//* A */ // A 278 // A 279 280 // Multiline 281 // Comment 282 283 test {} 284 285 // This 286 /* Is */ 287 // A 288 // Trailing 289 290 // Multiline 291 // Comment 292 `, 293 output: ` 294 /*test { 295 test: true, 296 }*/ 297 298 test { 299 /*test: true,*/ 300 } 301 302 // This 303 /* Is */ /* A */ // A 304 // A 305 306 // Multiline 307 // Comment 308 309 test {} 310 311 // This 312 /* Is */ 313 // A 314 // Trailing 315 316 // Multiline 317 // Comment 318 `, 319 }, 320 { 321 input: ` 322 test // test 323 324 // test 325 { 326 } 327 `, 328 output: ` 329 test { // test 330 331 // test 332 333 } 334 `, 335 }, 336 } 337 338 func TestPrinter(t *testing.T) { 339 for _, testCase := range validPrinterTestCases { 340 in := testCase.input[1:] 341 expected := testCase.output[1:] 342 343 r := bytes.NewBufferString(in) 344 file, errs := Parse("", r, NewScope(nil)) 345 if len(errs) != 0 { 346 t.Errorf("test case: %s", in) 347 t.Errorf("unexpected errors:") 348 for _, err := range errs { 349 t.Errorf(" %s", err) 350 } 351 t.FailNow() 352 } 353 354 SortLists(file) 355 356 got, err := Print(file) 357 if err != nil { 358 t.Errorf("test case: %s", in) 359 t.Errorf("unexpected error: %s", err) 360 t.FailNow() 361 } 362 363 if string(got) != expected { 364 t.Errorf("test case: %s", in) 365 t.Errorf(" expected: %s", expected) 366 t.Errorf(" got: %s", string(got)) 367 } 368 } 369 }