##// END OF EJS Templates
improved behavior of FileLinks.__repr__ to be more like the behvior of _repr_html_
improved behavior of FileLinks.__repr__ to be more like the behvior of _repr_html_

File last commit:

r8440:c726188a
r8443:9585fef3
Show More
test_display.py
97 lines | 3.4 KiB | text/x-python | PythonLexer
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366 """Tests for IPython.lib.display.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2012, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function
Greg Caporaso
added tests of formatting functionality
r8369 from tempfile import NamedTemporaryFile, mkdtemp
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366
# Third-party imports
import nose.tools as nt
# Our own imports
from IPython.lib import display
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
#--------------------------
# FileLink tests
#--------------------------
def test_instantiation_FileLink():
"""Test classes can be instantiated"""
fl = display.FileLink('example.txt')
def test_warning_on_non_existant_path_FileLink():
"""Calling _repr_html_ on non-existant files returns a warning"""
fl = display.FileLink('example.txt')
nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
Greg Caporaso
added tests of formatting functionality
r8369 def test_existing_path_FileLink():
""" Calling _repr_html_ functions as expected on existing filepath """
tf = NamedTemporaryFile()
fl = display.FileLink(tf.name)
actual = fl._repr_html_()
Greg Caporaso
modified to join urls with '/'.join instead of os.path.join. @takluyver pointed out that while os.path.join does work for this on OS X and Linux, it's not meant for constructing URLs, so fails on Windows where paths are constructed using \ rather than /.
r8440 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
Greg Caporaso
added tests of formatting functionality
r8369 nt.assert_equal(actual,expected)
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366 #--------------------------
# FileLinks tests
#--------------------------
def test_instantiation_FileLinks():
"""Test classes can be instantiated"""
Greg Caporaso
added tests of formatting functionality
r8369 fls = display.FileLinks('example')
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366
def test_warning_on_non_existant_path_FileLinks():
"""Calling _repr_html_ on non-existant files returns a warning"""
fls = display.FileLinks('example')
nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
Greg Caporaso
added tests of formatting functionality
r8369 def test_existing_path_FileLinks():
""" Calling _repr_html_ functions as expected on existing directory """
td = mkdtemp()
tf1 = NamedTemporaryFile(dir=td)
tf2 = NamedTemporaryFile(dir=td)
fl = display.FileLinks(td)
actual = fl._repr_html_()
actual = actual.split('\n')
actual.sort()
Greg Caporaso
modified to join urls with '/'.join instead of os.path.join. @takluyver pointed out that while os.path.join does work for this on OS X and Linux, it's not meant for constructing URLs, so fails on Windows where paths are constructed using \ rather than /.
r8440 expected = ["<a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,tf2.name),
"<a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,tf1.name)]
Greg Caporaso
added tests of formatting functionality
r8369 expected.sort()
# We compare the sorted list of links here as that's more reliable
nt.assert_equal(actual,expected)
Greg Caporaso
added test file for the display module, beginning with basic tests of the FileLink, FileLinks, and DirectoryLink classes
r8366 #--------------------------
# DirectoryLink tests
#--------------------------
def test_instantiation_DirectoryLink():
"""Test classes can be instantiated"""
dl = display.DirectoryLink('example')
def test_warning_on_non_existant_path_DirectoryLink():
"""Calling _repr_html_ on non-existant files returns a warning"""
dl = display.DirectoryLink('example')
nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)'))
Greg Caporaso
added tests of formatting functionality
r8369
def test_existing_path_FileLinks():
""" Calling _repr_html_ functions as expected on existing directory """
td = mkdtemp()
dl = display.DirectoryLink(td)
actual = dl._repr_html_()
Greg Caporaso
modified to join urls with '/'.join instead of os.path.join. @takluyver pointed out that while os.path.join does work for this on OS X and Linux, it's not meant for constructing URLs, so fails on Windows where paths are constructed using \ rather than /.
r8440 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (td,td)
Greg Caporaso
added tests of formatting functionality
r8369 nt.assert_equal(actual,expected)