2022-01-31 09:32:48 +01:00
|
|
|
# standard imports
|
|
|
|
import unittest
|
2022-03-16 17:49:00 +01:00
|
|
|
import logging
|
2022-01-31 09:32:48 +01:00
|
|
|
|
|
|
|
# local imports
|
2022-01-31 10:33:21 +01:00
|
|
|
from shep import State
|
|
|
|
from shep.error import (
|
2022-01-31 09:38:14 +01:00
|
|
|
StateExists,
|
|
|
|
StateInvalid,
|
|
|
|
)
|
2022-01-31 09:32:48 +01:00
|
|
|
|
2022-03-16 17:49:00 +01:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
2022-01-31 09:32:48 +01:00
|
|
|
|
2022-03-16 18:13:05 +01:00
|
|
|
class MockCallback:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.items = {}
|
2022-03-29 11:24:57 +02:00
|
|
|
self.items_from = {}
|
2022-03-16 18:13:05 +01:00
|
|
|
|
|
|
|
|
2022-03-29 11:24:57 +02:00
|
|
|
def add(self, k, v_from, v_to):
|
2022-03-16 18:13:05 +01:00
|
|
|
if self.items.get(k) == None:
|
|
|
|
self.items[k] = []
|
2022-03-29 11:24:57 +02:00
|
|
|
self.items_from[k] = []
|
|
|
|
self.items[k].append(v_to)
|
|
|
|
self.items_from[k].append(v_from)
|
2022-03-16 18:13:05 +01:00
|
|
|
|
|
|
|
|
2022-01-31 09:32:48 +01:00
|
|
|
class TestState(unittest.TestCase):
|
|
|
|
|
2022-01-31 10:12:49 +01:00
|
|
|
def test_key_check(self):
|
|
|
|
states = State(3)
|
|
|
|
states.add('foo')
|
|
|
|
|
|
|
|
for k in [
|
|
|
|
'f0o',
|
|
|
|
'f oo',
|
|
|
|
]:
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
states.add(k)
|
|
|
|
|
|
|
|
|
2022-01-31 09:32:48 +01:00
|
|
|
def test_get(self):
|
|
|
|
states = State(3)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.add('baz')
|
|
|
|
self.assertEqual(states.BAZ, 4)
|
|
|
|
|
|
|
|
|
|
|
|
def test_limit(self):
|
2022-03-17 22:36:07 +01:00
|
|
|
states = State(3)
|
2022-01-31 09:32:48 +01:00
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
2022-03-17 22:36:07 +01:00
|
|
|
states.add('baz')
|
2022-01-31 09:32:48 +01:00
|
|
|
with self.assertRaises(OverflowError):
|
2022-03-17 22:36:07 +01:00
|
|
|
states.add('gaz')
|
2022-01-31 09:32:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dup(self):
|
|
|
|
states = State(2)
|
|
|
|
states.add('foo')
|
|
|
|
with self.assertRaises(StateExists):
|
|
|
|
states.add('foo')
|
|
|
|
|
|
|
|
|
2022-01-31 09:38:14 +01:00
|
|
|
def test_alias(self):
|
|
|
|
states = State(2)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.alias('baz', states.FOO | states.BAR)
|
|
|
|
self.assertEqual(states.BAZ, 3)
|
|
|
|
|
|
|
|
|
|
|
|
def test_alias_limit(self):
|
|
|
|
states = State(2)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.alias('baz', states.FOO | states.BAR)
|
|
|
|
|
|
|
|
|
|
|
|
def test_alias_nopure(self):
|
|
|
|
states = State(3)
|
|
|
|
with self.assertRaises(ValueError):
|
2022-01-31 10:33:21 +01:00
|
|
|
states.alias('foo', 1)
|
2022-02-06 09:11:47 +01:00
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.alias('baz', states.FOO, states.BAR)
|
|
|
|
self.assertEqual(states.BAZ, 3)
|
|
|
|
|
|
|
|
|
|
|
|
def test_alias_multi(self):
|
|
|
|
states = State(3)
|
2022-01-31 09:38:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_alias_cover(self):
|
|
|
|
states = State(3)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
with self.assertRaises(StateInvalid):
|
|
|
|
states.alias('baz', 5)
|
2022-03-16 17:49:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_alias_invalid(self):
|
|
|
|
states = State(3)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.put('abcd')
|
|
|
|
states.set('abcd', states.FOO)
|
|
|
|
with self.assertRaises(StateInvalid):
|
|
|
|
states.set('abcd', states.BAR)
|
|
|
|
|
|
|
|
|
|
|
|
def test_alias_invalid_ignore(self):
|
|
|
|
states = State(3, check_alias=False)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
2022-03-29 13:29:10 +02:00
|
|
|
states.add('baz')
|
2022-03-16 17:49:00 +01:00
|
|
|
states.put('abcd')
|
|
|
|
states.set('abcd', states.FOO)
|
2022-03-29 13:29:10 +02:00
|
|
|
states.set('abcd', states.BAZ)
|
2022-03-16 17:49:00 +01:00
|
|
|
v = states.state('abcd')
|
|
|
|
s = states.name(v)
|
2022-03-31 10:34:51 +02:00
|
|
|
self.assertEqual(s, '_FOO.BAZ')
|
2022-03-16 17:49:00 +01:00
|
|
|
|
2022-01-31 09:38:14 +01:00
|
|
|
|
2022-02-02 08:59:52 +01:00
|
|
|
def test_peek(self):
|
2022-03-17 22:36:07 +01:00
|
|
|
states = State(2)
|
2022-02-02 08:59:52 +01:00
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
|
|
|
|
states.put('abcd')
|
|
|
|
self.assertEqual(states.peek('abcd'), states.FOO)
|
|
|
|
|
|
|
|
states.move('abcd', states.FOO)
|
|
|
|
self.assertEqual(states.peek('abcd'), states.BAR)
|
|
|
|
|
|
|
|
states.move('abcd', states.BAR)
|
|
|
|
|
|
|
|
with self.assertRaises(StateInvalid):
|
2022-03-17 22:36:07 +01:00
|
|
|
states.peek('abcd')
|
2022-02-02 08:59:52 +01:00
|
|
|
|
2022-01-31 09:38:14 +01:00
|
|
|
|
2022-02-03 11:05:59 +01:00
|
|
|
def test_from_name(self):
|
|
|
|
states = State(3)
|
|
|
|
states.add('foo')
|
|
|
|
self.assertEqual(states.from_name('foo'), states.FOO)
|
|
|
|
|
|
|
|
|
2022-03-11 13:01:56 +01:00
|
|
|
def test_change(self):
|
|
|
|
states = State(3)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.add('baz')
|
|
|
|
states.alias('inky', states.FOO | states.BAR)
|
|
|
|
states.alias('pinky', states.FOO | states.BAZ)
|
|
|
|
states.put('abcd')
|
|
|
|
states.next('abcd')
|
|
|
|
states.set('abcd', states.BAR)
|
|
|
|
states.change('abcd', states.BAZ, states.BAR)
|
|
|
|
self.assertEqual(states.state('abcd'), states.PINKY)
|
|
|
|
|
|
|
|
|
|
|
|
def test_change_onezero(self):
|
|
|
|
states = State(3)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.add('baz')
|
|
|
|
states.alias('inky', states.FOO | states.BAR)
|
|
|
|
states.alias('pinky', states.FOO | states.BAZ)
|
|
|
|
states.put('abcd')
|
|
|
|
states.next('abcd')
|
|
|
|
states.change('abcd', states.BAR, 0)
|
|
|
|
self.assertEqual(states.state('abcd'), states.INKY)
|
|
|
|
states.change('abcd', 0, states.BAR)
|
|
|
|
self.assertEqual(states.state('abcd'), states.FOO)
|
|
|
|
|
|
|
|
|
2022-03-13 17:36:17 +01:00
|
|
|
def test_change_dates(self):
|
|
|
|
states = State(3)
|
|
|
|
states.add('foo')
|
|
|
|
states.put('abcd')
|
|
|
|
states.put('bcde')
|
|
|
|
|
|
|
|
a = states.modified('abcd')
|
|
|
|
b = states.modified('bcde')
|
|
|
|
self.assertGreater(b, a)
|
|
|
|
|
|
|
|
states.set('abcd', states.FOO)
|
|
|
|
a = states.modified('abcd')
|
|
|
|
b = states.modified('bcde')
|
|
|
|
self.assertGreater(a, b)
|
|
|
|
|
|
|
|
|
2022-03-16 18:13:05 +01:00
|
|
|
def test_event_callback(self):
|
|
|
|
cb = MockCallback()
|
|
|
|
states = State(3, event_callback=cb.add)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.add('baz')
|
|
|
|
states.alias('xyzzy', states.FOO | states.BAR)
|
|
|
|
states.put('abcd')
|
|
|
|
states.set('abcd', states.FOO)
|
|
|
|
states.set('abcd', states.BAR)
|
|
|
|
states.change('abcd', states.BAZ, states.XYZZY)
|
|
|
|
events = cb.items['abcd']
|
|
|
|
self.assertEqual(len(events), 4)
|
2022-03-29 11:24:57 +02:00
|
|
|
self.assertEqual(states.from_name(events[0]), states.NEW)
|
|
|
|
self.assertEqual(states.from_name(events[1]), states.FOO)
|
|
|
|
self.assertEqual(states.from_name(events[2]), states.XYZZY)
|
|
|
|
self.assertEqual(states.from_name(events[3]), states.BAZ)
|
2022-03-16 18:13:05 +01:00
|
|
|
|
|
|
|
|
2022-03-16 20:25:29 +01:00
|
|
|
def test_dynamic(self):
|
|
|
|
states = State(0)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.alias('baz', states.FOO | states.BAR)
|
|
|
|
|
|
|
|
|
2022-03-18 19:34:49 +01:00
|
|
|
def test_mask(self):
|
|
|
|
states = State(3)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.add('baz')
|
|
|
|
states.alias('all', states.FOO | states.BAR | states.BAZ)
|
|
|
|
mask = states.mask('xyzzy', states.FOO | states.BAZ)
|
|
|
|
self.assertEqual(mask, states.BAR)
|
|
|
|
|
|
|
|
|
|
|
|
def test_mask_dynamic(self):
|
|
|
|
states = State(0)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.add('baz')
|
|
|
|
states.alias('all', states.FOO | states.BAR | states.BAZ)
|
|
|
|
mask = states.mask('xyzzy', states.FOO | states.BAZ)
|
|
|
|
self.assertEqual(mask, states.BAR)
|
|
|
|
|
|
|
|
|
2022-03-24 00:34:13 +01:00
|
|
|
def test_mask_zero(self):
|
|
|
|
states = State(0)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
states.add('baz')
|
|
|
|
states.alias('all', states.FOO | states.BAR | states.BAZ)
|
|
|
|
mask = states.mask('xyzzy')
|
|
|
|
self.assertEqual(mask, states.ALL)
|
|
|
|
|
|
|
|
|
2022-01-31 09:32:48 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|