From e7ba61b2aa094f54d36bb87e69c678b9d1e3817d Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 31 Mar 2021 19:17:04 -0400 Subject: [PATCH] Add more tests --- editor/editor_test.go | 35 +++++++++++++++++++++++++++++++++++ editor/row_test.go | 13 +++++++++++++ terminal/raw.go | 9 --------- terminal/size.go | 6 +++--- 4 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 editor/editor_test.go diff --git a/editor/editor_test.go b/editor/editor_test.go new file mode 100644 index 0000000..34c65e4 --- /dev/null +++ b/editor/editor_test.go @@ -0,0 +1,35 @@ +package editor + +import "testing" + +func TestNew(t *testing.T) { + e := New() + + if e == nil { + t.Errorf("Failed to create editor") + } +} + +func TestAppendRow(t *testing.T) { + e := New() + e.appendRow("Test Row") + + got := len(e.rows) + want := 1 + + if got != want { + t.Errorf("Failed to add a row to the editor") + } +} + +func TestRowCount(t *testing.T) { + e := New() + e.appendRow("Test Row") + + got := e.rowCount() + want := 1 + + if got != want { + t.Errorf("Expected %d rows, got %d rows", want, got) + } +} \ No newline at end of file diff --git a/editor/row_test.go b/editor/row_test.go index 6a9bf16..358eaab 100644 --- a/editor/row_test.go +++ b/editor/row_test.go @@ -25,3 +25,16 @@ func TestRowSize(t *testing.T) { t.Errorf("Row size not equal to number of runes in original string") } } + +func TestRenderSize(t *testing.T) { + str := "\tabcdefg" + row := newRow(str) + row.update() + + got := row.rSize() + want := 11 + + if got != want { + t.Errorf("Row rsize not equal to number of runes in original string") + } +} diff --git a/terminal/raw.go b/terminal/raw.go index 512903d..3daf2bb 100644 --- a/terminal/raw.go +++ b/terminal/raw.go @@ -8,8 +8,6 @@ import ( // Put the terminal in raw mode func RawOn() *term.State { - check() - oldState, err := term.MakeRaw(int(os.Stdin.Fd())) if err != nil { panic(err) @@ -25,10 +23,3 @@ func RawOff(oldState *term.State) { panic(err) } } - -// Is this a valid interactive terminal? -func check() { - if !term.IsTerminal(int(os.Stdin.Fd())) { - panic("An interactive terminal is required to use a text editor!") - } -} diff --git a/terminal/size.go b/terminal/size.go index 89c43b3..6364898 100644 --- a/terminal/size.go +++ b/terminal/size.go @@ -13,8 +13,6 @@ type Screen struct { // Get the size of the terminal in rows and columns func Size () *Screen { - check() - cols := 80 rows := 24 @@ -25,7 +23,9 @@ func Size () *Screen { } // Figure out the size the hard way - rows, cols = sizeTrick() + if term.IsTerminal(int(os.Stdin.Fd())) { + rows, cols = sizeTrick() + } return &Screen{ rows, cols } }