class TestUtterance(unittest.TestCase):
    """Tests correct working of the Utterance class."""

    def setUp(self):
        self.barbara = Utterance('b a r b a r a')
        self.ararat = Utterance('a r a r a t')

    def test_index(self):
        test_pairs = ((['b', 'a', 'r'], 0),
                      (['b', 'a', 'r', 'a'], 3),
                      (['a', 'r', 'a'], 4))
        for phrase, idx in test_pairs:
            self.assertEqual(self.barbara.index(phrase), idx)
        self.assertRaises(ValueError, self.barbara.index, ['r', 'a', 'b'])
        self.assertEqual(self.ararat.index(['a', 'r', 'a', 't']), 2)

    def test_ngram_iterator(self):
        # Normal use case.
        trigrams = [['b', 'a', 'r'],
                    ['a', 'r', 'b'],
                    ['r', 'b', 'a'],
                    ['b', 'a', 'r'],
                    ['a', 'r', 'a'],
                   ]
        trigrams_with_boundaries = [
                    [SENTENCE_START, 'b', 'a'],
                    ['b', 'a', 'r'],
                    ['a', 'r', 'b'],
                    ['r', 'b', 'a'],
                    ['b', 'a', 'r'],
                    ['a', 'r', 'a'],
                    ['r', 'a', SENTENCE_END],
                   ]
        act_trigrams = list(self.barbara.iter_ngrams(3))
        act_trigrams_with_boundaries = list(
            self.barbara.iter_ngrams(3, with_boundaries=True))
        self.assertItemsEqual(trigrams, act_trigrams)
        self.assertItemsEqual(trigrams_with_boundaries,
                         act_trigrams_with_boundaries)
        # Corner cases.
        self.assertItemsEqual(list(self.barbara.iter_ngrams(7)),
                              [['b', 'a', 'r', 'b', 'a', 'r', 'a']])
        self.assertItemsEqual(list(self.barbara.iter_ngrams(8)), [])
        self.assertItemsEqual(
            list(self.barbara.iter_ngrams(8, with_boundaries=True)),
            [[SENTENCE_START, 'b', 'a', 'r', 'b', 'a', 'r', 'a'],
             ['b', 'a', 'r', 'b', 'a', 'r', 'a', SENTENCE_END]])
        self.assertItemsEqual(
            list(self.barbara.iter_ngrams(9, with_boundaries=True)),
            [[SENTENCE_START, 'b', 'a', 'r', 'b', 'a', 'r', 'a',
              SENTENCE_END]])
        self.assertItemsEqual(
            list(self.barbara.iter_ngrams(10, with_boundaries=True)),
            [])