mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-26 20:59:27 +01:00
Add editable() method to registry.Json.
This commit is contained in:
parent
d8bd1f6f92
commit
d9f7e0e818
@ -686,4 +686,18 @@ class Json(String):
|
||||
def __call__(self):
|
||||
return json.loads(super(Json, self).__call__())
|
||||
|
||||
class _Context:
|
||||
def __init__(self, var):
|
||||
self._var = var
|
||||
def __enter__(self):
|
||||
self._dict = self._var()
|
||||
return self._dict
|
||||
def __exit__(self, *args):
|
||||
self._var.setValue(self._dict)
|
||||
|
||||
def editable(self):
|
||||
"""Return an editable dict usable within a 'with' statement and
|
||||
committed to the configuration variable at the end."""
|
||||
return self._Context(self)
|
||||
|
||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
||||
|
@ -138,6 +138,16 @@ class ValuesTestCase(SupyTestCase):
|
||||
self.assertEqual(v(), data)
|
||||
self.assertIsNot(v(), data)
|
||||
|
||||
with v.editable() as dict_:
|
||||
dict_['supy'] = 'bot'
|
||||
del dict_['qux']
|
||||
self.assertNotIn('supy', v())
|
||||
self.assertIn('qux', v())
|
||||
self.assertIn('supy', v())
|
||||
self.assertEqual(v()['supy'], 'bot')
|
||||
self.assertIsNot(v()['supy'], 'bot')
|
||||
self.assertNotIn('qux', v())
|
||||
|
||||
def testNormalizedString(self):
|
||||
v = registry.NormalizedString("""foo
|
||||
bar baz
|
||||
|
Loading…
Reference in New Issue
Block a user