go-chart/text_test.go

61 lines
1.7 KiB
Go
Raw Permalink Normal View History

2016-07-29 19:36:29 -04:00
package chart
import (
"testing"
2024-10-27 22:52:38 -04:00
"git.smarteching.com/zeni/go-chart/v2/testutil"
2016-07-29 19:36:29 -04:00
)
func TestTextWrapWord(t *testing.T) {
// replaced new assertions helper
2016-07-29 19:36:29 -04:00
r, err := PNG(1024, 1024)
testutil.AssertNil(t, err)
2016-07-29 19:36:29 -04:00
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
2016-07-29 19:36:29 -04:00
basicTextStyle := Style{Font: f, FontSize: 24}
output := Text.WrapFitWord(r, "this is a test string", 100, basicTextStyle)
testutil.AssertNotEmpty(t, output)
testutil.AssertLen(t, output, 3)
2016-07-29 19:36:29 -04:00
for _, line := range output {
basicTextStyle.WriteToRenderer(r)
lineBox := r.MeasureText(line)
testutil.AssertTrue(t, lineBox.Width() < 100, line+": "+lineBox.String())
2016-07-29 19:36:29 -04:00
}
testutil.AssertEqual(t, "this is", output[0])
testutil.AssertEqual(t, "a test", output[1])
testutil.AssertEqual(t, "string", output[2])
2016-07-29 19:36:29 -04:00
output = Text.WrapFitWord(r, "foo", 100, basicTextStyle)
testutil.AssertLen(t, output, 1)
testutil.AssertEqual(t, "foo", output[0])
2016-07-29 21:24:25 -04:00
// test that it handles newlines.
output = Text.WrapFitWord(r, "this\nis\na\ntest\nstring", 100, basicTextStyle)
testutil.AssertLen(t, output, 5)
2016-07-29 21:24:25 -04:00
// test that it handles newlines and long lines.
output = Text.WrapFitWord(r, "this\nis\na\ntest\nstring that is very long", 100, basicTextStyle)
testutil.AssertLen(t, output, 8)
2016-07-29 21:24:25 -04:00
}
func TestTextWrapRune(t *testing.T) {
// replaced new assertions helper
2016-07-29 21:24:25 -04:00
r, err := PNG(1024, 1024)
testutil.AssertNil(t, err)
2016-07-29 21:24:25 -04:00
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
2016-07-29 21:24:25 -04:00
basicTextStyle := Style{Font: f, FontSize: 24}
output := Text.WrapFitRune(r, "this is a test string", 150, basicTextStyle)
testutil.AssertNotEmpty(t, output)
testutil.AssertLen(t, output, 2)
testutil.AssertEqual(t, "this is a t", output[0])
testutil.AssertEqual(t, "est string", output[1])
2016-07-29 19:36:29 -04:00
}