def test_symbol_in_arch(self) -> None: self.assertTrue(symbolfile.symbol_in_arch(Tags(), Arch('arm'))) self.assertTrue( symbolfile.symbol_in_arch(Tags.from_strs(['arm']), Arch('arm'))) self.assertFalse( symbolfile.symbol_in_arch(Tags.from_strs(['x86']), Arch('arm')))
def test_parse_version(self) -> None: input_file = io.StringIO(textwrap.dedent("""\ VERSION_1 { # foo bar baz; qux; # woodly doodly }; VERSION_2 { } VERSION_1; # asdf """)) parser = symbolfile.SymbolFileParser(input_file, {}, Arch('arm'), 16, False, False) parser.next_line() version = parser.parse_version() self.assertEqual('VERSION_1', version.name) self.assertIsNone(version.base) self.assertEqual(Tags.from_strs(['foo', 'bar']), version.tags) expected_symbols = [ symbolfile.Symbol('baz', Tags()), symbolfile.Symbol('qux', Tags.from_strs(['woodly', 'doodly'])), ] self.assertEqual(expected_symbols, version.symbols) parser.next_line() version = parser.parse_version() self.assertEqual('VERSION_2', version.name) self.assertEqual('VERSION_1', version.base) self.assertEqual(Tags(), version.tags)
def test_write(self) -> None: src_file = io.StringIO() version_file = io.StringIO() symbol_list_file = io.StringIO() generator = ndkstubgen.Generator(src_file, version_file, symbol_list_file, Arch('arm'), 9, False, False) versions = [ symbolfile.Version('VERSION_1', None, Tags(), [ symbolfile.Symbol('foo', Tags()), symbolfile.Symbol('bar', Tags.from_strs(['var'])), symbolfile.Symbol('woodly', Tags.from_strs(['weak'])), symbolfile.Symbol('doodly', Tags.from_strs(['weak', 'var'])), ]), symbolfile.Version('VERSION_2', 'VERSION_1', Tags(), [ symbolfile.Symbol('baz', Tags()), ]), symbolfile.Version('VERSION_3', 'VERSION_1', Tags(), [ symbolfile.Symbol('qux', Tags.from_strs(['versioned=14'])), ]), ] generator.write(versions) expected_src = textwrap.dedent("""\ void foo() {} int bar = 0; __attribute__((weak)) void woodly() {} __attribute__((weak)) int doodly = 0; void baz() {} void qux() {} """) self.assertEqual(expected_src, src_file.getvalue()) expected_version = textwrap.dedent("""\ VERSION_1 { global: foo; bar; woodly; doodly; }; VERSION_2 { global: baz; } VERSION_1; """) self.assertEqual(expected_version, version_file.getvalue()) expected_allowlist = textwrap.dedent("""\ [abi_symbol_list] foo bar woodly doodly baz qux """) self.assertEqual(expected_allowlist, symbol_list_file.getvalue())
def test_omit_api(self) -> None: self.assertFalse( symbolfile.should_omit_symbol(symbolfile.Symbol('foo', Tags()), Arch('arm'), 9, False, False)) self.assertFalse( symbolfile.should_omit_symbol( symbolfile.Symbol('foo', Tags.from_strs(['introduced=9'])), Arch('arm'), 9, False, False)) self.assertTrue( symbolfile.should_omit_symbol( symbolfile.Symbol('foo', Tags.from_strs(['introduced=14'])), Arch('arm'), 9, False, False))
def test_omit_arch(self) -> None: self.assertFalse( symbolfile.should_omit_symbol(symbolfile.Symbol('foo', Tags()), Arch('arm'), 9, False, False)) self.assertFalse( symbolfile.should_omit_symbol( symbolfile.Symbol('foo', Tags.from_strs(['arm'])), Arch('arm'), 9, False, False)) self.assertTrue( symbolfile.should_omit_symbol( symbolfile.Symbol('foo', Tags.from_strs(['x86'])), Arch('arm'), 9, False, False))
def test_omit_arch(self) -> None: self.assertFalse( symbolfile.should_omit_version( symbolfile.Version('foo', None, Tags(), []), Arch('arm'), 9, False, False)) self.assertFalse( symbolfile.should_omit_version( symbolfile.Version('foo', None, Tags.from_strs(['arm']), []), Arch('arm'), 9, False, False)) self.assertTrue( symbolfile.should_omit_version( symbolfile.Version('foo', None, Tags.from_strs(['x86']), []), Arch('arm'), 9, False, False))
def test_parse_symbol(self) -> None: input_file = io.StringIO(textwrap.dedent("""\ foo; bar; # baz qux """)) parser = symbolfile.SymbolFileParser(input_file, {}, Arch('arm'), 16, False, False) parser.next_line() symbol = parser.parse_symbol() self.assertEqual('foo', symbol.name) self.assertEqual(Tags(), symbol.tags) parser.next_line() symbol = parser.parse_symbol() self.assertEqual('bar', symbol.name) self.assertEqual(Tags.from_strs(['baz', 'qux']), symbol.tags)
def test_omit_private(self) -> None: self.assertFalse( symbolfile.should_omit_version( symbolfile.Version('foo', None, Tags(), []), Arch('arm'), 9, False, False)) self.assertTrue( symbolfile.should_omit_version( symbolfile.Version('foo_PRIVATE', None, Tags(), []), Arch('arm'), 9, False, False)) self.assertTrue( symbolfile.should_omit_version( symbolfile.Version('foo_PLATFORM', None, Tags(), []), Arch('arm'), 9, False, False)) self.assertTrue( symbolfile.should_omit_version( symbolfile.Version('foo', None, Tags.from_strs(['platform-only']), []), Arch('arm'), 9, False, False))
def test_omit_version(self) -> None: # Thorough testing of the cases involved here is handled by # OmitVersionTest, PrivateVersionTest, and SymbolPresenceTest. src_file = io.StringIO() version_file = io.StringIO() symbol_list_file = io.StringIO() generator = ndkstubgen.Generator(src_file, version_file, symbol_list_file, Arch('arm'), 9, False, False) version = symbolfile.Version('VERSION_PRIVATE', None, Tags(), [ symbolfile.Symbol('foo', Tags()), ]) generator.write_version(version) self.assertEqual('', src_file.getvalue()) self.assertEqual('', version_file.getvalue()) version = symbolfile.Version('VERSION', None, Tags.from_strs(['x86']), [ symbolfile.Symbol('foo', Tags()), ]) generator.write_version(version) self.assertEqual('', src_file.getvalue()) self.assertEqual('', version_file.getvalue()) version = symbolfile.Version('VERSION', None, Tags.from_strs(['introduced=14']), [ symbolfile.Symbol('foo', Tags()), ]) generator.write_version(version) self.assertEqual('', src_file.getvalue()) self.assertEqual('', version_file.getvalue())
def test_parse(self) -> None: input_file = io.StringIO(textwrap.dedent("""\ VERSION_1 { local: hidden1; global: foo; bar; # baz }; VERSION_2 { # wasd # Implicit global scope. woodly; doodly; # asdf local: qwerty; } VERSION_1; """)) parser = symbolfile.SymbolFileParser(input_file, {}, Arch('arm'), 16, False, False) versions = parser.parse() expected = [ symbolfile.Version('VERSION_1', None, Tags(), [ symbolfile.Symbol('foo', Tags()), symbolfile.Symbol('bar', Tags.from_strs(['baz'])), ]), symbolfile.Version( 'VERSION_2', 'VERSION_1', Tags.from_strs(['wasd']), [ symbolfile.Symbol('woodly', Tags()), symbolfile.Symbol('doodly', Tags.from_strs(['asdf'])), ]), ] self.assertEqual(expected, versions)
def test_parse_llndk_apex_symbol(self) -> None: input_file = io.StringIO(textwrap.dedent("""\ VERSION_1 { foo; bar; # llndk baz; # llndk apex qux; # apex }; """)) parser = symbolfile.SymbolFileParser(input_file, {}, Arch('arm'), 16, False, True) parser.next_line() version = parser.parse_version() self.assertEqual('VERSION_1', version.name) self.assertIsNone(version.base) expected_symbols = [ symbolfile.Symbol('foo', Tags()), symbolfile.Symbol('bar', Tags.from_strs(['llndk'])), symbolfile.Symbol('baz', Tags.from_strs(['llndk', 'apex'])), symbolfile.Symbol('qux', Tags.from_strs(['apex'])), ] self.assertEqual(expected_symbols, version.symbols)
def test_get_tags(self) -> None: self.assertEqual(Tags.from_strs(['foo', 'bar']), symbolfile.get_tags('# foo bar', {})) self.assertEqual(Tags.from_strs(['bar', 'baz']), symbolfile.get_tags('foo # bar baz', {}))
def test_get_tags_no_tags(self) -> None: self.assertEqual(Tags(), symbolfile.get_tags('', {})) self.assertEqual(Tags(), symbolfile.get_tags('foo bar baz', {}))
def mock_version(name: str) -> Version: return Version(name, base=None, tags=Tags(), symbols=[])