-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tray_file.py
149 lines (129 loc) · 5.15 KB
/
test_tray_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
"""
-------------------------
Test phile.tray.tray_file
-------------------------
"""
# Standard library.
import pathlib
import tempfile
import unittest
# Internal packages.
from phile.configuration import Configuration
from phile.tray.tray_file import TrayFile
class TestTrayFile(unittest.TestCase):
"""Tests :class:`~phile.notify.tray.TrayFile`."""
def setUp(self) -> None:
"""
Create a directory to use as a tray directory.
The directories are recreated for each test
to make sure no leftover files from tests
would interfere with each other.
"""
self.tray_directory = tempfile.TemporaryDirectory()
self.tray_directory_path = pathlib.Path(self.tray_directory.name)
self.configuration = Configuration(
tray_directory=self.tray_directory_path
)
self.name = 'clock'
self.path = self.configuration.tray_directory / (
self.name + self.configuration.tray_suffix
)
self.tray = TrayFile(
name=self.name, configuration=self.configuration
)
def tearDown(self) -> None:
"""Remove tray directory."""
self.tray_directory.cleanup()
def test_construct_with_name(self) -> None:
"""Constructing with name must come with a configuration."""
# A successful construction in `setUp()`.
self.assertEqual(self.tray.name, self.name)
self.assertEqual(self.tray.path, self.path)
# It should fail without a configuration give.
with self.assertRaises(ValueError):
TrayFile(name=self.name)
def test_construct_with_path(self) -> None:
"""Constructing with just path should be possible."""
tray = TrayFile(
path=self.configuration.tray_directory /
(self.name + self.configuration.tray_suffix)
)
self.assertEqual(self.tray.path, self.path)
def test_construct_with_path_with_wrong_parent(self) -> None:
"""Constructing with path must be in configured directory."""
with self.assertRaises(TrayFile.ParentError):
tray = TrayFile(
configuration=self.configuration,
path=self.configuration.tray_directory / 'subdir' /
(self.name + self.configuration.tray_suffix)
)
def test_construct_with_path_with_wrong_suffix(self) -> None:
"""Constructing with path must be in configured suffix."""
with self.assertRaises(TrayFile.SuffixError):
tray = TrayFile(
configuration=self.configuration,
path=self.configuration.tray_directory /
(self.name + '.wrong_suffix')
)
def test_hash(self) -> None:
"""Can be used as keys in dictionaries."""
number = 1
tray_key_dictionary = {self.tray: number}
self.assertEqual(tray_key_dictionary[self.tray], number)
def test_lt(self) -> None:
"""Can be used as keys in dictionaries."""
smaller_tray = TrayFile(
name='smaller.tray', configuration=self.configuration
)
self.assertLess(self.tray, smaller_tray)
def test_remove_file(self) -> None:
"""Tray can be removed."""
self.tray.path.touch()
self.assertTrue(self.tray.path.is_file())
self.tray.remove()
self.assertTrue(not self.tray.path.is_file())
def test_remove_non_existent_file(self) -> None:
"""Removing trays that do not exist should be fine."""
self.assertTrue(not self.tray.path.is_file())
self.tray.remove()
self.assertTrue(not self.tray.path.is_file())
def test_load(self) -> None:
"""Parse a tray file for information."""
data = {
'icon_name': 'phile-tray',
'icon_path': self.tray_directory_path / 'phile-tray-read',
'text_icon': 'N',
}
content = '{text_icon}\n{{'
content += '"icon_name": "{icon_name}"'
content += ',"icon_path": "{icon_path}"'
content += '}}'
content = content.format(**data)
self.tray.path.write_text(content)
self.tray.load()
self.assertEqual(self.tray.icon_name, data['icon_name'])
self.assertEqual(self.tray.icon_path, data['icon_path'])
self.assertEqual(self.tray.text_icon, data['text_icon'])
def test_save(self) -> None:
"""Save a tray file with some information."""
data = {
'icon_name': 'phile-tray',
'text_icon': 'N',
}
expected_content = '{text_icon}\n{{'
expected_content += '"icon_name": "{icon_name}"'
expected_content += '}}'
expected_content = expected_content.format(**data)
self.tray.icon_name = data['icon_name']
self.tray.text_icon = data['text_icon']
self.tray.save()
content = self.tray.path.read_text()
self.assertEqual(content, expected_content)
def test_save_nothing(self) -> None:
"""Savea blank tray file. It should still have a new line."""
self.tray.save()
content = self.tray.path.read_text()
self.assertTrue(not content)
if __name__ == '__main__':
unittest.main()