2016-07-29 19:36:29 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-04-05 03:42:38 -04:00
|
|
|
assert "github.com/blend/go-sdk/assert"
|
2016-07-29 19:36:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTextWrapWord(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
r, err := PNG(1024, 1024)
|
|
|
|
assert.Nil(err)
|
|
|
|
f, err := GetDefaultFont()
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
basicTextStyle := Style{Font: f, FontSize: 24}
|
|
|
|
|
|
|
|
output := Text.WrapFitWord(r, "this is a test string", 100, basicTextStyle)
|
|
|
|
assert.NotEmpty(output)
|
2018-04-05 03:47:39 -04:00
|
|
|
assert.Len(3, output)
|
2016-07-29 19:36:29 -04:00
|
|
|
|
|
|
|
for _, line := range output {
|
|
|
|
basicTextStyle.WriteToRenderer(r)
|
|
|
|
lineBox := r.MeasureText(line)
|
|
|
|
assert.True(lineBox.Width() < 100, line+": "+lineBox.String())
|
|
|
|
}
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal("this is", output[0])
|
|
|
|
assert.Equal("a test", output[1])
|
|
|
|
assert.Equal("string", output[2])
|
2016-07-29 19:36:29 -04:00
|
|
|
|
|
|
|
output = Text.WrapFitWord(r, "foo", 100, basicTextStyle)
|
2018-04-05 03:47:39 -04:00
|
|
|
assert.Len(1, output)
|
2016-07-29 19:36:29 -04:00
|
|
|
assert.Equal("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)
|
2018-04-05 03:47:39 -04:00
|
|
|
assert.Len(5, output)
|
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)
|
2018-04-05 03:47:39 -04:00
|
|
|
assert.Len(8, output)
|
2016-07-29 21:24:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTextWrapRune(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
r, err := PNG(1024, 1024)
|
|
|
|
assert.Nil(err)
|
|
|
|
f, err := GetDefaultFont()
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
basicTextStyle := Style{Font: f, FontSize: 24}
|
|
|
|
|
|
|
|
output := Text.WrapFitRune(r, "this is a test string", 150, basicTextStyle)
|
|
|
|
assert.NotEmpty(output)
|
2018-04-05 03:47:39 -04:00
|
|
|
assert.Len(2, output)
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal("this is a t", output[0])
|
|
|
|
assert.Equal("est string", output[1])
|
2016-07-29 19:36:29 -04:00
|
|
|
}
|