예제 #1
0
    def test_misc(self):
        self.assertEqual(repr(Compare('=', 'foo', 'bar')),
                         "Compare('=', 'foo', 'bar')")
        self.assertEqual(hash(Compare('=', 'foo', 'bar')),
                         hash(Compare('=', 'foo', 'bar')))
        self.assertEqual(str(Compare('=', 'foo', 'bar')), 'foo = "bar"')
        self.assertEqual(str(Compare('=', 'foo', Identifier('bar'))),
                         'foo = bar')
        self.assertEqual(str(Compare('=', 'foo', 5)), 'foo = 5')

        self.assertEqual(repr(Identifier('bar')), "Identifier('bar')")

        self.assertFalse(Compare.do('<=', 15, 'pants'))

        self.assertEqual(RECompare.deflag('s'), re.DOTALL)
        self.assertEqual(RECompare.deflag('7'), 0)
        self.assertEqual(RECompare.static('==', '[', ''), No())

        self.assertEqual(str(RECompare('=', 'key', 'value', flags='i')),
                         'key = /value/i')
예제 #2
0
    def test_Parser(self):
        snipe.filters.parser = Parser(debug=True)
        self.assertEqual(makefilter('yes'), Yes())
        self.assertEqual(makefilter('yes and no'), And(Yes(), No()))
        self.assertEqual(makefilter('foo = "bar"'), Compare('=', 'foo', 'bar'))
        self.assertEqual(makefilter('foo < "bar"'), Compare('<', 'foo', 'bar'))
        self.assertEqual(makefilter('"bar" = foo'), Compare('=', 'foo', 'bar'))
        self.assertEqual(makefilter('foo = bar'),
                         Compare('=', 'foo', Identifier('bar')))
        self.assertEqual(makefilter('foo = /bar/'),
                         RECompare('=', 'foo', 'bar'))
        self.assertEqual(makefilter('/bar/ = foo'),
                         RECompare('=', 'foo', 'bar'))
        self.assertEqual(makefilter('1 = 1'), Yes())
        self.assertEqual(makefilter('1 = 2'), No())
        self.assertEqual(makefilter('"foo" = /foo/'), Yes())
        self.assertEqual(makefilter('"foo" = /bar/'), No())
        self.assertEqual(makefilter('"Foo" = /foo/i'), Yes())
        self.assertEqual(makefilter('"Foo" != /foo/i'), No())
        self.assertEqual(makefilter('(yes or no) and yes'),
                         And(Or(Yes(), No()), Yes()))
        self.assertEqual(makefilter('yes xor no'), Xor(Yes(), No()))

        self.assertTrue(makefilter('foo = "bar"')(mocks.Message(foo='bar')))
        self.assertFalse(makefilter('foo = "bar"')(mocks.Message(foo='baz')))

        self.assertTrue(makefilter('foo = /b.*/')(mocks.Message(foo='bar')))
        self.assertTrue(makefilter('foo = /b.*/')(mocks.Message(foo='baz')))
        self.assertFalse(makefilter('foo = /b.*/')(mocks.Message(foo='quux')))

        self.assertTrue(
            makefilter('foo = bar')(mocks.Message(foo='quux', bar='quux')))

        self.assertFalse(
            makefilter('foo = bar')(mocks.Message(foo='quux', bar='quuux')))

        self.assertTrue(
            makefilter('foo = "bar"')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertFalse(
            makefilter('not foo = "bar"')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertFalse(
            makefilter('foo == "bar"')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertTrue(
            makefilter('foo = /bar/')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertFalse(
            makefilter('foo == /bar/')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertFalse(
            makefilter('foo == /bar[/')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))

        self.assertEqual(str(makefilter('foo == "bar"')), 'foo == "bar"')
        self.assertEqual(str(makefilter('"bar" == foo')), 'foo == "bar"')

        self.assertEqual(str(makefilter('yes and yes and yes')),
                         'yes and yes and yes')

        self.assertEqual(str(makefilter('no and yes and yes')),
                         'no and yes and yes')

        self.assertEqual(str(makefilter('yes and no and yes')),
                         'yes and no and yes')

        self.assertEqual(str(makefilter('yes and yes and no')),
                         'yes and yes and no')

        self.assertTrue(makefilter('foo')(mocks.Message(foo=True)))
        self.assertFalse(makefilter('foo')(mocks.Message(foo=0)))
        self.assertFalse(makefilter('foo')(mocks.Message(foo=0)))

        self.assertIs(makefilter(''), None)

        self.assertFalse(makefilter('filter foo')(mocks.Message()))