test_strutils.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. import re
  3. import uuid
  4. from unittest import TestCase
  5. from boltons import strutils
  6. def test_strip_ansi():
  7. assert strutils.strip_ansi(
  8. '\x1b[0m\x1b[1;36mart\x1b[46;34m\xdc') == 'art\xdc'
  9. assert strutils.strip_ansi(
  10. u'\x1b[0m\x1b[1;36mart\x1b[46;34m\xdc') == u'artÜ'
  11. assert strutils.strip_ansi(
  12. u'╒══════╕\n│ \x1b[1mCell\x1b[0m │\n╘══════╛') == (
  13. u'╒══════╕\n'
  14. u'│ Cell │\n'
  15. u'╘══════╛')
  16. assert strutils.strip_ansi(
  17. u'ls\r\n\x1B[00m\x1b[01;31mfile.zip\x1b[00m\r\n\x1b[01;31m') == \
  18. u'ls\r\nfile.zip\r\n'
  19. assert strutils.strip_ansi(
  20. u'\t\u001b[0;35mIP\u001b[0m\t\u001b[0;36m192.1.0.2\u001b[0m') == \
  21. u'\tIP\t192.1.0.2'
  22. assert strutils.strip_ansi(u'(╯°□°)╯︵ \x1b[1m┻━┻\x1b[0m') == (
  23. u'(╯°□°)╯︵ ┻━┻')
  24. assert strutils.strip_ansi('(╯°□°)╯︵ \x1b[1m┻━┻\x1b[0m') == (
  25. '(╯°□°)╯︵ ┻━┻')
  26. assert strutils.strip_ansi(
  27. b'(\xe2\x95\xaf\xc2\xb0\xe2\x96\xa1\xc2\xb0)\xe2\x95\xaf\xef\xb8'
  28. b'\xb5 \x1b[1m\xe2\x94\xbb\xe2\x94\x81\xe2\x94\xbb\x1b[0m') == (
  29. b'(\xe2\x95\xaf\xc2\xb0\xe2\x96\xa1\xc2\xb0)\xe2\x95\xaf\xef\xb8'
  30. b'\xb5 \xe2\x94\xbb\xe2\x94\x81\xe2\x94\xbb')
  31. assert strutils.strip_ansi(
  32. bytearray(u'(╯°□°)╯︵ \x1b[1m┻━┻\x1b[0m', 'utf-8')) == \
  33. bytearray(
  34. b'(\xe2\x95\xaf\xc2\xb0\xe2\x96\xa1\xc2\xb0)\xe2\x95\xaf\xef\xb8'
  35. b'\xb5 \xe2\x94\xbb\xe2\x94\x81\xe2\x94\xbb')
  36. def test_asciify():
  37. ref = u'Beyoncé'
  38. b = strutils.asciify(ref)
  39. assert len(b) == len(b)
  40. assert b[-1:].decode('ascii') == 'e'
  41. def test_indent():
  42. to_indent = '\nabc\ndef\n\nxyz\n'
  43. ref = '\n abc\n def\n\n xyz\n'
  44. assert strutils.indent(to_indent, ' ') == ref
  45. def test_is_uuid():
  46. assert strutils.is_uuid(uuid.uuid4()) == True
  47. assert strutils.is_uuid(uuid.uuid4(), version=1) == False
  48. assert strutils.is_uuid(str(uuid.uuid4())) == True
  49. assert strutils.is_uuid(str(uuid.uuid4()), version=1) == False
  50. assert strutils.is_uuid(set('garbage')) == False
  51. def test_parse_int_list():
  52. assert strutils.parse_int_list("1,3,5-8,10-11,15") == [1, 3, 5, 6, 7, 8, 10, 11, 15]
  53. assert strutils.parse_int_list("1,3,5-8,10-11,15,") == [1, 3, 5, 6, 7, 8, 10, 11, 15]
  54. assert strutils.parse_int_list(",1,3,5-8,10-11,15") == [1, 3, 5, 6, 7, 8, 10, 11, 15]
  55. assert strutils.parse_int_list(" 1, 3 ,5-8,10-11,15 ") == [1, 3, 5, 6, 7, 8, 10, 11, 15]
  56. assert strutils.parse_int_list("3,1,5-8,10-11,15") == [1, 3, 5, 6, 7, 8, 10, 11, 15]
  57. assert strutils.parse_int_list("5-8") == [5, 6, 7, 8]
  58. assert strutils.parse_int_list("8-5") == [5, 6, 7, 8]
  59. def test_format_int_list():
  60. assert strutils.format_int_list([1, 3, 5, 6, 7, 8, 10, 11, 15]) == '1,3,5-8,10-11,15'
  61. assert strutils.format_int_list([5, 6, 7, 8]) == '5-8'
  62. assert strutils.format_int_list([1, 3, 5, 6, 7, 8, 10, 11, 15], delim_space=True) == '1, 3, 5-8, 10-11, 15'
  63. assert strutils.format_int_list([5, 6, 7, 8], delim_space=True) == '5-8'
  64. class TestMultiReplace(TestCase):
  65. def test_simple_substitutions(self):
  66. """Test replacing multiple values."""
  67. m = strutils.MultiReplace({r'cat': 'kedi', r'purple': 'mor', })
  68. self.assertEqual(m.sub('The cat is purple'), 'The kedi is mor')
  69. def test_shortcut_function(self):
  70. """Test replacing multiple values."""
  71. self.assertEqual(
  72. strutils.multi_replace(
  73. 'The cat is purple',
  74. {r'cat': 'kedi', r'purple': 'mor', }
  75. ),
  76. 'The kedi is mor'
  77. )
  78. def test_substitutions_in_word(self):
  79. """Test replacing multiple values that are substrings of a word."""
  80. m = strutils.MultiReplace({r'cat': 'kedi', r'purple': 'mor', })
  81. self.assertEqual(m.sub('Thecatispurple'), 'Thekediismor')
  82. def test_sub_with_regex(self):
  83. """Test substitutions with a regular expression."""
  84. m = strutils.MultiReplace({
  85. r'cat': 'kedi',
  86. r'purple': 'mor',
  87. r'q\w+?t': 'dinglehopper'
  88. }, regex=True)
  89. self.assertEqual(
  90. m.sub('The purple cat ate a quart of jelly'),
  91. 'The mor kedi ate a dinglehopper of jelly'
  92. )
  93. def test_sub_with_list(self):
  94. """Test substitutions from an iterable instead of a dictionary."""
  95. m = strutils.MultiReplace([
  96. (r'cat', 'kedi'),
  97. (r'purple', 'mor'),
  98. (r'q\w+?t', 'dinglehopper'),
  99. ], regex=True)
  100. self.assertEqual(
  101. m.sub('The purple cat ate a quart of jelly'),
  102. 'The mor kedi ate a dinglehopper of jelly'
  103. )
  104. def test_sub_with_compiled_regex(self):
  105. """Test substitutions where some regular expressiosn are compiled."""
  106. exp = re.compile(r'q\w+?t')
  107. m = strutils.MultiReplace([
  108. (r'cat', 'kedi'),
  109. (r'purple', 'mor'),
  110. (exp, 'dinglehopper'),
  111. ])
  112. self.assertEqual(
  113. m.sub('The purple cat ate a quart of jelly'),
  114. 'The mor kedi ate a dinglehopper of jelly'
  115. )
  116. def test_substitutions_with_regex_chars(self):
  117. """Test replacing values that have special regex characters."""
  118. m = strutils.MultiReplace({'cat.+': 'kedi', r'purple': 'mor', })
  119. self.assertEqual(m.sub('The cat.+ is purple'), 'The kedi is mor')
  120. def test_roundzip():
  121. aaa = b'a' * 10000
  122. assert strutils.gunzip_bytes(strutils.gzip_bytes(aaa)) == aaa
  123. assert strutils.gunzip_bytes(strutils.gzip_bytes(b'')) == b''