class SetListTests(TestCase):

    def setUp(self):
        self.setlist = SetList()

    def assertSetList(self, setlist, positions):
        self.assertEqual(
            len(setlist), len(positions),
            'len(%r) [%d] != len(%r) [%d]' % (
                setlist, len(setlist), positions, len(positions)))
        for index, (setlist_position, position) in enumerate(zip(setlist, positions)):
            self.assertEqual(
                set(setlist_position), set(position),
                'got[%d] [%r] != expected[%d] [%r]' % (
                    index, setlist_position, index, position))

    def assertSetListFlat(self, setlist, theset):
        self.assertEqual(set(setlist.flat), set(theset))

    def test_01_add_to_set(self):
        self.setlist[3].add('item1')
        self.assertSetList(self.setlist, [[], [], [], ['item1']])
        self.assertSetListFlat(self.setlist, ['item1'])

    def test_02_truncate(self):
        self.assertEqual(set(self.setlist[3]), set())
        self.assertSetList(self.setlist, [[], [], [], []])
        self.setlist.truncate()
        self.assertSetList(self.setlist, [])

    def test_03_discard_from_set(self):
        self.setlist[3].add('item1')
        self.setlist[3].discard('item1')
        self.assertSetListFlat(self.setlist, [])
        self.assertSetList(self.setlist, [])

    def test_04_assign_set(self):
        self.setlist[2] = ['item1', 'item2']
        self.assertSetList(self.setlist, [[], [], ['item1', 'item2']])

    def test_05_append_set(self):
        self.setlist[0] = []
        self.setlist.append(['item1', 'item2'])
        self.assertSetList(self.setlist, [[], ['item1', 'item2']])

    def test_05_initialize(self):
        data = [['item1'], [], ['item2', 'item3']]
        setlist = SetList(data)
        self.assertSetList(setlist, data)
    def lookup_ordered(self, normalized_spellings, create_missing=False):
        """
        Returns a 2-tuple of
         * a SetList which contains, for each word in
          `normalized_spellings`, a set of all meanings associated
           with the word
         * a set of all found words

        If `create_missing` is `True`, creates words and meanings for
        words not found.  In this case the set of found words contains
        all searched words.
        """
        Word._get_cache().seed(normalized_spellings)
        result = SetList()
        found_words = set()
        cache = {}
        for word in normalized_spellings:
            if word not in cache:
                meanings, words = self.lookup_splitting(
                    word, create_missing=create_missing)
                cache[word] = list(meanings)
                found_words.update(words)
            result.append(cache[word])
        return result, found_words