def test_ignores_punctuation_at_the_end_of_a_word(self):
        tree = Tree(1)
        tree.build(['alpha.', 'bids,', 'blue,', 'carves', 'charlie', 'dances', 'delta', 'eats'])

        tree.mergeCandidates([(0, 3)], 0)
        self.assertEqual([(0, 3)], tree.candidates)
        self.assertEqual([['alpha'], ['bids'], ['blue']], tree.get_words(0))
    def test_returns_word_when_single_word_records(self):
        tree = Tree(1)
        tree.build(['alpha', 'blue', 'charlie', 'delta'])

        tree.mergeCandidates([(0, 1)], 0)
        self.assertEqual([(0, 1)], tree.candidates)
        self.assertEqual([['alpha']], tree.get_words(0))
    def test_candidates_are_sorted(self):
        tree = Tree(1)
        tree.build(['alpha', 'blue', 'charlie', 'delta'])

        tree.mergeCandidates([(1, 1), (0, 0), (2, 2)], 0)
        self.assertEqual([(0, 0), (1, 1), (2, 2)], tree.candidates)

        tree.mergeCandidates([(0, 0), (2, 0), (1, 0)], 0)
        self.assertEqual([(0, 0), (1, 0), (2, 0)], tree.candidates)
    def test_does_nothing_if_no_candidates_are_passed(self):
        tree = Tree(1)
        tree.build(['alpha'])

        self.assertEqual(['alpha'], tree.records)

        tree.mergeCandidates([], 0)

        self.assertEqual(['alpha'], tree.records)
    def test_stores_last_passed_candidates(self):
        tree = Tree(1)
        tree.build(['alpha', 'blue'])

        tree.mergeCandidates([], 0)
        self.assertEqual([], tree.candidates)

        tree.mergeCandidates([(0, 0)], 0)
        self.assertEqual([(0, 0)], tree.candidates)

        tree.mergeCandidates([(1, 1)], 0)
        self.assertEqual([(1, 1)], tree.candidates)

        tree.mergeCandidates([(0, 1)], 0)
        self.assertEqual([(0, 1)], tree.candidates)

        tree.mergeCandidates([], 0)
        self.assertEqual([], tree.candidates)
    def test_candidates_are_merged_if_they_contain_the_same_word(self):
        tree = Tree(2)
        tree.build(['alpha', 'blue', 'charlie', 'charlie', 'delta'])

        # records = [
        #   0  'alpha blue',
        #   1  'blue charlie',
        #   2  'charlie charlie',
        #   3  'charlie delta',
        #   4  'delta'
        # ]

        # (2, 2) = ['charlie charlie']
        # (2, 3) = ['charlie charlie', 'charlie delta']
        tree.mergeCandidates([(2, 2), (2, 3)], 1)
        self.assertEqual([(2, 3)], tree.candidates)

        tree.mergeCandidates([(2, 2), (2, 3)], 2)
        self.assertEqual([(2, 2), (2, 2), (2, 3)], tree.candidates)
    def test_candidates_are_merged_into_the_bigger_range_if_they_share_the_start_index(self):
        tree = Tree(1)
        tree.build(['alpha', 'blue', 'charlie', 'delta'])

        tree.mergeCandidates([(0, 1), (0, 2)], 0)
        self.assertEqual([(0, 2)], tree.candidates)

        tree.mergeCandidates([(1, 1), (1, 4), (2, 3), (2, 5), (1, 5)], 0)
        self.assertEqual([(1, 5), (2, 5)], tree.candidates)

        tree.mergeCandidates([(1, 1), (0, 1), (1, 2), (2, 2), (0, 0)], 0)
        self.assertEqual([(0, 1), (1, 2), (2, 2)], tree.candidates)
    def test_returns_word_when_multiple_words_records_scanWindow_3(self):
        tree = Tree(3)
        tree.build(['alpha', 'bids', 'blue', 'carves', 'charlie', 'dances', 'delta', 'eats'])

        tree.mergeCandidates([(0, 2)], 0)
        self.assertEqual([(0, 2)], tree.candidates)
        self.assertEqual([['alpha', 'bids', 'blue'], ['bids', 'blue', 'carves']], tree.get_words(2))

        tree.mergeCandidates([(0, 3)], 0)
        self.assertEqual([(0, 3)], tree.candidates)
        self.assertEqual([['alpha', 'bids', 'blue'], ['bids', 'blue', 'carves'], ['blue', 'carves', 'charlie']], tree.get_words(2))

        tree.mergeCandidates([(0, 4)], 0)
        self.assertEqual([(0, 4)], tree.candidates)
        self.assertEqual([['alpha', 'bids', 'blue'], ['bids', 'blue', 'carves'], ['blue', 'carves', 'charlie'], ['carves', 'charlie', 'dances']], tree.get_words(2))

        tree.mergeCandidates([(1, 4)], 0)
        self.assertEqual([(1, 4)], tree.candidates)
        self.assertEqual([['bids', 'blue', 'carves'], ['blue', 'carves', 'charlie'], ['carves', 'charlie', 'dances']], tree.get_words(2))

        tree.mergeCandidates([(2, 4)], 0)
        self.assertEqual([(2, 4)], tree.candidates)
        self.assertEqual([['blue', 'carves', 'charlie'], ['carves', 'charlie', 'dances']], tree.get_words(2))

        tree.mergeCandidates([(3, 4)], 0)
        self.assertEqual([(3, 4)], tree.candidates)
        self.assertEqual([['carves', 'charlie', 'dances']], tree.get_words(2))
    def test_returns_words_from_avaialble_candidates(self):
        tree = Tree(1)
        tree.build(['alpha', 'bids', 'blue', 'carves', 'charlie', 'dances', 'delta', 'eats'])

        tree.mergeCandidates([(0, 2)], 0)
        self.assertEqual([(0, 2)], tree.candidates)
        self.assertEqual([['alpha'], ['bids']], tree.get_words(0))

        tree.mergeCandidates([(0, 3)], 0)
        self.assertEqual([(0, 3)], tree.candidates)
        self.assertEqual([['alpha'], ['bids'], ['blue']], tree.get_words(0))

        tree.mergeCandidates([(0, 4)], 0)
        self.assertEqual([(0, 4)], tree.candidates)
        self.assertEqual([['alpha'], ['bids'], ['blue'], ['carves']], tree.get_words(0))

        tree.mergeCandidates([(1, 4)], 0)
        self.assertEqual([(1, 4)], tree.candidates)
        self.assertEqual([['bids'], ['blue'], ['carves']], tree.get_words(0))

        tree.mergeCandidates([(2, 4)], 0)
        self.assertEqual([(2, 4)], tree.candidates)
        self.assertEqual([['blue'], ['carves']], tree.get_words(0))

        tree.mergeCandidates([(3, 4)], 0)
        self.assertEqual([(3, 4)], tree.candidates)
        self.assertEqual([['carves']], tree.get_words(0))
    def test_candidates_are_compared_against_the_last(self):
        tree = Tree(1)
        tree.build(['alpha', 'blue', 'charlie', 'delta'])

        tree.mergeCandidates([(3, 3), (4, 4), (2, 2)], 0)
        self.assertEqual([(2, 2), (3, 3), (4, 4)], tree.candidates)