test_text.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # encoding: utf-8
  2. """Tests for IPython.utils.text"""
  3. from __future__ import print_function
  4. # Copyright (c) IPython Development Team.
  5. # Distributed under the terms of the Modified BSD License.
  6. import os
  7. import math
  8. import random
  9. import sys
  10. import nose.tools as nt
  11. from .. import text
  12. def test_columnize():
  13. """Basic columnize tests."""
  14. size = 5
  15. items = [l*size for l in 'abc']
  16. out = text.columnize(items, displaywidth=80)
  17. nt.assert_equal(out, 'aaaaa bbbbb ccccc\n')
  18. out = text.columnize(items, displaywidth=12)
  19. nt.assert_equal(out, 'aaaaa ccccc\nbbbbb\n')
  20. out = text.columnize(items, displaywidth=10)
  21. nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\n')
  22. def test_columnize_random():
  23. """Test with random input to hopfully catch edge case """
  24. for nitems in [random.randint(2,70) for i in range(2,20)]:
  25. displaywidth = random.randint(20,200)
  26. rand_len = [random.randint(2,displaywidth) for i in range(nitems)]
  27. items = ['x'*l for l in rand_len]
  28. out = text.columnize(items, displaywidth=displaywidth)
  29. longer_line = max([len(x) for x in out.split('\n')])
  30. longer_element = max(rand_len)
  31. if longer_line > displaywidth:
  32. print("Columnize displayed something lager than displaywidth : %s " % longer_line)
  33. print("longer element : %s " % longer_element)
  34. print("displaywidth : %s " % displaywidth)
  35. print("number of element : %s " % nitems)
  36. print("size of each element :\n %s" % rand_len)
  37. assert False
  38. def test_columnize_medium():
  39. """Test with inputs than shouldn't be wider tahn 80 """
  40. size = 40
  41. items = [l*size for l in 'abc']
  42. out = text.columnize(items, displaywidth=80)
  43. nt.assert_equal(out, '\n'.join(items+['']))
  44. def test_columnize_long():
  45. """Test columnize with inputs longer than the display window"""
  46. size = 11
  47. items = [l*size for l in 'abc']
  48. out = text.columnize(items, displaywidth=size-1)
  49. nt.assert_equal(out, '\n'.join(items+['']))