test_expression_parser.py 1.84 KB
Newer Older
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
import unittest
import sys
sys.path.insert(1, '.')
sys.path.insert(1, '..')
import cconfigspace as ccs

class TestExpressionParser(unittest.TestCase):

  def test_parse(self):
    exp = "1.0 + 1 == 2 || +1 == 3e0 && \"y\\nes\" == 'no' "
    res = ccs.ccs_parser.parse(exp)
    self.assertIsInstance(res, ccs.Expression)
    self.assertEqual( "1.0 + 1 == 2 || +1 == 3.0 && 'y\\nes' == 'no'", res.__str__() )

  def test_parse_priority(self):
    exp = "(1 + 3) * 2"
    res = ccs.ccs_parser.parse(exp)
    self.assertIsInstance(res, ccs.Expression)
    self.assertEqual( exp, res.__str__() )

  def test_associativity(self):
    exp = "5 - 2 - 1"
    res = ccs.ccs_parser.parse(exp)
    self.assertIsInstance( res, ccs.Expression )
    self.assertEqual( 2, res.eval() )
    exp = "5 - +(+2 - 1)"
    res = ccs.ccs_parser.parse(exp)
    self.assertIsInstance( res, ccs.Expression )
    self.assertEqual( 4, res.eval() )

  def test_in(self):
    exp = "5 # [3.0, 5]"
    res = ccs.ccs_parser.parse(exp)
    self.assertIsInstance( res, ccs.Expression )
    self.assertTrue( res.eval() )
    exp = "5 # [3.0, 4]"
    res = ccs.ccs_parser.parse(exp)
    self.assertIsInstance( res, ccs.Expression )
    self.assertFalse( res.eval() )

  def test_boolean(self):
Brice Videau's avatar
Brice Videau committed
42
    exp = "true"
43
44
45
    res = ccs.ccs_parser.parse(exp)
    self.assertIsInstance( res, ccs.Literal )
    self.assertEqual( True, res.eval() )
Brice Videau's avatar
Brice Videau committed
46
47
    self.assertEqual( "true", res.__str__() )
    exp = "false"
48
49
50
    res = ccs.ccs_parser.parse(exp)
    self.assertIsInstance( res, ccs.Literal )
    self.assertEqual( False, res.eval() )
Brice Videau's avatar
Brice Videau committed
51
    self.assertEqual( "false", res.__str__() )
52
53

  def test_none(self):
Brice Videau's avatar
Brice Videau committed
54
    exp = "none"
55
56
57
    res = ccs.ccs_parser.parse(exp)
    self.assertIsInstance( res, ccs.Literal )
    self.assertIsNone( res.eval() )
Brice Videau's avatar
Brice Videau committed
58
    self.assertEqual( "none", res.__str__() )
59
60
61
62


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