xbc: support append to bare key, add array tests

current
Síle Ekaterin Liszka 2024-01-19 12:49:51 -08:00
parent 2c0dcb6e9e
commit e6205a4242
Signed by: VulpineAmethyst
SSH Key Fingerprint: SHA256:VcHwQ6SUfi/p0Csfxe3SabX/TImWER0PhoJqkt+GlmE
2 changed files with 24 additions and 0 deletions

View File

@ -215,12 +215,16 @@ def parse_block(key, seq):
if op == '+=':
if isinstance(ret[k], str):
assign = [ret[k]]
elif ret[k] is True:
assign = []
else:
assign = ret[k]
if isinstance(value, str):
assign.append(value)
else:
assign.extend(value)
if isinstance(assign, list) and len(assign) == 1:
assign = assign[0]
else:
assign = value

View File

@ -65,3 +65,23 @@ def test_ovewrite_nonexistent():
'Keys can only be updated if they exist.'
with pytest.raises(ParseError):
loads_xbc('a := 1')
def test_append_key():
'Append an item to a key.'
assert loads_xbc('a; a += 1') == {'a': '1'}
def test_append_single():
'Append an item to a single-item value.'
assert loads_xbc('a = 1; a += 2') == {'a': ['1', '2']}
def test_append_single2():
'Append multiple items to a single-item value.'
assert loads_xbc('a = 1; a += 2, 3') == {'a': ['1', '2', '3']}
def test_append_multi():
'Append an item to an array value'
assert loads_xbc('a = 1, 2; a += 3') == {'a': ['1', '2', '3']}
def test_append_multi2():
'Append multiple items to an array value.'
assert loads_xbc('a = 1, 2; a += 3, 4') == {'a': ['1', '2', '3', '4']}