You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
funcTestUserSelect(t*testing.T) {
// Create a pipe to simulate user inputr, w, err:=os.Pipe()
iferr!=nil {
t.Fatalf("Error creating pipe: %v", err)
}
// Redirect stdin to the read end of the pipeoldStdin:=os.Stdindeferfunc() {
r.Close()
w.Close()
os.Stdin=oldStdin
}()
os.Stdin=r// Write user input to the write end of the pipeinput:="\033[B\n"// Press down arrow key and then enter keyif_, err:=w.WriteString(input); err!=nil {
t.Fatalf("Error writing to pipe: %v", err)
}
// Call the function that reads from stdinresult, err:=UserSelect()
iferr!=nil {
t.Fatalf("Error selecting from prompt: %v", err)
}
// Verify the outputexpected:="Tuesday"ifresult!=expected {
t.Fatalf("Unexpected result: %q, expected: %q", result, expected)
}
}
The error
=== RUN TestUserSelect
Use the arrow keys to navigate: ↓ ↑ → ←
? Select Day:
▸ Monday
Tuesday
Wednesday
Thursday
↓ Friday
Prompt failed ^D
test_test.go:58: Error selecting from prompt: ^D
--- FAIL: TestUserSelect (0.00s)
FAIL
The text was updated successfully, but these errors were encountered:
I tried to write a unit test of Select but encounterd an error
The function
The test
The error
The text was updated successfully, but these errors were encountered: