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