2016-07-29 19:36:29 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
"github.com/wcharczuk/go-chart/v2/testutil"
|
2016-07-29 19:36:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTextWrapWord(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-29 19:36:29 -04:00
|
|
|
|
|
|
|
r, err := PNG(1024, 1024)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNil(t, err)
|
2016-07-29 19:36:29 -04:00
|
|
|
f, err := GetDefaultFont()
|
2020-11-22 19:45:10 -05:00
|
|
|
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)
|
2020-11-22 19:45:10 -05:00
|
|
|
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)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertTrue(t, lineBox.Width() < 100, line+": "+lineBox.String())
|
2016-07-29 19:36:29 -04:00
|
|
|
}
|
2020-11-22 19:45:10 -05: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)
|
2020-11-22 19:45:10 -05:00
|
|
|
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)
|
2020-11-22 19:45:10 -05:00
|
|
|
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)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertLen(t, output, 8)
|
2016-07-29 21:24:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTextWrapRune(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-29 21:24:25 -04:00
|
|
|
|
|
|
|
r, err := PNG(1024, 1024)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNil(t, err)
|
2016-07-29 21:24:25 -04:00
|
|
|
f, err := GetDefaultFont()
|
2020-11-22 19:45:10 -05:00
|
|
|
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)
|
2020-11-22 19:45:10 -05:00
|
|
|
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
|
|
|
}
|