|
22 | 22 | from tomlkit.toml_document import TOMLDocument |
23 | 23 |
|
24 | 24 |
|
| 25 | +@pytest.mark.parametrize( |
| 26 | + "content,path", |
| 27 | + [ |
| 28 | + ("before = 1\n value = 'old' # keep\nafter = 2\n", ()), |
| 29 | + ("before = 1\r\nvalue = 'old' # keep\r\nafter = 2\r\n", ()), |
| 30 | + ("[table]\nbefore = 1\n value = 'old' # keep\nafter = 2\n", ("table",)), |
| 31 | + ("table = {before = 1, value = 'old', after = 2}\n", ("table",)), |
| 32 | + ( |
| 33 | + "[table.a]\nx = 1\n[other]\ny = 2\n[table]\nvalue = 'old' # keep\n", |
| 34 | + ("table",), |
| 35 | + ), |
| 36 | + ], |
| 37 | +) |
| 38 | +def test_replace_literal_string_preserves_table_style( |
| 39 | + content: str, path: tuple[str, ...] |
| 40 | +) -> None: |
| 41 | + doc = parse(content) |
| 42 | + table: Any = doc |
| 43 | + for key in path: |
| 44 | + table = table[key] |
| 45 | + table["value"] = "new" |
| 46 | + assert doc.as_string() == content.replace("'old'", "'new'") |
| 47 | + assert table["value"] == dict(table)["value"] == table.unwrap()["value"] == "new" |
| 48 | + assert parse(doc.as_string()).unwrap() == doc.unwrap() |
| 49 | + |
| 50 | + # Explicit items retain the caller's quote choice instead of inheriting it. |
| 51 | + table["value"] = tomlkit.string("explicit") |
| 52 | + assert doc.as_string() == content.replace("'old'", '"explicit"') |
| 53 | + table["value"] = tomlkit.string("literal", literal=True) |
| 54 | + assert doc.as_string() == content.replace("'old'", "'literal'") |
| 55 | + table.update(value="updated") |
| 56 | + assert doc.as_string() == content.replace("'old'", "'updated'") |
| 57 | + table["value"] = "can't" |
| 58 | + assert doc.as_string() == content.replace("'old'", '"can\'t"') |
| 59 | + assert parse(doc.as_string()).unwrap() == doc.unwrap() |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize("value", ["", "unicode λ", "a\\b", 'a"b', "a\tb"]) |
| 63 | +def test_replace_literal_string_preserves_value(value: str) -> None: |
| 64 | + doc = parse("value = 'old'\n") |
| 65 | + doc["value"] = value |
| 66 | + assert doc.as_string() == f"value = '{value}'\n" |
| 67 | + assert parse(doc.as_string())["value"] == value |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize("value", ["can't", "a\nb", "a\rb", "a\x00b", "a\x7fb"]) |
| 71 | +def test_replace_literal_string_falls_back_to_basic(value: str) -> None: |
| 72 | + doc = parse("value = 'old' # keep\n") |
| 73 | + doc["value"] = value |
| 74 | + assert doc.as_string() == f"value = {tomlkit.string(value).as_string()} # keep\n" |
| 75 | + assert parse(doc.as_string())["value"] == value |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize("original", ['"old"', "'''old'''", '"""old"""', "1"]) |
| 79 | +def test_replace_other_items_keeps_default_string_style(original: str) -> None: |
| 80 | + doc = parse(f"value = {original}\n") |
| 81 | + doc["value"] = "new" |
| 82 | + assert doc.as_string() == 'value = "new"\n' |
| 83 | + |
| 84 | + |
| 85 | +def test_literal_replacement_does_not_change_new_keys_or_arrays() -> None: |
| 86 | + doc = parse("value = 'old'\narray = ['old']\n") |
| 87 | + doc["new"] = "new" |
| 88 | + doc["array"][0] = "new" |
| 89 | + doc["value"] = 2 |
| 90 | + assert doc.as_string() == 'value = 2\narray = ["new"]\nnew = "new"\n' |
| 91 | + |
| 92 | + |
25 | 93 | def test_document_is_a_dict(example: Callable[[str], str]) -> None: |
26 | 94 | content = example("example") |
27 | 95 |
|
|
0 commit comments