File: utils_test.go

package info (click to toggle)
pat 0.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,296 kB
  • sloc: javascript: 3,891; sh: 124; makefile: 11
file content (143 lines) | stat: -rw-r--r-- 3,335 bytes parent folder | download
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
package cli

import (
	"bufio"
	"bytes"
	"strings"
	"testing"
)

func setupMockStdin(input string) func() {
	originalStdin := stdin
	stdin = bufio.NewReader(strings.NewReader(input))
	return func() { stdin = originalStdin }
}

func TestPrompt(t *testing.T) {
	tests := []struct {
		name           string
		input          string
		question       string
		defaultValue   string
		options        []string
		expected       string
		expectedPrompt string
	}{
		{
			name:           "basic prompt with response",
			input:          "test response\n",
			question:       "Test question",
			defaultValue:   "default",
			expected:       "test response",
			expectedPrompt: "Test question [default]: ",
		},
		{
			name:           "prompt with default value but no input",
			input:          "\n",
			question:       "Test question",
			defaultValue:   "default",
			expected:       "default",
			expectedPrompt: "Test question [default]: ",
		},
		{
			name:           "prompt with options and matching input",
			input:          "YES\n",
			question:       "Test question",
			defaultValue:   "default",
			options:        []string{"yes", "no"},
			expected:       "YES",
			expectedPrompt: "Test question (DEFAULT/yes/no): ",
		},
		{
			name:           "prompt with options and default not in options",
			input:          "\n",
			question:       "Test question",
			defaultValue:   "default",
			options:        []string{"yes", "no"},
			expected:       "default",
			expectedPrompt: "Test question (DEFAULT/yes/no): ",
		},
		{
			name:           "prompt with default value not in options",
			input:          "\n",
			question:       "Test question",
			defaultValue:   "yes",
			options:        []string{"no"},
			expected:       "yes",
			expectedPrompt: "Test question (YES/no): ",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			restore := setupMockStdin(tt.input)
			defer restore()

			var buf bytes.Buffer
			result := prompt2(&buf, tt.question, tt.defaultValue, tt.options...)

			if result != tt.expected {
				t.Errorf("prompt(%q, %q, %v) = %q; expected %q", tt.question, tt.defaultValue, tt.options, result, tt.expected)
			}
			promptOutput := buf.String()
			if promptOutput != tt.expectedPrompt {
				t.Errorf("prompt output = %q; expected %q", promptOutput, tt.expectedPrompt)
			}
		})
	}
}

func TestSplitFunc(t *testing.T) {
	tests := []struct {
		input    rune
		expected bool
	}{
		{' ', true},
		{'\t', true},
		{'\n', true},
		{',', true},
		{';', true},
		{'a', false},
		{'A', false},
		{'1', false},
	}

	for _, test := range tests {
		result := SplitFunc(test.input)
		if result != test.expected {
			t.Errorf("SplitFunc(%c): expected %v, got %v", test.input, test.expected, result)
		}
	}
}

func TestReadLine(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:     "read single line",
			input:    "line1\n",
			expected: "line1",
		},
		{
			name:     "read multiple lines",
			input:    "line1\nline2\n",
			expected: "line1",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Set up mock stdin
			restore := setupMockStdin(tt.input)
			defer restore()

			result := readLine()
			if result != tt.expected {
				t.Errorf("readLine() = %q; expected %q", result, tt.expected)
			}
		})
	}
}