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 23 def test_blank_symbol(self): 24 sym = pw_symbolizer.Symbol(address=0x00000000, name='', file='', line=0) 25 self.assertEqual('??:?', sym.file_and_line()) 26 self.assertEqual('0x00000000 (??:?)', str(sym)) 27 28 def test_default_symbol(self): 29 sym = pw_symbolizer.Symbol(address=0x0000A400) 30 self.assertEqual('??:?', sym.file_and_line()) 31 self.assertEqual('0x0000A400 (??:?)', str(sym)) 32 33 def test_to_str(self): 34 sym = pw_symbolizer.Symbol( 35 address=0x12345678, 36 name='idle_thread_context', 37 file='device/system/threads.cc', 38 line=59, 39 ) 40 self.assertEqual('device/system/threads.cc:59', sym.file_and_line()) 41 self.assertEqual( 42 'idle_thread_context (device/system/threads.cc:59)', str(sym) 43 ) 44 45 def test_truncated_filename(self): 46 sym = pw_symbolizer.Symbol( 47 address=0x12345678, 48 name='idle_thread_context', 49 file='device/system/threads.cc', 50 line=59, 51 ) 52 self.assertEqual( 53 'idle_thread_context ([...]stem/threads.cc:59)', 54 sym.to_string(max_filename_len=15), 55 ) 56 57 58class TestFakeSymbolizer(unittest.TestCase): 59 """Tests the FakeSymbolizer class.""" 60 61 def test_empty_db(self): 62 symbolizer = pw_symbolizer.FakeSymbolizer() 63 symbol = symbolizer.symbolize(0x404) 64 self.assertEqual(symbol.address, 0x404) 65 self.assertEqual(symbol.name, '') 66 self.assertEqual(symbol.file, '') 67 68 def test_db_with_entries(self): 69 known_symbols = ( 70 pw_symbolizer.Symbol( 71 0x404, 'do_a_flip(int n)', 'source/tricks.cc', 1403 72 ), 73 pw_symbolizer.Symbol( 74 0xFFFFFFFF, 75 'a_variable_here_would_be_funny', 76 'source/globals.cc', 77 21, 78 ), 79 ) 80 symbolizer = pw_symbolizer.FakeSymbolizer(known_symbols) 81 82 symbol = symbolizer.symbolize(0x404) 83 self.assertEqual(symbol.address, 0x404) 84 self.assertEqual(symbol.name, 'do_a_flip(int n)') 85 self.assertEqual(symbol.file, 'source/tricks.cc') 86 self.assertEqual(symbol.line, 1403) 87 88 symbol = symbolizer.symbolize(0xFFFFFFFF) 89 self.assertEqual(symbol.address, 0xFFFFFFFF) 90 self.assertEqual(symbol.name, 'a_variable_here_would_be_funny') 91 self.assertEqual(symbol.file, 'source/globals.cc') 92 self.assertEqual(symbol.line, 21) 93 94 95if __name__ == '__main__': 96 unittest.main() 97