| 114 | |
| 115 | |
| 116 | class TestApi: |
| 117 | def test_set_get_delete(self): |
| 118 | bx = Box(a=1, b={"c": 2}) |
| 119 | bx.set_comment("a", "eol text") |
| 120 | bx.set_comment("a", "line one\nline two", position="before") |
| 121 | bx.b.set_comment("c", "nested") |
| 122 | assert bx.get_comment("a") == {"eol": "eol text", "before": "line one\nline two"} |
| 123 | assert bx.to_yaml() == "# line one\n# line two\na: 1 # eol text\nb:\n c: 2 # nested\n" |
| 124 | bx.del_comment("a") |
| 125 | assert bx.get_comment("a") is None |
| 126 | bx.set_comment("b", "") # empty text removes that position |
| 127 | assert bx.get_comment("b") is None |
| 128 | |
| 129 | def test_remove_one_position(self): |
| 130 | bx = Box(a=1) |
| 131 | bx.set_comment("a", "x") |
| 132 | bx.set_comment("a", "y", position="before") |
| 133 | bx.set_comment("a", None) |
| 134 | assert bx.get_comment("a") == {"before": "y"} |
| 135 | |
| 136 | def test_hash_prefix_is_optional(self): |
| 137 | bx = Box(a=1) |
| 138 | bx.set_comment("a", "# already commented") |
| 139 | assert bx.get_comment("a") == {"eol": "already commented"} |
| 140 | |
| 141 | def test_document_comments(self): |
| 142 | bx = Box(a=1) |
| 143 | bx.set_comment(None, "header\n\nmore") |
| 144 | bx.set_comment(None, "footer", position="end") |
| 145 | assert bx.to_yaml() == "# header\n\n# more\na: 1\n# footer\n" |
| 146 | bx.del_comment() |
| 147 | assert bx.to_yaml() == "a: 1\n" |
| 148 | |
| 149 | @pytest.mark.parametrize( |
| 150 | "args, error", |
| 151 | [ |
| 152 | (("a", "two\nlines"), "single line"), |
| 153 | (("a", "x", "start"), "position must be one of 'before', 'eol'"), |
| 154 | ((None, "x", "eol"), "position must be one of 'start', 'end'"), |
| 155 | (("a", 5), "must be a string"), |
| 156 | ], |
| 157 | ) |
| 158 | def test_invalid(self, args, error): |
| 159 | with pytest.raises(BoxValueError, match=error): |
| 160 | Box(a=1).set_comment(*args) |
| 161 | |
| 162 | def test_missing_keys(self): |
| 163 | with pytest.raises(BoxKeyError, match="existing keys"): |
| 164 | Box().set_comment("missing", "x") |
| 165 | with pytest.raises(BoxKeyError): |
| 166 | Box().get_comment("missing") |
| 167 | with pytest.raises(BoxKeyError, match="existing items"): |
| 168 | BoxList([1]).set_comment(3, "x") |
| 169 | with pytest.raises(BoxKeyError): |
| 170 | Box(a={"b": 1}).set_comment("a.b", "x") # dotted paths need box_dots |
| 171 | |
| 172 | def test_box_dots_paths(self): |
| 173 | bx = Box({"a": {"b": 1}, "l": [{"x": 1}, 2]}, box_dots=True) |
nothing calls this directly
no outgoing calls
no test coverage detected