diff options
author | Diego Russo <diego.russo@arm.com> | 2022-05-30 13:34:14 +0100 |
---|---|---|
committer | Diego Russo <diego.russo@arm.com> | 2022-05-30 13:34:14 +0100 |
commit | 0efca3cadbad5517a59884576ddb90cfe7ac30f8 (patch) | |
tree | abed6cb6fbf3c439fc8d947f505b6a53d5daeb1e /tests/mlia/test_utils_console.py | |
parent | 0777092695c143c3a54680b5748287d40c914c35 (diff) | |
download | mlia-0efca3cadbad5517a59884576ddb90cfe7ac30f8.tar.gz |
Add MLIA codebase0.3.0-rc.1
Add MLIA codebase including sources and tests.
Change-Id: Id41707559bd721edd114793618d12ccd188d8dbd
Diffstat (limited to 'tests/mlia/test_utils_console.py')
-rw-r--r-- | tests/mlia/test_utils_console.py | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/tests/mlia/test_utils_console.py b/tests/mlia/test_utils_console.py new file mode 100644 index 0000000..36975f8 --- /dev/null +++ b/tests/mlia/test_utils_console.py @@ -0,0 +1,100 @@ +# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-License-Identifier: Apache-2.0 +"""Tests for console utility functions.""" +from typing import Iterable +from typing import List +from typing import Optional + +import pytest + +from mlia.utils.console import apply_style +from mlia.utils.console import create_section_header +from mlia.utils.console import produce_table +from mlia.utils.console import remove_ascii_codes + + +@pytest.mark.parametrize( + "rows, headers, table_style, expected_result", + [ + [[], [], "no_borders", ""], + [ + [["1", "2", "3"]], + ["Col 1", "Col 2", "Col 3"], + "default", + """ +┌───────┬───────┬───────┐ +│ Col 1 │ Col 2 │ Col 3 │ +╞═══════╪═══════╪═══════╡ +│ 1 │ 2 │ 3 │ +└───────┴───────┴───────┘ +""".strip(), + ], + [ + [["1", "2", "3"]], + ["Col 1", "Col 2", "Col 3"], + "nested", + "Col 1 Col 2 Col 3 \n \n1 2 3", + ], + [ + [["1", "2", "3"]], + ["Col 1", "Col 2", "Col 3"], + "no_borders", + " Col 1 Col 2 Col 3 \n 1 2 3", + ], + ], +) +def test_produce_table( + rows: Iterable, headers: Optional[List[str]], table_style: str, expected_result: str +) -> None: + """Test produce_table function.""" + result = produce_table(rows, headers, table_style) + assert remove_ascii_codes(result) == expected_result + + +def test_produce_table_unknown_style() -> None: + """Test that function should fail if unknown style provided.""" + with pytest.raises(Exception, match="Unsupported table style unknown_style"): + produce_table([["1", "2", "3"]], [], "unknown_style") + + +@pytest.mark.parametrize( + "value, expected_result", + [ + ["some text", "some text"], + ["\033[32msome text\033[0m", "some text"], + ], +) +def test_remove_ascii_codes(value: str, expected_result: str) -> None: + """Test remove_ascii_codes function.""" + assert remove_ascii_codes(value) == expected_result + + +def test_apply_style() -> None: + """Test function apply_style.""" + assert apply_style("some text", "green") == "[green]some text" + + +@pytest.mark.parametrize( + "section_header, expected_result", + [ + [ + "Section header", + "\n--- Section header -------------------------------" + "------------------------------\n", + ], + [ + "", + f"\n{'-' * 80}\n", + ], + ], +) +def test_create_section_header(section_header: str, expected_result: str) -> None: + """Test function test_create_section.""" + assert create_section_header(section_header) == expected_result + + +def test_create_section_header_too_long_value() -> None: + """Test that header could not be created for the too long section names.""" + section_name = "section name" * 100 + with pytest.raises(ValueError, match="Section name too long"): + create_section_header(section_name) |