Add some tests

This commit is contained in:
Timothy Warren 2021-03-30 19:37:02 -04:00
parent 7d435d1556
commit 16f7f9464c
4 changed files with 142 additions and 0 deletions

17
Makefile Normal file
View File

@ -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

75
editor/fn_test.go Normal file
View File

@ -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)
}
}

27
editor/row_test.go Normal file
View File

@ -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")
}
}

23
terminal/ansi_test.go Normal file
View File

@ -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")
}
}