Skip to content

Commit ffaea18

Browse files
committed
Test for settings api
1 parent c7e1c3d commit ffaea18

File tree

4 files changed

+149
-19
lines changed

4 files changed

+149
-19
lines changed

computercraft/rproc.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,23 @@ def proc(result):
8989
return proc
9090

9191

92-
def fact_scheme_dict(components):
92+
def fact_scheme_dict(required, optional):
93+
required_keys = set(required.keys())
94+
optional_keys = set(optional.keys())
95+
assert required_keys & optional_keys == set()
96+
all_keys = required_keys | optional_keys
97+
all_fns = required.copy()
98+
all_fns.update(optional)
99+
93100
def proc(result):
94101
result = any_dict(result)
95-
assert set(result.keys()) == set(components.keys())
96-
return {key: component(result[key]) for key, component in components.items()}
102+
result_keys = set(result.keys())
103+
assert result_keys.issubset(all_keys)
104+
assert required_keys.issubset(result_keys)
105+
return {key: all_fns[key](value) for key, value in result.items()}
97106
return proc
98107

99108

100-
def fact_scheme_dict_kw(**components):
101-
return fact_scheme_dict(components)
102-
103-
104109
def fact_mono_dict(key, value):
105110
def proc(result):
106111
result = any_dict(result)

computercraft/subapis/fs.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
from typing import Optional, List
33

44
from .base import BaseSubAPI, lua_string
5-
from ..rproc import boolean, string, integer, nil, array_string, option_string, fact_scheme_dict_kw
5+
from ..rproc import boolean, string, integer, nil, array_string, option_string, fact_scheme_dict
66

77

8-
attribute = fact_scheme_dict_kw(
9-
created=integer,
10-
modification=integer,
11-
isDir=boolean,
12-
size=integer,
13-
)
8+
attribute = fact_scheme_dict({
9+
'created': integer,
10+
'modification': integer,
11+
'isDir': boolean,
12+
'size': integer,
13+
}, {})
1414

1515

1616
class BaseHandle(BaseSubAPI):

computercraft/subapis/settings.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
from typing import Any, List
22

33
from .base import BaseSubAPI
4-
from ..rproc import nil, boolean, array_string
4+
from ..rproc import nil, boolean, string, array_string, fact_scheme_dict
5+
6+
7+
setting = fact_scheme_dict({
8+
'changed': boolean,
9+
}, {
10+
'description': string,
11+
'default': lambda v: v,
12+
'type': string,
13+
'value': lambda v: v,
14+
})
515

616

717
class SettingsAPI(BaseSubAPI):
818
_API = 'settings'
919

20+
async def define(self, name: str, description: str = None, default: Any = None, type: str = None):
21+
options = {}
22+
if description is not None:
23+
options['description'] = description
24+
if default is not None:
25+
options['default'] = default
26+
if type is not None:
27+
options['type'] = type
28+
return nil(await self._send('define', name, options))
29+
30+
async def undefine(self, name: str):
31+
return nil(await self._send('undefine', name))
32+
33+
async def getDetails(self, name: str) -> dict:
34+
return setting(await self._send('getDetails', name))
35+
1036
async def set(self, name: str, value: Any):
1137
return nil(await self._send('set', name, value))
1238

13-
async def get(self, name: str, value=None) -> Any:
14-
return await self._send('get', name, value)
39+
async def get(self, name: str, default: Any = None) -> Any:
40+
return await self._send('get', name, default)
1541

1642
async def unset(self, name: str):
1743
return nil(await self._send('unset', name))
@@ -22,8 +48,8 @@ async def clear(self):
2248
async def getNames(self) -> List[str]:
2349
return array_string(await self._send('getNames'))
2450

25-
async def load(self, path: str) -> bool:
51+
async def load(self, path: str = None) -> bool:
2652
return boolean(await self._send('load', path))
2753

