Skip to content

Commit

Permalink
Adds tests for editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Oct 27, 2023
1 parent d254414 commit fad005b
Showing 1 changed file with 154 additions and 2 deletions.
156 changes: 154 additions & 2 deletions pkg/cli/editor_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cli

import (
"bytes"
"os"
"testing"

"github.com/eiannone/keyboard"
)

func TestNewEditor(t *testing.T) {
output := os.Stdout
output := new(bytes.Buffer)
history := NewHistory("", 0)
editor := NewEditor(output, history)

Expand Down Expand Up @@ -41,7 +42,7 @@ func TestEditRequest(t *testing.T) {

defer os.Remove(tmpfile.Name())

output := os.Stdout
output := new(bytes.Buffer)
history := NewHistory(tmpfile.Name(), 5)
editor := NewEditor(output, history)

Expand All @@ -66,3 +67,154 @@ func TestEditRequest(t *testing.T) {
t.Errorf("Expected empty request, got %s", req)
}
}

func TestEditRequestInterrupted(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test_history")
if err != nil {
t.Fatal(err)
}

defer os.Remove(tmpfile.Name())

output := new(bytes.Buffer)
history := NewHistory(tmpfile.Name(), 5)
editor := NewEditor(output, history)

keyStream := make(chan keyboard.KeyEvent)
defer close(keyStream)

go func() {
keyStream <- keyboard.KeyEvent{Key: keyboard.KeyCtrlC}
}()

req, err := editor.EditRequest(keyStream, "")

if err == nil {
t.Error("Expected error")
}

if req != "" {
t.Errorf("Expected empty request, got %s", req)
}

go func() {
keyStream <- keyboard.KeyEvent{Key: keyboard.KeyCtrlD}
}()

req, err = editor.EditRequest(keyStream, "")

if err == nil {
t.Error("Expected error")
}

if req != "" {
t.Errorf("Expected empty request, got %s", req)
}
}

func TestEditRequestExitEditor(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test_history")
if err != nil {
t.Fatal(err)
}

defer os.Remove(tmpfile.Name())

output := new(bytes.Buffer)
history := NewHistory(tmpfile.Name(), 5)
editor := NewEditor(output, history)

keyStream := make(chan keyboard.KeyEvent)
defer close(keyStream)

go func() {
keyStream <- keyboard.KeyEvent{Key: keyboard.KeyEsc}
}()

req, err := editor.EditRequest(keyStream, "")

if err != nil {
t.Error("Expected no error")
}

if req != "" {
t.Errorf("Expected empty request, got %s", req)
}
}

func TestEditRequestClosingKeyboard(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test_history")
if err != nil {
t.Fatal(err)
}

defer os.Remove(tmpfile.Name())

output := new(bytes.Buffer)
history := NewHistory(tmpfile.Name(), 5)
editor := NewEditor(output, history)

keyStream := make(chan keyboard.KeyEvent)
close(keyStream)

req, err := editor.EditRequest(keyStream, "")

if err == nil {
t.Error("Expected error")
}

if req != "" {
t.Errorf("Expected empty request, got %s", req)
}
}

func TestEditRequestSpecialKeys(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test_history")
if err != nil {
t.Fatal(err)
}

defer os.Remove(tmpfile.Name())

output := new(bytes.Buffer)
history := NewHistory(tmpfile.Name(), 5)
editor := NewEditor(output, history)

keyStream := make(chan keyboard.KeyEvent)

go func() {
for _, key := range []keyboard.Key{
keyboard.KeySpace,
keyboard.KeyCtrlU,
keyboard.KeyEsc,
} {
keyStream <- keyboard.KeyEvent{Key: key}
}
}()

req, err := editor.EditRequest(keyStream, "")

if err != nil {
t.Errorf("Unexpected error: %v", err)
}

if req != "" {
t.Errorf("Expected empty request, got %s", req)
}

outputStr := output.String()

if outputStr != " "+LineClear+"\r" {
t.Errorf("Unexpected output: %q", outputStr)
}

go func() {
for _, key := range []keyboard.Key{
keyboard.KeySpace,
keyboard.KeyCtrlU,
keyboard.KeyEsc,
} {
keyStream <- keyboard.KeyEvent{Key: key}
}
}()
}

0 comments on commit fad005b

Please sign in to comment.