OILS / opy / _regtest / src / osh / bool_parse_test.py View on Github | oilshell.org

126 lines, 88 significant
1#!/usr/bin/env python
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"""
9bool_parse_test.py: Tests for bool_parse.py
10"""
11
12import unittest
13
14from core import test_lib
15
16from osh.meta import ast, Id, types
17from osh import parse_lib
18from osh import bool_parse # module under test
19
20bool_expr_e = ast.bool_expr_e
21lex_mode_e = types.lex_mode_e
22
23
24def _ReadWords(w_parser):
25 words = []
26 while True:
27 w = w_parser.ReadWord(lex_mode_e.DBRACKET)
28 if w.Type() == Id.Eof_Real:
29 break
30 words.append(w)
31 print('')
32 print('words:', words)
33
34 return words
35
36
37def _MakeParser(code_str):
38 # NOTE: We need the extra ]] token
39 arena = test_lib.MakeArena('<bool_parse_test.py>')
40 w_parser, _ = parse_lib.MakeParserForCompletion(code_str + ' ]]', arena)
41 w_parser._Next(lex_mode_e.DBRACKET) # for tests only
42 p = bool_parse.BoolParser(w_parser)
43 if not p._Next():
44 raise AssertionError
45 return p
46
47
48class BoolParserTest(unittest.TestCase):
49
50 def testParseFactor(self):
51 p = _MakeParser('foo')
52 print(p.ParseFactor())
53 self.assertTrue(p._TestAtEnd())
54
55 p = _MakeParser('$foo"bar"')
56 print(p.ParseFactor())
57 self.assertTrue(p._TestAtEnd())
58
59 p = _MakeParser('-z foo')
60 print('-------------')
61 node = p.ParseFactor()
62 print(node)
63 self.assertTrue(p._TestAtEnd())
64 self.assertEqual(bool_expr_e.BoolUnary, node.tag)
65
66 p = _MakeParser('foo == bar')
67 node = p.ParseFactor()
68 print(node)
69 self.assertTrue(p._TestAtEnd())
70 self.assertEqual(bool_expr_e.BoolBinary, node.tag)
71
72 def testParseNegatedFactor(self):
73 p = _MakeParser('foo')
74 node = p.ParseNegatedFactor()
75 print(node)
76 self.assertTrue(p._TestAtEnd())
77 self.assertEqual(bool_expr_e.WordTest, node.tag)
78
79 p = _MakeParser('! foo')
80 node = p.ParseNegatedFactor()
81 print(node)
82 self.assertTrue(p._TestAtEnd())
83 self.assertEqual(bool_expr_e.LogicalNot, node.tag)
84
85 def testParseTerm(self):
86 p = _MakeParser('foo && ! bar')
87 node = p.ParseTerm()
88 print(node)
89 self.assertEqual(bool_expr_e.LogicalAnd, node.tag)
90
91 # TODO: This is an entire expression I guess
92 p = _MakeParser('foo && ! bar && baz')
93 node = p.ParseTerm()
94 print(node)
95 self.assertEqual(bool_expr_e.LogicalAnd, node.tag)
96
97 p = _MakeParser('-z foo && -z bar')
98 node = p.ParseTerm()
99 print(node)
100 self.assertEqual(bool_expr_e.LogicalAnd, node.tag)
101
102 def testParseExpr(self):
103 p = _MakeParser('foo || ! bar')
104 node = p.ParseExpr()
105 print(node)
106 self.assertEqual(bool_expr_e.LogicalOr, node.tag)
107
108 p = _MakeParser('a == b')
109 print(p.ParseExpr())
110
111 def testParseFactorInParens(self):
112 p = _MakeParser('( foo == bar )')
113 node = p.ParseFactor()
114 print(node)
115 self.assertTrue(p._TestAtEnd())
116 self.assertEqual(bool_expr_e.BoolBinary, node.tag)
117
118 def testParseParenthesized(self):
119 p = _MakeParser('zoo && ( foo == bar )')
120 node = p.ParseExpr()
121 print(node)
122 self.assertEqual(bool_expr_e.LogicalAnd, node.tag)
123
124
125if __name__ == '__main__':
126 unittest.main()