def assertEqualsExampleOutput(self, out_mod_name, stdout=None, verbose=False, dryrun=False, doprint=False, debug=False): testmod = importlib.import_module('test_inputs.example') with Capture() as capture: text = gen_code.gen_module(testmod, out_mod_name, '# HEADER', verbose=verbose, dryrun=dryrun, doprint=doprint, debug=debug) expected = inspect.getsource( importlib.import_module('test_inputs.output_example')) self.assertCodeEqual(expected, text) self.assertNotIn('error', text.lower()) if stdout: self.assertEqual(stdout, capture.stdout()) else: self.assertNotIn('error', capture.stdout().lower()) self.assertEqual('', capture.stderr())
def test_gen_module_empty(self): testmod = importlib.import_module('test_inputs.parse_empty') with Capture() as capture: text = gen_code.gen_module(testmod, 'tests.test_output_module', '# HEADER') self.assertEqual(EXPECTED_PARSE_EMPTY, text) self.assertEqual(EXPECTED_PARSE_EMPTY_STDOUT, capture.stdout()) self.assertEqual('', capture.stderr())
def test_gen_class_verbose(self): with Capture() as capture: self.test_gen_class(True) lines = [ '? gen_class(tests.x7.testing.maketests.test_gen_code.Klass)-what is IGNORE_ME:not a class or function', 'found class-local class: tests.x7.testing.maketests.test_gen_code.Klass.IgnoreThisToo, should already be handled', 'skipping apparently inherited function: ignore_this in tests.x7.testing.maketests.test_gen_code.Klass', ] self.assertEqual('\n'.join(lines), capture.stdout()) self.assertEqual('', capture.stderr())
def test_gen_module_empty_dryrun(self): testmod = importlib.import_module('test_inputs.parse_empty') with Capture() as capture: text = gen_code.gen_module(testmod, 'tests.test_output_module', '# HEADER', verbose=True, dryrun=True, doprint=True) self.assertEqual(EXPECTED_PARSE_EMPTY, text) self.assertEqual(EXPECTED_PARSE_EMPTY_DRYRUN_STDOUT, capture.stdout()) self.assertEqual('', capture.stderr())
def test_main_single(self): with Capture() as capture: with Argv( ['maketests', '-n', '-v', '-p', 'test_inputs.parse_empty']): maketests.main() lines = [ 'Input: test_inputs.parse_empty', 'class TestModParseEmpty(TestCase):', ] for line in lines: self.assertIn(line, capture.stdout()) self.assertNotIn('error', capture.stdout().lower()) self.assertEqual('', capture.stderr())
def test_gen_module_generated_exception(self): """Test that exception in parsed code is caught as expected""" testmod = importlib.import_module('test_inputs.tests') with Capture() as capture: gen_code.gen_module(testmod, 'tests.test_output_module', '# HEADER', verbose=True, dryrun=True, doprint=True) self.assertIn('error parsing generated result', capture.stdout().lower()) self.assertEqual('', capture.stderr())
def test_parse_module_verbose(self): from test_inputs import parse_basic with Capture() as capture: parsed = parse.parse_module(cm(parse_basic), True, False) self.assertEqual( {'TestModExample', 'TestModExample2', 'ClassGotImported'}, set(parsed.classes.keys()), ) self.assertEqual({'a_function', 'func_got_imported'}, set(parsed.functions.keys())) self.assertEqual(capture.stderr(), '') self.assertEqual('ParseModule: test_inputs.parse_basic', capture.stdout())
def test_build_verbose(self): """Test OutputMap.build with verbose output""" om = OutputMap(verbose=True) with Capture() as capture: om.build(cm(parse_basic)) out = capture.stdout() lines = [ 'Class: ClassGotImported -> tests', ' Class: TestModExample -> tests x7.lib.annotations', ' Class: TestModExample2 -> tests test_inputs.parse_basic.TestModExample', ] for l in lines: self.assertIn(l, out) self.assertEqual('', capture.stderr()) self.assertEqual(2, om.imports[('', 'sys')]) self.assertEqual(3, om.imports[('x7.lib', 'annotations')]) self.assertEqual( 5, om.imports[('test_inputs.example_imported', 'func_got_imported')])
def test_gen_class_extra_verbose(self): with Capture() as capture: self.test_gen_class_extra(True) lines = [ '.get_and_set: found property: <property object at 0x7fe7701cd5e0>', ' with fget=tests.x7.testing.maketests.test_gen_code.KlassExtra.get_and_set fset=tests.x7.testing.maketests.test_gen_code.KlassExtra.get_and_set fdel=None', '.get_and_set_and_del: found property: <property object at 0x7fe7701cd590>', ' with fget=tests.x7.testing.maketests.test_gen_code.KlassExtra.get_and_set_and_del fset=tests.x7.testing.maketests.test_gen_code.KlassExtra.get_and_set_and_del fdel=tests.x7.testing.maketests.test_gen_code.KlassExtra.get_and_set_and_del', '.get_only: found property: <property object at 0x7fe7701cd540>', ' with fget=tests.x7.testing.maketests.test_gen_code.KlassExtra.get_only fset=None fdel=None', '.prop_inherited: found property: <property object at 0x7fe7701cd4a0>', ' with fget=tests.x7.testing.maketests.test_gen_code.KlassExtraBase.prop_inherited fset=None fdel=None', 'skipping apparently inherited property: prop_inherited in tests.x7.testing.maketests.test_gen_code.KlassExtra', '? gen_class(tests.x7.testing.maketests.test_gen_code.KlassExtra)-what is value:1' ] stdout = capture.stdout() # import pprint; pprint.pp(stdout.splitlines(), width=160) lines = '\n'.join(lines) hex_pat = re.compile('0x([0-9A-Fa-f]+)') self.assertEqual(hex_pat.sub('0x...', lines), hex_pat.sub('0x...', stdout)) self.assertEqual('', capture.stderr())
def test_capture(self): with Capture() as cm: print('StdOut', file=sys.stdout) print('StdErr', file=sys.stderr) self.assertIn('StdOut', cm.stdout()) self.assertIn('StdErr', cm.stderr())
def test_main_usage(self): with Capture() as capture: with Argv(['maketests']): maketests.main() self.assertIn('Usage: maketests', capture.stdout()) self.assertEqual('', capture.stderr())
def test_gen_module_empty_exception(self): testmod = importlib.import_module('test_inputs.parse_empty') with self.assertRaises(ModuleNotFoundError): with Capture(): gen_code.gen_module(testmod, 'does_not_exist.whatever', '# HEADER')