def check(filterStr, primaryStr):
            filterGroup = prs(filterStr)
            if primaryStr is not None:
                expectedGrp = prs1(primaryStr)
            else:
                expectedGrp = None

            self.assertEqual(find_primary_template_group(filterGroup),
                             expectedGrp)
    def testUpdateAllErrors(self):
        # Duplicate primaries
        filters = {
            '1': prs('{(M3TAP foo) new}'),
            '2': prs('{(M3TAP foo) old}')
        }
        self.assertRaises(TemplateError, update_all_meta_groups, filters)

        # No labels
        filters = {
            '1': prs('{(M3TA) new}'),
        }
        self.assertRaises(TemplateError, update_all_meta_groups, filters)

        filters = {
            '1': prs('{(M3TAP) new}'),
        }
        self.assertRaises(TemplateError, update_all_meta_groups, filters)

        # Missing key
        filters = {
            '1': prs('{(M3TAP foo) new}'),
            '2': prs('x {(M3TA bar) old} {(M3TA baz) x}')
        }
        self.assertRaises(TemplateError, update_all_meta_groups, filters)
    def testReplace(self):
        primary = '{(M3TAP foo) (thing) thing2}'
        primaryGrp = find_primary_template_group(prs(primary))
        primary = primary.replace('M3TAP', 'M3TA')

        def check(filterStr, newFilterStr):
            filterGroup = prs(filterStr)
            sub_meta_groups(filterGroup, primaryGrp)
            self.assertEqual(str(filterGroup), newFilterStr)

        check('something {( fo) x}', 'something {( fo) x}')
        check('something {(M3TA fo) x}', 'something {(M3TA fo) x}')
        check('something {(M3TA foo) x}', 'something ' + primary)
        check('something {(M3TA foo) x} ({(foo M3TA) y})',
              'something %s (%s)' % (primary, primary))
    def testParse(self):
        def checkParse(filterStr, expectedElems):
            fes = self.parse_str(filterStr)
            self.assertEqual(fes, expectedElems)
            self.assertEqual(str(fes), filterStr)

        # Empty
        checkParse('', grp(Fe('')))
        checkParse(' ', grp(Fe('', preWs=' ')))
        checkParse('  ', grp(Fe('', preWs='  ')))

        # Single
        checkParse('bla', grp(Fe('bla')))
        checkParse(' bla', grp(Fe('bla', preWs=' ')))
        checkParse('  bla', grp(Fe('bla', preWs='  ')))
        checkParse('foo:bar', grp(Fe('foo:bar')))

        # Multiple
        checkParse('foo bar', grp(prs1('foo '), prs1('bar')))
        checkParse(' foo  bar ', grp(prs1(' foo  '), prs1('bar ')))

        # Group
        checkParse('()', grp2(prs1(''), delims='()'))
        checkParse('( )', grp2(prs1(' '), delims='()'))
        checkParse('{x}', grp2(prs1('x'), delims='{}'))
        checkParse(' {x} ', grp2(prs1('x'), delims='{}', preWs=' ',
                                 postWs=' '))
        checkParse('{ x y}', grp2(*prs(' x y').subElems, delims='{}'))

        # Multiple groups
        checkParse('{x}(y)', grp(prs1('{x}'), prs1('(y)')))
        checkParse(' {x} (y) ', grp(prs1(' {x} '), prs1('(y) ')))
        checkParse(' x (y) ', grp(prs1(' x '), prs1('(y) ')))
        checkParse(' (y) x ', grp(prs1(' (y) '), prs1('x ')))
        checkParse(' x:(y) ', grp(prs1(' x:'), prs1('(y) ')))
        checkParse('(y)x', grp(prs1('(y)'), prs1('x')))

        # Nested groups
        checkParse('{()}', grp2(prs1('()'), delims='{}'))
        checkParse('{(x)}', grp2(prs1('(x)'), delims='{}'))

        # Quotes
        checkParse('""', grp(Fe('', delims='""')))
        checkParse('"(blar)"', grp(Fe('(blar)', delims='""')))
        checkParse('("(blar)")', grp2(prs1('"(blar)"'), delims='()'))

        # Life-like
        subjectFooBarElemGrp = prs('subject:("Fo bar")')
        self.assertEqual(
            subjectFooBarElemGrp,
            grp(
                Fe('subject:'),
                grp(Fe('Fo bar', delims='""'), delims='()'),
            ))
        metaFooElem = prs1('{(M3TA label=foo) from:[email protected]} ')
        self.assertEqual(
            metaFooElem,
            grp(prs1('(M3TA label=foo) '),
                Fe('from:[email protected]'),
                delims='{}',
                postWs=' '))

        checkParse(
            '({(M3TA label=foo) from:[email protected]} subject:("Fo bar")) '
            'OR Foo',
            grp(
                grp(metaFooElem,
                    *subjectFooBarElemGrp.subElems,
                    delims='()',
                    postWs=' '), prs1('OR '), prs1('Foo')))
    def testUpdateAll(self):
        filters = {
            '1': prs('{(M3TAP foo) new}'),
            '2': prs('bla {(M3TA foo) old} x')
        }

        update_all_meta_groups(filters)
        expected = {
            '1': prs('{(M3TAP foo) new}'),
            '2': prs('bla {(M3TA foo) new} x')
        }
        self.assertDictEqual(filters, expected)

        # Other with root key
        filters = {'1': prs('{(M3TAP foo) new}'), '2': prs('{(M3TA foo) old}')}
        update_all_meta_groups(filters)
        expected = {
            '1': prs('{(M3TAP foo) new}'),
            '2': prs('{(M3TA foo) new}')
        }
        self.assertDictEqual(filters, expected)

        # updates within primaries
        filters = {
            '1': prs('{(M3TAP bar) newbarthing}'),
            '2': prs('{(M3TAP foo) newfoothing {(M3TA bar) oldbarthing}}'),
            '3': prs('xx {(M3TA foo) oldfoothig {(M3TA bar) olderbarthing}}'),
        }

        self.maxDiff = None
        update_all_meta_groups(filters)
        expected = {
            '1': prs('{(M3TAP bar) newbarthing}'),
            '2': prs('{(M3TAP foo) newfoothing {(M3TA bar) newbarthing}}'),
            '3': prs('xx {(M3TA foo) newfoothing {(M3TA bar) newbarthing}}'),
        }
        self.assertDictEqual(filters, expected)
 def check(fStr, keyList):
     self.assertEqual(find_all_meta_group_keys(prs(fStr)), set(keyList))
 def check(filterStr, newFilterStr):
     filterGroup = prs(filterStr)
     sub_meta_groups(filterGroup, primaryGrp)
     self.assertEqual(str(filterGroup), newFilterStr)
 def checkRaises(filterStr):
     filterGroup = prs(filterStr)
     self.assertRaises(TemplateError, find_primary_template_group,
                       filterGroup)