test_tools.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # encoding: utf-8
  2. """
  3. Tests for testing.tools
  4. """
  5. #-----------------------------------------------------------------------------
  6. # Copyright (C) 2008-2011 The IPython Development Team
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. from __future__ import with_statement
  15. from __future__ import print_function
  16. import os
  17. import unittest
  18. import nose.tools as nt
  19. from IPython.testing import decorators as dec
  20. from IPython.testing import tools as tt
  21. #-----------------------------------------------------------------------------
  22. # Tests
  23. #-----------------------------------------------------------------------------
  24. @dec.skip_win32
  25. def test_full_path_posix():
  26. spath = '/foo/bar.py'
  27. result = tt.full_path(spath,['a.txt','b.txt'])
  28. nt.assert_equal(result, ['/foo/a.txt', '/foo/b.txt'])
  29. spath = '/foo'
  30. result = tt.full_path(spath,['a.txt','b.txt'])
  31. nt.assert_equal(result, ['/a.txt', '/b.txt'])
  32. result = tt.full_path(spath,'a.txt')
  33. nt.assert_equal(result, ['/a.txt'])
  34. @dec.skip_if_not_win32
  35. def test_full_path_win32():
  36. spath = 'c:\\foo\\bar.py'
  37. result = tt.full_path(spath,['a.txt','b.txt'])
  38. nt.assert_equal(result, ['c:\\foo\\a.txt', 'c:\\foo\\b.txt'])
  39. spath = 'c:\\foo'
  40. result = tt.full_path(spath,['a.txt','b.txt'])
  41. nt.assert_equal(result, ['c:\\a.txt', 'c:\\b.txt'])
  42. result = tt.full_path(spath,'a.txt')
  43. nt.assert_equal(result, ['c:\\a.txt'])
  44. def test_parser():
  45. err = ("FAILED (errors=1)", 1, 0)
  46. fail = ("FAILED (failures=1)", 0, 1)
  47. both = ("FAILED (errors=1, failures=1)", 1, 1)
  48. for txt, nerr, nfail in [err, fail, both]:
  49. nerr1, nfail1 = tt.parse_test_output(txt)
  50. nt.assert_equal(nerr, nerr1)
  51. nt.assert_equal(nfail, nfail1)
  52. def test_temp_pyfile():
  53. src = 'pass\n'
  54. fname, fh = tt.temp_pyfile(src)
  55. assert os.path.isfile(fname)
  56. fh.close()
  57. with open(fname) as fh2:
  58. src2 = fh2.read()
  59. nt.assert_equal(src2, src)
  60. class TestAssertPrints(unittest.TestCase):
  61. def test_passing(self):
  62. with tt.AssertPrints("abc"):
  63. print("abcd")
  64. print("def")
  65. print(b"ghi")
  66. def test_failing(self):
  67. def func():
  68. with tt.AssertPrints("abc"):
  69. print("acd")
  70. print("def")
  71. print(b"ghi")
  72. self.assertRaises(AssertionError, func)
  73. class Test_ipexec_validate(unittest.TestCase, tt.TempFileMixin):
  74. def test_main_path(self):
  75. """Test with only stdout results.
  76. """
  77. self.mktmp("print('A')\n"
  78. "print('B')\n"
  79. )
  80. out = "A\nB"
  81. tt.ipexec_validate(self.fname, out)
  82. def test_main_path2(self):
  83. """Test with only stdout results, expecting windows line endings.
  84. """
  85. self.mktmp("print('A')\n"
  86. "print('B')\n"
  87. )
  88. out = "A\r\nB"
  89. tt.ipexec_validate(self.fname, out)
  90. def test_exception_path(self):
  91. """Test exception path in exception_validate.
  92. """
  93. self.mktmp("from __future__ import print_function\n"
  94. "import sys\n"
  95. "print('A')\n"
  96. "print('B')\n"
  97. "print('C', file=sys.stderr)\n"
  98. "print('D', file=sys.stderr)\n"
  99. )
  100. out = "A\nB"
  101. tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\nD")
  102. def test_exception_path2(self):
  103. """Test exception path in exception_validate, expecting windows line endings.
  104. """
  105. self.mktmp("from __future__ import print_function\n"
  106. "import sys\n"
  107. "print('A')\n"
  108. "print('B')\n"
  109. "print('C', file=sys.stderr)\n"
  110. "print('D', file=sys.stderr)\n"
  111. )
  112. out = "A\r\nB"
  113. tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\r\nD")
  114. def tearDown(self):
  115. # tear down correctly the mixin,
  116. # unittest.TestCase.tearDown does nothing
  117. tt.TempFileMixin.tearDown(self)