def test_set(self): a = FiFo('TestString', 10) ItemRegistry.RemoveCustomItem('TestString') SET_STR = 'asfdasdfasf' a[7] = SET_STR time.sleep(0.1) self.assertEqual(ItemRegistry.getItem('TestString7').state, SET_STR)
def test_AddCustomItem(self): CUSTOM_NAME = 'afdsvdadsffadsfadsf' l = ['TestCase', 'asdf'] ItemRegistry.AddCustomItem(CUSTOM_NAME, l) a = ItemRegistry.getItem(CUSTOM_NAME) self.assertIsInstance(a, list) for a, b in zip(l, a): self.assertEquals(a, b)
def test_push_11int(self): a = FiFo('TestNumber', 10) ItemRegistry.RemoveCustomItem('TestNumber') a.push(0) a.push(9) a.push(8) a.push(7) a.push(6) a.push(5) a.push(4) a.push(3) a.push(2) a.push(1) a.push(0) self.assertEqual(a[0], 0) self.assertEqual(a[1], 1) self.assertEqual(a[2], 2) self.assertEqual(a[3], 3) self.assertEqual(a[4], 4) self.assertEqual(a[5], 5) self.assertEqual(a[6], 6) self.assertEqual(a[7], 7) self.assertEqual(a[8], 8) self.assertEqual(a[9], 9)
def __init__(self, item_name, min=0, max=None, start=None, postUpdate=True): """ Thread-safe counter :param item_name: name of the item under which the counter will be found in item registry :param min: Maximum value of the counter :param max: Minimum value of the counter :param start: Start value of the counter, will be set when Initialize-Function is called (call manually!) :param postUpdate: Update item with same name as counter with current value """ self.item_name = item_name self.start = start self.min = min self.max = max self.val = 0 self.__postUpdate = postUpdate self.__lock = threading.Lock() #return this counter instead of item ItemRegistry.AddCustomItem(item_name, self)
def __init__(self, item_name, len): assert isinstance(item_name, str), type(item_name) assert isinstance(len, int) and len > 0, '{} : {}'.format( type(len), len) self.__data = [] self.__len = len self.__item_names = [] for i in range(self.__len): self.__item_names.append('{:s}{:d}'.format(item_name, i)) self.__data.append(None) #return this FiFo ItemRegistry.AddCustomItem(item_name, self) #if the items already exist if ItemRegistry.ItemExists(self.__item_names[0]): self.Initialize()
def test_max(self): a = Counter('TestNumber', max=10, start=999) ItemRegistry.RemoveCustomItem('TestNumber') a.Initialize() self.assertEqual(a.val, 10) a.Decrease(1) time.sleep(0.1) self.assertEqual(item.state, 9) a.Increase(6) time.sleep(0.1) self.assertEqual(item.state, 10)
def __init__(self, item_name, len): assert isinstance(item_name, str), type(item_name) assert isinstance(len, int) and len > 0, '{} : {}'.format( type(len), len) self.__len = len self.__item_names = [] _name = item_name + '{:d}' for i in range(self.__len): self.__item_names.append(_name.format(i)) #return this counter instead of item ItemRegistry.AddCustomItem(item_name, self)
def test_Increase(self): a = Counter('TestNumber') ItemRegistry.RemoveCustomItem('TestNumber') a.Initialize() self.assertEqual(a.val, 0) a.Increase(1) time.sleep(0.1) self.assertEqual(item.state, 1) a.Increase(5) time.sleep(0.1) self.assertEqual(item.state, 6) a.Decrease(6) time.sleep(0.1) self.assertEqual(item.state, 0)
def test_postUpdateString(self): BusEvent.postUpdate('TestString', 'asdf') time.sleep(0.1) a = ItemRegistry.getItem('TestString') print('-----') print('issubclass: {}'.format( issubclass(EasyRule.replacements.items.StringItem, EasyRule.replacements.items._BaseItem))) print('isinstance: {}'.format(isinstance(a, Items.StringItem))) print(' type: {}'.format(type(a))) print(' type1: {}'.format( type(EasyRule.replacements.items.StringItem))) print(' type2: {}'.format(type(Items.StringItem))) self.assertIsInstance(a, EasyRule.replacements.items.StringItem) self.assertIsInstance(a, Items.StringItem) self.assertEqual(a.type, "String", a.type) self.assertEqual(a.state, "asdf", a.state)
def Initialize(self): _start = None #if start value not set try to get it from item registry if self.start is None: item = ItemRegistry.getItem(self.item_name, search_custom_items=False) if item.state is None: _start = 0 else: assert isinstance( item.state, int), 'Item state has to be integer. Is: "{}"({})'.format( item.state, type(item.state)) _start = item.state else: _start = self.start assert _start is not None self.__process(None, _start) return self.val
def setUp(self): self.__name = 'adsfadsfr32z4536563564563456' ItemRegistry.AddCustomItem(self.__name, {'val': 9999})
def test_getItems(self): self.assertEqual(len(ItemRegistry.getItems('TestString\d+')), 10) self.assertEqual(len(ItemRegistry.getItems('TestString')), 1)
def test_getItemPlayer(self): self.assertEqual(ItemRegistry.getItem('TestPlayer').type, 'Player')
def test_getItemLocation(self): self.assertEqual(ItemRegistry.getItem('TestLocation').type, 'Location')
def test_getItemColor(self): self.assertEqual(ItemRegistry.getItem('TestColor').type, 'Color')
def test_constructor1(self): a = FiFo('TestNumber', 10) item = ItemRegistry.getItem('TestNumber') self.assertIsInstance(item, FiFo) ItemRegistry.RemoveCustomItem('TestNumber')
def test_AddCustomItem1(self): a = ItemRegistry.getItem(self.__name) self.assertEqual(a['val'], 9999)
def test_getItemSwitch(self): self.assertEqual(ItemRegistry.getItem('TestSwitch').type, 'Switch')
def test_constructor1(self): a = Counter('TestNumber') item = ItemRegistry.getItem('TestNumber') self.assertIsInstance(item, Counter) ItemRegistry.RemoveCustomItem('TestNumber')
def test_push_str(self): a = FiFo('TestString', 10) ItemRegistry.RemoveCustomItem('TestString') a.push('Leberwurst') time.sleep(0.1) self.assertEqual( ItemRegistry.getItem('TestString0').state, 'Leberwurst') a.push('Schinkenwurst') time.sleep(0.1) self.assertEqual( ItemRegistry.getItem('TestString0').state, 'Schinkenwurst') self.assertEqual( ItemRegistry.getItem('TestString1').state, 'Leberwurst') a.push('Streichwurst') time.sleep(0.1) self.assertEqual( ItemRegistry.getItem('TestString0').state, 'Streichwurst') self.assertEqual( ItemRegistry.getItem('TestString1').state, 'Schinkenwurst') self.assertEqual( ItemRegistry.getItem('TestString2').state, 'Leberwurst') a.push('Erdberkaese') time.sleep(0.1) self.assertEqual( ItemRegistry.getItem('TestString0').state, 'Erdberkaese') self.assertEqual( ItemRegistry.getItem('TestString1').state, 'Streichwurst') self.assertEqual( ItemRegistry.getItem('TestString2').state, 'Schinkenwurst') self.assertEqual( ItemRegistry.getItem('TestString3').state, 'Leberwurst')
def __iter__(self): for __item in self.__item_names: yield ItemRegistry.getItem(__item).state
def __getitem__(self, index): assert isinstance(index, int) return ItemRegistry.getItem(self.__item_names[index]).state
def test_push_int(self): a = FiFo('TestNumber', 10) ItemRegistry.RemoveCustomItem('TestNumber') a.push(0) time.sleep(0.1) self.assertEqual(ItemRegistry.getItem('TestNumber0').state, 0) a.push(1) time.sleep(0.1) self.assertEqual(ItemRegistry.getItem('TestNumber0').state, 1) self.assertEqual(ItemRegistry.getItem('TestNumber1').state, 0) a.push(3) time.sleep(0.1) self.assertEqual(ItemRegistry.getItem('TestNumber0').state, 3) self.assertEqual(ItemRegistry.getItem('TestNumber1').state, 1) self.assertEqual(ItemRegistry.getItem('TestNumber2').state, 0) a.push(5) time.sleep(0.1) self.assertEqual(ItemRegistry.getItem('TestNumber0').state, 5) self.assertEqual(ItemRegistry.getItem('TestNumber1').state, 3) self.assertEqual(ItemRegistry.getItem('TestNumber2').state, 1) self.assertEqual(ItemRegistry.getItem('TestNumber3').state, 0)
def test_getItemDateTime(self): self.assertEqual(ItemRegistry.getItem('TestDateTime').type, 'DateTime')
def test_ItemExists(self): self.assertEqual(ItemRegistry.ItemExists('TestString'), True) self.assertEqual(ItemRegistry.ItemExists('zzzzzzzzzzzzzzzz'), False)
def test_getItemGroup(self): self.assertEqual(ItemRegistry.getItem('TestGroup').type, 'Group')
def test_getItemString(self): self.assertEqual(ItemRegistry.getItem('TestString').type, 'String')
def Initialize(self): for i, name in enumerate(self.__item_names): item = ItemRegistry.getItem(name, search_custom_items=False) if item.state is not None: self.__data[i] = item.state
def test_getItemNumber(self): self.assertEqual(ItemRegistry.getItem('TestNumber').type, 'Number')
import unittest, time from EasyRule import ItemRegistry from EasyRule.Items import NumberItem from EasyModule import Counter item = ItemRegistry.getItem('TestNumber') #assert isinstance(item, NumberItem) class Basic(unittest.TestCase): def test_constructor1(self): a = Counter('TestNumber') item = ItemRegistry.getItem('TestNumber') self.assertIsInstance(item, Counter) ItemRegistry.RemoveCustomItem('TestNumber') def test_Increase(self): a = Counter('TestNumber') ItemRegistry.RemoveCustomItem('TestNumber') a.Initialize() self.assertEqual(a.val, 0) a.Increase(1) time.sleep(0.1) self.assertEqual(item.state, 1) a.Increase(5) time.sleep(0.1) self.assertEqual(item.state, 6) a.Decrease(6)