2022-01-31 09:32:48 +01:00
|
|
|
# standard imports
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
'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):
|
|
|
|
states = State(2)
|
|
|
|
states.add('foo')
|
|
|
|
states.add('bar')
|
|
|
|
with self.assertRaises(OverflowError):
|
|
|
|
states.add('baz')
|
|
|
|
|
|
|
|
|
|
|
|
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-02-01 08:47:07 +01:00
|
|
|
|
2022-01-31 09:38:14 +01:00
|
|
|
|
2022-02-02 08:59:52 +01:00
|
|
|
def test_peek(self):
|
|
|
|
states = State(3)
|
|
|
|
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):
|
|
|
|
self.assertEqual(states.peek('abcd'))
|
|
|
|
|
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-01-31 09:32:48 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|