summaryrefslogtreecommitdiff
path: root/perf2cfg/tests/test_parse.py
blob: 40ec243bb55597ad5298aa4bed26a6f658018b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright (C) 2020 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest

from perf2cfg import exceptions
from perf2cfg import parse


class TestParse(unittest.TestCase):

    def test_build_flags_without_arguments(self):
        got = parse.build_flags([])
        self.assertEqual(got.strip(), 'flags')

    def test_build_flags_with_arguments(self):
        got = parse.build_flags(['catch_block', 'critical'])
        self.assertEqual(got.strip(), 'flags "catch_block" "critical"')

    def test_build_name(self):
        got = parse.build_name('void hcf()')
        self.assertEqual(got.strip(), 'name "void hcf()"')

    def test_parse_invalid_address_line(self):
        with self.assertRaises(exceptions.ParseError) as ctx:
            parse.parse_address(':)')

        self.assertEqual(str(ctx.exception), 'Expected an address')

    def test_parse_valid_address_line(self):
        got = parse.parse_address('0x0000001c: d503201f nop')
        self.assertEqual(got, 0x1c)

    def test_parse_flags_wrong_directive(self):
        with self.assertRaises(exceptions.ParseError) as ctx:
            parse.parse_flags('name "void hcf()"')

        self.assertEqual(str(ctx.exception), 'Expected a `flags` directive')

    def test_parse_flags_without_arguments(self):
        got = parse.parse_flags('flags')
        self.assertEqual(got, [])

    def test_parse_flags_with_arguments(self):
        got = parse.parse_flags('flags "catch_block" "critical"')
        self.assertEqual(got, ['catch_block', 'critical'])

    def test_parse_name_wrong_directive(self):
        with self.assertRaises(exceptions.ParseError) as ctx:
            parse.parse_name('flags "catch_block" "critical"')

        self.assertEqual(str(ctx.exception), 'Expected a `name` directive')

    def test_parse_name_without_argument(self):
        with self.assertRaises(exceptions.ParseError) as ctx:
            parse.parse_name('name')

        self.assertEqual(str(ctx.exception),
                         'Expected an argument to the `name` directive')

    def test_parse_name_with_argument(self):
        got = parse.parse_name('name "void hcf()"')
        self.assertEqual(got, 'void hcf()')