test_core.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # coding: utf-8
  2. """Core tests module for wcwidth."""
  3. import wcwidth
  4. def test_hello_jp():
  5. u"""
  6. Width of Japanese phrase: コンニチハ, セカイ!
  7. Given a phrase of 5 and 3 Katakana ideographs, joined with
  8. 3 English-ASCII punctuation characters, totaling 11, this
  9. phrase consumes 19 cells of a terminal emulator.
  10. """
  11. # given,
  12. phrase = u'コンニチハ, セカイ!'
  13. expect_length_each = (2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 1)
  14. expect_length_phrase = sum(expect_length_each)
  15. # exercise,
  16. length_each = tuple(map(wcwidth.wcwidth, phrase))
  17. length_phrase = wcwidth.wcswidth(phrase)
  18. # verify,
  19. assert length_each == expect_length_each
  20. assert length_phrase == expect_length_phrase
  21. def test_wcswidth_substr():
  22. """
  23. Test wcswidth() optional 2nd parameter, ``n``.
  24. ``n`` determines at which position of the string
  25. to stop counting length.
  26. """
  27. # given,
  28. phrase = u'コンニチハ, セカイ!'
  29. end = 7
  30. expect_length_each = (2, 2, 2, 2, 2, 1, 1,)
  31. expect_length_phrase = sum(expect_length_each)
  32. # exercise,
  33. length_phrase = wcwidth.wcswidth(phrase, end)
  34. # verify,
  35. assert length_phrase == expect_length_phrase
  36. def test_null_width_0():
  37. """NULL (0) reports width 0."""
  38. # given,
  39. phrase = u'abc\x00def'
  40. expect_length_each = (1, 1, 1, 0, 1, 1, 1)
  41. expect_length_phrase = sum(expect_length_each)
  42. # exercise,
  43. length_each = tuple(map(wcwidth.wcwidth, phrase))
  44. length_phrase = wcwidth.wcswidth(phrase, len(phrase))
  45. # verify,
  46. assert length_each == expect_length_each
  47. assert length_phrase == expect_length_phrase
  48. def test_control_c0_width_negative_1():
  49. """CSI (Control sequence initiate) reports width -1."""
  50. # given,
  51. phrase = u'\x1b[0m'
  52. expect_length_each = (-1, 1, 1, 1)
  53. expect_length_phrase = -1
  54. # exercise,
  55. length_each = tuple(map(wcwidth.wcwidth, phrase))
  56. length_phrase = wcwidth.wcswidth(phrase, len(phrase))
  57. # verify,
  58. assert length_each == expect_length_each
  59. assert length_phrase == expect_length_phrase
  60. def test_combining_width_negative_1():
  61. """Simple test combining reports total width of 4."""
  62. # given,
  63. phrase = u'--\u05bf--'
  64. expect_length_each = (1, 1, 0, 1, 1)
  65. expect_length_phrase = 4
  66. # exercise,
  67. length_each = tuple(map(wcwidth.wcwidth, phrase))
  68. length_phrase = wcwidth.wcswidth(phrase, len(phrase))
  69. # verify,
  70. assert length_each == expect_length_each
  71. assert length_phrase == expect_length_phrase
  72. def test_combining_cafe():
  73. u"""Phrase cafe + COMBINING ACUTE ACCENT is café of length 4."""
  74. phrase = u"cafe\u0301"
  75. expect_length_each = (1, 1, 1, 1, 0)
  76. expect_length_phrase = 4
  77. # exercise,
  78. length_each = tuple(map(wcwidth.wcwidth, phrase))
  79. length_phrase = wcwidth.wcswidth(phrase, len(phrase))
  80. # verify,
  81. assert length_each == expect_length_each
  82. assert length_phrase == expect_length_phrase
  83. def test_combining_enclosing():
  84. u"""CYRILLIC CAPITAL LETTER A + COMBINING CYRILLIC HUNDRED THOUSANDS SIGN is А҈ of length 1."""
  85. phrase = u"\u0410\u0488"
  86. expect_length_each = (1, 0)
  87. expect_length_phrase = 1
  88. # exercise,
  89. length_each = tuple(map(wcwidth.wcwidth, phrase))
  90. length_phrase = wcwidth.wcswidth(phrase, len(phrase))
  91. # verify,
  92. assert length_each == expect_length_each
  93. assert length_phrase == expect_length_phrase
  94. def test_combining_spacing():
  95. u"""Balinese kapal (ship) is ᬓᬨᬮ᭄ of length 4."""
  96. phrase = u"\u1B13\u1B28\u1B2E\u1B44"
  97. expect_length_each = (1, 1, 1, 1)
  98. expect_length_phrase = 4
  99. # exercise,
  100. length_each = tuple(map(wcwidth.wcwidth, phrase))
  101. length_phrase = wcwidth.wcswidth(phrase, len(phrase))
  102. # verify,
  103. assert length_each == expect_length_each
  104. assert length_phrase == expect_length_phrase