|
| 1 | +# Copyright 2025 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 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import re |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +import logcat_error_report as sut |
| 22 | + |
| 23 | + |
| 24 | +class TestRegularExpressionPatterns: |
| 25 | + @pytest.mark.parametrize( |
| 26 | + "string", |
| 27 | + [ |
| 28 | + "", |
| 29 | + "XTestRunner: started: fooTest1234", |
| 30 | + "TestRunner: started:fooTest1234", |
| 31 | + pytest.param( |
| 32 | + "TestRunner: started: fooTest1234", |
| 33 | + marks=pytest.mark.xfail( |
| 34 | + reason="make sure that the test would otherwise pass on match", |
| 35 | + strict=True, |
| 36 | + ), |
| 37 | + ), |
| 38 | + ], |
| 39 | + ) |
| 40 | + def test_test_started_pattern_no_match(self, string: str) -> None: |
| 41 | + assert re.search(sut.TEST_STARTED_PATTERN, string) is None |
| 42 | + |
| 43 | + @pytest.mark.parametrize( |
| 44 | + ("string", "expected_name"), |
| 45 | + [ |
| 46 | + ("TestRunner: started: fooTest1234", "fooTest1234"), |
| 47 | + (" TestRunner: started: fooTest1234", "fooTest1234"), |
| 48 | + ("TestRunner: started: fooTest1234", "fooTest1234"), |
| 49 | + ("TestRunner: started: fooTest1234 ", "fooTest1234"), |
| 50 | + ("TestRunner: started: fooTest1234(abc.123)", "fooTest1234(abc.123)"), |
| 51 | + ("TestRunner: started: a $ 2 ^ %% . ", "a $ 2 ^ %% ."), |
| 52 | + pytest.param( |
| 53 | + "i do not match the pattern", |
| 54 | + None, |
| 55 | + marks=pytest.mark.xfail( |
| 56 | + reason="make sure that the test would otherwise pass on match", |
| 57 | + strict=True, |
| 58 | + ), |
| 59 | + ), |
| 60 | + ], |
| 61 | + ) |
| 62 | + def test_test_started_pattern_match(self, string: str, expected_name: str) -> None: |
| 63 | + match = re.search(sut.TEST_STARTED_PATTERN, string) |
| 64 | + assert match is not None |
| 65 | + assert match.group("name") == expected_name |
| 66 | + |
| 67 | + @pytest.mark.parametrize( |
| 68 | + "string", |
| 69 | + [ |
| 70 | + "", |
| 71 | + "XTestRunner: finished: fooTest1234", |
| 72 | + "TestRunner: finished:fooTest1234", |
| 73 | + pytest.param( |
| 74 | + "TestRunner: finished: fooTest1234", |
| 75 | + marks=pytest.mark.xfail( |
| 76 | + reason="make sure that the test would otherwise pass on match", |
| 77 | + strict=True, |
| 78 | + ), |
| 79 | + ), |
| 80 | + ], |
| 81 | + ) |
| 82 | + def test_test_finished_pattern_no_match(self, string: str) -> None: |
| 83 | + assert re.search(sut.TEST_FINISHED_PATTERN, string) is None |
| 84 | + |
| 85 | + @pytest.mark.parametrize( |
| 86 | + ("string", "expected_name"), |
| 87 | + [ |
| 88 | + ("TestRunner: finished: fooTest1234", "fooTest1234"), |
| 89 | + (" TestRunner: finished: fooTest1234", "fooTest1234"), |
| 90 | + ("TestRunner: finished: fooTest1234", "fooTest1234"), |
| 91 | + ("TestRunner: finished: fooTest1234 ", "fooTest1234"), |
| 92 | + ("TestRunner: finished: fooTest1234(abc.123)", "fooTest1234(abc.123)"), |
| 93 | + ("TestRunner: finished: a $ 2 ^ %% . ", "a $ 2 ^ %% ."), |
| 94 | + pytest.param( |
| 95 | + "i do not match the pattern", |
| 96 | + None, |
| 97 | + marks=pytest.mark.xfail( |
| 98 | + reason="make sure that the test would otherwise pass on match", |
| 99 | + strict=True, |
| 100 | + ), |
| 101 | + ), |
| 102 | + ], |
| 103 | + ) |
| 104 | + def test_test_finished_pattern_match(self, string: str, expected_name: str) -> None: |
| 105 | + match = re.search(sut.TEST_FINISHED_PATTERN, string) |
| 106 | + assert match is not None |
| 107 | + assert match.group("name") == expected_name |
| 108 | + |
| 109 | + @pytest.mark.parametrize( |
| 110 | + "string", |
| 111 | + [ |
| 112 | + "", |
| 113 | + "XTestRunner: failed: fooTest1234", |
| 114 | + "TestRunner: failed:fooTest1234", |
| 115 | + pytest.param( |
| 116 | + "TestRunner: failed: fooTest1234", |
| 117 | + marks=pytest.mark.xfail( |
| 118 | + reason="make sure that the test would otherwise pass on match", |
| 119 | + strict=True, |
| 120 | + ), |
| 121 | + ), |
| 122 | + ], |
| 123 | + ) |
| 124 | + def test_test_failed_pattern_no_match(self, string: str) -> None: |
| 125 | + assert re.search(sut.TEST_FAILED_PATTERN, string) is None |
| 126 | + |
| 127 | + @pytest.mark.parametrize( |
| 128 | + ("string", "expected_name"), |
| 129 | + [ |
| 130 | + ("TestRunner: failed: fooTest1234", "fooTest1234"), |
| 131 | + (" TestRunner: failed: fooTest1234", "fooTest1234"), |
| 132 | + ("TestRunner: failed: fooTest1234", "fooTest1234"), |
| 133 | + ("TestRunner: failed: fooTest1234 ", "fooTest1234"), |
| 134 | + ("TestRunner: failed: fooTest1234(abc.123)", "fooTest1234(abc.123)"), |
| 135 | + ("TestRunner: failed: a $ 2 ^ %% . ", "a $ 2 ^ %% ."), |
| 136 | + pytest.param( |
| 137 | + "i do not match the pattern", |
| 138 | + None, |
| 139 | + marks=pytest.mark.xfail( |
| 140 | + reason="make sure that the test would otherwise pass on match", |
| 141 | + strict=True, |
| 142 | + ), |
| 143 | + ), |
| 144 | + ], |
| 145 | + ) |
| 146 | + def test_test_failed_pattern_match(self, string: str, expected_name: str) -> None: |
| 147 | + match = re.search(sut.TEST_FAILED_PATTERN, string) |
| 148 | + assert match is not None |
| 149 | + assert match.group("name") == expected_name |
0 commit comments