~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lazy_import.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-11 21:56:26 UTC
  • mto: This revision was merged to the branch mainline in revision 2004.
  • Revision ID: john@arbash-meinel.com-20060911215626-0bbe834149b22b07
Create a method for handling 'import *' syntax.

This handles:
    import foo
    import foo.bar
    import foo.bar as baz
    import foo, foo.bar, baz.bing

Show diffs side-by-side

added added

removed removed

Lines of Context:
529
529
        # root5.sub5 should still be lazy, but not re-import root5
530
530
        self.assertEqual(InstrumentedImportReplacer,
531
531
                         object.__getattribute__(root5.sub5, '__class__'))
 
532
 
532
533
        # Accessing root5.sub5.submoda5 should import sub5, but not either
533
534
        # of the sub objects (they should be available as lazy objects
534
535
        self.assertEqual(InstrumentedImportReplacer,
568
569
                          ('_import', 'submodb5'),
569
570
                          ('import', submodb_path, []),
570
571
                         ], self.actions)
 
572
 
 
573
 
 
574
class TestConvertImportToList(TestCase):
 
575
    """Directly test the conversion from import strings to lists"""
 
576
 
 
577
    def check_result(self, expected, import_strings):
 
578
        imports = {}
 
579
        for import_str in import_strings:
 
580
            lazy_import._convert_import_str_to_map(import_str, imports)
 
581
        self.assertEqual(expected, imports,
 
582
                         'Import of %r was not converted correctly'
 
583
                         ' %s != %s' % (import_strings, expected, imports))
 
584
 
 
585
    def test_import_one(self):
 
586
        self.check_result({'one':(['one'], None, {}),
 
587
                          }, ['import one'])
 
588
 
 
589
    def test_import_one_two(self):
 
590
        one_two_map = {'one':(['one'], None,
 
591
                              {'two':(['one', 'two'], None, {}),
 
592
                              }),
 
593
                      }
 
594
        self.check_result(one_two_map, ['import one.two'])
 
595
        self.check_result(one_two_map, ['import one, one.two'])
 
596
        self.check_result(one_two_map, ['import one', 'import one.two'])
 
597
        self.check_result(one_two_map, ['import one.two', 'import one'])
 
598
 
 
599
    def test_import_one_two_three(self):
 
600
        one_two_three_map = {
 
601
            'one':(['one'], None,
 
602
                   {'two':(['one', 'two'], None,
 
603
                           {'three':(['one', 'two', 'three'], None, {}),
 
604
                           }),
 
605
                   }),
 
606
        }
 
607
        self.check_result(one_two_three_map, ['import one.two.three'])
 
608
        self.check_result(one_two_three_map, ['import one, one.two.three'])
 
609
        self.check_result(one_two_three_map, ['import one',
 
610
                                              'import one.two.three'])
 
611
        self.check_result(one_two_three_map, ['import one.two.three',
 
612
                                              'import one'])
 
613
 
 
614
    def test_import_one_as_x(self):
 
615
        self.check_result({'x':(['one'], None, {}),
 
616
                          }, ['import one as x'])
 
617
 
 
618
    def test_import_one_two_as_x(self):
 
619
        self.check_result({'x':(['one', 'two'], None, {}),
 
620
                          }, ['import one.two as x'])
 
621
 
 
622
    def test_import_mixed(self):
 
623
        mixed = {'x':(['one', 'two'], None, {}),
 
624
                 'one':(['one'], None,
 
625
                       {'two':(['one', 'two'], None, {}),
 
626
                       }),
 
627
                }
 
628
        self.check_result(mixed, ['import one.two as x, one.two'])
 
629
        self.check_result(mixed, ['import one.two as x', 'import one.two'])
 
630
        self.check_result(mixed, ['import one.two', 'import one.two as x'])
 
631
 
 
632
    def test_import_with_as(self):
 
633
        self.check_result({'fast':(['fast'], None, {})}, ['import fast'])
 
634
 
 
635
# These will be used when we want to check converting 'import foo' into
 
636
# the parameters necessary for creating ImportReplacer objects
 
637
#    def check_result(self, expected, import_string):
 
638
#        val = lazy_import._convert_import_to_list(import_string)
 
639
#        self.assertEqual(expected, val)
 
640
#
 
641
#    def test_import_one(self):
 
642
#        self.check_result(['one', ['one'], None, []], 'import one')
 
643
#
 
644
#    def test_import_one_two(self):
 
645
#        self.check_result(['one', ['one'], None,
 
646
#                            [('two', ['one', 'two'], [])]
 
647
#                          ],
 
648
#                          'import one.two')
 
649
#
 
650
#    def test_import_one_two_three(self):
 
651
#        self.check_result(['one', ['one'], None,
 
652
#                            [('two', ['one', 'two'],
 
653
#                                [('three', ['one', 'two', 'three'], []),
 
654
#                                ]
 
655
#                             ),
 
656
#                            ]
 
657
#                          ],
 
658
#                          'import one.two.three')
 
659
#
 
660
#    def test_from_one_import_two(self):
 
661
#        self.check_result(['two', ['one'], 'two', []], 'from one import two')
 
662
#
 
663
#    def test_from_one_two_import_three(self):
 
664
#        self.check_result(['three', ['one', 'two'], 'three', []],
 
665
#                          'from one.two import three')