def test_surrogate_high_without_low__garbage(): p = Properties() with pytest.raises(ParseError) as excinfo: p.load(StringIO(b"surrogate=Muuusic \\ud834 foobar\n")) # Caused by garbage after the first unicode escape assert "High surrogate unicode escape sequence not followed by" in str( excinfo.value)
def test_surrogate_high_without_low__eof(): p = Properties() with pytest.raises(ParseError) as excinfo: p.load(StringIO(b"surrogate=Muuusic \\ud834\n")) # Caused by short read (read 1 byte, wanted 6) after the first unicode escape assert "High surrogate unicode escape sequence not followed by" in str( excinfo.value)
def test_line_continuation_allowed(): p = Properties() p.load( StringIO(r""" multi\ line\ key = value """)) assert p.properties == {"multiline key": "value"}
def test_basic_whitespace(): p = Properties() p.load('''fruits apple, banana, pear, \\ cantaloupe, watermelon, \\ kiwi, mango''') assert p.properties == { 'fruits': 'apple, banana, pear, cantaloupe, ' 'watermelon, kiwi, mango' }
def test_setmeta_unicode(): p = Properties() p["a key"] = "the value", {u"metakey": u"ünicode metävalue!"} out = StringIO() p.store(out, encoding="utf-8", strip_meta=False, timestamp=False) out.seek(0) text = "#: metakey=ünicode metävalue\\!\na\\ key=the value\n" assert out.read() == text
def test_line_continuation_forbidden(): # In metadata comments, line continuation is disabled. p = Properties() p.load( StringIO(r""" #: metakey meta\ value continuation multi\ line\ key = value """)) assert p.properties == {"multiline key": "value", "value": "continuation"}
def test_simple_escape_parsing(): p = Properties() p.load( StringIO("key value with a\\ttab\n" "foo ba\\r\n" "new li\\ne\n" "form \\feed seen!")) assert p.properties == { "key": "value with a\ttab", "foo": "ba\r", "new": "li\ne", "form": "\feed seen!" }
def test_repeated_with_meta(): p = Properties() p.load(b""" key = value1 #: metakey = metaval1 #: metakey2 = metaval22 key = value2 # Expected: Metadata should ONLY contain the following # 'metakey' key. #: metakey = metaval2 key = value3 """) assert p.properties == {"key": "value3"} assert p["key"] == ("value3", {"metakey": "metaval2"})
def test_surrogate_roundtrip(out_encoding): p = Properties() p["surrogate"] = u"Muuusic \U0001D160" out = StringIO() p.store(out, encoding=out_encoding, timestamp=None) out.seek(0) dumped = out.read() assert dumped == b"surrogate=Muuusic \\ud834\\udd60\n" p2 = Properties() p2.load(dumped, out_encoding) assert p2["surrogate"] == (u"Muuusic \U0001D160", {})
def test_surrogate_roundtrip_utf8(): p = Properties() p["surrogate"] = u"Muuusic \U0001D160" out = StringIO() p.store(out, encoding="utf-8", timestamp=None) out.seek(0) dumped = out.read() assert dumped == b"surrogate=Muuusic \xF0\x9D\x85\xA0\n" p2 = Properties() p2.load(dumped, "utf-8") assert p2["surrogate"] == (u"Muuusic \U0001D160", {})
def test_euro(out_encoding): p = Properties() # The euro symbol is not present in ASCII/ISO-8859-1, # so it should be encoded as "\u20ac". p["test"] = u"Euro: €" out = StringIO() p.store(out, encoding=out_encoding, timestamp=None) out.seek(0) assert out.read() == b"test=Euro\\: \\u20ac\n" # Read it back again: out.seek(0) p2 = Properties() p2.load(out) assert p2.properties == {u"test": u"Euro: €"}
def test_simple_utf8_load_write_file(datadir, tmpdir): p = Properties() with datadir["simple.utf8.prop"].open("rb") as fh: p.load(fh, "utf-8") with tmpdir.join("dump.prop").open("wb+") as out: p.store(out, encoding="utf-8", timestamp=False) out.seek(0) assert out.read() == ( u"tästkey=This is the value\n" u"anotherkey=Not mäny välues in this file\n").encode('utf-8')
def test_basic_colon_and_leading_whitespace(): p = Properties() p.load(" Truth:Beauty") assert p.properties == {"Truth": "Beauty"}
def test_novalue(): p = Properties() p.load(br"no\ value!") assert p.properties == {"no value!": ""}
def test_nokey(): p = Properties() p.load(b"= no key!") assert p.properties == {"": "no key!"} assert p[""] == ("no key!", {})
def test_nokey_repeated(): p = Properties() p.load(b"= no key!\n: still no key!") assert p.properties == {"": "still no key!"} assert p[""] == ("still no key!", {})
def test_stray_line_continuation(): p = Properties() p.load(StringIO("key value\\")) assert p.properties == {"key": "value"}
def test_basic_key_only(): p = Properties() p.load('cheese\n') assert p.properties == {'cheese': ''}
def test_basic_equals_sign(): p = Properties() p.load("Truth = Beauty\n") assert p.properties == {"Truth": "Beauty"}
def test_basic_key_trailing_space(): p = Properties() p.load("Truth :Beauty") assert p.properties == {"Truth": "Beauty"}