28-
async def save(self, path: str) -> bool:
54+
async def save(self, path: str = None) -> bool:
2955
return boolean(await self._send('save', path))

testmod.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,102 @@ async def test_term_api(api):
752752
await term_step(api, 'You must have seen different shades of red made using palettes')
753753

754754
await api.print('Test finished successfully')
755+
756+
757+
async def test_settings_api(api):
758+
tbl = await get_object_table(api, 'settings')
759+
assert get_class_table(api.settings.__class__) == tbl
760+
761+
await step(api, 'Settings will be cleared')
762+
763+
assert await api.settings.clear() is None
764+
# names are not empty, there are system settings
765+
assert isinstance(await api.settings.getNames(), list)
766+
767+
assert await api.settings.define('test.a') is None
768+
assert await api.settings.define('test.b', description='b') is None
769+
assert await api.settings.define('test.c', type='string') is None
770+
assert await api.settings.define('test.d', default=42) is None
771+
772+
assert await api.settings.getDetails('test.a') == {
773+
'changed': False,
774+
}
775+
assert await api.settings.getDetails('test.b') == {
776+
'changed': False,
777+
'description': 'b',
778+
}
779+
assert await api.settings.getDetails('test.c') == {
780+
'changed': False,
781+
'type': 'string',
782+
}
783+
assert await api.settings.getDetails('test.d') == {
784+
'changed': False,
785+
'default': 42,
786+
'value': 42,
787+
}
788+
789+
# redefining
790+
assert await api.settings.define('test.a', type='number', default=11) is None
791+
792+
assert await api.settings.getDetails('test.a') == {
793+
'changed': False,
794+
'type': 'number',
795+
'default': 11,
796+
'value': 11,
797+
}
798+
799+
assert await api.settings.get('test.a') == 11
800+
assert await api.settings.set('test.a', 12) is None
801+
assert await api.settings.get('test.a') == 12
802+
with assert_raises(LuaException):
803+
await api.settings.set('test.a', 'text')
804+
assert await api.settings.get('test.a') == 12
805+
assert await api.settings.unset('test.a') is None
806+
assert await api.settings.get('test.a') == 11
807+
808+
assert await api.settings.set('test.c', 'hello') is None
809+
810+
assert {'test.a', 'test.b', 'test.c', 'test.d'}.issubset(set(await api.settings.getNames()))
811+
812+
assert await api.settings.undefine('test.a') is None
813+
assert await api.settings.undefine('test.b') is None
814+
assert await api.settings.undefine('test.c') is None
815+
assert await api.settings.undefine('test.d') is None
816+
817+
assert 'test.c' in await api.settings.getNames()
818+
assert await api.settings.get('test.c') == 'hello'
819+
assert await api.settings.getDetails('test.c') == {
820+
'changed': True,
821+
'value': 'hello',
822+
}
823+
824+
assert await api.settings.unset('test.c') is None
825+
826+
assert await api.settings.get('test.c') is None
827+
assert await api.settings.getDetails('test.c') == {
828+
'changed': False,
829+
}
830+
831+
assert {'test.a', 'test.b', 'test.c', 'test.d'} & set(await api.settings.getNames()) == set()
832+
833+
assert await api.settings.set('test.e', [9, 'text', False]) is None
834+
assert await api.settings.get('test.e') == [9, 'text', False]
835+
assert await api.settings.clear() is None
836+
assert await api.settings.get('test.e') is None
837+
838+
await api.fs.delete('.settings')
839+
840+
assert await api.settings.load() is False
841+
assert await api.settings.save() is True
842+
assert await api.settings.load() is True
843+
844+
await api.fs.delete('.settings')
845+
846+
assert await api.settings.set('key', 84) is None
847+
848+
assert await api.settings.save('sfile') is True
849+
assert await api.settings.load('sfile') is True
850+
851+
await api.fs.delete('sfile')
852+
853+
await api.print('Test finished successfully')

0 commit comments

Comments
 (0)