The following tests are broadly testing the accessible prompter, and NOT asserting on the prompter's complete and exact output strings. These tests generally operate with this logic: - Wait for a particular substring (a portion of the prompt) to appear - Send input - Wait for another substring to a
(t *testing.T)
| 35 | // are sufficient to ensure that the accessible prompter behaves roughly as expected |
| 36 | // but doesn't mandate that prompts always look exactly the same. |
| 37 | func TestAccessiblePrompter(t *testing.T) { |
| 38 | |
| 39 | t.Run("Select", func(t *testing.T) { |
| 40 | console := newTestVirtualTerminal(t) |
| 41 | p := newTestAccessiblePrompter(t, console) |
| 42 | |
| 43 | go func() { |
| 44 | // Wait for prompt to appear |
| 45 | _, err := console.ExpectString("Enter a number between 1 and 3:") |
| 46 | require.NoError(t, err) |
| 47 | |
| 48 | // Select option 1 |
| 49 | _, err = console.SendLine("1") |
| 50 | require.NoError(t, err) |
| 51 | }() |
| 52 | |
| 53 | selectValue, err := p.Select("Select a number", "", []string{"1", "2", "3"}) |
| 54 | require.NoError(t, err) |
| 55 | assert.Equal(t, 0, selectValue) |
| 56 | }) |
| 57 | |
| 58 | t.Run("Select - blank input returns default value", func(t *testing.T) { |
| 59 | console := newTestVirtualTerminal(t) |
| 60 | p := newTestAccessiblePrompter(t, console) |
| 61 | dummyDefaultValue := "12345abcdefg" |
| 62 | options := []string{"1", "2", dummyDefaultValue} |
| 63 | |
| 64 | go func() { |
| 65 | // Wait for prompt to appear |
| 66 | _, err := console.ExpectString("Enter a number between 1 and 3:") |
| 67 | require.NoError(t, err) |
| 68 | |
| 69 | // Just press enter to accept the default |
| 70 | _, err = console.SendLine("") |
| 71 | require.NoError(t, err) |
| 72 | }() |
| 73 | |
| 74 | selectValue, err := p.Select("Select a number", dummyDefaultValue, options) |
| 75 | require.NoError(t, err) |
| 76 | |
| 77 | expectedIndex := slices.Index(options, dummyDefaultValue) |
| 78 | assert.Equal(t, expectedIndex, selectValue) |
| 79 | }) |
| 80 | |
| 81 | t.Run("Select - default value is in prompt and in readable format", func(t *testing.T) { |
| 82 | console := newTestVirtualTerminal(t) |
| 83 | p := newTestAccessiblePrompter(t, console) |
| 84 | dummyDefaultValue := "12345abcdefg" |
| 85 | options := []string{"1", "2", dummyDefaultValue} |
| 86 | |
| 87 | go func() { |
| 88 | // Wait for prompt to appear |
| 89 | _, err := console.ExpectString("Select a number (default: 12345abcdefg)") |
| 90 | require.NoError(t, err) |
| 91 | |
| 92 | // Just press enter to accept the default |
| 93 | _, err = console.SendLine("") |
| 94 | require.NoError(t, err) |
nothing calls this directly
no test coverage detected