xref: /aosp_15_r20/external/swiftshader/third_party/SPIRV-Tools/test/tools/expect_unittest.py (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1# Copyright (c) 2019 Google LLC
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"""Tests for the expect module."""
15
16import expect
17from spirv_test_framework import TestStatus
18import re
19import unittest
20
21
22class TestStdoutMatchADotC(expect.StdoutMatch):
23    expected_stdout = re.compile('a.c')
24
25
26class TestExpect(unittest.TestCase):
27    def test_get_object_name(self):
28        """Tests get_object_filename()."""
29        source_and_object_names = [('a.vert', 'a.vert.spv'),
30                                   ('b.frag', 'b.frag.spv'),
31                                   ('c.tesc', 'c.tesc.spv'),
32                                   ('d.tese', 'd.tese.spv'),
33                                   ('e.geom', 'e.geom.spv'),
34                                   ('f.comp', 'f.comp.spv'),
35                                   ('file', 'file.spv'), ('file.', 'file.spv'),
36                                   ('file.uk',
37                                    'file.spv'), ('file.vert.',
38                                                  'file.vert.spv'),
39                                   ('file.vert.bla',
40                                    'file.vert.spv')]
41        actual_object_names = [
42            expect.get_object_filename(f[0]) for f in source_and_object_names
43        ]
44        expected_object_names = [f[1] for f in source_and_object_names]
45
46        self.assertEqual(actual_object_names, expected_object_names)
47
48    def test_stdout_match_regex_has_match(self):
49        test = TestStdoutMatchADotC()
50        status = TestStatus(
51            test_manager=None,
52            returncode=0,
53            stdout=b'0abc1',
54            stderr=None,
55            directory=None,
56            inputs=None,
57            input_filenames=None)
58        self.assertTrue(test.check_stdout_match(status)[0])
59
60    def test_stdout_match_regex_no_match(self):
61        test = TestStdoutMatchADotC()
62        status = TestStatus(
63            test_manager=None,
64            returncode=0,
65            stdout=b'ab',
66            stderr=None,
67            directory=None,
68            inputs=None,
69            input_filenames=None)
70        self.assertFalse(test.check_stdout_match(status)[0])
71
72    def test_stdout_match_regex_empty_stdout(self):
73        test = TestStdoutMatchADotC()
74        status = TestStatus(
75            test_manager=None,
76            returncode=0,
77            stdout=b'',
78            stderr=None,
79            directory=None,
80            inputs=None,
81            input_filenames=None)
82        self.assertFalse(test.check_stdout_match(status)[0])
83