diff --git a/src/xbc/__init__.py b/src/xbc/__init__.py index e26df55..b9a1109 100644 --- a/src/xbc/__init__.py +++ b/src/xbc/__init__.py @@ -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 diff --git a/tests/test_bare.py b/tests/test_bare.py index 3923c67..2e6ceba 100644 --- a/tests/test_bare.py +++ b/tests/test_bare.py @@ -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']}