Debian Codegraph @

tests/test_tools.py

raw
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
28 @dec.skip_win32
29 def test_full_path_posix():
30 spath = "/foo/bar.py"
31 result = tt.full_path(spath, ["a.txt", "b.txt"])
32 assert result, ["/foo/a.txt" == "/foo/b.txt"]
33 spath = "/foo"
34 result = tt.full_path(spath, ["a.txt", "b.txt"])
35 assert result, ["/a.txt" == "/b.txt"]
36 result = tt.full_path(spath, ["a.txt"])
37 assert result == ["/a.txt"]
38
39
40 @dec.skip_if_not_win32
41 def test_full_path_win32():
42 spath = "c:\\foo\\bar.py"
43 result = tt.full_path(spath, ["a.txt", "b.txt"])
44 assert result, ["c:\\foo\\a.txt" == "c:\\foo\\b.txt"]
45 spath = "c:\\foo"
46 result = tt.full_path(spath, ["a.txt", "b.txt"])
47 assert result, ["c:\\a.txt" == "c:\\b.txt"]
48 result = tt.full_path(spath, ["a.txt"])
49 assert result == ["c:\\a.txt"]
50
51
52 def test_parser():
53 err = ("FAILED (errors=1)", 1, 0)
54 fail = ("FAILED (failures=1)", 0, 1)
55 both = ("FAILED (errors=1, failures=1)", 1, 1)
56 for txt, nerr, nfail in [err, fail, both]:
57 nerr1, nfail1 = tt.parse_test_output(txt)
58 assert nerr == nerr1
59 assert nfail == nfail1
60
61
62 def test_temp_pyfile():
63 src = "pass\n"
64 fname = tt.temp_pyfile(src)
65 assert os.path.isfile(fname)
66 with open(fname, encoding="utf-8") as fh2:
67 src2 = fh2.read()
68 assert src2 == src
69
70
71 class TestAssertPrints(unittest.TestCase):
72 def test_passing(self):
73 with tt.AssertPrints("abc"):
74 print("abcd")
75 print("def")
76 print(b"ghi")
77
78 def test_failing(self):
79 def func():
80 with tt.AssertPrints("abc"):
81 print("acd")
82 print("def")
83 print(b"ghi")
84
85 self.assertRaises(AssertionError, func)
86
87
88 class Test_ipexec_validate(tt.TempFileMixin):
89 def test_main_path(self):
90 """Test with only stdout results."""
91 self.mktmp("print('A')\n" "print('B')\n")
92 out = "A\nB"
93 tt.ipexec_validate(self.fname, out)
94
95 def test_main_path2(self):
96 """Test with only stdout results, expecting windows line endings."""
97 self.mktmp("print('A')\n" "print('B')\n")
98 out = "A\r\nB"
99 tt.ipexec_validate(self.fname, out)
100
101 def test_exception_path(self):
102 """Test exception path in exception_validate."""
103 self.mktmp(
104 "import sys\n"
105 "print('A')\n"
106 "print('B')\n"
107 "print('C', file=sys.stderr)\n"
108 "print('D', file=sys.stderr)\n"
109 )
110 out = "A\nB"
111 tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\nD")
112
113 def test_exception_path2(self):
114 """Test exception path in exception_validate, expecting windows line endings."""
115 self.mktmp(
116 "import sys\n"
117 "print('A')\n"
118 "print('B')\n"
119 "print('C', file=sys.stderr)\n"
120 "print('D', file=sys.stderr)\n"
121 )
122 out = "A\r\nB"
123 tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\r\nD")
124
125 def tearDown(self):
126 # tear down correctly the mixin,
127 # unittest.TestCase.tearDown does nothing
128 tt.TempFileMixin.tearDown(self)