|
| 1 | +# Copyright 2022-2025 The Ramble Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | +# option. This file may not be copied, modified, or distributed |
| 7 | +# except according to those terms. |
| 8 | +"""Perform tests of the util/naming functions""" |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from ramble.util import naming |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "mod_name,expected_cls_name", |
| 17 | + [ |
| 18 | + ("test_mod_1", "TestMod1"), |
| 19 | + ("2_test_mod", "_2TestMod"), |
| 20 | + ], |
| 21 | +) |
| 22 | +def test_mod_to_class(mod_name, expected_cls_name): |
| 23 | + assert naming.mod_to_class(mod_name) == expected_cls_name |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize( |
| 27 | + "mod_name,expected_py_mod_name", |
| 28 | + [ |
| 29 | + ("app", "app"), |
| 30 | + ("intel-mlc", "intel_mlc"), |
| 31 | + ("1-app", "num1_app"), |
| 32 | + ], |
| 33 | +) |
| 34 | +def test_ramble_module_to_python_module(mod_name, expected_py_mod_name): |
| 35 | + assert naming.ramble_module_to_python_module(mod_name) == expected_py_mod_name |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize( |
| 39 | + "py_mod_name,expected_names", |
| 40 | + [ |
| 41 | + ("app", ["app"]), |
| 42 | + ("py_mod", ["py_mod", "py-mod"]), |
| 43 | + ("num1_app", ["1_app", "1-app"]), |
| 44 | + ], |
| 45 | +) |
| 46 | +def test_possible_ramble_module_names(py_mod_name, expected_names): |
| 47 | + assert naming.possible_ramble_module_names(py_mod_name) == expected_names |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "in_name,expected_out_name", |
| 52 | + [ |
| 53 | + ("ImageMagick", "imagemagick"), |
| 54 | + ("l_mkl", "mkl"), |
| 55 | + ("ALLCAPS", "allcaps"), |
| 56 | + ("a_b.c++", "a-b-cpp"), |
| 57 | + ], |
| 58 | +) |
| 59 | +def test_simplify_name(in_name, expected_out_name): |
| 60 | + assert naming.simplify_name(in_name) == expected_out_name |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize( |
| 64 | + "mod_name,expect_error", |
| 65 | + [ |
| 66 | + ("valid-mod", False), |
| 67 | + ("a.b", True), |
| 68 | + ("-ab", True), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_validate_module_name(mod_name, expect_error): |
| 72 | + if expect_error: |
| 73 | + with pytest.raises(naming.InvalidModuleNameError): |
| 74 | + naming.validate_module_name(mod_name) |
| 75 | + else: |
| 76 | + naming.validate_module_name(mod_name) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + "mod_name,expect_error", |
| 81 | + [ |
| 82 | + ("valid-mod", False), |
| 83 | + ("a.b", False), |
| 84 | + ("-ab", True), |
| 85 | + ("valid-mod.a.b-c", False), |
| 86 | + ], |
| 87 | +) |
| 88 | +def test_validate_fully_qualified_module_name(mod_name, expect_error): |
| 89 | + if expect_error: |
| 90 | + with pytest.raises(naming.InvalidFullyQualifiedModuleNameError): |
| 91 | + naming.validate_fully_qualified_module_name(mod_name) |
| 92 | + else: |
| 93 | + naming.validate_fully_qualified_module_name(mod_name) |
0 commit comments