Skip to content

Commit b244cd7

Browse files
committed
Add tests for escaping in Turtle decoder
1 parent 5e983be commit b244cd7

File tree

1 file changed

+88
-2
lines changed

1 file changed

+88
-2
lines changed

test/unit/serializations/turtle_decoder_test.exs

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,25 @@ defmodule RDF.Turtle.DecoderTest do
241241
end
242242

243243
describe "quoted literals" do
244-
test "an untyped string literal" do
244+
test "an untyped string literal with double quotes" do
245245
assert Turtle.Decoder.decode!("""
246246
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/realname> "Peter Parker" .
247247
""") == Graph.new({EX.spiderman(), P.realname(), RDF.literal("Peter Parker")})
248248
end
249249

250-
test "an untyped long quoted string literal" do
250+
test "an untyped string literal with single quotes" do
251+
assert Turtle.Decoder.decode!("""
252+
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/realname> 'Peter Parker' .
253+
""") == Graph.new({EX.spiderman(), P.realname(), RDF.literal("Peter Parker")})
254+
end
255+
256+
test "an untyped long double quoted string literal" do
257+
assert Turtle.Decoder.decode!("""
258+
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/realname> \"""Peter Parker\""" .
259+
""") == Graph.new({EX.spiderman(), P.realname(), RDF.literal("Peter Parker")})
260+
end
261+
262+
test "an untyped long single quoted string literal" do
251263
assert Turtle.Decoder.decode!("""
252264
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/realname> '''Peter Parker''' .
253265
""") == Graph.new({EX.spiderman(), P.realname(), RDF.literal("Peter Parker")})
@@ -284,6 +296,80 @@ defmodule RDF.Turtle.DecoderTest do
284296
end
285297
end
286298

299+
describe "escaping" do
300+
test "STRING_LITERAL_QUOTE" do
301+
assert Turtle.Decoder.decode!("""
302+
<http://example.org/#S> <http://example.org/#p> "String with \\t tab and \\n newline" .
303+
""") == Graph.new({EX.S, EX.p(), RDF.literal("String with \t tab and \n newline")})
304+
305+
assert Turtle.Decoder.decode!("""
306+
<http://example.org/#S> <http://example.org/#p> "String with Unicode: \\u00A9" .
307+
""") == Graph.new({EX.S, EX.p(), RDF.literal("String with Unicode: ©")})
308+
309+
assert Turtle.Decoder.decode!("""
310+
<http://example.org/#S> <http://example.org/#p> "String with \\"quotes\\"" .
311+
""") == Graph.new({EX.S, EX.p(), RDF.literal("String with \"quotes\"")})
312+
313+
assert Turtle.Decoder.decode!("""
314+
<http://example.org/#S> <http://example.org/#p> "String with \\b backspace and \\f form feed" .
315+
""") ==
316+
Graph.new({EX.S, EX.p(), RDF.literal("String with \b backspace and \f form feed")})
317+
318+
assert Turtle.Decoder.decode!("""
319+
<http://example.org/#S> <http://example.org/#p> "String with \\\\\\\\ multiple backslashes" .
320+
""") ==
321+
Graph.new({EX.S, EX.p(), RDF.literal("String with \\\\ multiple backslashes")})
322+
end
323+
324+
test "STRING_LITERAL_SINGLE_QUOTE" do
325+
assert Turtle.Decoder.decode!("""
326+
<http://example.org/#S> <http://example.org/#p> 'String with \\t tab and \\n newline' .
327+
""") == Graph.new({EX.S, EX.p(), RDF.literal("String with \t tab and \n newline")})
328+
329+
assert Turtle.Decoder.decode!("""
330+
<http://example.org/#S> <http://example.org/#p> 'String with Unicode: \\u00A9' .
331+
""") == Graph.new({EX.S, EX.p(), RDF.literal("String with Unicode: ©")})
332+
333+
assert Turtle.Decoder.decode!("""
334+
<http://example.org/#S> <http://example.org/#p> 'String with \\'quotes\\'' .
335+
""") == Graph.new({EX.S, EX.p(), RDF.literal("String with 'quotes'")})
336+
end
337+
338+
test "STRING_LITERAL_LONG_QUOTE" do
339+
assert Turtle.Decoder.decode!("""
340+
<http://example.org/#S> <http://example.org/#p> \"""Long string with "embedded quotes" and ""escaped quotes"\\\"""" .
341+
""") ==
342+
Graph.new(
343+
{EX.S, EX.p(),
344+
RDF.literal("Long string with \"embedded quotes\" and \"\"escaped quotes\"\"")}
345+
)
346+
347+
assert Turtle.Decoder.decode!("""
348+
<http://example.org/#S> <http://example.org/#p> \"\"\"Long string ending with quote\\\"\"\"\" .
349+
""") == Graph.new({EX.S, EX.p(), RDF.literal("Long string ending with quote\"")})
350+
end
351+
352+
test "STRING_LITERAL_LONG_SINGLE_QUOTE" do
353+
assert Turtle.Decoder.decode!("""
354+
<http://example.org/#S> <http://example.org/#p> '''Long string
355+
with multiple
356+
lines''' .
357+
""") == Graph.new({EX.S, EX.p(), RDF.literal("Long string\nwith multiple\nlines")})
358+
359+
assert Turtle.Decoder.decode!("""
360+
<http://example.org/#S> <http://example.org/#p> '''Long string with 'embedded quotes' and ''escaped quotes'\\'''' .
361+
""") ==
362+
Graph.new(
363+
{EX.S, EX.p(),
364+
RDF.literal("Long string with 'embedded quotes' and ''escaped quotes''")}
365+
)
366+
367+
assert Turtle.Decoder.decode!("""
368+
<http://example.org/#S> <http://example.org/#p> '''Long string ending with quote\\'''' .
369+
""") == Graph.new({EX.S, EX.p(), RDF.literal("Long string ending with quote'")})
370+
end
371+
end
372+
287373
describe "shorthand literals" do
288374
test "boolean" do
289375
assert Turtle.Decoder.decode!("""

0 commit comments

Comments
 (0)