github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/cli/cli_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"testing"
     7  )
     8  
     9  func TestAskForAction(t *testing.T) {
    10  	tests := map[string]struct {
    11  		input          string
    12  		options        []string
    13  		expectedAnswer string
    14  	}{
    15  		"input of key of one option": {
    16  			input:          "y\n",
    17  			options:        []string{"y=yes", "n=no"},
    18  			expectedAnswer: "y",
    19  		},
    20  		"input of value of one option": {
    21  			input:          "yes\n",
    22  			options:        []string{"y=yes", "n=no"},
    23  			expectedAnswer: "y",
    24  		},
    25  		"input of key with uppercase character": {
    26  			input:          "Y\n",
    27  			options:        []string{"y=yes", "n=no"},
    28  			expectedAnswer: "y",
    29  		},
    30  		"input of value with uppercase character": {
    31  			input:          "Yes\n",
    32  			options:        []string{"y=yes", "n=no"},
    33  			expectedAnswer: "y",
    34  		},
    35  		"input of invalid answer at first": {
    36  			input:          "m\nn\n",
    37  			options:        []string{"y=yes", "n=no"},
    38  			expectedAnswer: "n",
    39  		},
    40  		"empty input at first": {
    41  			input:          "\ny\n",
    42  			options:        []string{"y=yes", "n=no"},
    43  			expectedAnswer: "y",
    44  		},
    45  		"input with surrounding space": {
    46  			input:          " no \n",
    47  			options:        []string{"y=yes", "n=no"},
    48  			expectedAnswer: "n",
    49  		},
    50  	}
    51  
    52  	for name, tc := range tests {
    53  		t.Run(name, func(t *testing.T) {
    54  			var stdin bytes.Buffer
    55  			stdin.Write([]byte(tc.input))
    56  			stdinReader := bufio.NewReader(&stdin)
    57  			a := AskForAction("What?", tc.options, stdinReader)
    58  			if a != tc.expectedAnswer {
    59  				t.Fatalf("Want: '%s', got: '%s'", tc.expectedAnswer, a)
    60  			}
    61  		})
    62  	}
    63  }