1# Copyright (C) 2020 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://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, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14import unittest 15 16from perf2cfg import exceptions 17from perf2cfg import parse 18 19 20class TestParse(unittest.TestCase): 21 22 def test_build_flags_without_arguments(self): 23 got = parse.build_flags([]) 24 self.assertEqual(got.strip(), 'flags') 25 26 def test_build_flags_with_arguments(self): 27 got = parse.build_flags(['catch_block', 'critical']) 28 self.assertEqual(got.strip(), 'flags "catch_block" "critical"') 29 30 def test_build_name(self): 31 got = parse.build_name('void hcf()') 32 self.assertEqual(got.strip(), 'name "void hcf()"') 33 34 def test_parse_invalid_address_line(self): 35 with self.assertRaises(exceptions.ParseError) as ctx: 36 parse.parse_address(':)') 37 38 self.assertEqual(str(ctx.exception), 'Expected an address') 39 40 def test_parse_valid_address_line(self): 41 got = parse.parse_address('0x0000001c: d503201f nop') 42 self.assertEqual(got, 0x1c) 43 44 def test_parse_flags_wrong_directive(self): 45 with self.assertRaises(exceptions.ParseError) as ctx: 46 parse.parse_flags('name "void hcf()"') 47 48 self.assertEqual(str(ctx.exception), 'Expected a `flags` directive') 49 50 def test_parse_flags_without_arguments(self): 51 got = parse.parse_flags('flags') 52 self.assertEqual(got, []) 53 54 def test_parse_flags_with_arguments(self): 55 got = parse.parse_flags('flags "catch_block" "critical"') 56 self.assertEqual(got, ['catch_block', 'critical']) 57 58 def test_parse_name_wrong_directive(self): 59 with self.assertRaises(exceptions.ParseError) as ctx: 60 parse.parse_name('flags "catch_block" "critical"') 61 62 self.assertEqual(str(ctx.exception), 'Expected a `name` directive') 63 64 def test_parse_name_without_argument(self): 65 with self.assertRaises(exceptions.ParseError) as ctx: 66 parse.parse_name('name') 67 68 self.assertEqual(str(ctx.exception), 69 'Expected an argument to the `name` directive') 70 71 def test_parse_name_with_argument(self): 72 got = parse.parse_name('name "void hcf()"') 73 self.assertEqual(got, 'void hcf()') 74