def person_decl(): return Tag( 'Person', { 'first-name': Tag('String'), 'last-name': Tag('String?'), 'birthday': Tag('Date?'), })
def test_tag_encoder(self): assert dumps(Tag('Tag', True)) == 'Tag true' assert dumps(Tag('Complex Tag', True)) == r'\"Complex Tag" true' assert dumps(Tag('Tag', [1, 2, 3])) == 'Tag [1 2 3]' assert dumps(Tag('Tag', [1, 2, 3], foo=1, bar=2)) == 'Tag (foo=1 bar=2) [1 2 3]' assert dumps(Tag.new('Tag', {'key with space': True}, None)) == '(Tag "key with space"=true)'
def test_happy_validating_parsing(self, schema_src): person = schema(schema_src) # Happy stories assert person.loads('Person {first-name: "Joe"}') == \ Tag('Person', {'first-name': 'Joe', 'last-name': None, 'birthday': None}) assert person.loads('Person {first-name: "Joe", last-name: "Smith"}') == \ Tag('Person', {'first-name': 'Joe', 'last-name': 'Smith', 'birthday': None}) assert person.loads('Person {first-name: "Joe", birthday: 1970-01-01}') == \ Tag('Person', {'first-name': 'Joe', 'birthday': date(1970, 1, 1), 'last-name': None}) assert person.loads('Person {first-name: "Joe", last-name: "Smith", birthday: 1970-01-01}') == \ Tag('Person', {'first-name': 'Joe', 'last-name': 'Smith', 'birthday': date(1970, 1, 1)}) assert person.loads('Person {first-name: "Joe", last-name: "Smith", birthday: null}') == \ Tag('Person', {'first-name': 'Joe', 'last-name': 'Smith', 'birthday': None})
def test_basic_interface(self): assert str(Tag('foo')) == '(foo)' assert str(Tag('foo bar')) == r"(\'foo bar')" assert str(Tag('foo', bar=42)) == '(foo bar=42)' assert str(Tag('foo', 'bar')) == "foo 'bar'" assert str(Tag('foo', 'bar', baz=42)) == "foo (baz=42) 'bar'" assert repr(Tag('foo')) == "Tag('foo')" assert repr(Tag('foo', bar=42)) == "Tag('foo', bar=42)" assert repr(Tag('foo', 'bar', baz=42)) == "Tag('foo', 'bar', baz=42)"
def test_treat_string_as_atomic(self): tag = Tag('foo', 'bar') with pytest.raises(ValueError): print(len(tag)) with pytest.raises(ValueError): print(list(tag)) with pytest.raises(ValueError): print(tag[0])
def test_full_dot_fred(self): tag, meta, value = fred.load(examples / "full.fred").split() pprint(value) assert tag == 'FRED/Full' assert meta == {} assert math.isnan(value["keywords"].pop(3)) assert value["keywords"] == [ True, False, None, float('inf'), -float('inf') ] assert value["numbers"] == [ 0, -1, 2.0, -3.0, 4e+0, -5.0, 0b110, -0b111, 0o10, -0o11, 0xa, -0xb ] assert value["strings"] == [ "some string", 'with"escape"', b'some bytes', b'with`escape`' ] assert value["dates"] == [ date(1970, 1, 1), time(12, 0), time(13, 0, tzinfo=timezone.utc), datetime(2001, 1, 1, 10, 32, tzinfo=timezone(timedelta(hours=3))), ] assert value["arrays"] == [[["nested"]], [[True]]] assert value["objects"] == [{"simple": True}, {"quoted": "ok"}] assert value["symbols"] == [ Symbol('symbol'), Symbol("quoted symbol"), Symbol('dashed-case'), Symbol('name.space'), Symbol('main/variant') ] assert value["tags"] == [ Tag('tag', ['tagged value'], attr='value'), Tag('enclosed-tag', 'tagged value', attr='value'), Tag('void-tag', attr1=1, attr2=2), Tag('void-tag'), Tag.new('tag', { 'complex-attr': Tag('complex?', True), 'more...': [1, 2, 3] }, Symbol('simple-value')), Tag('nested', Tag('tag', Symbol('for-value'))), Tag.new('quoted tag', {'quoted attr': "value"}, {'tagged': "value"}), ]
def test_enclosed_tag_parsing(self): src = '(tag)' assert list(lex(src)) == ['(', 'tag', ')'] assert fred.loads(src) == Tag('tag') src = '(tag attr="value")' assert list(lex(src)) == ['(', 'tag', 'attr', '=', '"value"', ')'] assert fred.loads(src) == Tag('tag', attr='value') src = '(tag $value)' assert list(lex(src)) == ['(', 'tag', '$value', ')'] assert fred.loads(src) == Tag('tag', Symbol('value')) assert fred.loads('(tag "string")') == Tag('tag', 'string') src = '(tag attr="attr" $value)' assert list( lex(src)) == ['(', 'tag', 'attr', '=', '"attr"', '$value', ')'] assert fred.loads(src) == Tag('tag', Symbol('value'), attr='attr') src = '(tag attr="attr" "value")' assert list( lex(src)) == ['(', 'tag', 'attr', '=', '"attr"', '"value"', ')'] assert fred.loads(src) == Tag('tag', 'value', attr='attr')
def test_behaves_like_list(self): lst = [1, 2, 3] tag = Tag('Dic', lst) assert len(tag) == 3 assert tag[0] == lst[0] assert list(tag) == [1, 2, 3]
def test_behaves_like_dict(self): dic = {'foo': 1, 'bar': 2} tag = Tag('Dic', dic) assert len(tag) == 2 assert tag['foo'] == dic['foo'] assert list(tag) == ['foo', 'bar']
def test_equality(self): assert Tag('foo') == Tag('foo') assert Tag('foo') != Tag('bar') assert Tag('foo', 'bar') == Tag('foo', 'bar') assert Tag('foo', 'bar', baz='baz') == Tag('foo', 'bar', baz='baz') assert Tag('foo') != 'foo'
def test_invalid_constructions(self): with pytest.raises(TypeError): Tag(42)
def test_constructor(self): assert Tag('foo') == Tag(Symbol('foo')) assert type(Tag(Symbol('foo')).tag) is str assert Tag.new('foo bar', {}, None).tag == 'foo bar'
def test_make_simple_validator(self): validator = make_validator(Tag('Foo', Tag('String')), {}) assert validator(None, Tag('Foo', 'bar')) == Tag('Foo', 'bar')