py-xbc/tests/test_bare.py

88 lines
2.4 KiB
Python
Raw Normal View History

2024-01-16 01:08:01 +00:00
'''
Tests for base-level keys.
'''
2024-01-13 06:44:43 +00:00
import pytest
from xbc import loads_xbc, ParseError
def test_key():
2024-01-16 01:08:01 +00:00
'A key by itself exists.'
assert loads_xbc('a') == {'a': True}
2024-01-13 06:44:43 +00:00
def test_dot_key():
2024-01-16 01:08:01 +00:00
'A nested key by itself exists.'
assert loads_xbc('a.a') == {'a': False, 'a.a': True}
2024-01-13 06:44:43 +00:00
def test_key_eq():
2024-01-16 01:08:01 +00:00
'A key with an empty assignment exists.'
assert loads_xbc('a =') == {'a': True}
2024-01-13 06:44:43 +00:00
def test_keyvalue():
2024-01-16 01:08:01 +00:00
'A bare value can be assigned to a key.'
assert loads_xbc('a = 1') == {'a': '1'}
2024-01-13 06:44:43 +00:00
def test_keyvalue_space():
2024-01-16 01:08:01 +00:00
'A bare value can have spaces.'
assert loads_xbc('a = a b') == {'a': 'a b'}
2024-01-13 06:44:43 +00:00
def test_dot_keyvalue():
2024-01-16 01:08:01 +00:00
'A key being assigned to can have dots.'
assert loads_xbc('a.a = 1') == {'a': False, 'a.a': '1'}
2024-01-13 06:44:43 +00:00
def test_keys():
2024-01-16 01:08:01 +00:00
'Statements can be separated by semicolons.'
assert loads_xbc('a;b') == {'a': True, 'b': True}
2024-01-13 06:44:43 +00:00
def test_dot_keys():
2024-01-16 01:08:01 +00:00
'Keys in compound statements can have dots.'
assert loads_xbc('a.a;a.b') == {'a': False, 'a.a': True, 'a.b': True}
2024-01-13 06:44:43 +00:00
def test_quoted():
2024-01-16 01:08:01 +00:00
'Values can be quoted with single or double quotes.'
assert loads_xbc('a = "b"') == {'a': 'b'}
2024-01-13 06:44:43 +00:00
def test_quoted_space():
2024-01-16 01:08:01 +00:00
'Quoted values can have trailing whitespace preserved.'
assert loads_xbc('a = "b "') == {'a': 'b '}
2024-01-13 06:44:43 +00:00
def test_array():
2024-01-16 01:08:01 +00:00
'Multiple values can be assigned to a single key.'
assert loads_xbc('a = 1, 2') == {'a': ['1', '2']}
2024-01-13 06:44:43 +00:00
def test_reassignment():
2024-01-16 01:08:01 +00:00
'Keys cannot be reassigned.'
with pytest.raises(ParseError):
loads_xbc('a = 1\na = 2')
2024-01-13 06:44:43 +00:00
def test_reassignment_colon():
2024-01-16 01:08:01 +00:00
'Keys cannot be reassigned, even in compound statements.'
with pytest.raises(ParseError):
loads_xbc('a = 1;a = 2')
2024-01-13 06:44:43 +00:00
def test_ovewrite_nonexistent():
2024-01-16 01:08:01 +00:00
'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']}