1 | #!/usr/bin/env python2
|
2 | # Copyright 2016 Andy Chu. All rights reserved.
|
3 | # Licensed under the Apache License, Version 2.0 (the "License");
|
4 | # you may not use this file except in compliance with the License.
|
5 | # You may obtain a copy of the License at
|
6 | #
|
7 | # http://www.apache.org/licenses/LICENSE-2.0
|
8 | """
|
9 | bool_parse_test.py: Tests for bool_parse.py
|
10 | """
|
11 |
|
12 | import unittest
|
13 |
|
14 | from _devbuild.gen.id_kind_asdl import Id
|
15 | from _devbuild.gen.syntax_asdl import bool_expr_e
|
16 | from _devbuild.gen.types_asdl import lex_mode_e
|
17 | from core import test_lib
|
18 | from osh import bool_parse # module under test
|
19 |
|
20 |
|
21 | def _ReadWords(w_parser):
|
22 | words = []
|
23 | while True:
|
24 | w = w_parser.ReadWord(lex_mode_e.DBracket)
|
25 | if w.Type() == Id.Eof_Real:
|
26 | break
|
27 | words.append(w)
|
28 | print('')
|
29 | print('words:', words)
|
30 |
|
31 | return words
|
32 |
|
33 |
|
34 | def _MakeParser(code_str):
|
35 | # NOTE: We need the extra ]] token
|
36 | arena = test_lib.MakeArena('<bool_parse_test.py>')
|
37 | w_parser = test_lib.InitWordParser(code_str + ' ]]', arena=arena)
|
38 | w_parser._SetNext(lex_mode_e.DBracket) # for tests only
|
39 | p = bool_parse.BoolParser(w_parser)
|
40 | p._Next()
|
41 | return p
|
42 |
|
43 |
|
44 | class BoolParserTest(unittest.TestCase):
|
45 |
|
46 | def testParseFactor(self):
|
47 | p = _MakeParser('foo')
|
48 | print(p.ParseFactor())
|
49 | self.assertTrue(p._TestAtEnd())
|
50 |
|
51 | p = _MakeParser('$foo"bar"')
|
52 | print(p.ParseFactor())
|
53 | self.assertTrue(p._TestAtEnd())
|
54 |
|
55 | p = _MakeParser('-z foo')
|
56 | print('-------------')
|
57 | node = p.ParseFactor()
|
58 | print(node)
|
59 | self.assertTrue(p._TestAtEnd())
|
60 | self.assertEqual(bool_expr_e.Unary, node.tag())
|
61 |
|
62 | p = _MakeParser('foo == bar')
|
63 | node = p.ParseFactor()
|
64 | print(node)
|
65 | self.assertTrue(p._TestAtEnd())
|
66 | self.assertEqual(bool_expr_e.Binary, node.tag())
|
67 |
|
68 | def testParseNegatedFactor(self):
|
69 | p = _MakeParser('foo')
|
70 | node = p.ParseNegatedFactor()
|
71 | print(node)
|
72 | self.assertTrue(p._TestAtEnd())
|
73 | self.assertEqual(bool_expr_e.WordTest, node.tag())
|
74 |
|
75 | p = _MakeParser('! foo')
|
76 | node = p.ParseNegatedFactor()
|
77 | print(node)
|
78 | self.assertTrue(p._TestAtEnd())
|
79 | self.assertEqual(bool_expr_e.LogicalNot, node.tag())
|
80 |
|
81 | def testParseTerm(self):
|
82 | p = _MakeParser('foo && ! bar')
|
83 | node = p.ParseTerm()
|
84 | print(node)
|
85 | self.assertEqual(bool_expr_e.LogicalAnd, node.tag())
|
86 |
|
87 | # TODO: This is an entire expression I guess
|
88 | p = _MakeParser('foo && ! bar && baz')
|
89 | node = p.ParseTerm()
|
90 | print(node)
|
91 | self.assertEqual(bool_expr_e.LogicalAnd, node.tag())
|
92 |
|
93 | p = _MakeParser('-z foo && -z bar')
|
94 | node = p.ParseTerm()
|
95 | print(node)
|
96 | self.assertEqual(bool_expr_e.LogicalAnd, node.tag())
|
97 |
|
98 | def testParseExpr(self):
|
99 | p = _MakeParser('foo || ! bar')
|
100 | node = p.ParseExpr()
|
101 | print(node)
|
102 | self.assertEqual(bool_expr_e.LogicalOr, node.tag())
|
103 |
|
104 | p = _MakeParser('a == b')
|
105 | print(p.ParseExpr())
|
106 |
|
107 | def testParseFactorInParens(self):
|
108 | p = _MakeParser('( foo == bar )')
|
109 | node = p.ParseFactor()
|
110 | print(node)
|
111 | self.assertTrue(p._TestAtEnd())
|
112 | self.assertEqual(bool_expr_e.Binary, node.tag())
|
113 |
|
114 | def testParseParenthesized(self):
|
115 | p = _MakeParser('zoo && ( foo == bar )')
|
116 | node = p.ParseExpr()
|
117 | print(node)
|
118 | self.assertEqual(bool_expr_e.LogicalAnd, node.tag())
|
119 |
|
120 |
|
121 | if __name__ == '__main__':
|
122 | unittest.main()
|