1#!/usr/bin/env python3
2
3#
4# Copyright (C) 2018 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19"""This module contains the unit tests to check the Parser class."""
20
21import unittest
22
23from blueprint import Lexer, Parser, String, VarRef
24
25
26#------------------------------------------------------------------------------
27# Variable Definition
28#------------------------------------------------------------------------------
29
30class DefineVarTest(unittest.TestCase):
31    def test_define_var(self):
32        parser = Parser(None)
33
34        str1 = String(1)
35        parser.define_var('a', str1)
36        self.assertEqual(len(parser.var_defs), 1)
37        self.assertEqual(len(parser.vars), 1)
38        self.assertIn('a', parser.vars)
39        self.assertIs(parser.vars['a'], str1)
40
41        str2 = String(2)
42        parser.define_var('a', str2)
43        self.assertEqual(len(parser.var_defs), 2)
44        self.assertEqual(len(parser.vars), 1)
45        self.assertIn('a', parser.vars)
46        self.assertIs(parser.vars['a'], str2)
47
48
49    def test_create_var_ref(self):
50        parser = Parser(None)
51
52        str1 = String(1)
53        parser.define_var('a', str1)
54
55        var1 = parser.create_var_ref('a')
56        self.assertIsInstance(var1, VarRef)
57        self.assertEqual(var1.name, 'a')
58        self.assertIs(var1.value, str1)
59
60        var2 = parser.create_var_ref('b')
61        self.assertIsInstance(var2, VarRef)
62        self.assertEqual(var2.name, 'b')
63        self.assertIs(var2.value, None)
64
65
66#------------------------------------------------------------------------------
67# Parser
68#------------------------------------------------------------------------------
69
70class ParserTest(unittest.TestCase):
71    def test_assign_string(self):
72        lexer = Lexer('a = "example"')
73
74        parser = Parser(lexer)
75        parser.parse()
76        self.assertEqual(parser.var_defs[0][0], 'a')
77        self.assertEqual(repr(parser.var_defs[0][1]), repr('example'))
78
79
80    def test_list_empty(self):
81        lexer = Lexer('a = []')
82
83        parser = Parser(lexer)
84        parser.parse()
85        self.assertEqual(parser.var_defs[0][0], 'a')
86        self.assertEqual(repr(parser.var_defs[0][1]), repr([]))
87
88
89    def test_list_one_element(self):
90        lexer = Lexer('a = ["x"]')
91
92        parser = Parser(lexer)
93        parser.parse()
94        self.assertEqual(parser.var_defs[0][0], 'a')
95        self.assertEqual(repr(parser.var_defs[0][1]), repr(['x']))
96
97
98    def test_list_one_element_comma(self):
99        lexer = Lexer('a = ["x",]')
100
101        parser = Parser(lexer)
102        parser.parse()
103        self.assertEqual(parser.var_defs[0][0], 'a')
104        self.assertEqual(repr(parser.var_defs[0][1]), repr(['x']))
105
106
107    def test_list_two_elements(self):
108        lexer = Lexer('a = ["x", "y"]')
109
110        parser = Parser(lexer)
111        parser.parse()
112        self.assertEqual(parser.var_defs[0][0], 'a')
113        self.assertEqual(repr(parser.var_defs[0][1]), repr(['x', 'y']))
114
115
116    def test_list_two_elements_comma(self):
117        lexer = Lexer('a = ["x", "y",]')
118
119        parser = Parser(lexer)
120        parser.parse()
121        self.assertEqual(parser.var_defs[0][0], 'a')
122        self.assertEqual(repr(parser.var_defs[0][1]), repr(['x', 'y']))
123
124
125    def test_dict_empty(self):
126        lexer = Lexer('a = {}')
127
128        parser = Parser(lexer)
129        parser.parse()
130        self.assertEqual(parser.var_defs[0][0], 'a')
131        self.assertEqual(repr(parser.var_defs[0][1]), repr({}))
132
133
134    def test_dict_one_element(self):
135        lexer = Lexer('a = {x: "1"}')
136
137        parser = Parser(lexer)
138        parser.parse()
139        self.assertEqual(parser.var_defs[0][0], 'a')
140        self.assertEqual(repr(parser.var_defs[0][1]), '{x: \'1\'}')
141
142
143    def test_dict_one_element_comma(self):
144        lexer = Lexer('a = {x: "1",}')
145
146        parser = Parser(lexer)
147        parser.parse()
148        self.assertEqual(parser.var_defs[0][0], 'a')
149        self.assertEqual(repr(parser.var_defs[0][1]), '{x: \'1\'}')
150
151
152    def test_dict_two_elements(self):
153        lexer = Lexer('a = {x: "1", y: "2"}')
154
155        parser = Parser(lexer)
156        parser.parse()
157        self.assertEqual(parser.var_defs[0][0], 'a')
158        self.assertEqual(repr(parser.var_defs[0][1]), '{x: \'1\', y: \'2\'}')
159
160
161    def test_dict_two_elements_comma(self):
162        lexer = Lexer('a = {x: "1", y: "2",}')
163
164        parser = Parser(lexer)
165        parser.parse()
166        self.assertEqual(parser.var_defs[0][0], 'a')
167        self.assertEqual(repr(parser.var_defs[0][1]), '{x: \'1\', y: \'2\'}')
168
169
170if __name__ == '__main__':
171    unittest.main()
172