2015-03-20 14:21:50 -06:00
|
|
|
|
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
2015-02-03 12:13:51 -07:00
|
|
|
|
package termui
|
|
|
|
|
|
2015-03-25 16:04:15 -06:00
|
|
|
|
import (
|
|
|
|
|
"testing"
|
2015-04-01 15:40:22 -06:00
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-03-25 16:04:15 -06:00
|
|
|
|
)
|
2015-02-03 12:13:51 -07:00
|
|
|
|
|
|
|
|
|
func TestStr2Rune(t *testing.T) {
|
|
|
|
|
s := "你好,世界."
|
|
|
|
|
rs := str2runes(s)
|
|
|
|
|
if len(rs) != 6 {
|
2015-04-01 14:25:00 -06:00
|
|
|
|
t.Error(t)
|
2015-02-03 12:13:51 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-25 16:04:15 -06:00
|
|
|
|
|
|
|
|
|
func TestWidth(t *testing.T) {
|
|
|
|
|
s0 := "つのだ☆HIRO"
|
|
|
|
|
s1 := "11111111111"
|
|
|
|
|
// above not align for setting East Asian Ambiguous to wide!!
|
|
|
|
|
|
|
|
|
|
if strWidth(s0) != strWidth(s1) {
|
|
|
|
|
t.Error("str len failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len1 := []rune{'a', '2', '&', '「', 'オ', '。'} //will false: 'ᆵ', 'ᄚ', 'ᄒ'
|
|
|
|
|
for _, v := range len1 {
|
|
|
|
|
if charWidth(v) != 1 {
|
|
|
|
|
t.Error("len1 failed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len2 := []rune{'漢', '字', '한', '자', '你', '好', 'だ', '。', '%', 's', 'E', 'ョ', '、', 'ヲ'}
|
|
|
|
|
for _, v := range len2 {
|
|
|
|
|
if charWidth(v) != 2 {
|
|
|
|
|
t.Error("len2 failed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTrim(t *testing.T) {
|
|
|
|
|
s := "つのだ☆HIRO"
|
|
|
|
|
if string(trimStr2Runes(s, 10)) != "つのだ☆HI"+dot {
|
|
|
|
|
t.Error("trim failed")
|
|
|
|
|
}
|
|
|
|
|
if string(trimStr2Runes(s, 11)) != "つのだ☆HIRO" {
|
|
|
|
|
t.Error("avoid tail trim failed")
|
|
|
|
|
}
|
|
|
|
|
if string(trimStr2Runes(s, 15)) != "つのだ☆HIRO" {
|
|
|
|
|
t.Error("avoid trim failed")
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-01 14:25:00 -06:00
|
|
|
|
|
|
|
|
|
func TestTrimStrIfAppropriate_NoTrim(t *testing.T) {
|
2015-04-01 15:40:22 -06:00
|
|
|
|
assert.Equal(t, "hello", TrimStrIfAppropriate("hello", 5))
|
2015-04-01 14:25:00 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTrimStrIfAppropriate(t *testing.T) {
|
2015-04-01 15:40:22 -06:00
|
|
|
|
assert.Equal(t, "hel…", TrimStrIfAppropriate("hello", 4))
|
|
|
|
|
assert.Equal(t, "h…", TrimStrIfAppropriate("hello", 2))
|
2015-04-01 14:25:00 -06:00
|
|
|
|
}
|