def test_prologue_bad_new_shared(): """ Try to change Prologue's shared delimiter value """ pro = Prologue() # Check a bad value doesn't work with pytest.raises(PrologueError) as excinfo: pro.shared_delimiter = "banana" assert "Shared delimiter should be True or False" in str(excinfo.value) # Check both sane values work for val in (True, False): pro.shared_delimiter = val assert pro.shared_delimiter == val
def test_prologue_get_directive(): """ Request registered and unregistered directives """ pro = Prologue() # Register a bunch of directives class LineDirx(LineDirective): pass class BlockDirx(BlockDirective): pass line_opens = [random_str(3, 10) for _x in range(5)] block_opens = [random_str(3, 10, avoid=line_opens) for _x in range(5)] block_close = [ random_str(3, 10, avoid=(line_opens + block_opens)) for _x in range(5) ] wrap_line = DirectiveWrap(LineDirx, line_opens) wrap_block = DirectiveWrap(BlockDirx, block_opens, closing=block_close) for opening in line_opens: assert not pro.has_directive(opening) pro.register_directive(wrap_line) for tag in line_opens: assert pro.has_directive(tag) for tag in (block_opens + block_close): assert not pro.has_directive(tag) pro.register_directive(wrap_block) for tag in (block_opens + block_close): assert pro.has_directive(tag) # List all of the registered directives reg_dirx = pro.list_directives() assert wrap_line in reg_dirx assert wrap_block in reg_dirx # Test that correct directive is returned each time all_tags = line_opens + block_opens + block_close for use_shared in (False, True): pro.shared_delimiter = use_shared for _x in range(100): use_bad = choice((True, False)) tag = random_str(3, 10, avoid=all_tags) if use_bad else choice(all_tags) # If using a bad directive without shared delimiters, expect an exception if use_bad and not use_shared: with pytest.raises(PrologueError) as excinfo: pro.get_directive(tag) assert f"No directive known for tag '{tag}'" == str( excinfo.value) # If using a bad directive with shared delimiters, expect None elif use_bad and use_shared: assert pro.get_directive(tag) == None # Otherwise, expect the correct directive to be returned else: if tag in line_opens: assert pro.get_directive(tag) == wrap_line else: assert pro.get_directive(tag) == wrap_block # Test de-registering directives pro.deregister_directive(choice(line_opens)) for tag in line_opens: assert not pro.has_directive(tag) pro.deregister_directive(choice(block_opens + block_close)) for tag in (block_opens + block_close): assert not pro.has_directive(tag) # Test deregistering directives again for tags in (line_opens, block_opens + block_close): use_tag = choice(tags) with pytest.raises(PrologueError) as excinfo: pro.deregister_directive(use_tag) assert str( excinfo.value) == f"No directive registered for tag '{use_tag}'"