This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/textdraw/layout_test.go

112 lines
2.7 KiB
Go
Raw Normal View History

package textdraw
import "testing"
import "golang.org/x/image/math/fixed"
2023-03-31 19:14:40 -06:00
import defaultfont "git.tebibyte.media/sashakoshka/tomo/default/font"
func TestDoWord (test *testing.T) {
text := []rune("The quick brown fox")
word, remaining := DoWord(text, defaultfont.FaceRegular)
expect := "quick brown fox"
if string(remaining) != expect {
test.Fatalf (
`text: "%s", remaining: "%s" expected: "%s"`,
string(text), string(remaining), expect)
}
if len(word.Runes) != 4 {
test.Fatalf(`wrong rune length %d`, len(word.Runes))
}
2023-02-15 18:16:49 -07:00
if word.SpaceAfter != fixed.I(7) {
test.Fatalf(`wrong space after %d`, word.SpaceAfter.Round())
}
if word.FirstRune() != 'T' {
test.Fatalf(`wrong first rune %s`, string(word.FirstRune()))
}
if word.LastRune() != ' ' {
test.Fatalf(`wrong last rune %s`, string(word.FirstRune()))
}
}
func TestDoLine (test *testing.T) {
// case 1
text := []rune("The quick brown fox\njumped over the lazy dog")
line, remaining := DoLine(text, defaultfont.FaceRegular, 0)
expect := "jumped over the lazy dog"
if string(remaining) != expect {
test.Fatalf (
`text: "%s", remaining: "%s" expected: "%s"`,
string(text), string(remaining), expect)
}
if len(line.Words) != 4 {
test.Fatalf(`wrong word count %d`, len(line.Words))
}
if !line.BreakAfter {
test.Fatalf(`did not set BreakAfter to true`)
}
// case 2
text = []rune("jumped over the lazy dog")
line, remaining = DoLine(text, defaultfont.FaceRegular, 0)
expect = ""
if string(remaining) != expect {
test.Fatalf (
`text: "%s", remaining: "%s" expected: "%s"`,
string(text), string(remaining), expect)
}
if len(line.Words) != 5 {
test.Fatalf(`wrong word count %d`, len(line.Words))
}
if line.BreakAfter {
test.Fatalf(`did not set BreakAfter to false`)
}
// case 3
text = []rune("jumped over the lazy dog")
line, remaining = DoLine(text, defaultfont.FaceRegular, fixed.I(7 * 12))
expect = "the lazy dog"
if string(remaining) != expect {
test.Fatalf (
`text: "%s", remaining: "%s" expected: "%s"`,
string(text), string(remaining), expect)
}
if len(line.Words) != 2 {
test.Fatalf(`wrong word count %d`, len(line.Words))
}
if line.BreakAfter {
test.Fatalf(`did not set BreakAfter to false`)
}
2023-02-15 23:55:00 -07:00
// case 4
text = []rune("jumped over the lazy dog")
line, remaining = DoLine(text, defaultfont.FaceRegular, fixed.I(10))
expect = "over the lazy dog"
if string(remaining) != expect {
test.Fatalf (
`text: "%s", remaining: "%s" expected: "%s"`,
string(text), string(remaining), expect)
}
if len(line.Words) != 1 {
test.Fatalf(`wrong word count %d`, len(line.Words))
}
if line.BreakAfter {
test.Fatalf(`did not set BreakAfter to false`)
}
}