def test_build_exc() -> None: """Test the with statement handles exceptions correctly.""" class Exc(Exception): pass prop = Property('Root', []) with pytest.raises(Exc): # Should not swallow. with prop.build() as build: build.prop('Hi') raise Exc # Exception doesn't rollback. assert_tree(Property('Root', [ Property('prop', 'Hi'), ]), prop) prop.clear() with prop.build() as build: build.leaf('value') with pytest.raises(Exc): with build.subprop as sub: raise Exc assert_tree( Property('Root', [ Property('leaf', 'value'), Property('subprop', []), ]), prop)
def test_blockfuncs_fail_on_leaf() -> None: """Check that methods requiring a block fail on a leaf property.""" leaf = Property('Name', 'blah') with pytest.raises(ValueError): for _ in leaf.find_all("blah"): pass with pytest.raises(ValueError): leaf.find_key("blah") with pytest.raises(ValueError): for _ in leaf: pass with pytest.raises(ValueError): leaf['blah'] with pytest.raises(ValueError): leaf['blah'] = 't' with pytest.raises(ValueError): leaf.int('blah') with pytest.raises(ValueError): leaf.bool('blah') with pytest.raises(ValueError): leaf.float('blah') with pytest.raises(ValueError): leaf.vec('blah') with pytest.raises(ValueError): len(leaf) with pytest.raises(ValueError): with leaf.build(): pass with pytest.raises(ValueError): leaf.ensure_exists('blah') with pytest.raises(ValueError): leaf.set_key(("blah", "another"), 45) with pytest.raises(ValueError): leaf.merge_children()
def test_build(): """Test the .build() constructor.""" prop = Property(None, []) with prop.build() as b: with b.Root1: b.Key("Value") b.Extra("Spaces") with b.Block: with b.Empty: pass with b.Block: with b.bare: b.block('he\tre') with b.Root2: b['Name with " in it']('Value with \" inside') b.multiline( 'text\n\tcan continue\nfor many "lines" of\n possibly ' 'indented\n\ntext' ) # Note invalid = unchanged. b.Escapes('\t \n \\d') with b.Oneliner: b.name('value') with b.CommentChecks: b['after ']('value') b.Flag('allowed') b.FlagAllows('This') b.Replaced('toreplace') b.Replaced('alsothis') b.Replaced('toreplace2') b.Replaced('alsothis2') with b.Replaced: b.lambda_('should') b.replace('above') with b.Replaced: b['lambda2']('should2') b.replace2('above2') assert_tree(parse_result, prop)