|
| 1 | +# Copyright (c) 2025 Nordic Semiconductor ASA |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import textwrap |
| 6 | + |
| 7 | +import pytest |
| 8 | +from twister_harness.helpers.utils import match_lines, match_no_lines |
| 9 | + |
| 10 | +OUTPUT_LINES = textwrap.dedent("""\ |
| 11 | + The Zen of Python, by Tim Peters |
| 12 | + Beautiful is better than ugly. |
| 13 | + Explicit is better than implicit. |
| 14 | + Simple is better than complex.\ |
| 15 | +""").split('\n') |
| 16 | + |
| 17 | + |
| 18 | +CHECK_LINES = textwrap.dedent("""\ |
| 19 | + Although never is often better than right now. |
| 20 | + If the implementation is hard to explain, it's a bad idea. |
| 21 | + If the implementation is easy to explain, it may be a good idea. |
| 22 | + Namespaces are one honking great idea -- let's do more of those!\ |
| 23 | +""").split('\n') |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def output_lines() -> list[str]: |
| 28 | + return OUTPUT_LINES |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize( |
| 32 | + "expected", |
| 33 | + [ |
| 34 | + OUTPUT_LINES[0:1], # one line |
| 35 | + OUTPUT_LINES[0:2], # two lines |
| 36 | + OUTPUT_LINES[-1:], # last line |
| 37 | + [OUTPUT_LINES[0][2:-2]], # check partial of a text |
| 38 | + ], |
| 39 | +) |
| 40 | +def test_match_lines_positive(expected, output_lines): |
| 41 | + match_lines(output_lines, expected) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize( |
| 45 | + "not_expected", |
| 46 | + [ |
| 47 | + CHECK_LINES[0:1], |
| 48 | + CHECK_LINES[0:2], |
| 49 | + CHECK_LINES[-1:], |
| 50 | + [CHECK_LINES[-1][2:-2]], |
| 51 | + [CHECK_LINES[0], OUTPUT_LINES[0]], # one line match, one not |
| 52 | + CHECK_LINES[::-1], # reverted order |
| 53 | + ], |
| 54 | +) |
| 55 | +def test_match_lines_negative(not_expected, output_lines): |
| 56 | + with pytest.raises(AssertionError, match="Missing lines.*"): |
| 57 | + match_lines(output_lines, not_expected) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "not_expected", |
| 62 | + [ |
| 63 | + OUTPUT_LINES[0:1], |
| 64 | + OUTPUT_LINES[1:3], |
| 65 | + OUTPUT_LINES[-1:], |
| 66 | + [CHECK_LINES[0], OUTPUT_LINES[0]], # one line match, one not |
| 67 | + OUTPUT_LINES[::-1], # reverted order |
| 68 | + ], |
| 69 | +) |
| 70 | +def test_match_no_lines_negative(not_expected, output_lines): |
| 71 | + with pytest.raises(AssertionError, match="Found lines that should not be present.*"): |
| 72 | + match_no_lines(output_lines, not_expected) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize( |
| 76 | + "expected", |
| 77 | + [ |
| 78 | + CHECK_LINES[0:1], |
| 79 | + CHECK_LINES[3:5], |
| 80 | + CHECK_LINES[-1:], |
| 81 | + ], |
| 82 | +) |
| 83 | +def test_match_no_lines_positive(expected, output_lines): |
| 84 | + match_no_lines(output_lines, expected) |
0 commit comments