-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_test.py
157 lines (113 loc) · 4.68 KB
/
core_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
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
150
151
152
153
154
155
156
157
import pytest
from core import listdir, rename, Selection, changefn, FilenameCollisionError, prefixfn, suffixfn, insertfn, appendfn
def test_listdir(tmp_path):
fs, ds = {'f1', 'f2', 'f3'}, {'d1', 'd2', 'd3'}
for d in ds:
(tmp_path / d).mkdir()
for f in fs:
(tmp_path / f).write_text('')
assert set(listdir(tmp_path)) == fs
def test_rename(tmp_path):
for f in {'a', 'b', 'c', 'd', 'e', 'f'}:
(tmp_path / f).write_text('')
count = rename(tmp_path, [('a', 'A'), ('b', 'B'), ('c', 'c'), ('d', 'd')])
assert set(listdir(tmp_path)) == {'A', 'B', 'c', 'd', 'e', 'f'} and count == 2
@pytest.mark.parametrize("s, x, sx", [
('foo', 'd', 'dfoo'),
('foo', '', 'foo'),
])
def test_prefix(s, x, sx):
assert prefixfn(s, '', x) == sx
@pytest.mark.parametrize("s, x, sx", [
('foo', 'd', 'food'),
('foo', '', 'foo'),
])
def test_suffix(s, x, sx):
assert suffixfn(s, '', x) == sx
@pytest.mark.parametrize("s, ss, t, r", [
('abcd', 'bc', 'x', 'axbcd'),
('abcd', 'foo', 'x', 'abcd'),
('abcd', '', 'x', 'abcd'),
('abcd', 'bc', '', 'abcd'),
])
def test_insertfn(s, ss, t, r):
assert insertfn(s, ss, t) == r
@pytest.mark.parametrize("s, ss, t, r", [
('abcd', 'bc', 'x', 'axd'),
('abcd', 'foo', 'x', 'abcd'),
('abcd', '', 'x', 'abcd'),
('abcd', 'bc', '', 'ad'),
])
def test_changefn(s, ss, t, r):
assert changefn(s, ss, t) == r
@pytest.mark.parametrize("s, ss, t, r", [
('abcd', 'bc', 'x', 'abcxd'),
('abcd', 'foo', 'x', 'abcd'),
('abcd', '', 'x', 'abcd'),
('abcd', 'bc', '', 'abcd'),
])
def test_appendfn(s, ss, t, r):
assert appendfn(s, ss, t) == r
@pytest.fixture
def selection():
return Selection(['foo_123', 'foo_456', 'bar_123', 'baz_123'])
def test_everything_is_selected_by_default(selection):
assert selection.active() == [0, 1, 2, 3]
def test_selections_can_by_refined_using_patterns(selection):
selection.tighten('foo')
assert selection.active() == [0, 1]
def test_selections_can_be_progressively_refined(selection):
selection.tighten('123')
selection.tighten('ba')
assert selection.active() == [2, 3]
def test_invalid_patterns_result_in_empty_selections(selection):
selection.tighten('hello')
assert selection.active() == []
def test_selections_can_be_rolled_back(selection):
selection.tighten('123')
selection.tighten('ba')
selection.loosen()
assert selection.active() == [0, 2, 3]
def test_selections_can_be_resolved_to_filenames(selection):
selection.tighten('ba')
assert selection.peek() == [('bar_123', 'bar_123'), ('baz_123', 'baz_123')]
def test_selections_can_be_reset(selection):
selection.tighten('ba')
selection.tighten('z')
selection.clear()
assert selection.active() == [0, 1, 2, 3]
def test_selections_can_be_transformed(selection):
selection.tighten('foo')
selection.transform(lambda s: changefn(s, 'foo', 'FOO'))
assert selection.peek() == [('foo_123', 'FOO_123'), ('foo_456', 'FOO_456')]
def test_transformations_can_be_undone(selection):
selection.transform(lambda s: changefn(s, 'foo', 'FOO'))
selection.rollback()
assert [x for (_, x) in selection.peek()] == ['foo_123', 'foo_456', 'bar_123', 'baz_123']
def test_transformations_can_be_selectively_undone(selection):
selection.transform(lambda s: changefn(s, 'foo', 'FOO'))
selection.tighten('123')
selection.rollback()
selection.clear()
assert [x for (_, x) in selection.peek()] == ['foo_123', 'FOO_456', 'bar_123', 'baz_123']
def test_transformations_are_aborted_on_filename_collisions(selection):
with pytest.raises(FilenameCollisionError):
selection.transform(lambda s: changefn(s, '123', '456'))
assert [x for (_, x) in selection.peek()] == ['foo_123', 'foo_456', 'bar_123', 'baz_123']
def test_transformations_are_rolledback_only_for_modified_files(selection):
selection.transform(lambda s: changefn(s, 'foo', 'FOO'))
selection.tighten('foo')
with pytest.raises(FilenameCollisionError):
selection.transform(lambda s: changefn(s, 'FOO_123', 'bar_123'))
assert [x for (_, x) in selection.peek()] == ['FOO_123', 'FOO_456']
def test_file_cannot_be_renamed_to_the_empty_string(selection):
selection.transform(lambda s: changefn(s, 'foo_123', ''))
assert [x for (_, x) in selection.peek()] == ['foo_123', 'foo_456', 'bar_123', 'baz_123']
def test_uncommitted_filenames_shouldnot_be_considered_for_selection(selection):
selection.transform(lambda s: changefn(s, 'f', 'F'))
selection.tighten('f')
s1 = selection.peek()
selection.clear()
selection.tighten('F')
s2 = selection.peek()
assert s1 == [('foo_123', 'Foo_123'), ('foo_456', 'Foo_456')] and s2 == []