| 35 | |
| 36 | |
| 37 | class TestReadAndWrite: |
| 38 | def test_round_trip_is_unchanged(self): |
| 39 | assert Box.from_yaml(DOCUMENT).to_yaml() == DOCUMENT |
| 40 | |
| 41 | def test_comments_are_read(self): |
| 42 | bx = Box.from_yaml(DOCUMENT) |
| 43 | assert bx.get_comment(None) == {"start": "Service settings\nsecond line\n", "end": "end of the document"} |
| 44 | assert bx.get_comment("name") == {"eol": "the name"} |
| 45 | assert bx.get_comment("server") == {"before": "Where to listen"} |
| 46 | assert bx.server.get_comment("host") == {"eol": "bind address"} |
| 47 | assert bx.server.get_comment("port") == {"before": ""} # the blank line |
| 48 | assert bx["items"].get_comment(0) == {"before": "first", "eol": "eol a"} |
| 49 | assert bx["items"].get_comment(1) is None |
| 50 | assert bx.get_comment("last") is None |
| 51 | |
| 52 | def test_data_is_unaffected(self): |
| 53 | bx = Box.from_yaml(DOCUMENT) |
| 54 | plain = {"name": "box", "server": {"host": "localhost", "port": 8080}, "items": ["a", "b"], "last": 1} |
| 55 | assert bx.to_dict() == plain |
| 56 | assert json.loads(bx.to_json()) == plain |
| 57 | assert bx == plain |
| 58 | assert Box.from_yaml(DOCUMENT) == Box(plain) |
| 59 | |
| 60 | def test_edits_keep_comments_on_their_keys(self): |
| 61 | bx = Box.from_yaml(DOCUMENT) |
| 62 | del bx.name |
| 63 | bx.server.port = 9090 |
| 64 | bx.server.timeout = 30 |
| 65 | bx.new = "x" |
| 66 | out = bx.to_yaml() |
| 67 | assert out.startswith("# Service settings\n# second line\n\n# Where to listen\nserver:\n") |
| 68 | assert " host: localhost # bind address\n\n port: 9090\n timeout: 30\n" in out |
| 69 | assert "the name" not in out |
| 70 | assert out.endswith("last: 1\nnew: x\n# end of the document\n") |
| 71 | |
| 72 | def test_comment_between_key_and_nested_value(self): |
| 73 | bx = Box.from_yaml("server:\n # inside\n host: x\n") |
| 74 | assert bx.server.get_comment("host") == {"before": "inside"} |
| 75 | assert bx.to_yaml() == "server:\n # inside\n host: x\n" |
| 76 | |
| 77 | def test_nested_list_of_maps(self): |
| 78 | src = "items:\n- a: 1 # one\n b: 2\n# second item\n- a: 3\n" |
| 79 | bx = Box.from_yaml(src) |
| 80 | assert bx["items"][0].get_comment("a") == {"eol": "one"} |
| 81 | assert bx["items"].get_comment(1) == {"before": "second item"} |
| 82 | assert bx.to_yaml() == src |
| 83 | |
| 84 | def test_document_without_comments_has_none(self): |
| 85 | bx = Box.from_yaml("a: 1\nb: [1, 2]\n") |
| 86 | assert bx.get_comment(None) is None |
| 87 | assert bx._box_config.get("__comments") is None |
| 88 | |
| 89 | def test_comment_only_document(self): |
| 90 | bx = Box.from_yaml("# nothing here\n") |
| 91 | assert bx == {} |
| 92 | |
| 93 | def test_numeric_keys(self): |
| 94 | bx = Box.from_yaml("1: one # first\n2.5: x\n") |
nothing calls this directly
no outgoing calls
no test coverage detected