1
1
mirror of https://github.com/walles/moar.git synced 2024-12-12 02:23:59 +03:00

Improve test feedback

On comparison failure, print both flavors.
This commit is contained in:
Johan Walles 2021-05-23 16:12:00 +02:00
parent 8a5f6fe473
commit 0a40404070

View File

@ -5,7 +5,6 @@ import (
"testing" "testing"
"github.com/walles/moar/twin" "github.com/walles/moar/twin"
"gotest.tools/assert"
) )
func tokenize(input string) []twin.Cell { func tokenize(input string) []twin.Cell {
@ -13,25 +12,49 @@ func tokenize(input string) []twin.Cell {
return line.HighlightedTokens(nil) return line.HighlightedTokens(nil)
} }
func toString(cellLines [][]twin.Cell) string {
returnMe := ""
for _, cellLine := range cellLines {
lineString := ""
for _, cell := range cellLine {
lineString += string(cell.Rune)
}
if len(returnMe) > 0 {
returnMe += "\n"
}
returnMe += "<" + lineString + ">"
}
return returnMe
}
func assertEqual(t *testing.T, a [][]twin.Cell, b [][]twin.Cell) {
if reflect.DeepEqual(a, b) {
return
}
t.Errorf("Expected equal:\n%s\n\n%s", toString(a), toString(b))
}
func TestEnoughRoomNoWrapping(t *testing.T) { func TestEnoughRoomNoWrapping(t *testing.T) {
toWrap := tokenize("This is a test") toWrap := tokenize("This is a test")
wrapped := wrapLine(20, toWrap) wrapped := wrapLine(20, toWrap)
assert.Assert(t, reflect.DeepEqual(wrapped, [][]twin.Cell{toWrap})) assertEqual(t, wrapped, [][]twin.Cell{toWrap})
} }
func TestWrapEmpty(t *testing.T) { func TestWrapEmpty(t *testing.T) {
empty := tokenize("") empty := tokenize("")
wrapped := wrapLine(20, empty) wrapped := wrapLine(20, empty)
assert.Assert(t, reflect.DeepEqual(wrapped, [][]twin.Cell{empty})) assertEqual(t, wrapped, [][]twin.Cell{empty})
} }
func TestWordLongerThanLine(t *testing.T) { func TestWordLongerThanLine(t *testing.T) {
toWrap := tokenize("intermediary") toWrap := tokenize("intermediary")
wrapped := wrapLine(6, toWrap) wrapped := wrapLine(6, toWrap)
assert.Assert(t, reflect.DeepEqual(wrapped, [][]twin.Cell{ assertEqual(t, wrapped, [][]twin.Cell{
tokenize("interm"), tokenize("interm"),
tokenize("ediary"), tokenize("ediary"),
})) })
} }
// FIXME: Test word wrapping // FIXME: Test word wrapping