574
574
class TestConvertImportToMap(TestCase):
575
575
"""Directly test the conversion from import strings to maps"""
577
def check_result(self, expected, import_strings):
577
def check(self, expected, import_strings):
578
578
proc = lazy_import.ImportProcessor()
579
579
for import_str in import_strings:
580
580
proc._convert_import_str(import_str)
586
586
def test_import_one(self):
587
self.check_result({'one':(['one'], None, {}),
587
self.check({'one':(['one'], None, {}),
590
590
def test_import_one_two(self):
591
591
one_two_map = {'one':(['one'], None,
592
592
{'two':(['one', 'two'], None, {}),
595
self.check_result(one_two_map, ['import one.two'])
596
self.check_result(one_two_map, ['import one, one.two'])
597
self.check_result(one_two_map, ['import one', 'import one.two'])
598
self.check_result(one_two_map, ['import one.two', 'import one'])
595
self.check(one_two_map, ['import one.two'])
596
self.check(one_two_map, ['import one, one.two'])
597
self.check(one_two_map, ['import one', 'import one.two'])
598
self.check(one_two_map, ['import one.two', 'import one'])
600
600
def test_import_one_two_three(self):
601
601
one_two_three_map = {
608
self.check_result(one_two_three_map, ['import one.two.three'])
609
self.check_result(one_two_three_map, ['import one, one.two.three'])
610
self.check_result(one_two_three_map, ['import one',
608
self.check(one_two_three_map, ['import one.two.three'])
609
self.check(one_two_three_map, ['import one, one.two.three'])
610
self.check(one_two_three_map, ['import one',
611
611
'import one.two.three'])
612
self.check_result(one_two_three_map, ['import one.two.three',
612
self.check(one_two_three_map, ['import one.two.three',
615
615
def test_import_one_as_x(self):
616
self.check_result({'x':(['one'], None, {}),
616
self.check({'x':(['one'], None, {}),
617
617
}, ['import one as x'])
619
619
def test_import_one_two_as_x(self):
620
self.check_result({'x':(['one', 'two'], None, {}),
621
}, ['import one.two as x'])
620
self.check({'x':(['one', 'two'], None, {}),
621
}, ['import one.two as x'])
623
623
def test_import_mixed(self):
624
624
mixed = {'x':(['one', 'two'], None, {}),
626
626
{'two':(['one', 'two'], None, {}),
629
self.check_result(mixed, ['import one.two as x, one.two'])
630
self.check_result(mixed, ['import one.two as x', 'import one.two'])
631
self.check_result(mixed, ['import one.two', 'import one.two as x'])
629
self.check(mixed, ['import one.two as x, one.two'])
630
self.check(mixed, ['import one.two as x', 'import one.two'])
631
self.check(mixed, ['import one.two', 'import one.two as x'])
633
633
def test_import_with_as(self):
634
self.check_result({'fast':(['fast'], None, {})}, ['import fast'])
634
self.check({'fast':(['fast'], None, {})}, ['import fast'])
637
637
class TestFromToMap(TestCase):
704
704
self.check(['from one import two'], '\nfrom one import two\n\n')
705
705
self.check(['from one import two'], '\nfrom one import (two)\n')
706
706
self.check(['from one import two '], '\nfrom one import (\n\ttwo\n)\n')
708
def test_multiple(self):
709
self.check(['import one', 'import two, three', 'from one import four'],
710
'import one\nimport two, three\nfrom one import four')
711
self.check(['import one', 'import two, three', 'from one import four'],
712
'import one\nimport (two, three)\nfrom one import four')
713
self.check(['import one', 'import two, three', 'from one import four'],
717
'from one import four')
718
self.check(['import one, ',
719
'import two, three, ', 'from one import four, '],
727
'from one import (\n'