michael@0: #!/usr/bin/env python michael@0: michael@0: import unittest michael@0: from manifestparser import parse michael@0: michael@0: class ExpressionParserTest(unittest.TestCase): michael@0: """Test the conditional expression parser.""" michael@0: michael@0: def test_basic(self): michael@0: michael@0: self.assertEqual(parse("1"), 1) michael@0: self.assertEqual(parse("100"), 100) michael@0: self.assertEqual(parse("true"), True) michael@0: self.assertEqual(parse("false"), False) michael@0: self.assertEqual('', parse('""')) michael@0: self.assertEqual(parse('"foo bar"'), 'foo bar') michael@0: self.assertEqual(parse("'foo bar'"), 'foo bar') michael@0: self.assertEqual(parse("foo", foo=1), 1) michael@0: self.assertEqual(parse("bar", bar=True), True) michael@0: self.assertEqual(parse("abc123", abc123="xyz"), 'xyz') michael@0: michael@0: def test_equality(self): michael@0: michael@0: self.assertTrue(parse("true == true")) michael@0: self.assertTrue(parse("false == false")) michael@0: self.assertTrue(parse("1 == 1")) michael@0: self.assertTrue(parse("100 == 100")) michael@0: self.assertTrue(parse('"some text" == "some text"')) michael@0: self.assertTrue(parse("true != false")) michael@0: self.assertTrue(parse("1 != 2")) michael@0: self.assertTrue(parse('"text" != "other text"')) michael@0: self.assertTrue(parse("foo == true", foo=True)) michael@0: self.assertTrue(parse("foo == 1", foo=1)) michael@0: self.assertTrue(parse('foo == "bar"', foo='bar')) michael@0: self.assertTrue(parse("foo == bar", foo=True, bar=True)) michael@0: self.assertTrue(parse("true == foo", foo=True)) michael@0: self.assertTrue(parse("foo != true", foo=False)) michael@0: self.assertTrue(parse("foo != 2", foo=1)) michael@0: self.assertTrue(parse('foo != "bar"', foo='abc')) michael@0: self.assertTrue(parse("foo != bar", foo=True, bar=False)) michael@0: self.assertTrue(parse("true != foo", foo=False)) michael@0: self.assertTrue(parse("!false")) michael@0: michael@0: def test_conjunctures(self): michael@0: self.assertTrue(parse("true && true")) michael@0: self.assertTrue(parse("true || false")) michael@0: self.assertFalse(parse("false || false")) michael@0: self.assertFalse(parse("true && false")) michael@0: self.assertTrue(parse("true || false && false")) michael@0: michael@0: def test_parentheses(self): michael@0: self.assertTrue(parse("(true)")) michael@0: self.assertEqual(parse("(10)"), 10) michael@0: self.assertEqual(parse('("foo")'), 'foo') michael@0: self.assertEqual(parse("(foo)", foo=1), 1) michael@0: self.assertTrue(parse("(true == true)"), True) michael@0: self.assertTrue(parse("(true != false)")) michael@0: self.assertTrue(parse("(true && true)")) michael@0: self.assertTrue(parse("(true || false)")) michael@0: self.assertTrue(parse("(true && true || false)")) michael@0: self.assertFalse(parse("(true || false) && false")) michael@0: self.assertTrue(parse("(true || false) && true")) michael@0: self.assertTrue(parse("true && (true || false)")) michael@0: self.assertTrue(parse("true && (true || false)")) michael@0: self.assertTrue(parse("(true && false) || (true && (true || false))")) michael@0: michael@0: michael@0: def test_comments(self): michael@0: # comments in expressions work accidentally, via an implementation michael@0: # detail - the '#' character doesn't match any of the regular michael@0: # expressions we specify as tokens, and thus are ignored. michael@0: # However, having explicit tests for them means that should the michael@0: # implementation ever change, comments continue to work, even if that michael@0: # means a new implementation must handle them explicitly. michael@0: self.assertTrue(parse("true == true # it does!")) michael@0: self.assertTrue(parse("false == false # it does")) michael@0: self.assertTrue(parse("false != true # it doesnt")) michael@0: self.assertTrue(parse('"string with #" == "string with #" # really, it does')) michael@0: self.assertTrue(parse('"string with #" != "string with # but not the same" # no match!')) michael@0: michael@0: def test_not(self): michael@0: """ michael@0: Test the ! operator. michael@0: """ michael@0: self.assertTrue(parse("!false")) michael@0: self.assertTrue(parse("!(false)")) michael@0: self.assertFalse(parse("!true")) michael@0: self.assertFalse(parse("!(true)")) michael@0: self.assertTrue(parse("!true || true)")) michael@0: self.assertTrue(parse("true || !true)")) michael@0: self.assertFalse(parse("!true && true")) michael@0: self.assertFalse(parse("true && !true")) michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()