1#!/usr/bin/env python3
2#
3#   Copyright 2017 - 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
17from mock import Mock
18import unittest
19import tempfile
20
21from acts import keys
22from acts import test_runner
23
24import mock_controller
25
26
27class TestRunnerTest(unittest.TestCase):
28    def setUp(self):
29        self.tmp_dir = tempfile.mkdtemp()
30        self.base_mock_test_config = {
31            "testbed": {
32                "name": "SampleTestBed",
33            },
34            "logpath": self.tmp_dir,
35            "cli_args": None,
36            "testpaths": ["./"],
37            "icecream": 42,
38            "extra_param": "haha"
39        }
40
41    def create_mock_context(self):
42        context = Mock()
43        context.__exit__ = Mock()
44        context.__enter__ = Mock()
45        return context
46
47    def create_test_classes(self, class_names):
48        return {
49            class_name: Mock(return_value=self.create_mock_context())
50            for class_name in class_names
51        }
52
53    def test_class_name_pattern_single(self):
54        class_names = ['test_class_1', 'test_class_2']
55        pattern = 'test*1'
56        tr = test_runner.TestRunner(self.base_mock_test_config, [(pattern,
57                                                                  None)])
58
59        test_classes = self.create_test_classes(class_names)
60        tr.import_test_modules = Mock(return_value=test_classes)
61        tr.run()
62        self.assertTrue(test_classes[class_names[0]].called)
63        self.assertFalse(test_classes[class_names[1]].called)
64
65    def test_class_name_pattern_multi(self):
66        class_names = ['test_class_1', 'test_class_2', 'other_name']
67        pattern = 'test_class*'
68        tr = test_runner.TestRunner(self.base_mock_test_config, [(pattern,
69                                                                  None)])
70
71        test_classes = self.create_test_classes(class_names)
72        tr.import_test_modules = Mock(return_value=test_classes)
73        tr.run()
74        self.assertTrue(test_classes[class_names[0]].called)
75        self.assertTrue(test_classes[class_names[1]].called)
76        self.assertFalse(test_classes[class_names[2]].called)
77
78    def test_class_name_pattern_question_mark(self):
79        class_names = ['test_class_1', 'test_class_12']
80        pattern = 'test_class_?'
81        tr = test_runner.TestRunner(self.base_mock_test_config, [(pattern,
82                                                                  None)])
83
84        test_classes = self.create_test_classes(class_names)
85        tr.import_test_modules = Mock(return_value=test_classes)
86        tr.run()
87        self.assertTrue(test_classes[class_names[0]].called)
88        self.assertFalse(test_classes[class_names[1]].called)
89
90    def test_class_name_pattern_char_seq(self):
91        class_names = ['test_class_1', 'test_class_2', 'test_class_3']
92        pattern = 'test_class_[1357]'
93        tr = test_runner.TestRunner(self.base_mock_test_config, [(pattern,
94                                                                  None)])
95
96        test_classes = self.create_test_classes(class_names)
97        tr.import_test_modules = Mock(return_value=test_classes)
98        tr.run()
99        self.assertTrue(test_classes[class_names[0]].called)
100        self.assertFalse(test_classes[class_names[1]].called)
101        self.assertTrue(test_classes[class_names[2]].called)
102
103
104if __name__ == "__main__":
105    unittest.main()
106