def test_items_can_be_appended_to_and_removed_from_an_inline_table(): string = """table = {} """ parser = Parser(string) _, table = parser._parse_item() assert isinstance(table, InlineTable) assert "{}" == table.as_string() table.append(Key("foo"), String(StringType.SLB, "bar", "bar", Trivia(trail=""))) assert '{foo = "bar"}' == table.as_string() table.append(Key("baz"), Integer(34, Trivia(trail=""), "34")) assert '{foo = "bar", baz = 34}' == table.as_string() table.remove(Key("baz")) assert '{foo = "bar"}' == table.as_string() table.remove(Key("foo")) assert "{}" == table.as_string() with pytest.raises(NonExistentKey): table.remove(Key("foo"))
def test_items_can_be_appended_to_and_removed_from_a_table(): string = """[table] """ parser = Parser(string) _, table = parser._parse_table() assert isinstance(table, Table) assert "\n" == table.as_string() table.append(Key("foo"), String(StringType.SLB, "bar", "bar", Trivia(trail="\n"))) assert '\nfoo = "bar"\n' == table.as_string() table.append( Key("baz"), Integer(34, Trivia(comment_ws=" ", comment="# Integer", trail=""), "34"), ) assert '\nfoo = "bar"\nbaz = 34 # Integer' == table.as_string() table.remove(Key("baz")) assert '\nfoo = "bar"\n' == table.as_string() table.remove(Key("foo")) assert "\n" == table.as_string() with pytest.raises(NonExistentKey): table.remove(Key("foo"))
def test_key_comparison(): k = Key("foo") assert k == Key("foo") assert k == "foo" assert k != "bar" assert k != 5
def test_key_automatically_sets_proper_string_type_if_not_bare(): key = Key("foo.bar") assert key.t == KeyType.Basic key = Key("") assert key.t == KeyType.Basic
def reorder_container(container, sort_key=None, reverse=False, key_type=None): # type: (Container, SortKeyFunc, bool, KeyType) -> None items = container.body if sort_key is not None or reverse: items.sort(key=sort_key, reverse=reverse) container._map = {} container._body = [] for key, value in items: if key is None: pass elif isinstance(key, Key): if key_type is not None: key = Key(key.key, key_type) else: key = Key(str(key), key_type) container.append(key, value)
def reorder_container( container: Container, sort_key: Callable[[ContainerItem], Hashable] = None, reverse: bool = False, key_type: KeyType = None, ) -> None: items = container._body if sort_key is not None or reverse: items.sort(key=sort_key, reverse=reverse) container._map = {} container._body = [] for key, value in items: if key is None: pass elif isinstance(key, Key): if key_type is not None: key = Key(key.key, key_type) else: key = Key(key, key_type) container.append(key, value)