test_text.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # encoding: utf-8
  2. """Tests for IPython.utils.text"""
  3. from __future__ import print_function
  4. #-----------------------------------------------------------------------------
  5. # Copyright (C) 2011 The IPython Development Team
  6. #
  7. # Distributed under the terms of the BSD License. The full license is in
  8. # the file COPYING, distributed as part of this software.
  9. #-----------------------------------------------------------------------------
  10. #-----------------------------------------------------------------------------
  11. # Imports
  12. #-----------------------------------------------------------------------------
  13. import os
  14. import math
  15. import random
  16. import sys
  17. import nose.tools as nt
  18. try:
  19. from pathlib import Path
  20. except ImportError:
  21. # Python 2 backport
  22. from pathlib2 import Path
  23. from IPython.utils import text
  24. #-----------------------------------------------------------------------------
  25. # Globals
  26. #-----------------------------------------------------------------------------
  27. def test_columnize():
  28. """Basic columnize tests."""
  29. size = 5
  30. items = [l*size for l in 'abcd']
  31. out = text.columnize(items, displaywidth=80)
  32. nt.assert_equal(out, 'aaaaa bbbbb ccccc ddddd\n')
  33. out = text.columnize(items, displaywidth=25)
  34. nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n')
  35. out = text.columnize(items, displaywidth=12)
  36. nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n')
  37. out = text.columnize(items, displaywidth=10)
  38. nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n')
  39. out = text.columnize(items, row_first=True, displaywidth=80)
  40. nt.assert_equal(out, 'aaaaa bbbbb ccccc ddddd\n')
  41. out = text.columnize(items, row_first=True, displaywidth=25)
  42. nt.assert_equal(out, 'aaaaa bbbbb\nccccc ddddd\n')
  43. out = text.columnize(items, row_first=True, displaywidth=12)
  44. nt.assert_equal(out, 'aaaaa bbbbb\nccccc ddddd\n')
  45. out = text.columnize(items, row_first=True, displaywidth=10)
  46. nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n')
  47. out = text.columnize(items, displaywidth=40, spread=True)
  48. nt.assert_equal(out, 'aaaaa bbbbb ccccc ddddd\n')
  49. out = text.columnize(items, displaywidth=20, spread=True)
  50. nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n')
  51. out = text.columnize(items, displaywidth=12, spread=True)
  52. nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n')
  53. out = text.columnize(items, displaywidth=10, spread=True)
  54. nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n')
  55. def test_columnize_random():
  56. """Test with random input to hopfully catch edge case """
  57. for row_first in [True, False]:
  58. for nitems in [random.randint(2,70) for i in range(2,20)]:
  59. displaywidth = random.randint(20,200)
  60. rand_len = [random.randint(2,displaywidth) for i in range(nitems)]
  61. items = ['x'*l for l in rand_len]
  62. out = text.columnize(items, row_first=row_first, displaywidth=displaywidth)
  63. longer_line = max([len(x) for x in out.split('\n')])
  64. longer_element = max(rand_len)
  65. if longer_line > displaywidth:
  66. print("Columnize displayed something lager than displaywidth : %s " % longer_line)
  67. print("longer element : %s " % longer_element)
  68. print("displaywidth : %s " % displaywidth)
  69. print("number of element : %s " % nitems)
  70. print("size of each element :\n %s" % rand_len)
  71. assert False, "row_first={0}".format(row_first)
  72. def test_columnize_medium():
  73. """Test with inputs than shouldn't be wider than 80"""
  74. size = 40
  75. items = [l*size for l in 'abc']
  76. for row_first in [True, False]:
  77. out = text.columnize(items, row_first=row_first, displaywidth=80)
  78. nt.assert_equal(out, '\n'.join(items+['']), "row_first={0}".format(row_first))
  79. def test_columnize_long():
  80. """Test columnize with inputs longer than the display window"""
  81. size = 11
  82. items = [l*size for l in 'abc']
  83. for row_first in [True, False]:
  84. out = text.columnize(items, row_first=row_first, displaywidth=size-1)
  85. nt.assert_equal(out, '\n'.join(items+['']), "row_first={0}".format(row_first))
  86. def eval_formatter_check(f):
  87. ns = dict(n=12, pi=math.pi, stuff='hello there', os=os, u=u"café", b="café")
  88. s = f.format("{n} {n//4} {stuff.split()[0]}", **ns)
  89. nt.assert_equal(s, "12 3 hello")
  90. s = f.format(' '.join(['{n//%i}'%i for i in range(1,8)]), **ns)
  91. nt.assert_equal(s, "12 6 4 3 2 2 1")
  92. s = f.format('{[n//i for i in range(1,8)]}', **ns)
  93. nt.assert_equal(s, "[12, 6, 4, 3, 2, 2, 1]")
  94. s = f.format("{stuff!s}", **ns)
  95. nt.assert_equal(s, ns['stuff'])
  96. s = f.format("{stuff!r}", **ns)
  97. nt.assert_equal(s, repr(ns['stuff']))
  98. # Check with unicode:
  99. s = f.format("{u}", **ns)
  100. nt.assert_equal(s, ns['u'])
  101. # This decodes in a platform dependent manner, but it shouldn't error out
  102. s = f.format("{b}", **ns)
  103. nt.assert_raises(NameError, f.format, '{dne}', **ns)
  104. def eval_formatter_slicing_check(f):
  105. ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
  106. s = f.format(" {stuff.split()[:]} ", **ns)
  107. nt.assert_equal(s, " ['hello', 'there'] ")
  108. s = f.format(" {stuff.split()[::-1]} ", **ns)
  109. nt.assert_equal(s, " ['there', 'hello'] ")
  110. s = f.format("{stuff[::2]}", **ns)
  111. nt.assert_equal(s, ns['stuff'][::2])
  112. nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns)
  113. def eval_formatter_no_slicing_check(f):
  114. ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
  115. s = f.format('{n:x} {pi**2:+f}', **ns)
  116. nt.assert_equal(s, "c +9.869604")
  117. s = f.format('{stuff[slice(1,4)]}', **ns)
  118. nt.assert_equal(s, 'ell')
  119. if sys.version_info >= (3, 4):
  120. # String formatting has changed in Python 3.4, so this now works.
  121. s = f.format("{a[:]}", a=[1, 2])
  122. nt.assert_equal(s, "[1, 2]")
  123. else:
  124. nt.assert_raises(SyntaxError, f.format, "{a[:]}")
  125. def test_eval_formatter():
  126. f = text.EvalFormatter()
  127. eval_formatter_check(f)
  128. eval_formatter_no_slicing_check(f)
  129. def test_full_eval_formatter():
  130. f = text.FullEvalFormatter()
  131. eval_formatter_check(f)
  132. eval_formatter_slicing_check(f)
  133. def test_dollar_formatter():
  134. f = text.DollarFormatter()
  135. eval_formatter_check(f)
  136. eval_formatter_slicing_check(f)
  137. ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
  138. s = f.format("$n", **ns)
  139. nt.assert_equal(s, "12")
  140. s = f.format("$n.real", **ns)
  141. nt.assert_equal(s, "12")
  142. s = f.format("$n/{stuff[:5]}", **ns)
  143. nt.assert_equal(s, "12/hello")
  144. s = f.format("$n $$HOME", **ns)
  145. nt.assert_equal(s, "12 $HOME")
  146. s = f.format("${foo}", foo="HOME")
  147. nt.assert_equal(s, "$HOME")
  148. def test_long_substr():
  149. data = ['hi']
  150. nt.assert_equal(text.long_substr(data), 'hi')
  151. def test_long_substr2():
  152. data = ['abc', 'abd', 'abf', 'ab']
  153. nt.assert_equal(text.long_substr(data), 'ab')
  154. def test_long_substr_empty():
  155. data = []
  156. nt.assert_equal(text.long_substr(data), '')
  157. def test_strip_email():
  158. src = """\
  159. >> >>> def f(x):
  160. >> ... return x+1
  161. >> ...
  162. >> >>> zz = f(2.5)"""
  163. cln = """\
  164. >>> def f(x):
  165. ... return x+1
  166. ...
  167. >>> zz = f(2.5)"""
  168. nt.assert_equal(text.strip_email_quotes(src), cln)
  169. def test_strip_email2():
  170. src = '> > > list()'
  171. cln = 'list()'
  172. nt.assert_equal(text.strip_email_quotes(src), cln)
  173. def test_LSString():
  174. lss = text.LSString("abc\ndef")
  175. nt.assert_equal(lss.l, ['abc', 'def'])
  176. nt.assert_equal(lss.s, 'abc def')
  177. lss = text.LSString(os.getcwd())
  178. nt.assert_is_instance(lss.p[0], Path)
  179. def test_SList():
  180. sl = text.SList(['a 11', 'b 1', 'a 2'])
  181. nt.assert_equal(sl.n, 'a 11\nb 1\na 2')
  182. nt.assert_equal(sl.s, 'a 11 b 1 a 2')
  183. nt.assert_equal(sl.grep(lambda x: x.startswith('a')), text.SList(['a 11', 'a 2']))
  184. nt.assert_equal(sl.fields(0), text.SList(['a', 'b', 'a']))
  185. nt.assert_equal(sl.sort(field=1, nums=True), text.SList(['b 1', 'a 2', 'a 11']))