DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (27a812186ff4)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#!/usr/bin/env python

import unittest
from manifestparser import parse

class ExpressionParserTest(unittest.TestCase):
    """Test the conditional expression parser."""

    def test_basic(self):

        self.assertEqual(parse("1"), 1)
        self.assertEqual(parse("100"), 100)
        self.assertEqual(parse("true"), True)
        self.assertEqual(parse("false"), False)
        self.assertEqual('', parse('""'))
        self.assertEqual(parse('"foo bar"'), 'foo bar')
        self.assertEqual(parse("'foo bar'"), 'foo bar')
        self.assertEqual(parse("foo", foo=1), 1)
        self.assertEqual(parse("bar", bar=True), True)
        self.assertEqual(parse("abc123", abc123="xyz"), 'xyz')

    def test_equality(self):

        self.assertTrue(parse("true == true"))
        self.assertTrue(parse("false == false"))
        self.assertTrue(parse("1 == 1"))
        self.assertTrue(parse("100 == 100"))
        self.assertTrue(parse('"some text" == "some text"'))
        self.assertTrue(parse("true != false"))
        self.assertTrue(parse("1 != 2"))
        self.assertTrue(parse('"text" != "other text"'))
        self.assertTrue(parse("foo == true", foo=True))
        self.assertTrue(parse("foo == 1", foo=1))
        self.assertTrue(parse('foo == "bar"', foo='bar'))
        self.assertTrue(parse("foo == bar", foo=True, bar=True))
        self.assertTrue(parse("true == foo", foo=True))
        self.assertTrue(parse("foo != true", foo=False))
        self.assertTrue(parse("foo != 2", foo=1))
        self.assertTrue(parse('foo != "bar"', foo='abc'))
        self.assertTrue(parse("foo != bar", foo=True, bar=False))
        self.assertTrue(parse("true != foo", foo=False))
        self.assertTrue(parse("!false"))

    def test_conjunctures(self):
        self.assertTrue(parse("true && true"))
        self.assertTrue(parse("true || false"))
        self.assertFalse(parse("false || false"))
        self.assertFalse(parse("true && false"))
        self.assertTrue(parse("true || false && false"))

    def test_parentheses(self):
        self.assertTrue(parse("(true)"))
        self.assertEqual(parse("(10)"), 10)
        self.assertEqual(parse('("foo")'), 'foo')
        self.assertEqual(parse("(foo)", foo=1), 1)
        self.assertTrue(parse("(true == true)"), True)
        self.assertTrue(parse("(true != false)"))
        self.assertTrue(parse("(true && true)"))
        self.assertTrue(parse("(true || false)"))
        self.assertTrue(parse("(true && true || false)"))
        self.assertFalse(parse("(true || false) && false"))
        self.assertTrue(parse("(true || false) && true"))
        self.assertTrue(parse("true && (true || false)"))
        self.assertTrue(parse("true && (true || false)"))
        self.assertTrue(parse("(true && false) || (true && (true || false))"))


    def test_comments(self):
        # comments in expressions work accidentally, via an implementation
        # detail - the '#' character doesn't match any of the regular
        # expressions we specify as tokens, and thus are ignored.
        # However, having explicit tests for them means that should the
        # implementation ever change, comments continue to work, even if that
        # means a new implementation must handle them explicitly.
        self.assertTrue(parse("true == true # it does!"))
        self.assertTrue(parse("false == false # it does"))
        self.assertTrue(parse("false != true # it doesnt"))
        self.assertTrue(parse('"string with #" == "string with #" # really, it does'))
        self.assertTrue(parse('"string with #" != "string with # but not the same" # no match!'))

    def test_not(self):
        """
        Test the ! operator.
        """
        self.assertTrue(parse("!false"))
        self.assertTrue(parse("!(false)"))
        self.assertFalse(parse("!true"))
        self.assertFalse(parse("!(true)"))
        self.assertTrue(parse("!true || true)"))
        self.assertTrue(parse("true || !true)"))
        self.assertFalse(parse("!true && true"))
        self.assertFalse(parse("true && !true"))

if __name__ == '__main__':
    unittest.main()