~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_graph.py

Create a SearchResult object which can be used as a replacement for sets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
728
728
        self.assertEqual(set(['other_2']), search.next())
729
729
        self.assertRaises(StopIteration, search.next)
730
730
 
731
 
    def assertSeenAndRecipes(self, instructions, search, next):
732
 
        """Check the results of .seen and get_recipe() for a seach.
 
731
    def assertSeenAndResult(self, instructions, search, next):
 
732
        """Check the results of .seen and get_result() for a seach.
733
733
 
734
 
        :param instructions: A list of tuples (seen, get_recipe_result, starts,
735
 
            stops). seen and get_recipe_result are results to check. starts and
736
 
            stops are parameters to pass to start_searching and
737
 
            stop_searching_any during each iteration, if they are not None.
 
734
        :param instructions: A list of tuples:
 
735
            (seen, recipe, included_keys, starts, stops).
 
736
            seen, recipe and included_keys are results to check on the search
 
737
            and the searches get_result(). starts and stops are parameters to
 
738
            pass to start_searching and stop_searching_any during each
 
739
            iteration, if they are not None.
738
740
        :param search: The search to use.
739
741
        :param next: A callable to advance the search.
740
742
        """
741
 
        for seen, recipe, starts, stops in instructions:
 
743
        for seen, recipe, included_keys, starts, stops in instructions:
742
744
            next()
743
745
            if starts is not None:
744
746
                search.start_searching(starts)
745
747
            if stops is not None:
746
748
                search.stop_searching_any(stops)
747
 
            self.assertEqual(recipe, search.get_recipe())
 
749
            result = search.get_result()
 
750
            self.assertEqual(recipe, result.get_recipe())
 
751
            self.assertEqual(set(included_keys), result.get_keys())
748
752
            self.assertEqual(seen, search.seen)
749
753
 
750
 
    def test_breadth_first_get_recipe_excludes_current_pending(self):
 
754
    def test_breadth_first_get_result_excludes_current_pending(self):
751
755
        graph = self.make_graph({
752
756
            'head':['child'],
753
757
            'child':[NULL_REVISION],
755
759
            })
756
760
        search = graph._make_breadth_first_searcher(['head'])
757
761
        # At the start, nothing has been seen, to its all excluded:
 
762
        result = search.get_result()
758
763
        self.assertEqual((set(['head']), set(['head']), 0),
759
 
            search.get_recipe())
 
764
            result.get_recipe())
 
765
        self.assertEqual(set(), result.get_keys())
760
766
        self.assertEqual(set(), search.seen)
761
767
        # using next:
762
768
        expected = [
763
 
            (set(['head']), (set(['head']), set(['child']), 1), None, None),
 
769
            (set(['head']), (set(['head']), set(['child']), 1),
 
770
             ['head'], None, None),
764
771
            (set(['head', 'child']), (set(['head']), set([NULL_REVISION]), 2),
765
 
             None, None),
 
772
             ['head', 'child'], None, None),
766
773
            (set(['head', 'child', NULL_REVISION]), (set(['head']), set(), 3),
767
 
             None, None),
 
774
             ['head', 'child', NULL_REVISION], None, None),
768
775
            ]
769
 
        self.assertSeenAndRecipes(expected, search, search.next)
 
776
        self.assertSeenAndResult(expected, search, search.next)
770
777
        # using next_with_ghosts:
771
778
        search = graph._make_breadth_first_searcher(['head'])
772
 
        self.assertSeenAndRecipes(expected, search, search.next_with_ghosts)
 
779
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
773
780
 
774
 
    def test_breadth_first_get_recipe_starts_stops(self):
 
781
    def test_breadth_first_get_result_starts_stops(self):
775
782
        graph = self.make_graph({
776
783
            'head':['child'],
777
784
            'child':[NULL_REVISION],
784
791
        # Starting with nothing and adding a search works:
785
792
        search.start_searching(['head'])
786
793
        # head has been seen:
 
794
        result = search.get_result()
787
795
        self.assertEqual((set(['head']), set(['child']), 1),
788
 
            search.get_recipe())
 
796
            result.get_recipe())
 
797
        self.assertEqual(set(['head']), result.get_keys())
789
798
        self.assertEqual(set(['head']), search.seen)
790
799
        # using next:
791
800
        expected = [
794
803
            # called.
795
804
            (set(['head', 'child', 'otherhead']),
796
805
             (set(['head', 'otherhead']), set(['child', 'otherchild']), 2),
797
 
             ['otherhead'], ['child']),
 
806
             ['head', 'otherhead'], ['otherhead'], ['child']),
798
807
            (set(['head', 'child', 'otherhead', 'otherchild']),
799
808
             (set(['head', 'otherhead']), set(['child', 'excluded']), 3),
800
 
             None, None),
 
809
             ['head', 'otherhead', 'otherchild'], None, None),
801
810
            # stop searching excluded now
802
811
            (set(['head', 'child', 'otherhead', 'otherchild', 'excluded']),
803
812
             (set(['head', 'otherhead']), set(['child', 'excluded']), 3),
804
 
             None, ['excluded']),
 
813
             ['head', 'otherhead', 'otherchild'], None, ['excluded']),
805
814
            ]
806
 
        self.assertSeenAndRecipes(expected, search, search.next)
 
815
        self.assertSeenAndResult(expected, search, search.next)
807
816
        # using next_with_ghosts:
808
817
        search = graph._make_breadth_first_searcher([])
809
818
        search.start_searching(['head'])
810
 
        self.assertSeenAndRecipes(expected, search, search.next_with_ghosts)
 
819
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
811
820
 
812
 
    def test_breadth_first_get_recipe_ghosts_are_excluded(self):
 
821
    def test_breadth_first_get_result_ghosts_are_excluded(self):
813
822
        graph = self.make_graph({
814
823
            'head':['child', 'ghost'],
815
824
            'child':[NULL_REVISION],
820
829
        expected = [
821
830
            (set(['head']),
822
831
             (set(['head']), set(['ghost', 'child']), 1),
823
 
             None, None),
 
832
             ['head'], None, None),
824
833
            (set(['head', 'child', 'ghost']),
825
834
             (set(['head']), set([NULL_REVISION, 'ghost']), 2),
826
 
             None, None),
 
835
             ['head', 'child'], None, None),
827
836
            ]
828
 
        self.assertSeenAndRecipes(expected, search, search.next)
 
837
        self.assertSeenAndResult(expected, search, search.next)
829
838
        # using next_with_ghosts:
830
839
        search = graph._make_breadth_first_searcher(['head'])
831
 
        self.assertSeenAndRecipes(expected, search, search.next_with_ghosts)
 
840
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
832
841
 
833
 
    def test_breadth_first_get_recipe_starting_a_ghost_ghost_is_excluded(self):
 
842
    def test_breadth_first_get_result_starting_a_ghost_ghost_is_excluded(self):
834
843
        graph = self.make_graph({
835
844
            'head':['child'],
836
845
            'child':[NULL_REVISION],
841
850
        expected = [
842
851
            (set(['head', 'ghost']),
843
852
             (set(['head', 'ghost']), set(['child', 'ghost']), 1),
844
 
             ['ghost'], None),
 
853
             ['head'], ['ghost'], None),
845
854
            (set(['head', 'child', 'ghost']),
846
855
             (set(['head', 'ghost']), set([NULL_REVISION, 'ghost']), 2),
847
 
             None, None),
 
856
             ['head', 'child'], None, None),
848
857
            ]
849
 
        self.assertSeenAndRecipes(expected, search, search.next)
 
858
        self.assertSeenAndResult(expected, search, search.next)
850
859
        # using next_with_ghosts:
851
860
        search = graph._make_breadth_first_searcher(['head'])
852
 
        self.assertSeenAndRecipes(expected, search, search.next_with_ghosts)
 
861
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
853
862
 
854
863
    def test_breadth_first_revision_count_includes_NULL_REVISION(self):
855
864
        graph = self.make_graph({
861
870
        expected = [
862
871
            (set(['head']),
863
872
             (set(['head']), set([NULL_REVISION]), 1),
864
 
             None, None),
 
873
             ['head'], None, None),
865
874
            (set(['head', NULL_REVISION]),
866
875
             (set(['head']), set([]), 2),
867
 
             None, None),
 
876
             ['head', NULL_REVISION], None, None),
868
877
            ]
869
 
        self.assertSeenAndRecipes(expected, search, search.next)
 
878
        self.assertSeenAndResult(expected, search, search.next)
870
879
        # using next_with_ghosts:
871
880
        search = graph._make_breadth_first_searcher(['head'])
872
 
        self.assertSeenAndRecipes(expected, search, search.next_with_ghosts)
 
881
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
873
882
 
874
 
    def test_breadth_first_search_get_recipe_after_StopIteration(self):
 
883
    def test_breadth_first_search_get_result_after_StopIteration(self):
875
884
        # StopIteration should not invalid anything..
876
885
        graph = self.make_graph({
877
886
            'head':[NULL_REVISION],
882
891
        expected = [
883
892
            (set(['head']),
884
893
             (set(['head']), set([NULL_REVISION]), 1),
885
 
             None, None),
 
894
             ['head'], None, None),
886
895
            (set(['head', 'ghost', NULL_REVISION]),
887
896
             (set(['head', 'ghost']), set(['ghost']), 2),
888
 
             ['ghost'], None),
 
897
             ['head', NULL_REVISION], ['ghost'], None),
889
898
            ]
890
 
        self.assertSeenAndRecipes(expected, search, search.next)
 
899
        self.assertSeenAndResult(expected, search, search.next)
891
900
        self.assertRaises(StopIteration, search.next)
892
901
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
 
902
        result = search.get_result()
893
903
        self.assertEqual((set(['ghost', 'head']), set(['ghost']), 2),
894
 
            search.get_recipe())
 
904
            result.get_recipe())
 
905
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
895
906
        # using next_with_ghosts:
896
907
        search = graph._make_breadth_first_searcher(['head'])
897
 
        self.assertSeenAndRecipes(expected, search, search.next_with_ghosts)
 
908
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
898
909
        self.assertRaises(StopIteration, search.next)
899
910
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
 
911
        result = search.get_result()
900
912
        self.assertEqual((set(['ghost', 'head']), set(['ghost']), 2),
901
 
            search.get_recipe())
902
 
 
 
913
            result.get_recipe())
 
914
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
903
915
 
904
916
 
905
917
class TestCachingParentsProvider(tests.TestCase):