forked from pimutils/khal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.py
121 lines (97 loc) · 4.93 KB
/
utils_test.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
"""testing functions from the khal.utils"""
import datetime as dt
from freezegun import freeze_time
from khal import utils
def test_relative_timedelta_str():
with freeze_time('2016-9-19'):
assert utils.relative_timedelta_str(dt.date(2016, 9, 24)) == '5 days from now'
assert utils.relative_timedelta_str(dt.date(2016, 9, 29)) == '~1 week from now'
assert utils.relative_timedelta_str(dt.date(2017, 9, 29)) == '~1 year from now'
assert utils.relative_timedelta_str(dt.date(2016, 7, 29)) == '~7 weeks ago'
weekheader = """[1m Mo Tu We Th Fr Sa Su [0m"""
today_line = """[1mToday[0m[0m"""
calendarline = (
"[1mNov [0m[1;33m31[0m [32m 1[0m [1;33m 2[0m "
"[1;33m 3[0m [1;33m 4[0m [32m 5[0m [32m 6[0m"
)
def test_last_reset():
assert utils.find_last_reset(weekheader) == (31, 35, '\x1b[0m')
assert utils.find_last_reset(today_line) == (13, 17, '\x1b[0m')
assert utils.find_last_reset(calendarline) == (99, 103, '\x1b[0m')
assert utils.find_last_reset('Hello World') == (-2, -1, '')
def test_last_sgr():
assert utils.find_last_sgr(weekheader) == (0, 4, '\x1b[1m')
assert utils.find_last_sgr(today_line) == (0, 4, '\x1b[1m')
assert utils.find_last_sgr(calendarline) == (92, 97, '\x1b[32m')
assert utils.find_last_sgr('Hello World') == (-2, -1, '')
def test_find_unmatched_sgr():
assert utils.find_unmatched_sgr(weekheader) is None
assert utils.find_unmatched_sgr(today_line) is None
assert utils.find_unmatched_sgr(calendarline) is None
assert utils.find_unmatched_sgr('\x1b[31mHello World') == '\x1b[31m'
assert utils.find_unmatched_sgr('\x1b[31mHello\x1b[0m \x1b[32mWorld') == '\x1b[32m'
assert utils.find_unmatched_sgr('foo\x1b[1;31mbar') == '\x1b[1;31m'
assert utils.find_unmatched_sgr('\x1b[0mfoo\x1b[1;31m') == '\x1b[1;31m'
def test_color_wrap():
text = (
"Lorem ipsum \x1b[31mdolor sit amet, consetetur sadipscing "
"elitr, sed diam nonumy\x1b[0m eirmod tempor"
)
expected = [
"Lorem ipsum \x1b[31mdolor sit amet,\x1b[0m",
"\x1b[31mconsetetur sadipscing elitr, sed\x1b[0m",
"\x1b[31mdiam nonumy\x1b[0m eirmod tempor",
]
assert utils.color_wrap(text, 35) == expected
def test_color_wrap_256():
text = (
"\x1b[38;2;17;255;0mLorem ipsum dolor sit amet, consetetur sadipscing "
"elitr, sed diam nonumy\x1b[0m"
)
expected = [
"\x1b[38;2;17;255;0mLorem ipsum\x1b[0m",
"\x1b[38;2;17;255;0mdolor sit amet, consetetur\x1b[0m",
"\x1b[38;2;17;255;0msadipscing elitr, sed diam\x1b[0m",
"\x1b[38;2;17;255;0mnonumy\x1b[0m"
]
assert utils.color_wrap(text, 30) == expected
def test_color_wrap_multiple_colors_and_tabs():
text = (
"\x1b[31m14:00-14:50 AST-1002-102 INTRO AST II/STAR GALAX (R) Classes",
"15:30-16:45 PHL-2000-104 PHILOSOPHY, SOCIETY & ETHICS (R) Classes",
"\x1b[38;2;255;0m17:00-18:00 Pay Ticket Deadline Calendar",
"09:30-10:45 PHL-1501-101 MIND, KNOWLEDGE & REALITY (R) Classes",
"\x1b[38;2;255;0m11:00-14:00 Rivers Street (noodles and pizza) (R) Calendar",
)
expected = [
'\x1b[31m14:00-14:50 AST-1002-102 INTRO AST II/STAR GALAX (R)\x1b[0m',
'\x1b[31mClasses\x1b[0m',
'15:30-16:45 PHL-2000-104 PHILOSOPHY, SOCIETY & ETHICS (R)',
'Classes',
'\x1b[38;2;255;0m17:00-18:00 Pay Ticket Deadline Calendar\x1b[0m',
'09:30-10:45 PHL-1501-101 MIND, KNOWLEDGE & REALITY (R)',
'Classes',
'\x1b[38;2;255;0m11:00-14:00 Rivers Street (noodles and\x1b[0m',
'\x1b[38;2;255;0mpizza) (R) Calendar\x1b[0m'
]
actual = []
for line in text:
actual += utils.color_wrap(line, 60)
assert actual == expected
def test_get_weekday_occurrence():
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 1)) == (2, 1)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 2)) == (3, 1)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 3)) == (4, 1)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 4)) == (5, 1)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 5)) == (6, 1)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 6)) == (0, 1)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 7)) == (1, 1)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 8)) == (2, 2)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 9)) == (3, 2)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 10)) == (4, 2)
assert utils.get_weekday_occurrence(dt.datetime(2017, 3, 31)) == (4, 5)
assert utils.get_weekday_occurrence(dt.date(2017, 5, 1)) == (0, 1)
assert utils.get_weekday_occurrence(dt.date(2017, 5, 7)) == (6, 1)
assert utils.get_weekday_occurrence(dt.date(2017, 5, 8)) == (0, 2)
assert utils.get_weekday_occurrence(dt.date(2017, 5, 28)) == (6, 4)
assert utils.get_weekday_occurrence(dt.date(2017, 5, 29)) == (0, 5)