| 
									
										
										
										
											2011-06-16 23:21:01 +02:00
										 |  |  | import textwrap | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from packaging.tests import unittest, support | 
					
						
							|  |  |  | from packaging.compat import Mixin2to3 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Mixin2to3TestCase(support.TempdirManager, | 
					
						
							|  |  |  |                         support.LoggingCatcher, | 
					
						
							|  |  |  |                         unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |     def setUp(self): | 
					
						
							|  |  |  |         super(Mixin2to3TestCase, self).setUp() | 
					
						
							|  |  |  |         self.filename = self.mktempfile().name | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |     def check(self, source, wanted, **kwargs): | 
					
						
							|  |  |  |         source = textwrap.dedent(source) | 
					
						
							|  |  |  |         with open(self.filename, 'w') as fp: | 
					
						
							|  |  |  |             fp.write(source) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |         Mixin2to3()._run_2to3(**kwargs) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |         wanted = textwrap.dedent(wanted) | 
					
						
							|  |  |  |         with open(self.filename) as fp: | 
					
						
							| 
									
										
										
										
											2011-06-16 23:21:01 +02:00
										 |  |  |             converted = fp.read() | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |         self.assertMultiLineEqual(converted, wanted) | 
					
						
							| 
									
										
										
										
											2011-06-16 23:21:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |     def test_conversion(self): | 
					
						
							|  |  |  |         # check that code and doctests get converted | 
					
						
							|  |  |  |         self.check('''\
 | 
					
						
							| 
									
										
										
										
											2011-06-16 23:21:01 +02:00
										 |  |  |             """Example docstring.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             >>> print test | 
					
						
							|  |  |  |             test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             It works. | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |             """
 | 
					
						
							|  |  |  |             print 'test' | 
					
						
							|  |  |  |             ''',
 | 
					
						
							|  |  |  |             '''\
 | 
					
						
							| 
									
										
										
										
											2011-06-16 23:21:01 +02:00
										 |  |  |             """Example docstring.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             >>> print(test) | 
					
						
							|  |  |  |             test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             It works. | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |             """
 | 
					
						
							|  |  |  |             print('test') | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |             ''',  # 2to3 adds a newline here
 | 
					
						
							|  |  |  |             files=[self.filename]) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |     def test_doctests_conversion(self): | 
					
						
							|  |  |  |         # check that doctest files are converted | 
					
						
							|  |  |  |         self.check('''\
 | 
					
						
							|  |  |  |             Welcome to the doc. | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |             >>> print test | 
					
						
							|  |  |  |             test | 
					
						
							|  |  |  |             ''',
 | 
					
						
							|  |  |  |             '''\
 | 
					
						
							|  |  |  |             Welcome to the doc. | 
					
						
							| 
									
										
										
										
											2011-06-16 23:21:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |             >>> print(test) | 
					
						
							|  |  |  |             test | 
					
						
							| 
									
										
										
										
											2011-06-16 23:21:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |             ''',
 | 
					
						
							|  |  |  |             doctests=[self.filename]) | 
					
						
							| 
									
										
										
										
											2011-06-16 23:21:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:37:14 +01:00
										 |  |  |     def test_additional_fixers(self): | 
					
						
							|  |  |  |         # make sure the fixers argument works | 
					
						
							|  |  |  |         self.check("""\
 | 
					
						
							|  |  |  |             echo('42') | 
					
						
							|  |  |  |             echo2('oh no') | 
					
						
							|  |  |  |             """,
 | 
					
						
							|  |  |  |             """\
 | 
					
						
							|  |  |  |             print('42') | 
					
						
							|  |  |  |             print('oh no') | 
					
						
							|  |  |  |             """,
 | 
					
						
							|  |  |  |             files=[self.filename], | 
					
						
							|  |  |  |             fixers=['packaging.tests.fixer']) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_suite(): | 
					
						
							|  |  |  |     return unittest.makeSuite(Mixin2to3TestCase) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     unittest.main(defaultTest="test_suite") |