-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_test.go
57 lines (52 loc) · 1.16 KB
/
gpt_test.go
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
package main
import (
"io"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestGetUserInput(t *testing.T) {
testCases := []struct {
name string
mockInput string
expectedInput string
delimiter string
}{
{
name: "Test with newline delimiter",
mockInput: "Hello World\n",
expectedInput: "Hello World",
delimiter: "\n",
},
{
name: "Test with , delimiter",
mockInput: "Hello World,",
expectedInput: "Hello World",
delimiter: ",",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockStdin := strings.NewReader(tc.mockInput)
oldStdin := os.Stdin
f, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
if _, err := io.Copy(f, mockStdin); err != nil {
t.Fatal(err)
}
if _, err := f.Seek(0, 0); err != nil {
t.Fatal(err)
}
os.Stdin = f
actualInput := getUserInput(tc.delimiter)
os.Stdin = oldStdin
if actualInput != tc.expectedInput {
t.Errorf("getUserInput() with newline delimiter returned unexpected result: expected %q, got %q", tc.expectedInput, actualInput)
}
})
}
}