test_display.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. """Tests for IPython.lib.display.
  2. """
  3. #-----------------------------------------------------------------------------
  4. # Copyright (c) 2012, the IPython Development Team.
  5. #
  6. # Distributed under the terms of the Modified BSD License.
  7. #
  8. # The full license is in the file COPYING.txt, distributed with this software.
  9. #-----------------------------------------------------------------------------
  10. #-----------------------------------------------------------------------------
  11. # Imports
  12. #-----------------------------------------------------------------------------
  13. from __future__ import print_function
  14. from tempfile import NamedTemporaryFile, mkdtemp
  15. from os.path import split, join as pjoin, dirname
  16. # Third-party imports
  17. import nose.tools as nt
  18. # Our own imports
  19. from IPython.lib import display
  20. from IPython.testing.decorators import skipif_not_numpy
  21. #-----------------------------------------------------------------------------
  22. # Classes and functions
  23. #-----------------------------------------------------------------------------
  24. #--------------------------
  25. # FileLink tests
  26. #--------------------------
  27. def test_instantiation_FileLink():
  28. """FileLink: Test class can be instantiated"""
  29. fl = display.FileLink('example.txt')
  30. def test_warning_on_non_existant_path_FileLink():
  31. """FileLink: Calling _repr_html_ on non-existant files returns a warning
  32. """
  33. fl = display.FileLink('example.txt')
  34. nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
  35. def test_existing_path_FileLink():
  36. """FileLink: Calling _repr_html_ functions as expected on existing filepath
  37. """
  38. tf = NamedTemporaryFile()
  39. fl = display.FileLink(tf.name)
  40. actual = fl._repr_html_()
  41. expected = "<a href='%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
  42. nt.assert_equal(actual,expected)
  43. def test_existing_path_FileLink_repr():
  44. """FileLink: Calling repr() functions as expected on existing filepath
  45. """
  46. tf = NamedTemporaryFile()
  47. fl = display.FileLink(tf.name)
  48. actual = repr(fl)
  49. expected = tf.name
  50. nt.assert_equal(actual,expected)
  51. def test_error_on_directory_to_FileLink():
  52. """FileLink: Raises error when passed directory
  53. """
  54. td = mkdtemp()
  55. nt.assert_raises(ValueError,display.FileLink,td)
  56. #--------------------------
  57. # FileLinks tests
  58. #--------------------------
  59. def test_instantiation_FileLinks():
  60. """FileLinks: Test class can be instantiated
  61. """
  62. fls = display.FileLinks('example')
  63. def test_warning_on_non_existant_path_FileLinks():
  64. """FileLinks: Calling _repr_html_ on non-existant files returns a warning
  65. """
  66. fls = display.FileLinks('example')
  67. nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
  68. def test_existing_path_FileLinks():
  69. """FileLinks: Calling _repr_html_ functions as expected on existing dir
  70. """
  71. td = mkdtemp()
  72. tf1 = NamedTemporaryFile(dir=td)
  73. tf2 = NamedTemporaryFile(dir=td)
  74. fl = display.FileLinks(td)
  75. actual = fl._repr_html_()
  76. actual = actual.split('\n')
  77. actual.sort()
  78. # the links should always have forward slashes, even on windows, so replace
  79. # backslashes with forward slashes here
  80. expected = ["%s/<br>" % td,
  81. "&nbsp;&nbsp;<a href='%s' target='_blank'>%s</a><br>" %\
  82. (tf2.name.replace("\\","/"),split(tf2.name)[1]),
  83. "&nbsp;&nbsp;<a href='%s' target='_blank'>%s</a><br>" %\
  84. (tf1.name.replace("\\","/"),split(tf1.name)[1])]
  85. expected.sort()
  86. # We compare the sorted list of links here as that's more reliable
  87. nt.assert_equal(actual,expected)
  88. def test_existing_path_FileLinks_alt_formatter():
  89. """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter
  90. """
  91. td = mkdtemp()
  92. tf1 = NamedTemporaryFile(dir=td)
  93. tf2 = NamedTemporaryFile(dir=td)
  94. def fake_formatter(dirname,fnames,included_suffixes):
  95. return ["hello","world"]
  96. fl = display.FileLinks(td,notebook_display_formatter=fake_formatter)
  97. actual = fl._repr_html_()
  98. actual = actual.split('\n')
  99. actual.sort()
  100. expected = ["hello","world"]
  101. expected.sort()
  102. # We compare the sorted list of links here as that's more reliable
  103. nt.assert_equal(actual,expected)
  104. def test_existing_path_FileLinks_repr():
  105. """FileLinks: Calling repr() functions as expected on existing directory """
  106. td = mkdtemp()
  107. tf1 = NamedTemporaryFile(dir=td)
  108. tf2 = NamedTemporaryFile(dir=td)
  109. fl = display.FileLinks(td)
  110. actual = repr(fl)
  111. actual = actual.split('\n')
  112. actual.sort()
  113. expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]]
  114. expected.sort()
  115. # We compare the sorted list of links here as that's more reliable
  116. nt.assert_equal(actual,expected)
  117. def test_existing_path_FileLinks_repr_alt_formatter():
  118. """FileLinks: Calling repr() functions as expected w/ alt formatter
  119. """
  120. td = mkdtemp()
  121. tf1 = NamedTemporaryFile(dir=td)
  122. tf2 = NamedTemporaryFile(dir=td)
  123. def fake_formatter(dirname,fnames,included_suffixes):
  124. return ["hello","world"]
  125. fl = display.FileLinks(td,terminal_display_formatter=fake_formatter)
  126. actual = repr(fl)
  127. actual = actual.split('\n')
  128. actual.sort()
  129. expected = ["hello","world"]
  130. expected.sort()
  131. # We compare the sorted list of links here as that's more reliable
  132. nt.assert_equal(actual,expected)
  133. def test_error_on_file_to_FileLinks():
  134. """FileLinks: Raises error when passed file
  135. """
  136. td = mkdtemp()
  137. tf1 = NamedTemporaryFile(dir=td)
  138. nt.assert_raises(ValueError,display.FileLinks,tf1.name)
  139. def test_recursive_FileLinks():
  140. """FileLinks: Does not recurse when recursive=False
  141. """
  142. td = mkdtemp()
  143. tf = NamedTemporaryFile(dir=td)
  144. subtd = mkdtemp(dir=td)
  145. subtf = NamedTemporaryFile(dir=subtd)
  146. fl = display.FileLinks(td)
  147. actual = str(fl)
  148. actual = actual.split('\n')
  149. nt.assert_equal(len(actual), 4, actual)
  150. fl = display.FileLinks(td, recursive=False)
  151. actual = str(fl)
  152. actual = actual.split('\n')
  153. nt.assert_equal(len(actual), 2, actual)
  154. @skipif_not_numpy
  155. def test_audio_from_file():
  156. path = pjoin(dirname(__file__), 'test.wav')
  157. display.Audio(filename=path)