Coverage for tests/test_rofi.py: 100%

58 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-24 11:53 -0300

1# test_rofi.py 

2 

3from typing import NamedTuple 

4from typing import Type 

5from typing import Union 

6 

7import pytest 

8from pyselector.menus.rofi import Rofi 

9 

10 

11class Case(NamedTuple): 

12 input: str 

13 expected: Union[str, Type[Exception]] 

14 

15 

16@pytest.fixture 

17def rofi() -> Rofi: 

18 return Rofi() 

19 

20 

21@pytest.fixture 

22def items() -> list[str]: 

23 return ["Option 1", "Option 2", "Option 3"] 

24 

25 

26def test_build_command(rofi) -> None: 

27 alt_r = rofi.keybind.add( 

28 key="alt-r", 

29 description="Testing add keybind", 

30 callback=lambda: None, 

31 ) 

32 args = rofi._build_command( 

33 case_sensitive=True, 

34 multi_select=True, 

35 prompt="test>", 

36 lines=5, 

37 theme="default", 

38 mesg="Testing...", 

39 filter="Testing...", 

40 location="upper-right", 

41 width="60%", 

42 height="50%", 

43 ) 

44 

45 assert isinstance(args, list) 

46 assert "-case-sensitive" in args 

47 assert "-multi-select" in args 

48 assert "-p" in args 

49 assert "-l" in args 

50 assert "-theme" in args 

51 assert "-mesg" in args 

52 assert "-filter" in args 

53 assert f"-kb-custom-{alt_r.id}" in args 

54 assert "-location" in args 

55 assert "-theme-str" in args 

56 

57 

58def test_build_command_warning(rofi) -> None: 

59 with pytest.warns(UserWarning): 

60 rofi._build_command( 

61 case_sensitive=True, 

62 multi_select=True, 

63 prompt="test>", 

64 lines=5, 

65 theme="default", 

66 invalid_arg=True, 

67 ) 

68 

69 

70def test_build_command_not_case_sensitive(rofi: Rofi) -> None: 

71 args = rofi._build_command( 

72 case_sensitive=False, 

73 multi_select=True, 

74 prompt="test>", 

75 ) 

76 assert "-i" in args 

77 

78 

79def test_system_exit(rofi: Rofi, items) -> None: 

80 """Test case user hits escape raises SystemExit""" 

81 with pytest.raises(SystemExit): 

82 lines, _ = rofi.prompt(items=items, prompt="PressEscape>") 

83 

84 

85def test_multi_lines_selected(rofi, items) -> None: 

86 """Test case where multi_select is True""" 

87 lines, _ = rofi.prompt( 

88 items=items, 

89 prompt="Shift+Enter>", 

90 multi_select=True, 

91 ) 

92 assert isinstance(lines, list) 

93 assert len(lines) == 3 

94 

95 

96def test_case_sensitive(rofi) -> None: 

97 """Test case where case_sensitive is True""" 

98 result, _ = rofi.prompt( 

99 items=["OPTION 1"], 

100 prompt="CaseSensitive>", 

101 case_sensitive=True, 

102 ) 

103 assert result == "OPTION 1" 

104 

105 

106def test_int_items_to_str(rofi) -> None: 

107 result, _ = rofi.prompt( 

108 items=[1, 2, 3], 

109 prompt="Select item 1>", 

110 ) 

111 assert isinstance(result, str) 

112 assert result == "1" 

113 

114 

115def test_invalid_arg_warning(rofi, items) -> None: 

116 with pytest.warns(UserWarning): 

117 result, _ = rofi.prompt( 

118 prompt="PressEnter>", 

119 invalid_arg=True, 

120 ) 

121 

122 

123@pytest.mark.parametrize( 

124 ("input", "expected"), 

125 ( 

126 Case(input="upper-left", expected="1"), 

127 Case(input="left", expected="8"), 

128 Case(input="bottom-left", expected="7"), 

129 Case(input="upper-center", expected="2"), 

130 Case(input="center", expected="0"), 

131 Case(input="bottom-center", expected="6"), 

132 Case(input="upper-right", expected="3"), 

133 Case(input="right", expected="4"), 

134 Case(input="bottom-right", expected="5"), 

135 ), 

136) 

137def test_rofi_location_success(rofi, input, expected) -> None: 

138 assert rofi.location(input) == expected 

139 

140 

141@pytest.mark.parametrize( 

142 ("input", "expected"), 

143 ( 

144 Case(input="upper-bottom", expected=KeyError), 

145 Case(input="upper-up", expected=KeyError), 

146 ), 

147) 

148def test_rofi_location_failure(rofi, input, expected) -> None: 

149 with pytest.raises(expected): 

150 rofi.location(input)