]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_print.py
1 """Test correct operation of the print function.
4 # In 2.6, this gives us the behavior we want. In 3.0, it has
5 # no function, but it still must parse correctly.
6 from __future__
import print_function
9 from test
import test_support
11 from StringIO
import StringIO
15 # A dispatch table all 8 combinations of providing
17 # I use this machinery so that I'm not just passing default
18 # values to print, I'm either passing or not passing in the
21 (False, False, False):
22 lambda args
, sep
, end
, file: print(*args
),
24 lambda args
, sep
, end
, file: print(file=file, *args
),
26 lambda args
, sep
, end
, file: print(end
=end
, *args
),
28 lambda args
, sep
, end
, file: print(end
=end
, file=file, *args
),
30 lambda args
, sep
, end
, file: print(sep
=sep
, *args
),
32 lambda args
, sep
, end
, file: print(sep
=sep
, file=file, *args
),
34 lambda args
, sep
, end
, file: print(sep
=sep
, end
=end
, *args
),
36 lambda args
, sep
, end
, file: print(sep
=sep
, end
=end
, file=file, *args
),
39 # Class used to test __str__ and print
40 class ClassWith__str__
:
41 def __init__(self
, x
):
46 class TestPrint(unittest
.TestCase
):
47 def check(self
, expected
, args
,
48 sep
=NotDefined
, end
=NotDefined
, file=NotDefined
):
49 # Capture sys.stdout in a StringIO. Call print with args,
50 # and with sep, end, and file, if they're defined. Result
51 # must match expected.
53 # Look up the actual function to call, based on if sep, end, and file
55 fn
= dispatch
[(sep
is not NotDefined
,
56 end
is not NotDefined
,
57 file is not NotDefined
)]
59 with test_support
.captured_stdout() as t
:
60 fn(args
, sep
, end
, file)
62 self
.assertEqual(t
.getvalue(), expected
)
65 def x(expected
, args
, sep
=NotDefined
, end
=NotDefined
):
66 # Run the test 2 ways: not using file, and using
67 # file directed to a StringIO
69 self
.check(expected
, args
, sep
=sep
, end
=end
)
71 # When writing to a file, stdout is expected to be empty
73 self
.check('', args
, sep
=sep
, end
=end
, file=o
)
75 # And o will contain the expected output
76 self
.assertEqual(o
.getvalue(), expected
)
82 x('1 2\n', (1, ' ', 2))
83 x('1*2\n', (1, 2), sep
='*')
84 x('1 s', (1, 's'), end
='')
85 x('a\nb\n', ('a', 'b'), sep
='\n')
86 x('1.01', (1.0, 1), sep
='', end
='')
87 x('1*a*1.3+', (1, 'a', 1.3), sep
='*', end
='+')
88 x('a\n\nb\n', ('a\n', 'b'), sep
='\n')
89 x('\0+ +\0\n', ('\0', ' ', '\0'), sep
='+')
91 x('a\n b\n', ('a\n', 'b'))
92 x('a\n b\n', ('a\n', 'b'), sep
=None)
93 x('a\n b\n', ('a\n', 'b'), end
=None)
94 x('a\n b\n', ('a\n', 'b'), sep
=None, end
=None)
96 x('*\n', (ClassWith__str__('*'),))
97 x('abc 1\n', (ClassWith__str__('abc'), 1))
100 x(u
'1 2\n', ('1', u
'2'))
101 x(u
'u\1234\n', (u
'u\1234',))
102 x(u
' abc 1\n', (' ', ClassWith__str__(u
'abc'), 1))
105 self
.assertRaises(TypeError, print, '', sep
=3)
106 self
.assertRaises(TypeError, print, '', end
=3)
107 self
.assertRaises(AttributeError, print, '', file='')
109 def test_mixed_args(self
):
110 # If an unicode arg is passed, sep and end should be unicode, too.
111 class Recorder(object):
113 def __init__(self
, must_be_unicode
):
115 self
.force_unicode
= must_be_unicode
117 def write(self
, what
):
118 if self
.force_unicode
and not isinstance(what
, unicode):
119 raise AssertionError("{0!r} is not unicode".format(what
))
120 self
.buf
.append(what
)
123 print(u
'hi', file=buf
)
124 self
.assertEqual(u
''.join(buf
.buf
), 'hi\n')
126 print(u
'hi', u
'nothing', file=buf
)
127 self
.assertEqual(u
''.join(buf
.buf
), 'hi nothing\n')
128 buf
= Recorder(False)
129 print('hi', 'bye', end
=u
'\n', file=buf
)
130 self
.assertIsInstance(buf
.buf
[1], unicode)
131 self
.assertIsInstance(buf
.buf
[3], unicode)
133 print(sep
=u
'x', file=buf
)
134 self
.assertIsInstance(buf
.buf
[-1], unicode)
138 test_support
.run_unittest(TestPrint
)
140 if __name__
== "__main__":