1#!/usr/bin/env python 2# 3# Copyright (C) 2016 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17"""Tests for ndk_api_coverage_parser.py.""" 18import io 19import textwrap 20import unittest 21 22from xml.etree.ElementTree import fromstring 23from symbolfile import FUTURE_API_LEVEL, SymbolFileParser 24import ndk_api_coverage_parser as nparser 25 26 27# pylint: disable=missing-docstring 28 29 30# https://stackoverflow.com/a/24349916/632035 31def etree_equal(elem1, elem2): 32 """Returns true if the two XML elements are equal. 33 34 xml.etree.ElementTree's comparison operator cares about the ordering of 35 elements and attributes, but they are stored in an unordered dict so the 36 ordering is not deterministic. 37 38 lxml is apparently API compatible with xml and does use an OrderedDict, but 39 we don't have it in the tree. 40 """ 41 if elem1.tag != elem2.tag: 42 return False 43 if elem1.text != elem2.text: 44 return False 45 if elem1.tail != elem2.tail: 46 return False 47 if elem1.attrib != elem2.attrib: 48 return False 49 if len(elem1) != len(elem2): 50 return False 51 return all(etree_equal(c1, c2) for c1, c2 in zip(elem1, elem2)) 52 53 54class ApiCoverageSymbolFileParserTest(unittest.TestCase): 55 def test_parse(self): 56 input_file = io.StringIO(textwrap.dedent(u"""\ 57 LIBLOG { # introduced-arm64=24 introduced-x86=24 introduced-x86_64=24 58 global: 59 android_name_to_log_id; # apex llndk introduced=23 60 android_log_id_to_name; # llndk arm 61 __android_log_assert; # introduced-x86=23 62 __android_log_buf_print; # var 63 __android_log_buf_write; 64 local: 65 *; 66 }; 67 68 LIBLOG_PLATFORM { 69 android_fdtrack; # llndk 70 android_net; # introduced=23 71 }; 72 73 LIBLOG_FOO { # var 74 android_var; 75 }; 76 """)) 77 parser = SymbolFileParser(input_file, {}, "", FUTURE_API_LEVEL, True, True) 78 generator = nparser.XmlGenerator(io.StringIO()) 79 result = generator.convertToXml(parser.parse()) 80 expected = fromstring('<ndk-library><symbol apex="True" arch="" introduced="23" introduced-arm64="24" introduced-x86="24" introduced-x86_64="24" is_deprecated="False" is_platform="False" llndk="True" name="android_name_to_log_id" /><symbol arch="arm" introduced-arm64="24" introduced-x86="24" introduced-x86_64="24" is_deprecated="False" is_platform="False" llndk="True" name="android_log_id_to_name" /><symbol arch="" introduced-arm64="24" introduced-x86="23" introduced-x86_64="24" is_deprecated="False" is_platform="False" name="__android_log_assert" /><symbol arch="" introduced-arm64="24" introduced-x86="24" introduced-x86_64="24" is_deprecated="False" is_platform="False" name="__android_log_buf_write" /><symbol arch="" is_deprecated="False" is_platform="True" llndk="True" name="android_fdtrack" /><symbol arch="" introduced="23" is_deprecated="False" is_platform="True" name="android_net" /></ndk-library>') 81 self.assertTrue(etree_equal(expected, result)) 82 83 84def main(): 85 suite = unittest.TestLoader().loadTestsFromName(__name__) 86 unittest.TextTestRunner(verbosity=3).run(suite) 87 88 89if __name__ == '__main__': 90 main() 91