1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-11-18 04:59:03 +00:00
dupeguru/core/tests/markable_test.py

157 lines
3.6 KiB
Python
Raw Normal View History

2016-05-29 19:02:39 +00:00
# Copyright 2016 Hardcoded Software (http://www.hardcoded.net)
2016-05-29 19:02:39 +00:00
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html
from hscommon.testutil import eq_
2016-05-29 19:02:39 +00:00
from ..markable import MarkableList, Markable
def gen():
ml = MarkableList()
ml.extend(list(range(10)))
return ml
def test_unmarked():
ml = gen()
for i in ml:
assert not ml.is_marked(i)
def test_mark():
ml = gen()
assert ml.mark(3)
assert ml.is_marked(3)
assert not ml.is_marked(2)
def test_unmark():
ml = gen()
ml.mark(4)
assert ml.unmark(4)
assert not ml.is_marked(4)
def test_unmark_unmarked():
ml = gen()
assert not ml.unmark(4)
assert not ml.is_marked(4)
2016-05-29 19:02:39 +00:00
def test_mark_twice_and_unmark():
ml = gen()
assert ml.mark(5)
assert not ml.mark(5)
ml.unmark(5)
assert not ml.is_marked(5)
def test_mark_toggle():
ml = gen()
ml.mark_toggle(6)
assert ml.is_marked(6)
ml.mark_toggle(6)
assert not ml.is_marked(6)
ml.mark_toggle(6)
assert ml.is_marked(6)
def test_is_markable():
class Foobar(Markable):
def _is_markable(self, o):
return o == 'foobar'
f = Foobar()
assert not f.is_marked('foobar')
assert not f.mark('foo')
assert not f.is_marked('foo')
f.mark_toggle('foo')
assert not f.is_marked('foo')
f.mark('foobar')
assert f.is_marked('foobar')
ml = gen()
ml.mark(11)
assert not ml.is_marked(11)
def test_change_notifications():
class Foobar(Markable):
def _did_mark(self, o):
self.log.append((True, o))
2016-05-29 19:02:39 +00:00
def _did_unmark(self, o):
self.log.append((False, o))
f = Foobar()
f.log = []
f.mark('foo')
f.mark('foo')
f.mark_toggle('bar')
f.unmark('foo')
f.unmark('foo')
f.mark_toggle('bar')
2016-05-29 19:02:39 +00:00
eq_([(True, 'foo'), (True, 'bar'), (False, 'foo'), (False, 'bar')], f.log)
def test_mark_count():
ml = gen()
2016-05-29 19:02:39 +00:00
eq_(0, ml.mark_count)
ml.mark(7)
2016-05-29 19:02:39 +00:00
eq_(1, ml.mark_count)
ml.mark(11)
2016-05-29 19:02:39 +00:00
eq_(1, ml.mark_count)
def test_mark_none():
log = []
ml = gen()
ml._did_unmark = lambda o: log.append(o)
ml.mark(1)
ml.mark(2)
2016-05-29 19:02:39 +00:00
eq_(2, ml.mark_count)
ml.mark_none()
2016-05-29 19:02:39 +00:00
eq_(0, ml.mark_count)
eq_([1, 2], log)
def test_mark_all():
ml = gen()
2016-05-29 19:02:39 +00:00
eq_(0, ml.mark_count)
ml.mark_all()
2016-05-29 19:02:39 +00:00
eq_(10, ml.mark_count)
assert ml.is_marked(1)
def test_mark_invert():
ml = gen()
ml.mark(1)
ml.mark_invert()
assert not ml.is_marked(1)
assert ml.is_marked(2)
def test_mark_while_inverted():
log = []
ml = gen()
2016-05-29 19:02:39 +00:00
ml._did_unmark = lambda o: log.append((False, o))
ml._did_mark = lambda o: log.append((True, o))
ml.mark(1)
ml.mark_invert()
assert ml.mark_inverted
assert ml.mark(1)
assert ml.unmark(2)
assert ml.unmark(1)
ml.mark_toggle(3)
assert not ml.is_marked(3)
2016-05-29 19:02:39 +00:00
eq_(7, ml.mark_count)
eq_([(True, 1), (False, 1), (True, 2), (True, 1), (True, 3)], log)
def test_remove_mark_flag():
ml = gen()
ml.mark(1)
ml._remove_mark_flag(1)
assert not ml.is_marked(1)
ml.mark(1)
ml.mark_invert()
assert not ml.is_marked(1)
ml._remove_mark_flag(1)
assert ml.is_marked(1)
2016-05-29 19:02:39 +00:00
def test_is_marked_returns_false_if_object_not_markable():
class MyMarkableList(MarkableList):
def _is_markable(self, o):
return o != 4
ml = MyMarkableList()
ml.extend(list(range(10)))
ml.mark_invert()
assert ml.is_marked(1)
assert not ml.is_marked(4)