Coverage for tests/test_rofi.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-29 11:30 -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 

58# def 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( 

83 items=items, prompt="PressEscape>", mesg="> Hit <Escape>" 

84 ) 

85 

86 

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

88 """Test case where multi_select is True""" 

89 lines, _ = rofi.prompt( 

90 items=items, 

91 prompt="Shift+Enter>", 

92 multi_select=True, 

93 mesg="> Select all items with <Shift+Enter>", 

94 ) 

95 assert isinstance(lines, list) 

96 assert len(lines) == 3 

97 

98 

99def test_case_sensitive(rofi) -> None: 

100 """Test case where case_sensitive is True""" 

101 result, _ = rofi.prompt( 

102 items=["OPTION 1"], 

103 prompt="CaseSensitive>", 

104 case_sensitive=True, 

105 mesg="> Type some option with CAPS on", 

106 ) 

107 assert result == "OPTION 1" 

108 

109 

110def test_int_items_to_str(rofi) -> None: 

111 items = [1, 2, 3] 

112 result, _ = rofi.prompt(items=items, prompt="Select>", mesg="> Select first item") 

113 assert isinstance(result, str) 

114 assert result == "1" 

115 

116 

117# def test_invalid_arg_warning(rofi, items) -> None: 

118# with pytest.warns(UserWarning): 

119# result, _ = rofi.prompt( 

120# prompt="PressEnter>", 

121# invalid_arg=True, 

122# ) 

123 

124 

125@pytest.mark.parametrize( 

126 ("input", "expected"), 

127 ( 

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

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

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

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

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

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

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

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

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

137 ), 

138) 

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

140 assert rofi.location(input) == expected 

141 

142 

143@pytest.mark.parametrize( 

144 ("input", "expected"), 

145 ( 

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

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

148 ), 

149) 

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

151 with pytest.raises(expected): 

152 rofi.location(input)