helper_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
  2. // Use of this source code is governed by a MIT license that can
  3. // be found in the LICENSE file.
  4. package termui
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestStr2Rune(t *testing.T) {
  10. s := "你好,世界."
  11. rs := str2runes(s)
  12. if len(rs) != 6 {
  13. t.Error(t)
  14. }
  15. }
  16. func TestWidth(t *testing.T) {
  17. s0 := "つのだ☆HIRO"
  18. s1 := "11111111111"
  19. // above not align for setting East Asian Ambiguous to wide!!
  20. if strWidth(s0) != strWidth(s1) {
  21. t.Error("str len failed")
  22. }
  23. len1 := []rune{'a', '2', '&', '「', 'オ', '。'} //will false: 'ᆵ', 'ᄚ', 'ᄒ'
  24. for _, v := range len1 {
  25. if charWidth(v) != 1 {
  26. t.Error("len1 failed")
  27. }
  28. }
  29. len2 := []rune{'漢', '字', '한', '자', '你', '好', 'だ', '。', '%', 's', 'E', 'ョ', '、', 'ヲ'}
  30. for _, v := range len2 {
  31. if charWidth(v) != 2 {
  32. t.Error("len2 failed")
  33. }
  34. }
  35. }
  36. func TestTrim(t *testing.T) {
  37. s := "つのだ☆HIRO"
  38. if string(trimStr2Runes(s, 10)) != "つのだ☆HI"+dot {
  39. t.Error("trim failed")
  40. }
  41. if string(trimStr2Runes(s, 11)) != "つのだ☆HIRO" {
  42. t.Error("avoid tail trim failed")
  43. }
  44. if string(trimStr2Runes(s, 15)) != "つのだ☆HIRO" {
  45. t.Error("avoid trim failed")
  46. }
  47. }
  48. func TestTrimStrIfAppropriate_NoTrim(t *testing.T) {
  49. assert.Equal(t, "hello", TrimStrIfAppropriate("hello", 5))
  50. }
  51. func TestTrimStrIfAppropriate(t *testing.T) {
  52. assert.Equal(t, "hel…", TrimStrIfAppropriate("hello", 4))
  53. assert.Equal(t, "h…", TrimStrIfAppropriate("hello", 2))
  54. }
  55. func TestStringToAttribute(t *testing.T) {
  56. assert.Equal(t, ColorRed, StringToAttribute("ReD"))
  57. assert.Equal(t, ColorRed|AttrBold, StringToAttribute("RED, bold"))
  58. }