diff --git a/lib/cfpropertylist/rbCFTypes.rb b/lib/cfpropertylist/rbCFTypes.rb index 8563721..80e1782 100644 --- a/lib/cfpropertylist/rbCFTypes.rb +++ b/lib/cfpropertylist/rbCFTypes.rb @@ -60,7 +60,7 @@ def to_binary(bplist) end def to_plain(plist) - if @value =~ /^\w+$/ + if @value =~ /\A\w+\z/ @value else quoted @@ -74,7 +74,7 @@ def quoted when '"' '\\"' when '\\' - '\\' + '\\\\' when "\a" "\\a" when "\b" @@ -82,7 +82,7 @@ def quoted when "\f" "\\f" when "\n" - "\n" + "\\n" when "\v" "\\v" when "\r" diff --git a/test/test_plain.rb b/test/test_plain.rb index c53bc1e..1f299e2 100644 --- a/test/test_plain.rb +++ b/test/test_plain.rb @@ -114,4 +114,28 @@ def test_serialize plist.value = CFPropertyList.guess(CFPropertyList::Blob.new("\x00\n")) assert_equal raw_plain("binary"), plist.to_str(CFPropertyList::List::FORMAT_PLAIN) end + + def test_string_with_backslash_roundtrip + original = "foo\\bar" + plist = CFPropertyList::List.new + plist.value = CFPropertyList.guess(original) + plain = plist.to_str(CFPropertyList::List::FORMAT_PLAIN) + + reparsed = CFPropertyList::List.new(:data => plain, :format => CFPropertyList::List::FORMAT_PLAIN) + result = CFPropertyList.native_types(reparsed.value) + + assert_equal original, result + end + + def test_string_with_newline_roundtrip + original = "foo\nbar" + plist = CFPropertyList::List.new + plist.value = CFPropertyList.guess(original) + plain = plist.to_str(CFPropertyList::List::FORMAT_PLAIN) + + reparsed = CFPropertyList::List.new(:data => plain, :format => CFPropertyList::List::FORMAT_PLAIN) + result = CFPropertyList.native_types(reparsed.value) + + assert_equal original, result + end end