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()))
def test_Or(self): self.assertEqual(repr(Or(None, Yes(), No())), 'Or(Yes(), No())') self.assertTrue(Or(Yes(), Yes())._check(None)) self.assertTrue(Or(Yes(), No())._check(None)) self.assertFalse(Or(No(), No())._check(None)) self.assertTrue(Or(Yes(), Yes()).simplify({})) self.assertTrue(Or(Yes(), No()).simplify({})) self.assertTrue(Or(Yes(), Truth('foo')).simplify({})) self.assertEqual(Or(Truth('foo'), No()).simplify({}), Truth('foo')) self.assertEqual( Or(No(), Truth('foo'), Truth('bar')).simplify({}), Or(Truth('foo'), Truth('bar')).simplify({})) self.assertFalse(Or().simplify(None))