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 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
|
package huh
import (
"context"
"errors"
"fmt"
"io"
"regexp"
"strings"
"testing"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/exp/term/ansi"
)
var pretty = lipgloss.NewStyle().
Width(60).
Border(lipgloss.NormalBorder()).
MarginTop(1).
Padding(1, 3, 1, 2)
func TestForm(t *testing.T) {
type Taco struct {
Shell string
Base string
Toppings []string
}
type Order struct {
Taco Taco
Name string
Instructions string
Discount bool
}
var taco Taco
order := Order{Taco: taco}
f := NewForm(
NewGroup(
NewSelect[string]().
Options(NewOptions("Soft", "Hard")...).
Title("Shell?").
Description("Our tortillas are made fresh in-house every day.").
Validate(func(t string) error {
if t == "Hard" {
return fmt.Errorf("we're out of hard shells, sorry")
}
return nil
}).
Value(&order.Taco.Shell),
NewSelect[string]().
Options(NewOptions("Chicken", "Beef", "Fish", "Beans")...).
Value(&order.Taco.Base).
Title("Base"),
),
// Prompt for toppings and special instructions.
// The customer can ask for up to 4 toppings.
NewGroup(
NewMultiSelect[string]().
Title("Toppings").
Description("Choose up to 4.").
Options(
NewOption("Lettuce", "lettuce").Selected(true),
NewOption("Tomatoes", "tomatoes").Selected(true),
NewOption("Corn", "corn"),
NewOption("Salsa", "salsa"),
NewOption("Sour Cream", "sour cream"),
NewOption("Cheese", "cheese"),
).
Validate(func(t []string) error {
if len(t) <= 0 {
return fmt.Errorf("at least one topping is required")
}
return nil
}).
Value(&order.Taco.Toppings).
Filterable(true).
Limit(4),
),
// Gather final details for the order.
NewGroup(
NewInput().
Value(&order.Name).
Title("What's your name?").
Placeholder("Margaret Thatcher").
Description("For when your order is ready."),
NewText().
Value(&order.Instructions).
Placeholder("Just put it in the mailbox please").
Title("Special Instructions").
Description("Anything we should know?").
CharLimit(400),
NewConfirm().
Title("Would you like 15% off?").
Value(&order.Discount).
Affirmative("Yes!").
Negative("No."),
),
)
f.Update(f.Init())
view := ansi.Strip(f.View())
//
// ┃ Shell?
// ┃ Our tortillas are made fresh in-house every day.
// ┃ > Soft
// ┃ Hard
//
// Base
// > Chicken
// Beef
// Fish
// Beans
//
// ↑ up • ↓ down • / filter • enter select
//
if !strings.Contains(view, "┃ Shell?") {
t.Log(pretty.Render(view))
t.Error("Expected form to contain Shell? title")
}
if !strings.Contains(view, "Our tortillas are made fresh in-house every day.") {
t.Log(pretty.Render(view))
t.Error("Expected form to contain tortilla description")
}
if !strings.Contains(view, "Base") {
t.Log(pretty.Render(view))
t.Error("Expected form to contain Base title")
}
// Attempt to select hard shell and retrieve error.
m, _ := f.Update(keys('j'))
m, _ = m.Update(tea.KeyMsg{Type: tea.KeyTab})
view = ansi.Strip(m.View())
if !strings.Contains(view, "* we're out of hard shells, sorry") {
t.Log(pretty.Render(view))
t.Error("Expected form to show out of hard shells error")
}
m, _ = m.Update(keys('k'))
m, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = batchUpdate(m, cmd)
view = ansi.Strip(m.View())
if !strings.Contains(view, "┃ > Chicken") {
t.Log(pretty.Render(view))
t.Fatal("Expected form to continue to base group")
}
// batchMsg + nextGroup
m, cmd = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = batchUpdate(m, cmd)
view = ansi.Strip(m.View())
//
// ┃ Toppings
// ┃ Choose up to 4.
// ┃ > ✓ Lettuce
// ┃ ✓ Tomatoes
// ┃ • Corn
// ┃ • Salsa
// ┃ • Sour Cream
// ┃ • Cheese
//
// x toggle • ↑ up • ↓ down • enter confirm • shift+tab back
//
if !strings.Contains(view, "Toppings") {
t.Log(pretty.Render(view))
t.Fatal("Expected form to show toppings group")
}
if !strings.Contains(view, "Choose up to 4.") {
t.Log(pretty.Render(view))
t.Error("Expected form to show toppings description")
}
if !strings.Contains(view, "> ✓ Lettuce ") {
t.Log(pretty.Render(view))
t.Error("Expected form to preselect lettuce")
}
if !strings.Contains(view, " ✓ Tomatoes") {
t.Log(pretty.Render(view))
t.Error("Expected form to preselect tomatoes")
}
m, _ = m.Update(keys('j'))
m, _ = m.Update(keys('j'))
view = ansi.Strip(m.View())
if !strings.Contains(view, "> • Corn") {
t.Log(pretty.Render(view))
t.Error("Expected form to change selection to corn")
}
m, _ = m.Update(keys('x'))
view = ansi.Strip(m.View())
if !strings.Contains(view, "> ✓ Corn") {
t.Log(pretty.Render(view))
t.Error("Expected form to change selection to corn")
}
m = batchUpdate(m.Update(tea.KeyMsg{Type: tea.KeyEnter}))
view = ansi.Strip(m.View())
if !strings.Contains(view, "What's your name?") {
t.Log(pretty.Render(view))
t.Error("Expected form to prompt for name")
}
if !strings.Contains(view, "Special Instructions") {
t.Log(pretty.Render(view))
t.Error("Expected form to prompt for special instructions")
}
if !strings.Contains(view, "Would you like 15% off?") {
t.Log(pretty.Render(view))
t.Error("Expected form to prompt for discount")
}
//
// ┃ What's your name?
// ┃ For when your order is ready.
// ┃ > Margaret Thatcher
//
// Special Instructions
// Anything we should know?
// Just put it in the mailbox please
//
// Would you like 15% off?
//
// Yes! No.
//
// enter next • shift+tab back
//
m.Update(keys('G', 'l', 'e', 'n'))
view = ansi.Strip(m.View())
if !strings.Contains(view, "Glen") {
t.Log(pretty.Render(view))
t.Error("Expected form to accept user input")
}
if order.Taco.Shell != "Soft" {
t.Error("Expected order shell to be Soft")
}
if order.Taco.Base != "Chicken" {
t.Error("Expected order shell to be Chicken")
}
if len(order.Taco.Toppings) != 3 {
t.Error("Expected order to have 3 toppings")
}
if order.Name != "Glen" {
t.Error("Expected order name to be Glen")
}
// TODO: Finish and submit form.
}
func TestInput(t *testing.T) {
field := NewInput()
f := NewForm(NewGroup(field))
f.Update(f.Init())
view := ansi.Strip(f.View())
if !strings.Contains(view, ">") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain prompt.")
}
// Type Huh in the form.
m, _ := f.Update(keys('H', 'u', 'h'))
f = m.(*Form)
view = ansi.Strip(f.View())
if !strings.Contains(view, "Huh") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Huh.")
}
if !strings.Contains(view, "enter submit") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
if field.GetValue() != "Huh" {
t.Error("Expected field value to be Huh")
}
}
func TestInlineInput(t *testing.T) {
field := NewInput().
Title("Input ").
Prompt(": ").
Description("Description").
Inline(true)
f := NewForm(NewGroup(field)).WithWidth(40)
f.Update(f.Init())
view := ansi.Strip(f.View())
if !strings.Contains(view, "┃ Input Description:") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain inline input.")
}
// Type Huh in the form.
m, _ := f.Update(keys('H', 'u', 'h'))
f = m.(*Form)
view = ansi.Strip(f.View())
if !strings.Contains(view, "Huh") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Huh.")
}
if !strings.Contains(view, "enter submit") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
if !strings.Contains(view, "┃ Input Description: Huh") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
if field.GetValue() != "Huh" {
t.Error("Expected field value to be Huh")
}
}
func TestText(t *testing.T) {
field := NewText()
f := NewForm(NewGroup(field))
f.Update(f.Init())
// Type Huh in the form.
m, _ := f.Update(keys('H', 'u', 'h'))
f = m.(*Form)
view := ansi.Strip(f.View())
if !strings.Contains(view, "Huh") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Huh.")
}
if !strings.Contains(view, "alt+enter / ctrl+j new line • ctrl+e open editor • enter submit") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
if field.GetValue() != "Huh" {
t.Error("Expected field value to be Huh")
}
}
func TestConfirm(t *testing.T) {
field := NewConfirm().Title("Are you sure?")
f := NewForm(NewGroup(field))
f.Update(f.Init())
// Type Huh in the form.
m, _ := f.Update(keys('H'))
f = m.(*Form)
view := ansi.Strip(f.View())
if !strings.Contains(view, "Yes") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Yes.")
}
if !strings.Contains(view, "No") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain No.")
}
if !strings.Contains(view, "Are you sure?") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Are you sure?.")
}
if !strings.Contains(view, "←/→ toggle • enter submit") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
if field.GetValue() != false {
t.Error("Expected field value to be false")
}
// Toggle left
m, _ = f.Update(tea.KeyMsg{Type: tea.KeyLeft})
if field.GetValue() != true {
t.Error("Expected field value to be true")
}
// Toggle right
m, _ = f.Update(tea.KeyMsg{Type: tea.KeyRight})
if field.GetValue() != false {
t.Error("Expected field value to be false")
}
}
func TestSelect(t *testing.T) {
field := NewSelect[string]().Options(NewOptions("Foo", "Bar", "Baz")...).Title("Which one?")
f := NewForm(NewGroup(field))
f.Update(f.Init())
view := ansi.Strip(f.View())
if !strings.Contains(view, "Foo") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Foo.")
}
if !strings.Contains(view, "Which one?") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Which one?.")
}
if !strings.Contains(view, "> Foo") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Foo.")
}
// Move selection cursor down
m, _ := f.Update(tea.KeyMsg{Type: tea.KeyDown})
f = m.(*Form)
view = ansi.Strip(f.View())
if strings.Contains(view, "> Foo") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
if !strings.Contains(view, "> Bar") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
if !strings.Contains(view, "↑ up • ↓ down • / filter • enter submit") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
// Submit
m, _ = f.Update(tea.KeyMsg{Type: tea.KeyEnter})
f = m.(*Form)
if field.GetValue() != "Bar" {
t.Error("Expected field value to be Bar")
}
}
func TestMultiSelect(t *testing.T) {
field := NewMultiSelect[string]().Options(NewOptions("Foo", "Bar", "Baz")...).Title("Which one?")
f := NewForm(NewGroup(field))
f.Update(f.Init())
view := ansi.Strip(f.View())
if !strings.Contains(view, "Foo") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Foo.")
}
if !strings.Contains(view, "Which one?") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Which one?.")
}
if !strings.Contains(view, "> • Foo") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Foo.")
}
// Move selection cursor down
m, _ := f.Update(keys('j'))
view = ansi.Strip(m.View())
if strings.Contains(view, "> • Foo") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
if !strings.Contains(view, "> • Bar") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
// Toggle
m, _ = f.Update(keys('x'))
view = ansi.Strip(m.View())
if !strings.Contains(view, "> ✓ Bar") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
if !strings.Contains(view, "x toggle • ↑ up • ↓ down • / filter • enter submit") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
// Submit
m, _ = f.Update(tea.KeyMsg{Type: tea.KeyEnter})
f = m.(*Form)
value := field.GetValue()
if value, ok := value.([]string); !ok {
t.Error("Expected field value to a slice of string")
} else {
if len(value) != 1 {
t.Error("Expected field value length to be 1")
} else {
if value[0] != "Bar" {
t.Error("Expected first field value length to be Bar")
}
}
}
}
func TestSelectPageNavigation(t *testing.T) {
opts := NewOptions(
"Qux",
"Quux",
"Foo",
"Bar",
"Baz",
"Corge",
"Grault",
"Garply",
"Waldo",
"Fred",
"Plugh",
"Xyzzy",
"Thud",
"Norf",
"Blip",
"Flob",
"Zorp",
"Smurf",
"Bloop",
"Ping",
)
reFirst := regexp.MustCompile(`>( •)? Qux`)
reLast := regexp.MustCompile(`>( •)? Ping`)
reHalfDown := regexp.MustCompile(`>( •)? Baz`)
for _, field := range []Field{
NewMultiSelect[string]().Options(opts...).Title("Choose"),
NewSelect[string]().Options(opts...).Title("Choose"),
} {
f := NewForm(NewGroup(field)).WithHeight(10)
f.Update(f.Init())
view := ansi.Strip(f.View())
if !reFirst.MatchString(view) {
t.Log(pretty.Render(view))
t.Error("Wrong item selected")
}
m, _ := f.Update(keys('G'))
view = ansi.Strip(m.View())
if !reLast.MatchString(view) {
t.Log(pretty.Render(view))
t.Error("Wrong item selected")
}
m, _ = f.Update(keys('g'))
view = ansi.Strip(m.View())
if !reFirst.MatchString(view) {
t.Log(pretty.Render(view))
t.Error("Wrong item selected")
}
m, _ = f.Update(tea.KeyMsg{Type: tea.KeyCtrlD})
view = ansi.Strip(m.View())
if !reHalfDown.MatchString(view) {
t.Log(pretty.Render(view))
t.Error("Wrong item selected")
}
// sends multiple to verify it stays within boundaries
f.Update(tea.KeyMsg{Type: tea.KeyCtrlU})
f.Update(tea.KeyMsg{Type: tea.KeyCtrlU})
m, _ = f.Update(tea.KeyMsg{Type: tea.KeyCtrlU})
view = ansi.Strip(m.View())
if !reFirst.MatchString(view) {
t.Log(pretty.Render(view))
t.Error("Wrong item selected")
}
// verify it stays within boundaries
f.Update(tea.KeyMsg{Type: tea.KeyCtrlD})
f.Update(tea.KeyMsg{Type: tea.KeyCtrlD})
f.Update(tea.KeyMsg{Type: tea.KeyCtrlD})
f.Update(tea.KeyMsg{Type: tea.KeyCtrlD})
f.Update(tea.KeyMsg{Type: tea.KeyCtrlD})
m, _ = f.Update(tea.KeyMsg{Type: tea.KeyCtrlD})
view = ansi.Strip(m.View())
if !reLast.MatchString(view) {
t.Log(pretty.Render(view))
t.Error("Wrong item selected")
}
}
}
func TestFile(t *testing.T) {
field := NewFilePicker().Title("Which file?")
cmd := field.Init()
field.Update(cmd())
view := ansi.Strip(field.View())
if !strings.Contains(view, "No file selected") {
t.Log(pretty.Render(view))
t.Error("Expected file picker to show no file selected.")
}
if !strings.Contains(view, "Which file?") {
t.Log(pretty.Render(view))
t.Error("Expected file picker to show title.")
}
}
func TestHideGroup(t *testing.T) {
f := NewForm(
NewGroup(NewNote().Description("Foo")).
WithHide(true),
NewGroup(NewNote().Description("Bar")),
NewGroup(NewNote().Description("Baz")),
NewGroup(NewNote().Description("Qux")).
WithHideFunc(func() bool { return false }).
WithHide(true),
)
f = batchUpdate(f, f.Init()).(*Form)
if v := f.View(); !strings.Contains(v, "Bar") {
t.Log(pretty.Render(v))
t.Error("expected Bar to not be hidden")
}
// should have no effect as previous group is hidden
f.Update(prevGroup())
if v := f.View(); !strings.Contains(v, "Bar") {
t.Log(pretty.Render(v))
t.Error("expected Bar to not be hidden")
}
f.Update(nextGroup())
if v := f.View(); !strings.Contains(v, "Baz") {
t.Log(pretty.Render(v))
t.Error("expected Baz to not be hidden")
}
f.Update(nextGroup())
if v := f.View(); strings.Contains(v, "Qux") {
t.Log(pretty.Render(v))
t.Error("expected Qux to be hidden")
}
if v := f.State; v != StateCompleted {
t.Error("should have been completed")
}
}
func TestHideGroupLastAndFirstGroupsNotHidden(t *testing.T) {
f := NewForm(
NewGroup(NewNote().Description("Bar")),
NewGroup(NewNote().Description("Foo")).
WithHide(true),
NewGroup(NewNote().Description("Baz")),
)
f = batchUpdate(f, f.Init()).(*Form)
if v := ansi.Strip(f.View()); !strings.Contains(v, "Bar") {
t.Log(pretty.Render(v))
t.Error("expected Bar to not be hidden")
}
// should have no effect as there isn't any
f.Update(prevGroup())
if v := f.View(); !strings.Contains(v, "Bar") {
t.Log(pretty.Render(v))
t.Error("expected Bar to not be hidden")
}
f.Update(nextGroup())
if v := ansi.Strip(f.View()); !strings.Contains(v, "Baz") {
t.Log(pretty.Render(v))
t.Error("expected Baz to not be hidden")
}
// should submit the form
f.Update(nextGroup())
if v := f.State; v != StateCompleted {
t.Error("should have been completed")
}
}
func TestPrevGroup(t *testing.T) {
f := NewForm(
NewGroup(NewNote().Description("Bar")),
NewGroup(NewNote().Description("Foo")),
NewGroup(NewNote().Description("Baz")),
)
f = batchUpdate(f, f.Init()).(*Form)
f.Update(nextGroup())
f.Update(nextGroup())
f.Update(prevGroup())
f.Update(prevGroup())
if v := ansi.Strip(f.View()); !strings.Contains(v, "Bar") {
t.Log(pretty.Render(v))
t.Error("expected Bar to not be hidden")
}
}
func TestNote(t *testing.T) {
field := NewNote().Title("Taco").Description("How may we take your order?").Next(true)
f := NewForm(NewGroup(field))
f.Update(f.Init())
view := ansi.Strip(f.View())
if !strings.Contains(view, "Taco") {
t.Log(view)
t.Error("Expected field to contain Taco title.")
}
if !strings.Contains(view, "order?") {
t.Log(view)
t.Error("Expected field to contain Taco description.")
}
if !strings.Contains(view, "Next") {
t.Log(view)
t.Error("Expected field to contain next button")
}
if !strings.Contains(view, "enter submit") {
t.Log(view)
t.Error("Expected field to contain help.")
}
}
func TestDynamicHelp(t *testing.T) {
f := NewForm(
NewGroup(
NewInput().Title("Dynamic Help"),
NewInput().Title("Dynamic Help"),
NewInput().Title("Dynamic Help"),
),
)
f.Update(f.Init())
view := ansi.Strip(f.View())
if !strings.Contains(view, "Dynamic Help") {
t.Log(pretty.Render(view))
t.Fatal("Expected help to contain title.")
}
if strings.Contains(view, "shift+tab") || strings.Contains(view, "submit") {
t.Log(pretty.Render(view))
t.Error("Expected help not to contain shift+tab or submit.")
}
}
func TestSkip(t *testing.T) {
f := NewForm(
NewGroup(
NewInput().Title("First"),
NewNote().Title("Skipped"),
NewNote().Title("Skipped"),
NewInput().Title("Second"),
),
).WithWidth(25)
f = batchUpdate(f, f.Init()).(*Form)
view := ansi.Strip(f.View())
if !strings.Contains(view, "┃ First") {
t.Log(pretty.Render(view))
t.Error("Expected first field to be focused")
}
// next field should skip both of the notes and proceed to the last input.
f.Update(NextField())
view = ansi.Strip(f.View())
if strings.Contains(view, "┃ First") {
t.Log(pretty.Render(view))
t.Error("Expected first field to be blurred")
}
if !strings.Contains(view, "┃ Second") {
t.Log(pretty.Render(view))
t.Error("Expected second field to be focused")
}
// previous field should skip both of the notes and focus the first input.
f.Update(PrevField())
view = ansi.Strip(f.View())
if strings.Contains(view, "┃ Second") {
t.Log(pretty.Render(view))
t.Error("Expected second field to be blurred")
}
if !strings.Contains(view, "┃ First") {
t.Log(pretty.Render(view))
t.Error("Expected first field to be focused")
}
}
func TestTimeout(t *testing.T) {
// This test requires a real program, so make sure it doesn't interfere with our test runner.
f := formProgram()
// Test that the form times out after 1ms and returns a timeout error.
err := f.WithTimeout(1 * time.Millisecond).Run()
if err == nil || !errors.Is(err, ErrTimeout) {
t.Errorf("expected timeout error, got %v", err)
}
}
func TestAbort(t *testing.T) {
// This test requires a real program, so make sure it doesn't interfere with our test runner.
f := formProgram()
// Test that the form aborts without throwing a timeout error when explicitly told to abort.
ctx, cancel := context.WithCancel(context.Background())
// Since the context is cancelled, the program should exit immediately.
cancel()
// Tell the form to abort.
f.Update(tea.KeyMsg{Type: tea.KeyCtrlC})
// Run the program.
err := f.RunWithContext(ctx)
if err == nil || !errors.Is(err, ErrUserAborted) {
t.Errorf("expected user aborted error, got %v", err)
}
}
// formProgram returns a new Form with a nil input and output, so it can be used as a test program.
func formProgram() *Form {
return NewForm(NewGroup(NewInput().Title("Foo"))).
WithInput(nil).WithOutput(io.Discard).
WithAccessible(false)
}
func batchUpdate(m tea.Model, cmd tea.Cmd) tea.Model {
if cmd == nil {
return m
}
msg := cmd()
m, cmd = m.Update(msg)
if cmd == nil {
return m
}
msg = cmd()
m, cmd = m.Update(msg)
return m
}
func keys(runes ...rune) tea.KeyMsg {
return tea.KeyMsg{
Type: tea.KeyRunes,
Runes: runes,
}
}
|