Skip to content

Commit

Permalink
test: simplify cmd/micro tests (#3470)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Sep 16, 2024
1 parent a3211dc commit 1539da7
Showing 1 changed file with 26 additions and 47 deletions.
73 changes: 26 additions & 47 deletions cmd/micro/micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
Expand All @@ -26,7 +25,7 @@ func init() {
func startup(args []string) (tcell.SimulationScreen, error) {
var err error

tempDir, err = ioutil.TempDir("", "micro_test")
tempDir, err = os.MkdirTemp("", "micro_test")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -164,20 +163,22 @@ func findBuffer(file string) *buffer.Buffer {
return buf
}

func createTestFile(name string, content string) (string, error) {
testf, err := ioutil.TempFile("", name)
func createTestFile(t *testing.T, content string) string {
f, err := os.CreateTemp(t.TempDir(), "")
if err != nil {
return "", err
t.Fatal(err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatal(err)
}
}()

if _, err := testf.Write([]byte(content)); err != nil {
return "", err
}
if err := testf.Close(); err != nil {
return "", err
if _, err := f.WriteString(content); err != nil {
t.Fatal(err)
}

return testf.Name(), nil
return f.Name()
}

func TestMain(m *testing.M) {
Expand All @@ -194,18 +195,12 @@ func TestMain(m *testing.M) {
}

func TestSimpleEdit(t *testing.T) {
file, err := createTestFile("micro_simple_edit_test", "base content")
if err != nil {
t.Error(err)
return
}
defer os.Remove(file)
file := createTestFile(t, "base content")

openFile(file)

if findBuffer(file) == nil {
t.Errorf("Could not find buffer %s", file)
return
t.Fatalf("Could not find buffer %s", file)
}

injectKey(tcell.KeyEnter, rune(tcell.KeyEnter), tcell.ModNone)
Expand All @@ -223,28 +218,21 @@ func TestSimpleEdit(t *testing.T) {

injectKey(tcell.KeyCtrlS, rune(tcell.KeyCtrlS), tcell.ModCtrl)

data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

assert.Equal(t, "firstfoobar\nbase content\n", string(data))
}

func TestMouse(t *testing.T) {
file, err := createTestFile("micro_mouse_test", "base content")
if err != nil {
t.Error(err)
return
}
defer os.Remove(file)
file := createTestFile(t, "base content")

openFile(file)

if findBuffer(file) == nil {
t.Errorf("Could not find buffer %s", file)
return
t.Fatalf("Could not find buffer %s", file)
}

// buffer:
Expand Down Expand Up @@ -275,10 +263,9 @@ func TestMouse(t *testing.T) {
// base content
injectKey(tcell.KeyCtrlS, rune(tcell.KeyCtrlS), tcell.ModCtrl)

data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

assert.Equal(t, "firstline\nsecondline\nbase content\n", string(data))
Expand All @@ -301,18 +288,12 @@ Ernleȝe test_string æðelen
`

func TestSearchAndReplace(t *testing.T) {
file, err := createTestFile("micro_search_replace_test", srTestStart)
if err != nil {
t.Error(err)
return
}
defer os.Remove(file)
file := createTestFile(t, srTestStart)

openFile(file)

if findBuffer(file) == nil {
t.Errorf("Could not find buffer %s", file)
return
t.Fatalf("Could not find buffer %s", file)
}

injectKey(tcell.KeyCtrlE, rune(tcell.KeyCtrlE), tcell.ModCtrl)
Expand All @@ -321,10 +302,9 @@ func TestSearchAndReplace(t *testing.T) {

injectKey(tcell.KeyCtrlS, rune(tcell.KeyCtrlS), tcell.ModCtrl)

data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

assert.Equal(t, srTest2, string(data))
Expand All @@ -337,10 +317,9 @@ func TestSearchAndReplace(t *testing.T) {

injectKey(tcell.KeyCtrlS, rune(tcell.KeyCtrlS), tcell.ModCtrl)

data, err = ioutil.ReadFile(file)
data, err = os.ReadFile(file)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

assert.Equal(t, srTest3, string(data))
Expand Down

0 comments on commit 1539da7

Please sign in to comment.