• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Tests for pw_symbolizer's python tooling."""
15
16import unittest
17import pw_symbolizer
18
19
20class TestSymbolFormatting(unittest.TestCase):
21    """Tests Symbol objects to validate formatted output."""
22    def test_blank_symbol(self):
23        sym = pw_symbolizer.Symbol(address=0x00000000,
24                                   name='',
25                                   file='',
26                                   line=0)
27        self.assertEqual('??:?', sym.file_and_line())
28        self.assertEqual('0x00000000 (??:?)', str(sym))
29
30    def test_default_symbol(self):
31        sym = pw_symbolizer.Symbol(address=0x0000a400)
32        self.assertEqual('??:?', sym.file_and_line())
33        self.assertEqual('0x0000A400 (??:?)', str(sym))
34
35    def test_to_str(self):
36        sym = pw_symbolizer.Symbol(address=0x12345678,
37                                   name='idle_thread_context',
38                                   file='device/system/threads.cc',
39                                   line=59)
40        self.assertEqual('device/system/threads.cc:59', sym.file_and_line())
41        self.assertEqual('idle_thread_context (device/system/threads.cc:59)',
42                         str(sym))
43
44    def test_truncated_filename(self):
45        sym = pw_symbolizer.Symbol(address=0x12345678,
46                                   name='idle_thread_context',
47                                   file='device/system/threads.cc',
48                                   line=59)
49        self.assertEqual('idle_thread_context ([...]stem/threads.cc:59)',
50                         sym.to_string(max_filename_len=15))
51
52
53class TestFakeSymbolizer(unittest.TestCase):
54    """Tests the FakeSymbolizer class."""
55    def test_empty_db(self):
56        symbolizer = pw_symbolizer.FakeSymbolizer()
57        symbol = symbolizer.symbolize(0x404)
58        self.assertEqual(symbol.address, 0x404)
59        self.assertEqual(symbol.name, '')
60        self.assertEqual(symbol.file, '')
61
62    def test_db_with_entries(self):
63        known_symbols = (
64            pw_symbolizer.Symbol(0x404, 'do_a_flip(int n)', 'source/tricks.cc',
65                                 1403),
66            pw_symbolizer.Symbol(0xffffffff, 'a_variable_here_would_be_funny',
67                                 'source/globals.cc', 21),
68        )
69        symbolizer = pw_symbolizer.FakeSymbolizer(known_symbols)
70
71        symbol = symbolizer.symbolize(0x404)
72        self.assertEqual(symbol.address, 0x404)
73        self.assertEqual(symbol.name, 'do_a_flip(int n)')
74        self.assertEqual(symbol.file, 'source/tricks.cc')
75        self.assertEqual(symbol.line, 1403)
76
77        symbol = symbolizer.symbolize(0xffffffff)
78        self.assertEqual(symbol.address, 0xffffffff)
79        self.assertEqual(symbol.name, 'a_variable_here_would_be_funny')
80        self.assertEqual(symbol.file, 'source/globals.cc')
81        self.assertEqual(symbol.line, 21)
82
83
84if __name__ == '__main__':
85    unittest.main()
86