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 whether module paths are kept
20properly."""
21
22import os
23import unittest
24
25from blueprint import Lexer, Parser, RecursiveParser
26
27
28#------------------------------------------------------------------------------
29# Module Path
30#------------------------------------------------------------------------------
31
32class ModulePathTest(unittest.TestCase):
33    """Test cases for module path attribute."""
34
35    def test_module_path_from_lexer(self):
36        """Test whether the path are passed from Lexer to parsed modules."""
37        content = '''
38            cc_library {
39                name: "libfoo",
40            }
41            '''
42
43        parser = Parser(Lexer(content, path='test_path'))
44        parser.parse()
45
46        self.assertEqual('test_path', parser.modules[0][1]['_path'])
47
48
49    def test_module_path_functional(self):
50        SUBNAME = 'MockBuild.txt'
51
52        test_dir = os.path.join(
53                os.path.dirname(__file__), 'testdata', 'example')
54        test_root_file = os.path.join(test_dir, SUBNAME)
55
56        parser = RecursiveParser()
57        parser.parse_file(test_root_file, default_sub_name=SUBNAME)
58
59        named_mods = {module[1]['name']: module for module in parser.modules}
60
61        self.assertEqual(os.path.join(test_dir, 'foo', SUBNAME),
62                         named_mods['libfoo'][1]['_path'])
63        self.assertEqual(os.path.join(test_dir, 'bar', SUBNAME),
64                         named_mods['libbar'][1]['_path'])
65
66if __name__ == '__main__':
67    unittest.main()
68