Skip to content

Commit

Permalink
fix: delete inline table element does not leave trailing sep (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Jan 29, 2023
1 parent 7134436 commit 588ecce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
### Fixed

- Parse empty table name if it is quoted. ([#258](https://github.com/sdispater/tomlkit/issues/258))
- Fix a bug that remove last element of an Inline Table leaves a comma. ([#259](https://github.com/sdispater/tomlkit/issues/259))
- Fix the `unwrap()` method for `Container` children values which sometimes returns an internal object if the table is an out-of-order table. ([#264](https://github.com/sdispater/tomlkit/issues/264))

## [0.11.6] - 2022-10-27
Expand Down
18 changes: 17 additions & 1 deletion tests/test_items.py
Expand Up @@ -824,7 +824,7 @@ def test_trim_comments_when_building_inline_table():
assert table.as_string() == '{foo = "bar", baz = "foobaz"}'


def test_deleting_inline_table_elemeent_does_not_leave_trailing_separator():
def test_deleting_inline_table_element_does_not_leave_trailing_separator():
table = api.inline_table()
table["foo"] = "bar"
table["baz"] = "boom"
Expand All @@ -845,6 +845,22 @@ def test_deleting_inline_table_elemeent_does_not_leave_trailing_separator():
assert table.as_string() == '{baz = "boom"}'


def test_deleting_inline_table_element_does_not_leave_trailing_separator2():
doc = parse('a = {foo = "bar", baz = "boom"}')
table = doc["a"]
assert table.as_string() == '{foo = "bar", baz = "boom"}'

del table["baz"]
assert table.as_string() == '{foo = "bar" }'

del table["foo"]
assert table.as_string() == "{ }"

table["baz"] = "boom"

assert table.as_string() == '{ baz = "boom"}'


def test_booleans_comparison():
boolean = Bool(True, Trivia())

Expand Down
10 changes: 9 additions & 1 deletion tomlkit/items.py
Expand Up @@ -1719,6 +1719,14 @@ def append(self, key, _item):

def as_string(self) -> str:
buf = "{"
last_item_idx = next(
(
i
for i in range(len(self._value.body) - 1, -1, -1)
if self._value.body[i][0] is not None
),
None,
)
for i, (k, v) in enumerate(self._value.body):
if k is None:
if i == len(self._value.body) - 1:
Expand All @@ -1741,7 +1749,7 @@ def as_string(self) -> str:
f"{v_trivia_trail}"
)

if i != len(self._value.body) - 1:
if last_item_idx is not None and i < last_item_idx:
buf += ","
if self._new:
buf += " "
Expand Down

0 comments on commit 588ecce

Please sign in to comment.