Example #1
0
 def test_parser_python(self):
     snipe.filters.parser = Parser(debug=True)
     self.assertEqual(str(makefilter("$'True'")), "$'True'")
     self.assertEqual(str(makefilter('$"True"')), "$'True'")
     self.assertEqual(str(makefilter('$"True"')), "$'True'")
     self.assertEqual(str(makefilter('$"True or \'flase\'"')),
                      "$\"True or 'flase'\"")
Example #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()))