Coverage for tests/test_options.py: 97%

55 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-14 09:29 +0000

1"""Tests the global settings.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any 

6 

7import pytest 

8 

9from chrisjen import options, utilities 

10 

11 

12@pytest.fixture(autouse = True) 

13def restore_options(monkeypatch: pytest.MonkeyPatch) -> None: 

14 """Puts the global settings back after each test.""" 

15 for name in ( 

16 '_KEY_NAMER', 

17 '_VERBOSE', 

18 '_STRICT_COMPATIBILITY', 

19 '_METHOD_NAMER', 

20 '_OVERWRITE', 

21 ): 

22 if hasattr(options, name): 

23 monkeypatch.setattr(options, name, getattr(options, name)) 

24 else: 

25 monkeypatch.setattr(options, name, None, raising = False) 

26 

27 

28def test_defaults() -> None: 

29 assert options._KEY_NAMER is utilities._namify 

30 assert options._VERBOSE is False 

31 

32 

33def test_set_keyer() -> None: 

34 def shout(item: Any) -> str: 

35 return str(item).upper() 

36 

37 options.set_keyer(shout) 

38 assert options._KEY_NAMER is shout 

39 with pytest.raises(TypeError, match = 'callable'): 

40 options.set_keyer('not a function') # type: ignore[arg-type] 

41 assert options._KEY_NAMER is shout 

42 

43 

44def test_set_method_namer() -> None: 

45 def namer(item: Any) -> str: 

46 return f'make_{item}' 

47 

48 options.set_method_namer(namer) 

49 assert options._METHOD_NAMER is namer 

50 with pytest.raises(TypeError, match = 'callable'): 

51 options.set_method_namer(3) # type: ignore[arg-type] 

52 

53 

54@pytest.mark.parametrize( 

55 ('function', 'attribute'), 

56 [ 

57 (options.set_compatibility_rule, '_STRICT_COMPATIBILITY'), 

58 (options.set_overwrite_rule, '_OVERWRITE'), 

59 (options.set_verbose_rule, '_VERBOSE'), 

60 ], 

61) 

62def test_boolean_rules(function: Any, attribute: str) -> None: 

63 function(True) 

64 assert getattr(options, attribute) is True 

65 function(False) 

66 assert getattr(options, attribute) is False 

67 with pytest.raises(TypeError, match = 'boolean'): 

68 function('yes') 

69 with pytest.raises(TypeError, match = 'boolean'): 

70 function(1) 

71 assert getattr(options, attribute) is False 

72 

73 

74def test_special_settings(monkeypatch: pytest.MonkeyPatch) -> None: 

75 monkeypatch.setattr( 

76 options, '_SPECIAL_SETTINGS', list(options._SPECIAL_SETTINGS)) 

77 assert options.is_special_setting('general') 

78 assert not options.is_special_setting('feature_groups') 

79 options.add_special_setting('feature_groups') 

80 options.add_special_setting('feature_groups') 

81 assert options.is_special_setting('feature_groups') 

82 assert options._SPECIAL_SETTINGS.count('feature_groups') == 1 

83 with pytest.raises(TypeError, match = 'str'): 

84 options.add_special_setting(3) # type: ignore[arg-type] 

85 

86 

87def test_private_aliases_of_public_helpers() -> None: 

88 from chrisjen import workshop 

89 assert utilities._listify_names is utilities.listify_names 

90 assert utilities._flatten_dict is utilities.flatten_dict 

91 assert utilities._label_path is utilities.label_path 

92 assert workshop._class_names is workshop.class_names