Skip to content

Commit

Permalink
Improve string escapes for RDF::Literal#to_sxp.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Jan 16, 2022
1 parent d9682cf commit 2df5a35
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/sxp/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def to_sxp(prefixes: nil, base_uri: nil, **options)

class RDF::URI
##
# Returns the SXP representation of this a URI. Uses Lexical representation, if set, otherwise, any PName match, otherwise, the relativized version of the URI if a base_uri is given, otherwise just the URI.
# Returns the SXP representation of this URI. Uses Lexical representation, if set, otherwise, any PName match, otherwise, the relativized version of the URI if a base_uri is given, otherwise just the URI.
#
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
Expand Down Expand Up @@ -284,7 +284,7 @@ def to_sxp(**options)
# Retain stated lexical form if possible
valid? ? to_s : object.to_sxp(**options)
else
text = value.dump
text = value.to_sxp
text << "@#{language}" if self.has_language?
text << "^^#{datatype.to_sxp(**options)}" if self.has_datatype?
text
Expand Down
29 changes: 29 additions & 0 deletions spec/extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@
}.each_pair do |l, sxp|
specify {expect(l.to_sxp).to eq sxp}
end

describe "string escapes" do
{
"\b" => %{"\\b"},
"\f" => %{"\\f"},
"\n" => %{"\\n"},
"\r" => %{"\\r"},
"\t" => %{"\\t"},
"\u0080" => %{"\u0080"},
"\u07FF" => %("\u07FF"),
"\u0800" => %("\u0800"),
"\u0FFF" => %("\u0FFF"),
"\u1000" => %("\u1000"),
"\uD000" => %("\uD000"),
"\uD7FF" => %("\uD7FF"),
"\uE000" => %("\uE000"),
"\uFFFD" => %("\uFFFD"),
"\u{10000}" => %("\u{010000}"),
"\u{3FFFD}" => %("\u{03FFFD}"),
"\u{40000}" => %("\u{040000}"),
"\u{FFFFD}" => %("\u{0FFFFD}"),
"\u{100000}" => %("\u{100000}"),
"\u{10FFFD}" => %("\u{10FFFD}"),
}.each do |value, result|
it "writes #{value} as #{result.inspect}" do
expect(RDF::Literal(value).to_sxp).to eq result
end
end
end
end

describe "RDF::Literal#to_sxp with prefix" do
Expand Down

0 comments on commit 2df5a35

Please sign in to comment.