From 16f7f9464c0f578fc99acdc363884c0bc9e28e7e Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 30 Mar 2021 19:37:02 -0400 Subject: [PATCH] Add some tests --- Makefile | 17 ++++++++++ editor/fn_test.go | 75 +++++++++++++++++++++++++++++++++++++++++++ editor/row_test.go | 27 ++++++++++++++++ terminal/ansi_test.go | 23 +++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 Makefile create mode 100644 editor/fn_test.go create mode 100644 editor/row_test.go create mode 100644 terminal/ansi_test.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e36dae1 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +test: + go test timshome.page/gilo/editor timshome.page/gilo/terminal + +coverage.out: + go test timshome.page/gilo/editor timshome.page/gilo/terminal -coverprofile=coverage.out + +coverage: coverage.out + go tool cover -html=coverage.out + +clean: + rm -f gilo + rm -f coverage.out + +gilo: + go build + +.PHONY: test clean \ No newline at end of file diff --git a/editor/fn_test.go b/editor/fn_test.go new file mode 100644 index 0000000..943d508 --- /dev/null +++ b/editor/fn_test.go @@ -0,0 +1,75 @@ +package editor + +import ( + "testing" +) + +type isRune struct { + arg1 rune + expected bool +} + +// (╯°□°)╯︵ ┻━┻ +var isAsciiTest = []isRune { + {'┻', false}, + {'$', true}, + {'︵', false}, + {0x7f, true}, +} + +func TestIsAscii(t *testing.T) { + for _, test := range isAsciiTest { + if output := isAscii(test.arg1); output != test.expected { + t.Errorf("Output %v not equal to expected %v for input %q", output, test.expected, test.arg1) + } + } +} + +var isCtrlTest = []isRune { + {0x78, false}, + {0x7f, true}, + {0x02, true}, + {0x98, false}, + {'a', false}, +} + +func TestIsCtrl(t *testing.T) { + for _, test := range isCtrlTest { + if output := isCtrl(test.arg1); output != test.expected { + t.Errorf("Output %v not equal to expected %v for input %q", output, test.expected, test.arg1) + } + } +} + +func TestTruncateString(t *testing.T) { + firstString := "abcdefghijklmnopqrstuvwxyz" + truncated := truncateString(firstString, 13) + + got := len(truncated) + want := 13 + + if got != want { + t.Errorf("Truncated length: %q, expected length: %q", got, want) + } +} + +func TestTruncateStringNegative(t *testing.T) { + got := truncateString("fdlkjf", -5) + want := "" + + if got != want { + t.Errorf("Truncated value: %q, expected value: %q", got, want) + } +} + +func TestTruncateShorterString(t *testing.T) { + str := "abcdefg" + + got := truncateString(str, 13) + want := str + + if got != want { + t.Errorf("Truncated value: %q, expected value: %q", got, want) + } +} + diff --git a/editor/row_test.go b/editor/row_test.go new file mode 100644 index 0000000..0eb07ec --- /dev/null +++ b/editor/row_test.go @@ -0,0 +1,27 @@ +package editor + +import "testing" + +func TestNewRow(t *testing.T) { + str := "abcdefg" + row := NewRow(str) + + got := string(row.chars) + want := str + + if got != want { + t.Errorf("Row chars not equal to original string. Row: %q, String: %q", row, str) + } +} + +func TestRowSize(t *testing.T) { + str := "abcdefg" + row := NewRow(str) + + got := row.Size() + want := 7 + + if got != want { + t.Errorf("Row size not equal to number of runes in original string") + } +} diff --git a/terminal/ansi_test.go b/terminal/ansi_test.go new file mode 100644 index 0000000..cadfb5e --- /dev/null +++ b/terminal/ansi_test.go @@ -0,0 +1,23 @@ +package terminal + +import "testing" + +func TestCode(t *testing.T) { + input := "H" + + got := Code(input) + want := EscPrefix + input + + if got != want { + t.Errorf("Failed to prefix %q with the escape prefix: %q", input, EscPrefix) + } +} + +func TestMoveCursor(t *testing.T) { + got := MoveCursor(0, 0) + want := Code("%d;%dH", 1, 1) + + if got != want { + t.Errorf("Failed to create escape sequence to move cursor") + } +} \ No newline at end of file