1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
|
package jen_test
import (
"testing"
. "github.com/dave/jennifer/jen"
)
var gencases = []tc{
{
desc: `bool group`,
code: BlockFunc(func(g *Group) {
g.Bool()
}),
expect: `{
bool
}`,
},
{
desc: `recover func`,
code: Recover(),
expect: `recover()`,
},
{
desc: `recover statement`,
code: Null().Recover(),
expect: `recover()`,
},
{
desc: `recover group`,
code: BlockFunc(func(g *Group) {
g.Recover()
}),
expect: `{
recover()
}`,
},
{
desc: `real func`,
code: Real(Id("a")),
expect: `real(a)`,
},
{
desc: `real statement`,
code: Null().Real(Id("a")),
expect: `real(a)`,
},
{
desc: `real group`,
code: BlockFunc(func(g *Group) {
g.Real(Id("a"))
}),
expect: `{
real(a)
}`,
},
{
desc: `printlnfunc func`,
code: PrintlnFunc(func(g *Group) { g.Id("a") }),
expect: `println(a)`,
},
{
desc: `printlnfunc statement`,
code: Null().PrintlnFunc(func(g *Group) { g.Id("a") }),
expect: `println(a)`,
},
{
desc: `printlnfunc group`,
code: BlockFunc(func(bg *Group) {
bg.PrintlnFunc(func(pg *Group) { pg.Id("a") })
}),
expect: `{
println(a)
}`,
},
{
desc: `println func`,
code: Println(Id("a")),
expect: `println(a)`,
},
{
desc: `println statement`,
code: Null().Println(Id("a")),
expect: `println(a)`,
},
{
desc: `println group`,
code: BlockFunc(func(g *Group) {
g.Println(Id("a"))
}),
expect: `{
println(a)
}`,
},
{
desc: `printfunc func`,
code: PrintFunc(func(g *Group) { g.Id("a") }),
expect: `print(a)`,
},
{
desc: `printfunc statement`,
code: Null().PrintFunc(func(g *Group) { g.Id("a") }),
expect: `print(a)`,
},
{
desc: `printfunc group`,
code: BlockFunc(func(bg *Group) {
bg.PrintFunc(func(pg *Group) { pg.Id("a") })
}),
expect: `{
print(a)
}`,
},
{
desc: `print func`,
code: Print(Id("a")),
expect: `print(a)`,
},
{
desc: `print statement`,
code: Null().Print(Id("a")),
expect: `print(a)`,
},
{
desc: `print group`,
code: BlockFunc(func(g *Group) {
g.Print(Id("a"))
}),
expect: `{
print(a)
}`,
},
{
desc: `panic func`,
code: Panic(Id("a")),
expect: `panic(a)`,
},
{
desc: `panic statement`,
code: Null().Panic(Id("a")),
expect: `panic(a)`,
},
{
desc: `panic group`,
code: BlockFunc(func(g *Group) {
g.Panic(Id("a"))
}),
expect: `{
panic(a)
}`,
},
{
desc: `new func`,
code: New(Id("a")),
expect: `new(a)`,
},
{
desc: `new statement`,
code: Id("a").Op(":=").New(Id("a")),
expect: `a := new(a)`,
},
{
desc: `new group`,
code: BlockFunc(func(g *Group) {
g.New(Id("a"))
}),
expect: `{
new(a)
}`,
},
{
desc: `make func`,
code: Make(Id("a")),
expect: `make(a)`,
},
{
desc: `make statement`,
code: Id("a").Op(":=").Make(Id("a")),
expect: `a := make(a)`,
},
{
desc: `make group`,
code: BlockFunc(func(g *Group) {
g.Make(Id("a"))
}),
expect: `{
make(a)
}`,
},
{
desc: `len func`,
code: Len(Id("a")),
expect: `len(a)`,
},
{
desc: `len statement`,
code: Id("a").Op(":=").Len(Id("a")),
expect: `a := len(a)`,
},
{
desc: `len group`,
code: BlockFunc(func(g *Group) {
g.Len(Id("a"))
}),
expect: `{
len(a)
}`,
},
{
desc: `imag func`,
code: Imag(Id("a")),
expect: `imag(a)`,
},
{
desc: `imag statement`,
code: Id("a").Op(":=").Imag(Id("a")),
expect: `a := imag(a)`,
},
{
desc: `imag group`,
code: BlockFunc(func(g *Group) {
g.Imag(Id("a"))
}),
expect: `{
imag(a)
}`,
},
{
desc: `delete func`,
code: Delete(Id("a"), Id("b")),
expect: `delete(a, b)`,
},
{
desc: `delete statement`,
code: Null().Delete(Id("a"), Id("b")),
expect: `delete(a, b)`,
},
{
desc: `delete group`,
code: BlockFunc(func(g *Group) {
g.Delete(Id("a"), Id("b"))
}),
expect: `{
delete(a, b)
}`,
},
{
desc: `copy func`,
code: Copy(Id("a"), Id("b")),
expect: `copy(a, b)`,
},
{
desc: `copy statement`,
code: Id("a").Op(":=").Copy(Id("a"), Id("b")),
expect: `a := copy(a, b)`,
},
{
desc: `copy group`,
code: BlockFunc(func(g *Group) {
g.Copy(Id("a"), Id("b"))
}),
expect: `{
copy(a, b)
}`,
},
{
desc: `complex func`,
code: Complex(Id("a"), Id("b")),
expect: `complex(a, b)`,
},
{
desc: `complex statement`,
code: Id("a").Op(":=").Complex(Id("a"), Id("b")),
expect: `a := complex(a, b)`,
},
{
desc: `complex group`,
code: BlockFunc(func(g *Group) {
g.Complex(Id("a"), Id("b"))
}),
expect: `{
complex(a, b)
}`,
},
{
desc: `close group`,
code: BlockFunc(func(g *Group) { g.Close(Id("a")) }),
expect: `{
close(a)
}`,
},
{
desc: `cap func`,
code: Cap(Id("a")),
expect: `cap(a)`,
},
{
desc: `cap statement`,
code: Id("a").Op(":=").Cap(Id("b")),
expect: `a := cap(b)`,
},
{
desc: `cap group`,
code: BlockFunc(func(g *Group) {
g.Cap(Id("a"))
}),
expect: `{
cap(a)
}`,
},
{
desc: `append group`,
code: BlockFunc(func(g *Group) {
g.Append(Id("a"))
}),
expect: `{
append(a)
}`,
},
{
desc: `appendfunc statement`,
code: Id("a").Op("=").AppendFunc(func(ag *Group) { ag.Id("a") }),
expect: `a = append(a)`,
},
{
desc: `appendfunc func`,
code: AppendFunc(func(ag *Group) { ag.Id("a") }),
expect: `append(a)`,
},
{
desc: `appendfunc group`,
code: BlockFunc(func(bg *Group) {
bg.AppendFunc(func(ag *Group) { ag.Id("a") })
}),
expect: `{
append(a)
}`,
},
{
desc: `casefunc group`,
code: Switch().BlockFunc(func(g *Group) {
g.CaseFunc(func(g *Group) { g.Id("a") }).Block()
}),
expect: `switch {
case a:
}`,
},
{
desc: `case group`,
code: Switch().BlockFunc(func(g *Group) {
g.Case(Id("a")).Block()
}),
expect: `switch {
case a:
}`,
},
{
desc: `structfunc statement`,
code: Id("a").Op(":=").StructFunc(func(g *Group) {}).Values(),
expect: `a := struct{}{}`,
},
{
desc: `structfunc group`,
// Don't do this! ListFunc used to kludge Group.Struct usage
// without syntax error.
code: Id("a").Op(":=").ListFunc(func(g *Group) { g.StructFunc(func(g *Group) {}) }).Values(),
expect: `a := struct{}{}`,
},
{
desc: `structfunc func`,
code: Id("a").Op(":=").Add(StructFunc(func(g *Group) {})).Values(),
expect: `a := struct{}{}`,
},
{
desc: `struct group`,
// Don't do this! ListFunc used to kludge Group.Struct usage
// without syntax error.
code: Id("a").Op(":=").ListFunc(func(g *Group) { g.Struct() }).Values(),
expect: `a := struct{}{}`,
},
{
desc: `struct func`,
code: Id("a").Op(":=").Add(Struct()).Values(),
expect: `a := struct{}{}`,
},
{
desc: `interfacefunc func`,
code: Id("a").Assert(InterfaceFunc(func(g *Group) {
g.Id("a").Call().Int()
g.Id("b").Call().Int()
})),
expect: `a.(interface{
a() int
b() int
})`,
},
{
desc: `interfacefunc statement`,
code: Id("a").Assert(Null().InterfaceFunc(func(g *Group) {
g.Id("a").Call().Int()
g.Id("b").Call().Int()
})),
expect: `a.(interface{
a() int
b() int
})`,
},
{
desc: `interfacefunc group`,
// Don't do this! ListFunc used to kludge Group.InterfaceFunc usage
// without syntax error.
code: Id("a").Assert(ListFunc(func(lg *Group) {
lg.InterfaceFunc(func(ig *Group) {
ig.Id("a").Call().Int()
ig.Id("b").Call().Int()
})
})),
expect: `a.(interface{
a() int
b() int
})`,
},
{
desc: `interface func`,
code: Interface().Parens(Id("a")),
expect: `interface{}(a)`,
},
{
desc: `interface group`,
code: BlockFunc(func(g *Group) {
g.Interface().Parens(Id("a"))
}),
expect: `{
interface{}(a)
}`,
},
{
desc: `interface statement`,
code: Null().Interface().Parens(Id("a")),
expect: `interface{}(a)`,
},
{
desc: `switchfunc func`,
code: SwitchFunc(func(rg *Group) {
rg.Id("a")
}).Block(),
expect: `switch a {}`,
},
{
desc: `switchfunc statement`,
code: Null().SwitchFunc(func(rg *Group) {
rg.Id("a")
}).Block(),
expect: `switch a {
}`,
},
{
desc: `switchfunc group`,
code: BlockFunc(func(bg *Group) {
bg.SwitchFunc(func(rg *Group) {
rg.Id("a")
}).Block()
}),
expect: `{
switch a {
}
}`,
},
{
desc: `switch group`,
code: BlockFunc(func(bg *Group) {
bg.Switch().Block()
}),
expect: `{
switch {
}
}`,
},
{
desc: `forfunc func`,
code: ForFunc(func(rg *Group) {
rg.Id("a")
}).Block(),
expect: `for a {}`,
},
{
desc: `forfunc statement`,
code: Null().ForFunc(func(rg *Group) {
rg.Id("a")
}).Block(),
expect: `for a {
}`,
},
{
desc: `forfunc group`,
code: BlockFunc(func(bg *Group) {
bg.ForFunc(func(rg *Group) {
rg.Id("a")
}).Block()
}),
expect: `{
for a {
}
}`,
},
{
desc: `for group`,
code: BlockFunc(func(g *Group) {
g.For(Id("a")).Block()
}),
expect: `{
for a {}
}`,
},
{
desc: `returnfunc func`,
code: ReturnFunc(func(rg *Group) {
rg.Lit(1)
rg.Lit(2)
}),
expect: `return 1, 2`,
},
{
desc: `returnfunc statement`,
code: Empty().ReturnFunc(func(rg *Group) {
rg.Lit(1)
rg.Lit(2)
}),
expect: `return 1, 2`,
},
{
desc: `returnfunc group`,
code: BlockFunc(func(bg *Group) {
bg.ReturnFunc(func(rg *Group) {
rg.Lit(1)
rg.Lit(2)
})
}),
expect: `{
return 1, 2
}`,
},
{
desc: `return group`,
code: BlockFunc(func(g *Group) {
g.Return()
}),
expect: `{
return
}`,
},
{
desc: `iffunc group`,
code: BlockFunc(func(bg *Group) {
bg.IfFunc(func(ig *Group) {
ig.Id("a")
}).Block()
}),
expect: `{
if a {}
}`,
},
{
desc: `iffunc func`,
code: IfFunc(func(ig *Group) {
ig.Id("a")
}).Block(),
expect: `if a {}`,
},
{
desc: `iffunc statement`,
code: Null().IfFunc(func(ig *Group) {
ig.Id("a")
}).Block(),
expect: `if a {}`,
},
{
desc: `if group`,
code: BlockFunc(func(g *Group) { g.If(Id("a")).Block() }),
expect: `{
if a {}
}`,
},
{
desc: `map group`,
code: BlockFunc(func(g *Group) { g.Map(Int()).Int().Values(Dict{Lit(1): Lit(1)}) }),
expect: `{
map[int]int{1:1}
}`,
},
{
desc: `assert group`,
// Don't do this! ListFunc used to kludge Group.Assert usage without
// syntax error.
code: Id("a").ListFunc(func(g *Group) { g.Assert(Id("b")) }),
expect: `a.(b)`,
},
{
desc: `assert func`,
code: Id("a").Add(Assert(Id("b"))),
expect: `a.(b)`,
},
{
desc: `paramsfunc group`,
// Don't do this! ListFunc used to kludge Group.ParamsFunc usage without
// syntax error.
code: Id("a").ListFunc(func(lg *Group) { lg.ParamsFunc(func(cg *Group) { cg.Lit(1) }) }),
expect: `a(1)`,
},
{
desc: `paramsfunc func`,
code: Id("a").Add(ParamsFunc(func(g *Group) { g.Lit(1) })),
expect: `a(1)`,
},
{
desc: `paramsfunc statement`,
code: Id("a").ParamsFunc(func(g *Group) { g.Lit(1) }),
expect: `a(1)`,
},
{
desc: `params group`,
// Don't do this! ListFunc used to kludge Group.Params usage without
// syntax error.
code: Id("a").ListFunc(func(g *Group) { g.Params(Lit(1)) }),
expect: `a(1)`,
},
{
desc: `params func`,
code: Id("a").Add(Params(Lit(1))),
expect: `a(1)`,
},
{
desc: `callfunc group`,
// Don't do this! ListFunc used to kludge Group.CallFunc usage without
// syntax error.
code: Id("a").ListFunc(func(lg *Group) { lg.CallFunc(func(cg *Group) { cg.Lit(1) }) }),
expect: `a(1)`,
},
{
desc: `callfunc func`,
code: Id("a").Add(CallFunc(func(g *Group) { g.Lit(1) })),
expect: `a(1)`,
},
{
desc: `call group`,
// Don't do this! ListFunc used to kludge Group.Call usage without
// syntax error.
code: Id("a").ListFunc(func(g *Group) { g.Call(Lit(1)) }),
expect: `a(1)`,
},
{
desc: `call func`,
code: Id("a").Add(Call(Lit(1))),
expect: `a(1)`,
},
{
desc: `defsfunc statement`,
code: Const().DefsFunc(func(g *Group) { g.Id("a").Op("=").Lit(1) }),
expect: `const (
a = 1
)`,
},
{
desc: `defsfunc func`,
code: Const().Add(DefsFunc(func(g *Group) { g.Id("a").Op("=").Lit(1) })),
expect: `const (
a = 1
)`,
},
{
desc: `defsfunc group`,
// Don't do this! ListFunc used to kludge Group.DefsFunc usage without
// syntax error.
code: Const().ListFunc(func(lg *Group) { lg.DefsFunc(func(dg *Group) { dg.Id("a").Op("=").Lit(1) }) }),
expect: `const (
a = 1
)`,
},
{
desc: `defs group`,
// Don't do this! ListFunc used to kludge Group.Defs usage without
// syntax error.
code: Const().ListFunc(func(g *Group) { g.Defs(Id("a").Op("=").Lit(1)) }),
expect: `const (
a = 1
)`,
},
{
desc: `defs func`,
code: Const().Add(Defs(Id("a").Op("=").Lit(1))),
expect: `const (
a = 1
)`,
},
{
desc: `blockfunc group`,
code: BlockFunc(func(g *Group) { g.BlockFunc(func(g *Group) {}) }),
expect: `{{}}`,
},
{
desc: `block group`,
code: BlockFunc(func(g *Group) { g.Block() }),
expect: `{{}}`,
},
{
desc: `indexfunc group`,
code: BlockFunc(func(g *Group) { g.IndexFunc(func(g *Group) { g.Lit(1) }).Int().Values(Lit(1)) }),
expect: `{[1]int{1}}`,
},
{
desc: `indexfunc statement`,
code: Id("a").IndexFunc(func(g *Group) { g.Lit(1) }),
expect: `a[1]`,
},
{
desc: `indexfunc func`,
code: Id("a").Add(IndexFunc(func(g *Group) { g.Lit(1) })),
expect: `a[1]`,
},
{
desc: `index group`,
code: BlockFunc(func(g *Group) { g.Index(Lit(1)).Int().Values(Lit(1)) }),
expect: `{[1]int{1}}`,
},
{
desc: `index func`,
code: Id("a").Add(Index(Lit(1))),
expect: `a[1]`,
},
{
desc: `valuesfunc func`,
code: ValuesFunc(func(vg *Group) {
vg.Lit(1)
}),
expect: `{1}`,
},
{
desc: `valuesfunc group`,
code: BlockFunc(func(bg *Group) {
bg.ValuesFunc(func(vg *Group) {
vg.Lit(1)
})
}),
expect: `{
{1}
}`,
},
{
desc: `values group`,
code: BlockFunc(func(g *Group) {
g.Values(Lit(1))
}),
expect: `{
{1}
}`,
},
{
desc: `listfunc statement`,
code: Add(Null()).ListFunc(func(lg *Group) {
lg.Id("a")
lg.Id("b")
}).Op("=").Id("c"),
expect: `a, b = c`,
},
{
desc: `listfunc func`,
code: ListFunc(func(lg *Group) {
lg.Id("a")
lg.Id("b")
}).Op("=").Id("c"),
expect: `a, b = c`,
},
{
desc: `listfunc group`,
code: BlockFunc(func(bg *Group) {
bg.ListFunc(func(lg *Group) {
lg.Id("a")
lg.Id("b")
}).Op("=").Id("c")
}),
expect: `{
a, b = c
}`,
},
{
desc: `list group`,
code: BlockFunc(func(g *Group) { g.List(Id("a"), Id("b")).Op("=").Id("c") }),
expect: `{
a, b = c
}`,
},
{
desc: `parens func`,
code: Parens(Lit(1)),
expect: `(1)`,
},
{
desc: `parens group`,
code: BlockFunc(func(g *Group) { g.Parens(Lit(1)) }),
expect: `{
(1)
}`,
},
}
func TestGen(t *testing.T) {
caseTester(t, gencases)
}
|