bou.ke/statictemplate@v0.0.0-20180821122055-510411a5e7dd/statictemplate/translate_test.go (about) 1 package statictemplate 2 3 import ( 4 "go/types" 5 "gopkg.in/stretchr/testify.v1/assert" 6 "strings" 7 "testing" 8 "text/template" 9 ) 10 11 func equalish(t *testing.T, a string, b []byte, input string) { 12 assert.Equal(t, strings.Replace(strings.TrimSpace(a), "\t", " ", -1), strings.Replace(strings.TrimSpace(string(b)), "\t", " ", -1), input) 13 } 14 15 func TestTranslate(t *testing.T) { 16 for _, c := range []struct { 17 input, expected string 18 }{ 19 {"hello", ` 20 package main 21 22 import ( 23 "io" 24 ) 25 26 func Name(w io.Writer, dot string) (err error) { 27 defer func() { 28 if recovered := recover(); recovered != nil { 29 var ok bool 30 if err, ok = recovered.(error); !ok { 31 panic(recovered) 32 } 33 } 34 }() 35 return fun0(w, dot) 36 } 37 38 // template.tmpl(string) 39 func fun0(w io.Writer, dot string) error { 40 _, _ = io.WriteString(w, "hello") 41 return nil 42 }`}, 43 {"hi{{/* comment*/}}there", ` 44 package main 45 46 import ( 47 "io" 48 ) 49 50 func Name(w io.Writer, dot string) (err error) { 51 defer func() { 52 if recovered := recover(); recovered != nil { 53 var ok bool 54 if err, ok = recovered.(error); !ok { 55 panic(recovered) 56 } 57 } 58 }() 59 return fun0(w, dot) 60 } 61 62 // template.tmpl(string) 63 func fun0(w io.Writer, dot string) error { 64 _, _ = io.WriteString(w, "hi") 65 _, _ = io.WriteString(w, "there") 66 return nil 67 }`}, 68 {`{{ "hi" }}`, ` 69 package main 70 71 import ( 72 "io" 73 ) 74 75 func Name(w io.Writer, dot string) (err error) { 76 defer func() { 77 if recovered := recover(); recovered != nil { 78 var ok bool 79 if err, ok = recovered.(error); !ok { 80 panic(recovered) 81 } 82 } 83 }() 84 return fun0(w, dot) 85 } 86 87 // template.tmpl(string) 88 func fun0(w io.Writer, dot string) error { 89 _, _ = io.WriteString(w, "hi") 90 return nil 91 }`}, 92 {`{{ print ( "hi" | print ) }}`, ` 93 package main 94 95 import ( 96 "bou.ke/statictemplate/funcs" 97 "io" 98 ) 99 100 func Name(w io.Writer, dot string) (err error) { 101 defer func() { 102 if recovered := recover(); recovered != nil { 103 var ok bool 104 if err, ok = recovered.(error); !ok { 105 panic(recovered) 106 } 107 } 108 }() 109 return fun0(w, dot) 110 } 111 112 // template.tmpl(string) 113 func fun0(w io.Writer, dot string) error { 114 _, _ = io.WriteString(w, funcs.Print(funcs.Print("hi"))) 115 return nil 116 }`}, 117 {`{{ printf "%d" (or 0 1) }}`, ` 118 package main 119 120 import ( 121 "bou.ke/statictemplate/funcs" 122 "io" 123 ) 124 125 func Name(w io.Writer, dot string) (err error) { 126 defer func() { 127 if recovered := recover(); recovered != nil { 128 var ok bool 129 if err, ok = recovered.(error); !ok { 130 panic(recovered) 131 } 132 } 133 }() 134 return fun0(w, dot) 135 } 136 137 // template.tmpl(string) 138 func fun0(w io.Writer, dot string) error { 139 _, _ = io.WriteString(w, funcs.Printf("%d", funcs.Or(0, 1))) 140 return nil 141 }`}, 142 {`{{ 1 }}`, ` 143 package main 144 145 import ( 146 "fmt" 147 "io" 148 ) 149 150 func Name(w io.Writer, dot string) (err error) { 151 defer func() { 152 if recovered := recover(); recovered != nil { 153 var ok bool 154 if err, ok = recovered.(error); !ok { 155 panic(recovered) 156 } 157 } 158 }() 159 return fun0(w, dot) 160 } 161 162 // template.tmpl(string) 163 func fun0(w io.Writer, dot string) error { 164 _, _ = fmt.Fprint(w, 1) 165 return nil 166 }`}, 167 {`{{ . }}`, ` 168 package main 169 170 import ( 171 "io" 172 ) 173 174 func Name(w io.Writer, dot string) (err error) { 175 defer func() { 176 if recovered := recover(); recovered != nil { 177 var ok bool 178 if err, ok = recovered.(error); !ok { 179 panic(recovered) 180 } 181 } 182 }() 183 return fun0(w, dot) 184 } 185 186 // template.tmpl(string) 187 func fun0(w io.Writer, dot string) error { 188 _, _ = io.WriteString(w, dot) 189 return nil 190 }`}, 191 {`{{ true }}`, ` 192 package main 193 194 import ( 195 "fmt" 196 "io" 197 ) 198 199 func Name(w io.Writer, dot string) (err error) { 200 defer func() { 201 if recovered := recover(); recovered != nil { 202 var ok bool 203 if err, ok = recovered.(error); !ok { 204 panic(recovered) 205 } 206 } 207 }() 208 return fun0(w, dot) 209 } 210 211 // template.tmpl(string) 212 func fun0(w io.Writer, dot string) error { 213 _, _ = fmt.Fprint(w, true) 214 return nil 215 }`}, 216 {`{{ false }}`, ` 217 package main 218 219 import ( 220 "fmt" 221 "io" 222 ) 223 224 func Name(w io.Writer, dot string) (err error) { 225 defer func() { 226 if recovered := recover(); recovered != nil { 227 var ok bool 228 if err, ok = recovered.(error); !ok { 229 panic(recovered) 230 } 231 } 232 }() 233 return fun0(w, dot) 234 } 235 236 // template.tmpl(string) 237 func fun0(w io.Writer, dot string) error { 238 _, _ = fmt.Fprint(w, false) 239 return nil 240 }`}, 241 {`{{ $a := 1 }}{{ $a }}`, ` 242 package main 243 244 import ( 245 "fmt" 246 "io" 247 ) 248 249 func Name(w io.Writer, dot string) (err error) { 250 defer func() { 251 if recovered := recover(); recovered != nil { 252 var ok bool 253 if err, ok = recovered.(error); !ok { 254 panic(recovered) 255 } 256 } 257 }() 258 return fun0(w, dot) 259 } 260 261 // template.tmpl(string) 262 func fun0(w io.Writer, dot string) error { 263 _Vara := 1 264 _ = _Vara 265 _, _ = fmt.Fprint(w, _Vara) 266 return nil 267 }`}, 268 {`{{ $a := "hey" }}{{ $a }}`, ` 269 package main 270 271 import ( 272 "io" 273 ) 274 275 func Name(w io.Writer, dot string) (err error) { 276 defer func() { 277 if recovered := recover(); recovered != nil { 278 var ok bool 279 if err, ok = recovered.(error); !ok { 280 panic(recovered) 281 } 282 } 283 }() 284 return fun0(w, dot) 285 } 286 287 // template.tmpl(string) 288 func fun0(w io.Writer, dot string) error { 289 _Vara := "hey" 290 _ = _Vara 291 _, _ = io.WriteString(w, _Vara) 292 return nil 293 }`}, 294 {`{{ $a := 1 }}{{ $a := 2 }}`, ` 295 package main 296 297 import ( 298 "io" 299 ) 300 301 func Name(w io.Writer, dot string) (err error) { 302 defer func() { 303 if recovered := recover(); recovered != nil { 304 var ok bool 305 if err, ok = recovered.(error); !ok { 306 panic(recovered) 307 } 308 } 309 }() 310 return fun0(w, dot) 311 } 312 313 // template.tmpl(string) 314 func fun0(w io.Writer, dot string) error { 315 _Vara := 1 316 _ = _Vara 317 _Vara = 2 318 return nil 319 }`}, 320 {`{{ $a := 1 }}{{ if . }}{{ $a := 2 }}{{ end }}{{ $a := 3 }}`, ` 321 package main 322 323 import ( 324 "io" 325 ) 326 327 func Name(w io.Writer, dot string) (err error) { 328 defer func() { 329 if recovered := recover(); recovered != nil { 330 var ok bool 331 if err, ok = recovered.(error); !ok { 332 panic(recovered) 333 } 334 } 335 }() 336 return fun0(w, dot) 337 } 338 339 // template.tmpl(string) 340 func fun0(w io.Writer, dot string) error { 341 _Vara := 1 342 _ = _Vara 343 if eval := dot; len(eval) != 0 { 344 _Vara := 2 345 _ = _Vara 346 } 347 _Vara = 3 348 return nil 349 }`}, 350 {`{{ "hi" | print }}`, ` 351 package main 352 353 import ( 354 "bou.ke/statictemplate/funcs" 355 "io" 356 ) 357 358 func Name(w io.Writer, dot string) (err error) { 359 defer func() { 360 if recovered := recover(); recovered != nil { 361 var ok bool 362 if err, ok = recovered.(error); !ok { 363 panic(recovered) 364 } 365 } 366 }() 367 return fun0(w, dot) 368 } 369 370 // template.tmpl(string) 371 func fun0(w io.Writer, dot string) error { 372 _, _ = io.WriteString(w, funcs.Print("hi")) 373 return nil 374 }`}, 375 {`{{ ( "hi" | printf "%v" ) | print }}`, ` 376 package main 377 378 import ( 379 "bou.ke/statictemplate/funcs" 380 "io" 381 ) 382 383 func Name(w io.Writer, dot string) (err error) { 384 defer func() { 385 if recovered := recover(); recovered != nil { 386 var ok bool 387 if err, ok = recovered.(error); !ok { 388 panic(recovered) 389 } 390 } 391 }() 392 return fun0(w, dot) 393 } 394 395 // template.tmpl(string) 396 func fun0(w io.Writer, dot string) error { 397 _, _ = io.WriteString(w, funcs.Print(funcs.Printf("%v", "hi"))) 398 return nil 399 }`}, 400 {`{{ ( "hi" | print ) | printf "%v" }}`, ` 401 package main 402 403 import ( 404 "bou.ke/statictemplate/funcs" 405 "io" 406 ) 407 408 func Name(w io.Writer, dot string) (err error) { 409 defer func() { 410 if recovered := recover(); recovered != nil { 411 var ok bool 412 if err, ok = recovered.(error); !ok { 413 panic(recovered) 414 } 415 } 416 }() 417 return fun0(w, dot) 418 } 419 420 // template.tmpl(string) 421 func fun0(w io.Writer, dot string) error { 422 _, _ = io.WriteString(w, funcs.Printf("%v", funcs.Print("hi"))) 423 return nil 424 }`}, 425 {`{{ "hi" | print | print }}`, ` 426 package main 427 428 import ( 429 "bou.ke/statictemplate/funcs" 430 "io" 431 ) 432 433 func Name(w io.Writer, dot string) (err error) { 434 defer func() { 435 if recovered := recover(); recovered != nil { 436 var ok bool 437 if err, ok = recovered.(error); !ok { 438 panic(recovered) 439 } 440 } 441 }() 442 return fun0(w, dot) 443 } 444 445 // template.tmpl(string) 446 func fun0(w io.Writer, dot string) error { 447 _, _ = io.WriteString(w, funcs.Print(funcs.Print("hi"))) 448 return nil 449 }`}, 450 {`{{ "<wow>" | html }}`, ` 451 package main 452 453 import ( 454 "bou.ke/statictemplate/funcs" 455 "io" 456 ) 457 458 func Name(w io.Writer, dot string) (err error) { 459 defer func() { 460 if recovered := recover(); recovered != nil { 461 var ok bool 462 if err, ok = recovered.(error); !ok { 463 panic(recovered) 464 } 465 } 466 }() 467 return fun0(w, dot) 468 } 469 470 // template.tmpl(string) 471 func fun0(w io.Writer, dot string) error { 472 _, _ = io.WriteString(w, funcs.Html("<wow>")) 473 return nil 474 }`}, 475 {`{{ if true }}a{{end}}`, ` 476 package main 477 478 import ( 479 "io" 480 ) 481 482 func Name(w io.Writer, dot string) (err error) { 483 defer func() { 484 if recovered := recover(); recovered != nil { 485 var ok bool 486 if err, ok = recovered.(error); !ok { 487 panic(recovered) 488 } 489 } 490 }() 491 return fun0(w, dot) 492 } 493 494 // template.tmpl(string) 495 func fun0(w io.Writer, dot string) error { 496 if eval := true; eval { 497 _, _ = io.WriteString(w, "a") 498 } 499 return nil 500 }`}, 501 {`{{ if true }}a{{else}}b{{end}}`, ` 502 package main 503 504 import ( 505 "io" 506 ) 507 508 func Name(w io.Writer, dot string) (err error) { 509 defer func() { 510 if recovered := recover(); recovered != nil { 511 var ok bool 512 if err, ok = recovered.(error); !ok { 513 panic(recovered) 514 } 515 } 516 }() 517 return fun0(w, dot) 518 } 519 520 // template.tmpl(string) 521 func fun0(w io.Writer, dot string) error { 522 if eval := true; eval { 523 _, _ = io.WriteString(w, "a") 524 } else { 525 _, _ = io.WriteString(w, "b") 526 } 527 return nil 528 }`}, 529 {`{{define "T1"}}ONE{{end}} 530 {{define "T2"}}TWO {{template "T1"}}{{end}} 531 {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}} 532 {{template "T3"}}`, ` 533 package main 534 535 import ( 536 "io" 537 ) 538 539 func Name(w io.Writer, dot string) (err error) { 540 defer func() { 541 if recovered := recover(); recovered != nil { 542 var ok bool 543 if err, ok = recovered.(error); !ok { 544 panic(recovered) 545 } 546 } 547 }() 548 return fun0(w, dot) 549 } 550 551 // T1(nil) 552 func fun2(w io.Writer, dot interface{}) error { 553 _, _ = io.WriteString(w, "ONE") 554 return nil 555 } 556 557 // T2(nil) 558 func fun3(w io.Writer, dot interface{}) error { 559 _, _ = io.WriteString(w, "TWO ") 560 if err := fun2(w, nil); err != nil { 561 return err 562 } 563 return nil 564 } 565 566 // T3(nil) 567 func fun1(w io.Writer, dot interface{}) error { 568 if err := fun2(w, nil); err != nil { 569 return err 570 } 571 _, _ = io.WriteString(w, " ") 572 if err := fun3(w, nil); err != nil { 573 return err 574 } 575 return nil 576 } 577 578 // template.tmpl(string) 579 func fun0(w io.Writer, dot string) error { 580 _, _ = io.WriteString(w, "\n") 581 _, _ = io.WriteString(w, "\n") 582 _, _ = io.WriteString(w, "\n") 583 if err := fun1(w, nil); err != nil { 584 return err 585 } 586 return nil 587 }`}, 588 {` 589 {{define "T1"}}{{if .}}TWO{{else}}ONE{{template "T1" true}}{{end}}{{end}} 590 {{template "T1"}}`, ` 591 package main 592 593 import ( 594 "io" 595 ) 596 597 func Name(w io.Writer, dot string) (err error) { 598 defer func() { 599 if recovered := recover(); recovered != nil { 600 var ok bool 601 if err, ok = recovered.(error); !ok { 602 panic(recovered) 603 } 604 } 605 }() 606 return fun0(w, dot) 607 } 608 609 // T1(bool) 610 func fun2(w io.Writer, dot bool) error { 611 if eval := dot; eval { 612 _, _ = io.WriteString(w, "TWO") 613 } else { 614 _, _ = io.WriteString(w, "ONE") 615 if err := fun2(w, true); err != nil { 616 return err 617 } 618 } 619 return nil 620 } 621 622 // T1(nil) 623 func fun1(w io.Writer, dot interface{}) error { 624 if eval := dot; eval != nil { 625 _, _ = io.WriteString(w, "TWO") 626 } else { 627 _, _ = io.WriteString(w, "ONE") 628 if err := fun2(w, true); err != nil { 629 return err 630 } 631 } 632 return nil 633 } 634 635 // template.tmpl(string) 636 func fun0(w io.Writer, dot string) error { 637 _, _ = io.WriteString(w, "\n") 638 _, _ = io.WriteString(w, "\n") 639 if err := fun1(w, nil); err != nil { 640 return err 641 } 642 return nil 643 }`}, 644 } { 645 temp := template.Must(template.New("template.tmpl").Parse(c.input)) 646 actual, err := Translate(temp, "main", []TranslateInstruction{ 647 {"Name", "template.tmpl", types.Typ[types.String]}, 648 }) 649 if assert.NoError(t, err, c.input) { 650 equalish(t, c.expected, actual, c.input) 651 } 652 } 653 }