1# Copyright 2018, 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.
14
15"""
16Classes for test mapping related objects
17"""
18
19
20import copy
21
22
23class TestDetail(object):
24    """Stores the test details set in a TEST_MAPPING file."""
25
26    def __init__(self, details):
27        """TestDetail constructor
28
29        Parse test detail from a dictionary, e.g.,
30        {
31          "name": "SettingsUnitTests",
32          "options": [
33            {
34              "instrumentation-arg":
35                  "annotation=android.platform.test.annotations.Presubmit"
36            }
37          ]
38        }
39
40        Args:
41            details: A dictionary of test detail.
42        """
43        self.name = details['name']
44        self.options = []
45        options = details.get('options', [])
46        for option in options:
47            assert len(option) == 1, 'Each option can only have one key.'
48            self.options.append(copy.deepcopy(option).popitem())
49        self.options.sort(key=lambda o: o[0])
50
51    def __str__(self):
52        """String value of the TestDetail object."""
53        if not self.options:
54            return self.name
55        options = ''
56        for option in self.options:
57            options += '%s: %s, ' % option
58
59        return '%s (%s)' % (self.name, options.strip(', '))
60
61    def __hash__(self):
62        """Get the hash of TestDetail based on the details"""
63        return hash(str(self))
64
65    def __eq__(self, other):
66        return str(self) == str(other)
67