rdf_context 0.4.8 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (248) hide show
  1. data/History.txt +27 -0
  2. data/README.rdoc +22 -9
  3. data/Rakefile +11 -6
  4. data/VERSION +1 -1
  5. data/bin/rdf_context +12 -4
  6. data/lib/rdf_context.rb +3 -1
  7. data/lib/rdf_context/aggregate_graph.rb +86 -0
  8. data/lib/rdf_context/array_hacks.rb +53 -0
  9. data/lib/rdf_context/conjunctive_graph.rb +1 -1
  10. data/lib/rdf_context/exceptions.rb +9 -7
  11. data/lib/rdf_context/graph.rb +174 -15
  12. data/lib/rdf_context/literal.rb +34 -2
  13. data/lib/rdf_context/n3_grammar.treetop +213 -129
  14. data/lib/rdf_context/n3parser.rb +281 -57
  15. data/lib/rdf_context/namespace.rb +23 -12
  16. data/lib/rdf_context/parser.rb +4 -3
  17. data/lib/rdf_context/quoted_graph.rb +38 -0
  18. data/lib/rdf_context/rdfaparser.rb +2 -1
  19. data/lib/rdf_context/rdfxmlparser.rb +4 -3
  20. data/lib/rdf_context/store/abstract_sql_store.rb +4 -4
  21. data/lib/rdf_context/store/abstract_store.rb +5 -1
  22. data/lib/rdf_context/store/sqlite3_store.rb +10 -8
  23. data/lib/rdf_context/string_hacks.rb +44 -21
  24. data/lib/rdf_context/term_utils.rb +73 -4
  25. data/lib/rdf_context/triple.rb +32 -47
  26. data/lib/rdf_context/uriref.rb +33 -26
  27. data/spec/aggregate_graph_spec.rb +59 -0
  28. data/spec/conjunctive_graph_spec.rb +1 -1
  29. data/spec/cwm_spec.rb +32 -0
  30. data/spec/graph_spec.rb +114 -3
  31. data/spec/literal_spec.rb +107 -5
  32. data/spec/matchers.rb +104 -51
  33. data/spec/n3parser_spec.rb +798 -99
  34. data/spec/namespaces_spec.rb +26 -0
  35. data/spec/quoted_graph_spec.rb +0 -0
  36. data/spec/rdf_helper.rb +197 -0
  37. data/spec/rdfa_helper.rb +15 -11
  38. data/spec/rdfa_parser_spec.rb +6 -8
  39. data/spec/rdfxml_spec.rb +21 -28
  40. data/spec/spec_helper.rb +20 -0
  41. data/spec/sqlite3_store_spec.rb +6 -5
  42. data/spec/store_helper.rb +9 -1
  43. data/spec/string_hacks_spec.rb +14 -0
  44. data/spec/{rdfxml_helper.rb → swap_helper.rb} +8 -9
  45. data/spec/swap_spec.rb +77 -0
  46. data/spec/swap_test/animal.rdf +17 -0
  47. data/spec/swap_test/anon-prop.n3 +14 -0
  48. data/spec/swap_test/anonymous_loop.n3 +2 -0
  49. data/spec/swap_test/contexts.n3 +16 -0
  50. data/spec/swap_test/daml-pref.n3 +10 -0
  51. data/spec/swap_test/i18n/hiragana.n3 +22 -0
  52. data/spec/swap_test/i18n/n3string.n3 +4 -0
  53. data/spec/swap_test/list/itemType.rdf +12 -0
  54. data/spec/swap_test/lists-simple.n3 +40 -0
  55. data/spec/swap_test/lists.n3 +35 -0
  56. data/spec/swap_test/n3/n3parser.tests_n3_10001.nt +3 -0
  57. data/spec/swap_test/n3/n3parser.tests_n3_10002.nt +7 -0
  58. data/spec/swap_test/n3/n3parser.tests_n3_10003.nt +3 -0
  59. data/spec/swap_test/n3/n3parser.tests_n3_10004.nt +119 -0
  60. data/spec/swap_test/n3/n3parser.tests_n3_10005.nt +3 -0
  61. data/spec/swap_test/n3/n3parser.tests_n3_10006.nt +225 -0
  62. data/spec/swap_test/n3/n3parser.tests_n3_10007.nt +79 -0
  63. data/spec/swap_test/n3/n3parser.tests_n3_10008.nt +5 -0
  64. data/spec/swap_test/n3/n3parser.tests_n3_10009.nt +13 -0
  65. data/spec/swap_test/n3/n3parser.tests_n3_10010.nt +21 -0
  66. data/spec/swap_test/n3/n3parser.tests_n3_10011.nt +9 -0
  67. data/spec/swap_test/n3/n3parser.tests_n3_10012.nt +53 -0
  68. data/spec/swap_test/n3/n3parser.tests_n3_10013.nt +19 -0
  69. data/spec/swap_test/n3/n3parser.tests_n3_10014.nt +103 -0
  70. data/spec/swap_test/n3/n3parser.tests_n3_10015.nt +103 -0
  71. data/spec/swap_test/n3/n3parser.tests_n3_10016.nt +3 -0
  72. data/spec/swap_test/n3/n3parser.tests_n3_10017.nt +151 -0
  73. data/spec/swap_test/n3/n3parser.tests_n3_10018.nt +9 -0
  74. data/spec/swap_test/n3/n3parser.tests_n3_10019.nt +3 -0
  75. data/spec/swap_test/n3/n3parser.tests_n3_10020.nt +13 -0
  76. data/spec/swap_test/n3parser.tests +160 -0
  77. data/spec/swap_test/nodeID/classes.n3 +9 -0
  78. data/spec/swap_test/nodeID/classes.ref.rdf +15 -0
  79. data/spec/swap_test/nodeID/ex1.rdf +15 -0
  80. data/spec/swap_test/norm/fix.rdf +33 -0
  81. data/spec/swap_test/owl-ex.rdf +150 -0
  82. data/spec/swap_test/ref/animal.n3 +11 -0
  83. data/spec/swap_test/ref/anon-prop-1.n3 +8 -0
  84. data/spec/swap_test/ref/anonymous_loop.ref +7 -0
  85. data/spec/swap_test/ref/bnode.n3 +6 -0
  86. data/spec/swap_test/ref/bnode.rdf +16 -0
  87. data/spec/swap_test/ref/colon-in-uri.n3 +15 -0
  88. data/spec/swap_test/ref/daml-ex.n3 +103 -0
  89. data/spec/swap_test/ref/daml-ont.n3 +289 -0
  90. data/spec/swap_test/ref/djb1a-out.n3 +4 -0
  91. data/spec/swap_test/ref/dot-dash.n3 +8 -0
  92. data/spec/swap_test/ref/in-xml-t.n3 +4 -0
  93. data/spec/swap_test/ref/itemType.n3 +9 -0
  94. data/spec/swap_test/ref/keywords1.n3 +11 -0
  95. data/spec/swap_test/ref/keywords2.n3 +11 -0
  96. data/spec/swap_test/ref/lists-simple-1.rdf +108 -0
  97. data/spec/swap_test/ref/lists.n3 +43 -0
  98. data/spec/swap_test/ref/lstring-out.n3 +20 -0
  99. data/spec/swap_test/ref/n3string.n3 +13 -0
  100. data/spec/swap_test/ref/no-last-nl.n3 +3 -0
  101. data/spec/swap_test/ref/numbers.n3 +18 -0
  102. data/spec/swap_test/ref/path1.n3 +8 -0
  103. data/spec/swap_test/ref/path2.n3 +39 -0
  104. data/spec/swap_test/ref/prefix1.rdf +31 -0
  105. data/spec/swap_test/ref/prefix3.rdf +31 -0
  106. data/spec/swap_test/ref/rdf-redefine.rdf +11 -0
  107. data/spec/swap_test/ref/reluri-1.rdf +18 -0
  108. data/spec/swap_test/ref/strquot.n3 +23 -0
  109. data/spec/swap_test/ref/strquot_a.n3 +23 -0
  110. data/spec/swap_test/ref/xml-base3.n3 +5 -0
  111. data/spec/swap_test/ref/xml-redefine.rdf +20 -0
  112. data/spec/swap_test/ref/xml-redefine2.rdf +23 -0
  113. data/spec/swap_test/ref/xml-syntax-basic-serialization.rdf +10 -0
  114. data/spec/swap_test/ref/xmllit.nt +3 -0
  115. data/spec/swap_test/regression.n3 +231 -0
  116. data/spec/swap_test/reluri-1.n3 +10 -0
  117. data/spec/swap_test/strquot.n3 +23 -0
  118. data/spec/swap_test/syntax/colon-in-uri.rdf +27 -0
  119. data/spec/swap_test/syntax/djb1a.n3 +3 -0
  120. data/spec/swap_test/syntax/dot-dash.n3 +15 -0
  121. data/spec/swap_test/syntax/equals1.n3 +1 -0
  122. data/spec/swap_test/syntax/equals2.n3 +1 -0
  123. data/spec/swap_test/syntax/keywords1.n3 +17 -0
  124. data/spec/swap_test/syntax/keywords2.n3 +18 -0
  125. data/spec/swap_test/syntax/lstring.n3 +26 -0
  126. data/spec/swap_test/syntax/neg-formula-predicate.n3 +1 -0
  127. data/spec/swap_test/syntax/neg-keywords3.n3 +1 -0
  128. data/spec/swap_test/syntax/neg-literal-predicate.n3 +1 -0
  129. data/spec/swap_test/syntax/neg-single-quote.n3 +1 -0
  130. data/spec/swap_test/syntax/neg-thisadoc.n3 +1 -0
  131. data/spec/swap_test/syntax/no-last-nl.n3 +2 -0
  132. data/spec/swap_test/syntax/numbers.n3 +26 -0
  133. data/spec/swap_test/syntax/path1.n3 +23 -0
  134. data/spec/swap_test/syntax/path2.n3 +31 -0
  135. data/spec/swap_test/syntax/qvars1.n3 +19 -0
  136. data/spec/swap_test/syntax/qvars2.n3 +19 -0
  137. data/spec/swap_test/syntax/this-quantifiers.n3 +164 -0
  138. data/spec/swap_test/syntax/this-rules.n3 +43 -0
  139. data/spec/swap_test/syntax/too-nested.n3 +25 -0
  140. data/spec/swap_test/syntax/trailing-semicolon.n3 +12 -0
  141. data/spec/swap_test/syntax/zero-objects.n3 +1 -0
  142. data/spec/swap_test/syntax/zero-predicates.n3 +3 -0
  143. data/spec/swap_test/tests-work.txt +25 -0
  144. data/spec/swap_test/xml-syntax/basic-serialization.n3 +8 -0
  145. data/spec/swap_test/xml-syntax/in-xml.xml +13 -0
  146. data/spec/swap_test/xml-syntax/non-ascii-pred.rdf +14 -0
  147. data/spec/swap_test/xml-syntax/rdf_prefix.n3 +2 -0
  148. data/spec/swap_test/xml-syntax/xml_prefix.n3 +7 -0
  149. data/spec/swap_test/xml-syntax/xml_prefix2.n3 +9 -0
  150. data/spec/swap_test/xml-syntax/xmlbase3.rdf +10 -0
  151. data/spec/swap_test/xml-syntax/xmllit.rdf +33 -0
  152. data/spec/triple_spec.rb +90 -46
  153. data/spec/turtle/README.txt +20 -0
  154. data/spec/turtle/bad-00.ttl +2 -0
  155. data/spec/turtle/bad-01.ttl +3 -0
  156. data/spec/turtle/bad-02.ttl +3 -0
  157. data/spec/turtle/bad-03.ttl +3 -0
  158. data/spec/turtle/bad-04.ttl +3 -0
  159. data/spec/turtle/bad-05.ttl +4 -0
  160. data/spec/turtle/bad-06.ttl +3 -0
  161. data/spec/turtle/bad-07.ttl +4 -0
  162. data/spec/turtle/bad-08.ttl +2 -0
  163. data/spec/turtle/bad-09.ttl +3 -0
  164. data/spec/turtle/bad-10.ttl +3 -0
  165. data/spec/turtle/bad-11.ttl +3 -0
  166. data/spec/turtle/bad-12.ttl +3 -0
  167. data/spec/turtle/bad-13.ttl +3 -0
  168. data/spec/turtle/bad-14.ttl +6 -0
  169. data/spec/turtle/manifest-bad.ttl +88 -0
  170. data/spec/turtle/manifest.ttl +215 -0
  171. data/spec/turtle/rdf-schema.out +126 -0
  172. data/spec/turtle/rdf-schema.ttl +156 -0
  173. data/spec/turtle/rdfq-results.out +36 -0
  174. data/spec/turtle/rdfq-results.ttl +39 -0
  175. data/spec/turtle/rdfs-namespace.out +131 -0
  176. data/spec/turtle/rdfs-namespace.ttl +160 -0
  177. data/spec/turtle/test-00.out +1 -0
  178. data/spec/turtle/test-00.ttl +2 -0
  179. data/spec/turtle/test-01.out +3 -0
  180. data/spec/turtle/test-01.ttl +7 -0
  181. data/spec/turtle/test-02.out +3 -0
  182. data/spec/turtle/test-02.ttl +5 -0
  183. data/spec/turtle/test-03.out +3 -0
  184. data/spec/turtle/test-03.ttl +5 -0
  185. data/spec/turtle/test-04.out +2 -0
  186. data/spec/turtle/test-04.ttl +4 -0
  187. data/spec/turtle/test-05.out +4 -0
  188. data/spec/turtle/test-05.ttl +4 -0
  189. data/spec/turtle/test-06.out +1 -0
  190. data/spec/turtle/test-06.ttl +3 -0
  191. data/spec/turtle/test-07.out +5 -0
  192. data/spec/turtle/test-07.ttl +3 -0
  193. data/spec/turtle/test-08.out +1 -0
  194. data/spec/turtle/test-08.ttl +3 -0
  195. data/spec/turtle/test-09.out +4 -0
  196. data/spec/turtle/test-09.ttl +10 -0
  197. data/spec/turtle/test-10.out +5 -0
  198. data/spec/turtle/test-10.ttl +5 -0
  199. data/spec/turtle/test-11.out +4 -0
  200. data/spec/turtle/test-11.ttl +10 -0
  201. data/spec/turtle/test-12.out +4 -0
  202. data/spec/turtle/test-12.ttl +9 -0
  203. data/spec/turtle/test-13.out +2 -0
  204. data/spec/turtle/test-13.ttl +7 -0
  205. data/spec/turtle/test-14.out +10000 -0
  206. data/spec/turtle/test-14.ttl +10002 -0
  207. data/spec/turtle/test-15.out +10000 -0
  208. data/spec/turtle/test-15.ttl +3 -0
  209. data/spec/turtle/test-16.out +10000 -0
  210. data/spec/turtle/test-16.ttl +10002 -0
  211. data/spec/turtle/test-17.out +1 -0
  212. data/spec/turtle/test-17.ttl +6 -0
  213. data/spec/turtle/test-18.out +2 -0
  214. data/spec/turtle/test-18.ttl +9 -0
  215. data/spec/turtle/test-19.out +1 -0
  216. data/spec/turtle/test-19.ttl +4 -0
  217. data/spec/turtle/test-20.out +2 -0
  218. data/spec/turtle/test-20.ttl +6 -0
  219. data/spec/turtle/test-21.out +3 -0
  220. data/spec/turtle/test-21.ttl +4 -0
  221. data/spec/turtle/test-22.out +3 -0
  222. data/spec/turtle/test-22.ttl +4 -0
  223. data/spec/turtle/test-23.out +1 -0
  224. data/spec/turtle/test-23.ttl +3 -0
  225. data/spec/turtle/test-24.out +2 -0
  226. data/spec/turtle/test-24.ttl +3 -0
  227. data/spec/turtle/test-25.out +7 -0
  228. data/spec/turtle/test-25.ttl +14 -0
  229. data/spec/turtle/test-26.out +1 -0
  230. data/spec/turtle/test-26.ttl +4 -0
  231. data/spec/turtle/test-27.out +1 -0
  232. data/spec/turtle/test-27.ttl +5 -0
  233. data/spec/turtle/test-28-out.ttl +6 -0
  234. data/spec/turtle/test-28.out +22 -0
  235. data/spec/turtle/test-28.ttl +22 -0
  236. data/spec/turtle/test-29.out +1 -0
  237. data/spec/turtle/test-29.ttl +1 -0
  238. data/spec/turtle/test-30.out +5 -0
  239. data/spec/turtle/test-30.ttl +12 -0
  240. data/spec/turtle_spec.rb +64 -0
  241. data/spec/uriref_spec.rb +74 -34
  242. data/test/n3_tests/rdflib/n3-writer-teset-26.n3 +31 -0
  243. data/test/n3_tests/rdflib/n3-writer-teset-26.nt +14 -0
  244. data/test/rdf_tests/xml-literal-mixed.nt +1 -2
  245. metadata +278 -39
  246. data/lib/rdf_context/n3_grammar.rb +0 -2171
  247. data/test/perf_test/test.rb +0 -11
  248. data/test/perf_test/tommorris.rdf +0 -2267
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
3
 
3
4
  describe "Literals: " do
@@ -15,14 +16,23 @@ describe "Literals: " do
15
16
 
16
17
  describe "should handle specific cases" do
17
18
  {
18
- '"Gregg"' => Literal.untyped("Gregg"),
19
+ '"Gregg"' => Literal.untyped("Gregg"),
19
20
  '"\u677E\u672C \u540E\u5B50"' => Literal.untyped("松本 后子"),
20
- '"D\u00FCrst"' => Literal.untyped("Dürst")
21
+ '"D\u00FCrst"' => Literal.untyped("Dürst"),
21
22
  }.each_pair do |encoded, literal|
22
23
  it "should encode '#{literal.contents}'" do
23
24
  literal.to_n3.should == encoded
24
25
  end
25
26
  end
27
+
28
+ # Ruby 1.9 only
29
+ {
30
+ '"\U00015678another"' => Literal.untyped("\u{15678}another"),
31
+ }.each_pair do |encoded, literal|
32
+ it "should encode '#{literal.contents}'" do
33
+ literal.to_n3.should == encoded
34
+ end
35
+ end if defined?(::Encoding)
26
36
  end
27
37
 
28
38
  describe "encodings" do
@@ -119,8 +129,6 @@ describe "Literals: " do
119
129
 
120
130
  it "should have string contents" do subject.contents.should == "true" end
121
131
  it "should have native contents" do subject.to_native.should == true end
122
- it "should coerce 1" do Literal.typed("1", XSD_NS.boolean).contents.should == "true" end
123
- it "should coerce 1" do Literal.typed("0", XSD_NS.boolean).contents.should == "false" end
124
132
  end
125
133
 
126
134
  describe "an integer" do
@@ -392,6 +400,101 @@ describe "Literals: " do
392
400
  end
393
401
  end
394
402
 
403
+ describe "valid content" do
404
+ {
405
+ "true" => %("true"^^<http://www.w3.org/2001/XMLSchema#boolean>),
406
+ "false" => %("false"^^<http://www.w3.org/2001/XMLSchema#boolean>),
407
+ "tRuE" => %("true"^^<http://www.w3.org/2001/XMLSchema#boolean>),
408
+ "FaLsE" => %("false"^^<http://www.w3.org/2001/XMLSchema#boolean>),
409
+ "1" => %("true"^^<http://www.w3.org/2001/XMLSchema#boolean>),
410
+ "0" => %("false"^^<http://www.w3.org/2001/XMLSchema#boolean>),
411
+ }.each_pair do |lit, n3|
412
+ it "should validate boolean '#{lit}'" do
413
+ Literal.typed(lit, XSD_NS.boolean).valid?.should be_true
414
+ end
415
+
416
+ it "should canonicalize boolean '#{lit}'" do
417
+ Literal.typed(lit, XSD_NS.boolean).to_ntriples.should == n3
418
+ end
419
+ end
420
+
421
+ {
422
+ "01" => %("1"^^<http://www.w3.org/2001/XMLSchema#integer>),
423
+ "1" => %("1"^^<http://www.w3.org/2001/XMLSchema#integer>),
424
+ "-1" => %("-1"^^<http://www.w3.org/2001/XMLSchema#integer>),
425
+ "+1" => %("1"^^<http://www.w3.org/2001/XMLSchema#integer>),
426
+ }.each_pair do |lit, n3|
427
+ it "should validate integer '#{lit}'" do
428
+ Literal.typed(lit, XSD_NS.integer).valid?.should be_true
429
+ end
430
+
431
+ it "should canonicalize integer '#{lit}'" do
432
+ Literal.typed(lit, XSD_NS.integer).to_ntriples.should == n3
433
+ end
434
+ end
435
+
436
+ {
437
+ "1" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
438
+ "-1" => %("-1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
439
+ "1." => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
440
+ "1.0" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
441
+ "1.00" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
442
+ "+001.00" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
443
+ "123.456" => %("123.456"^^<http://www.w3.org/2001/XMLSchema#decimal>),
444
+ "2.345" => %("2.345"^^<http://www.w3.org/2001/XMLSchema#decimal>),
445
+ "1.000000000" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
446
+ "2.3" => %("2.3"^^<http://www.w3.org/2001/XMLSchema#decimal>),
447
+ "2.234000005" => %("2.234000005"^^<http://www.w3.org/2001/XMLSchema#decimal>),
448
+ "2.2340000000000005" => %("2.2340000000000005"^^<http://www.w3.org/2001/XMLSchema#decimal>),
449
+ "2.23400000000000005" => %("2.234"^^<http://www.w3.org/2001/XMLSchema#decimal>),
450
+ "2.23400000000000000000005" => %("2.234"^^<http://www.w3.org/2001/XMLSchema#decimal>),
451
+ "1.2345678901234567890123457890" => %("1.2345678901234567"^^<http://www.w3.org/2001/XMLSchema#decimal>),
452
+ }.each_pair do |lit, n3|
453
+ it "should validate decimal '#{lit}'" do
454
+ Literal.typed(lit, XSD_NS.decimal).valid?.should be_true
455
+ end
456
+
457
+ it "should canonicalize decimal '#{lit}'" do
458
+ Literal.typed(lit, XSD_NS.decimal).to_ntriples.should == n3
459
+ end
460
+ end
461
+
462
+ {
463
+ "1" => %("1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
464
+ "-1" => %("-1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
465
+ "+01.000" => %("1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
466
+ "1." => %("1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
467
+ "1.0" => %("1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
468
+ "123.456" => %("1.23456E2"^^<http://www.w3.org/2001/XMLSchema#double>),
469
+ "1.0e+1" => %("1.0E1"^^<http://www.w3.org/2001/XMLSchema#double>),
470
+ "1.0e-10" => %("1.0E-10"^^<http://www.w3.org/2001/XMLSchema#double>),
471
+ "123.456e4" => %("1.23456E6"^^<http://www.w3.org/2001/XMLSchema#double>),
472
+ }.each_pair do |lit, n3|
473
+ it "should validate double '#{lit}'" do
474
+ Literal.typed(lit, XSD_NS.double).valid?.should be_true
475
+ end
476
+
477
+ it "should canonicalize double '#{lit}'" do
478
+ Literal.typed(lit, XSD_NS.double).to_ntriples.should == n3
479
+ end
480
+ end
481
+ end
482
+
483
+ describe "invalid content" do
484
+ [
485
+ Literal.typed("foo", XSD_NS.boolean),
486
+ Literal.typed("xyz", XSD_NS.integer),
487
+ Literal.typed("12xyz", XSD_NS.integer),
488
+ Literal.typed("12.xyz", XSD_NS.decimal),
489
+ Literal.typed("xy.z", XSD_NS.double),
490
+ Literal.typed("+1.0z", XSD_NS.double),
491
+ ].each do |lit|
492
+ it "should detect invalid encoding for '#{lit.to_n3}'" do
493
+ lit.valid?.should be_false
494
+ end
495
+ end
496
+ end
497
+
395
498
  describe "Encodings" do
396
499
  specify "integer" do
397
500
  Literal::Encoding.integer.should == Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#int")
@@ -415,5 +518,4 @@ describe "Literals: " do
415
518
  Literal::Encoding.the_null_encoding.should == Literal::Null.new(nil)
416
519
  end
417
520
  end
418
-
419
521
  end
@@ -1,19 +1,64 @@
1
- require 'rdf/redland'
2
-
3
1
  module Matchers
4
2
  class BeEquivalentGraph
3
+ Info = Struct.new(:about, :information, :trace, :compare, :inputDocument, :outputDocument)
5
4
  def normalize(graph)
6
- case graph
7
- when Graph then graph
8
- when Parser then graph.graph
5
+ case @info.compare
6
+ when :array
7
+ array = case graph
8
+ when Graph, Parser
9
+ graph = graph.graph if graph.respond_to?(:graph)
10
+ anon = "a"
11
+ anon_ctx = {}
12
+ graph.triples.collect {|triple| triple.to_ntriples }.each do |t|
13
+ t.gsub(/_:nbn\d+[a-z]+N/, "_:").
14
+ gsub!(/_:bn\d+[a-z]+/) do |bn|
15
+ # Normalize anon BNodes
16
+ if anon_ctx[bn]
17
+ anon_ctx[bn]
18
+ else
19
+ anon_ctx[bn] = anon
20
+ anon = anon.succ
21
+ end
22
+ "_:#{anon_ctx[bn]}"
23
+ end
24
+ end.sort
25
+ when Array
26
+ graph.sort
27
+ else
28
+ graph.to_s.split("\n").
29
+ map {|t| t.gsub(/^\s*(.*)\s*$/, '\1')}.
30
+ reject {|t2| t2.match(/^\s*$/)}.
31
+ compact.
32
+ sort.
33
+ uniq
34
+ end
35
+
36
+ # Implement to_ntriples on array, to simplify logic later
37
+ def array.to_ntriples; self.join("\n") + "\n"; end
38
+ array
9
39
  else
10
- triples = [graph].flatten.join("\n")
11
- N3Parser.parse(graph.to_s, @info.about, :strict => true)
40
+ case graph
41
+ when Graph then graph
42
+ when Parser then graph.graph
43
+ when IO, StringIO
44
+ Parser.parse(graph, @info.about)
45
+ else
46
+ parser = Parser.new(:struct => true)
47
+ fmt = parser.detect_format(graph.to_s)
48
+ parser.parse(graph.to_s, @info.about, :type => fmt)
49
+ end
12
50
  end
13
51
  end
14
52
 
15
53
  def initialize(expected, info)
16
- @info = info
54
+ @info = if info.respond_to?(:about)
55
+ info
56
+ elsif info.is_a?(Hash)
57
+ identifier = info[:identifier] || expected.is_a?(Graph) ? expected.identifier : info[:about]
58
+ Info.new(identifier, info[:information] || "", info[:trace], info[:compare])
59
+ else
60
+ Info.new(expected.is_a?(Graph) ? expected.identifier : info, info.to_s)
61
+ end
17
62
  @expected = normalize(expected)
18
63
  end
19
64
 
@@ -24,69 +69,77 @@ module Matchers
24
69
 
25
70
  def failure_message_for_should
26
71
  info = @info.respond_to?(:information) ? @info.information : ""
27
- if @actual.size != @expected.size
72
+ if @expected.is_a?(Graph) && @actual.size != @expected.size
28
73
  "Graph entry count differs:\nexpected: #{@expected.size}\nactual: #{@actual.size}"
29
- elsif @actual.identifier != @expected.identifier
74
+ elsif @expected.is_a?(Array) && @actual.size != @expected.length
75
+ "Graph entry count differs:\nexpected: #{@expected.length}\nactual: #{@actual.size}"
76
+ elsif @expected.is_a?(Graph) && @actual.identifier != @expected.identifier
30
77
  "Graph identifiers differ:\nexpected: #{@expected.identifier}\nactual: #{@actual.identifier}"
31
78
  else
32
- "Graph differs\n"
79
+ "Graph differs#{@info.compare == :array ? '(array)' : ''}\n"
33
80
  end +
34
- "\n\n#{info + "\n" unless info.empty?}" +
81
+ "\n#{info + "\n" unless info.empty?}" +
82
+ (@info.inputDocument ? "Input file: #{@info.inputDocument}\n" : "") +
83
+ (@info.outputDocument ? "Output file: #{@info.outputDocument}\n" : "") +
35
84
  "Unsorted Expected:\n#{@expected.to_ntriples}" +
36
85
  "Unsorted Results:\n#{@actual.to_ntriples}" +
37
- (@info.respond_to?(:trace) ? "\nDebug:\n#{@info.trace}" : "")
86
+ (@info.trace ? "\nDebug:\n#{@info.trace}" : "")
38
87
  end
39
88
  def negative_failure_message
40
89
  "Graphs do not differ\n"
41
90
  end
42
91
  end
43
92
 
44
- def be_equivalent_graph(expected, info = "")
93
+ def be_equivalent_graph(expected, info = nil)
45
94
  BeEquivalentGraph.new(expected, info)
46
95
  end
47
96
 
48
97
  # Run expected SPARQL query against actual
49
- class PassQuery
50
- def initialize(expected, info)
51
- @expected = expected
52
- @query = Redland::Query.new(expected)
53
- @info = info
54
- end
55
- def matches?(actual)
56
- @actual = actual
57
- @expected_results = @info.respond_to?(:expectedResults) ? @info.expectedResults : true
58
- model = Redland::Model.new
59
- ntriples_parser = Redland::Parser.ntriples
60
- ntriples_parser.parse_string_into_model(model, actual.to_ntriples, "http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/")
98
+ if $redland_enabled
99
+ class PassQuery
100
+ def initialize(expected, info)
101
+ @expected = expected
102
+ @query = Redland::Query.new(expected)
103
+ @info = info
104
+ end
105
+ def matches?(actual)
106
+ @actual = actual
107
+ @expected_results = @info.respond_to?(:expectedResults) ? @info.expectedResults : true
108
+ model = Redland::Model.new
109
+ ntriples_parser = Redland::Parser.ntriples
110
+ ntriples_parser.parse_string_into_model(model, actual.to_ntriples, "http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/")
61
111
 
62
- @results = @query.execute(model)
63
- if @expected_results
64
- @results.is_boolean? && @results.get_boolean?
65
- else
66
- @results.nil? || @results.is_boolean? && !@results.get_boolean?
112
+ @results = @query.execute(model)
113
+ if @expected_results
114
+ @results.is_boolean? && @results.get_boolean?
115
+ else
116
+ @results.nil? || @results.is_boolean? && !@results.get_boolean?
117
+ end
118
+ end
119
+ def failure_message_for_should
120
+ info = @info.respond_to?(:information) ? @info.information : ""
121
+ "#{info + "\n" unless info.empty?}" +
122
+ if @results.nil?
123
+ "Query failed to return results"
124
+ elsif !@results.is_boolean?
125
+ "Query returned non-boolean results"
126
+ elsif @expected_results
127
+ "Query returned false"
128
+ else
129
+ "Query returned true (expected false)"
130
+ end +
131
+ "\n#{@expected}" +
132
+ "\n#{@info.input}" +
133
+ "\nResults:\n#{@actual.to_ntriples}" +
134
+ "\nDebug:\n#{@info.trace}"
67
135
  end
68
136
  end
69
- def failure_message_for_should
70
- info = @info.respond_to?(:information) ? @info.information : ""
71
- "#{info + "\n" unless info.empty?}" +
72
- if @results.nil?
73
- "Query failed to return results"
74
- elsif !@results.is_boolean?
75
- "Query returned non-boolean results"
76
- elsif @expected_results
77
- "Query returned false"
78
- else
79
- "Query returned true (expected false)"
80
- end +
81
- "\n#{@expected}" +
82
- "\n#{@info.input}" +
83
- "\nResults:\n#{@actual.to_ntriples}" +
84
- "\nDebug:\n#{@info.trace}"
85
- end
86
- end
87
137
 
88
- def pass_query(expected, info = "")
89
- PassQuery.new(expected, info)
138
+ def pass_query(expected, info = "")
139
+ PassQuery.new(expected, info)
140
+ end
141
+ else
142
+ def pass_query(expect, info = ""); false; end
90
143
  end
91
144
 
92
145
  class BeValidXML
@@ -1,12 +1,13 @@
1
+ # coding: utf-8
1
2
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
3
  include RdfContext
3
4
 
4
5
  describe "N3 parser" do
5
6
  before(:each) { @parser = N3Parser.new(:strict => true) }
6
7
 
7
- describe "parse simple ntriples" do
8
+ describe "with simple ntriples" do
8
9
  it "should parse simple triple" do
9
- n3_string = "<http://example.org/> <http://xmlns.com/foaf/0.1/name> \"Tom Morris\" . "
10
+ n3_string = %(<http://example.org/> <http://xmlns.com/foaf/0.1/name> "Tom Morris" .)
10
11
  @parser.parse(n3_string)
11
12
  @parser.graph[0].subject.to_s.should == "http://example.org/"
12
13
  @parser.graph[0].predicate.to_s.should == "http://xmlns.com/foaf/0.1/name"
@@ -24,7 +25,7 @@ describe "N3 parser" do
24
25
  end
25
26
 
26
27
  # NTriple tests from http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt
27
- describe "recognize blank lines" do
28
+ describe "with blank lines" do
28
29
  {
29
30
  "comment" => "# comment lines",
30
31
  "comment after whitespace" => " # comment after whitespace",
@@ -38,6 +39,66 @@ describe "N3 parser" do
38
39
  end
39
40
  end
40
41
 
42
+ describe "with literal encodings" do
43
+ {
44
+ 'Dürst' => ':a :b "D\u00FCrst" .',
45
+ 'simple literal' => ':a :b "simple literal" .',
46
+ 'backslash:\\' => ':a :b "backslash:\\\\" .',
47
+ 'dquote:"' => ':a :b "dquote:\"" .',
48
+ "newline:\n" => ':a :b "newline:\n" .',
49
+ "return\r" => ':a :b "return\r" .',
50
+ "tab:\t" => ':a :b "tab:\t" .',
51
+ "é" => ':a :b "\u00E9" .',
52
+ "€" => ':a :b "\u20AC" .',
53
+ }.each_pair do |contents, triple|
54
+ specify "test #{contents}" do
55
+ @parser.parse(triple, "http://a/b")
56
+ @parser.graph.should_not be_nil
57
+ @parser.graph.size.should == 1
58
+ @parser.graph[0].object.contents.should == contents
59
+ end
60
+ end
61
+
62
+ it "should parse long literal with escape" do
63
+ n3 = %(@prefix : <http://example.org/foo#> . :a :b "\\U00015678another" .)
64
+ if defined?(::Encoding)
65
+ @parser.parse(n3)
66
+ @parser.graph[0].object.contents.should == "\u{15678}another"
67
+ else
68
+ lambda { @parser.parse(n3) }.should raise_error(RdfException, "Long Unicode escapes no supported in Ruby 1.8")
69
+ pending("Not supported in Ruby 1.8")
70
+ end
71
+ end
72
+
73
+ it "should parse multi-line literal" do
74
+ @parser.parse(%(
75
+ <http://www.example.com/books#book12345> <http://purl.org/dc/elements/1.1/title> """
76
+ Foo
77
+ <html:b xmlns:html="http://www.w3.org/1999/xhtml" html:a="b">bar<rdf:Thing xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><a:b xmlns:a="foo:"></a:b>here<a:c xmlns:a="foo:"></a:c></rd
78
+ f:Thing></html:b>
79
+ baz
80
+ <html:i xmlns:html="http://www.w3.org/1999/xhtml">more</html:i>
81
+ """ .
82
+ ))
83
+
84
+ @parser.graph.should_not be_nil
85
+ @parser.graph.size.should == 1
86
+ @parser.graph[0].object.contents.should == %(
87
+ Foo
88
+ <html:b xmlns:html="http://www.w3.org/1999/xhtml" html:a="b">bar<rdf:Thing xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><a:b xmlns:a="foo:"></a:b>here<a:c xmlns:a="foo:"></a:c></rd
89
+ f:Thing></html:b>
90
+ baz
91
+ <html:i xmlns:html="http://www.w3.org/1999/xhtml">more</html:i>
92
+ )
93
+ end
94
+
95
+ it "should parse long literal ending in double quote" do
96
+ @parser.parse(%(:a :b """ \\"""" .), "http://a/b")
97
+ @parser.graph.size.should == 1
98
+ @parser.graph[0].object.contents.should == ' "'
99
+ end
100
+ end
101
+
41
102
  it "should create named subject bnode" do
42
103
  @parser.parse("_:anon <http://example.org/property> <http://example.org/resource2> .")
43
104
  @parser.graph.should_not be_nil
@@ -49,6 +110,17 @@ describe "N3 parser" do
49
110
  triple.object.should == "http://example.org/resource2"
50
111
  end
51
112
 
113
+ it "should create named predicate bnode" do
114
+ @parser.parse("<http://example.org/resource2> _:anon <http://example.org/object> .")
115
+ @parser.graph.should_not be_nil
116
+ @parser.graph.size.should == 1
117
+ triple = @parser.graph[0]
118
+ triple.subject.should == "http://example.org/resource2"
119
+ triple.predicate.should be_a(BNode)
120
+ triple.predicate.identifier.should =~ /anon/
121
+ triple.object.should == "http://example.org/object"
122
+ end
123
+
52
124
  it "should create named object bnode" do
53
125
  @parser.parse("<http://example.org/resource2> <http://example.org/property> _:anon .")
54
126
  @parser.graph.should_not be_nil
@@ -96,89 +168,736 @@ describe "N3 parser" do
96
168
  @parser.graph[0].to_ntriples.should == [statement].flatten.last.gsub(/\s+/, " ").strip
97
169
  end
98
170
  end
171
+
172
+ it "should create typed literals" do
173
+ n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/name> \"Joe\"^^<http://www.w3.org/2001/XMLSchema#string> ."
174
+ @parser.parse(n3doc)
175
+ @parser.graph[0].object.class.should == RdfContext::Literal
176
+ end
177
+
178
+ it "should create BNodes" do
179
+ n3doc = "_:a a _:c ."
180
+ @parser.parse(n3doc)
181
+ @parser.graph[0].subject.class.should == RdfContext::BNode
182
+ @parser.graph[0].object.class.should == RdfContext::BNode
183
+ end
184
+
185
+ describe "should create URIRefs" do
186
+ {
187
+ %(<http://example.org/joe> <http://xmlns.com/foaf/0.1/knows> <http://example.org/jane> .) => %(<http://example.org/joe> <http://xmlns.com/foaf/0.1/knows> <http://example.org/jane> .),
188
+ %(<joe> <knows> <jane> .) => %(<http://a/joe> <http://a/knows> <http://a/jane> .),
189
+ %(:joe :knows :jane .) => %(<http://a/b#joe> <http://a/b#knows> <http://a/b#jane> .),
190
+ %(<#D%C3%BCrst> a "URI percent ^encoded as C3, BC".) => %(<http://a/b#D%C3%BCrst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI percent ^encoded as C3, BC" .),
191
+ }.each_pair do |n3, nt|
192
+ it "for '#{n3}'" do
193
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
194
+ end
195
+ end
196
+
197
+ {
198
+ %(<#Dürst> a "URI straight in UTF8".) => %(<http://a/b#D\\u00FCrst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI straight in UTF8" .),
199
+ %(:a :related :\u3072\u3089\u304C\u306A.) => %(<http://a/b#a> <http://a/b#related> <http://a/b#\\u3072\\u3089\\u304C\\u306A> .),
200
+ }.each_pair do |n3, nt|
201
+ it "for '#{n3}'" do
202
+ begin
203
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
204
+ rescue
205
+ if defined?(::Encoding)
206
+ raise
207
+ else
208
+ pending("Unicode URIs not supported in Ruby 1.8") { raise }
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
214
+
215
+ it "should create URIRefs" do
216
+ n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/knows> <http://example.org/jane> ."
217
+ @parser.parse(n3doc)
218
+ @parser.graph[0].subject.class.should == RdfContext::URIRef
219
+ @parser.graph[0].object.class.should == RdfContext::URIRef
220
+ end
221
+
222
+ it "should create literals" do
223
+ n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/name> \"Joe\"."
224
+ @parser.parse(n3doc)
225
+ @parser.graph[0].object.class.should == RdfContext::Literal
226
+ end
99
227
  end
100
228
 
101
- describe "literal encodings" do
229
+ describe "with illegal syntax" do
102
230
  {
103
- 'Dürst' => '_:a <http://example.org/named> "D\u00FCrst" .',
104
- 'simple literal' => '<http://example.org/resource7> <http://example.org/property> "simple literal" .',
105
- 'backslash:\\' => '<http://example.org/resource8> <http://example.org/property> "backslash:\\\\" .',
106
- 'dquote:"' => '<http://example.org/resource9> <http://example.org/property> "dquote:\"" .',
107
- "newline:\n" => '<http://example.org/resource10> <http://example.org/property> "newline:\n" .',
108
- "return\r" => '<http://example.org/resource11> <http://example.org/property> "return\r" .',
109
- "tab:\t" => '<http://example.org/resource12> <http://example.org/property> "tab:\t" .',
110
- "é" => '<http://example.org/resource16> <http://example.org/property> "\u00E9" .',
111
- "€" => '<http://example.org/resource17> <http://example.org/property> "\u20AC" .',
112
- }.each_pair do |contents, triple|
113
- specify "test #{contents}" do
114
- @parser.parse(triple)
115
- @parser.graph.should_not be_nil
116
- @parser.graph.size.should == 1
117
- @parser.graph[0].object.contents.should == contents
231
+ %(:y :p1 "xyz"^^xsd:integer .) => %r(Typed literal has an invalid lexical value: .* "xyz"),
232
+ %(:y :p1 "12xyz"^^xsd:integer .) => %r(Typed literal has an invalid lexical value: .* "12xyz"),
233
+ %(:y :p1 "xy.z"^^xsd:double .) => %r(Typed literal has an invalid lexical value: .* "xy\.z"),
234
+ %(:y :p1 "+1.0z"^^xsd:double .) => %r(Typed literal has an invalid lexical value: .* "\+1.0z"),
235
+ %(:a :b .) => %r(Illegal statment: ".*" missing object),
236
+ %(:a :b 'single quote' .) => RdfException,
237
+ %(:a "literal value" :b .) => InvalidPredicate,
238
+ %(@keywords prefix. :e prefix :f .) => %r(Keyword ".*" used as expression)
239
+ }.each_pair do |n3, error|
240
+ it "should raise error for '#{n3}'" do
241
+ lambda {
242
+ @parser.parse("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . #{n3}", "http://a/b")
243
+ }.should raise_error(error)
118
244
  end
119
245
  end
120
246
  end
121
247
 
122
- # n3p tests taken from http://inamidst.com/n3p/test/
123
- describe "parsing n3p test" do
124
- dir_name = File.join(File.dirname(__FILE__), '..', 'test', 'n3_tests', 'n3p', '*.n3')
125
- Dir.glob(dir_name).each do |n3|
126
- it n3 do
127
- test_file(n3)
248
+ describe "with n3 grammer" do
249
+ describe "syntactic expressions" do
250
+ it "should create typed literals with qname" do
251
+ n3doc = %(
252
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
253
+ @prefix foaf: <http://xmlns.com/foaf/0.1/>
254
+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#>
255
+ <http://example.org/joe> foaf:name \"Joe\"^^xsd:string .
256
+ )
257
+ @parser.parse(n3doc)
258
+ @parser.graph[0].object.class.should == RdfContext::Literal
128
259
  end
260
+
261
+ it "should map <> to document uri" do
262
+ n3doc = "@prefix : <> ."
263
+ @parser.parse(n3doc, "http://the.document.itself")
264
+ @parser.graph.nsbinding.should == {"" => Namespace.new("http://the.document.itself", "", true)}
265
+ end
266
+
267
+ it "should use <> as a prefix and as a triple node" do
268
+ n3 = %(@prefix : <> . <> a :a.)
269
+ nt = %(
270
+ <http://a/b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://a/b#a> .
271
+ )
272
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
273
+ end
274
+
275
+ it "should use <#> as a prefix and as a triple node" do
276
+ n3 = %(@prefix : <#> . <#> a :a.)
277
+ nt = %(
278
+ <http://a/b#> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://a/b#a> .
279
+ )
280
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
281
+ end
282
+
283
+ it "should generate rdf:type for 'a'" do
284
+ n3 = %(@prefix a: <http://foo/a#> . a:b a <http://www.w3.org/2000/01/rdf-schema#resource> .)
285
+ nt = %(<http://foo/a#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#resource> .)
286
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
287
+ end
288
+
289
+ it "should generate rdf:type for '@a'" do
290
+ n3 = %(@prefix a: <http://foo/a#> . a:b @a <http://www.w3.org/2000/01/rdf-schema#resource> .)
291
+ nt = %(<http://foo/a#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#resource> .)
292
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
293
+ end
294
+
295
+ it "should generate inverse predicate for 'is xxx of'" do
296
+ n3 = %("value" is :prop of :b . :b :prop "value" .)
297
+ nt = %(<http://a/b#b> <http://a/b#prop> "value" .)
298
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
299
+ end
300
+
301
+ it "should generate inverse predicate for '@is xxx @of'" do
302
+ n3 = %("value" @is :prop @of :b . :b :prop "value" .)
303
+ nt = %(<http://a/b#b> <http://a/b#prop> "value" .)
304
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
305
+ end
306
+
307
+ it "should generate inverse predicate for 'is xxx of' with object list" do
308
+ n3 = %("value" is :prop of :b, :c . )
309
+ nt = %(
310
+ <http://a/b#b> <http://a/b#prop> "value" .
311
+ <http://a/b#c> <http://a/b#prop> "value" .
312
+ )
313
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
314
+ end
315
+
316
+ it "should generate predicate for 'has xxx'" do
317
+ n3 = %(@prefix a: <http://foo/a#> . a:b has :pred a:c .)
318
+ nt = %(<http://foo/a#b> <http://a/b#pred> <http://foo/a#c> .)
319
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
320
+ end
321
+
322
+ it "should generate predicate for '@has xxx'" do
323
+ n3 = %(@prefix a: <http://foo/a#> . a:b @has :pred a:c .)
324
+ nt = %(<http://foo/a#b> <http://a/b#pred> <http://foo/a#c> .)
325
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
326
+ end
327
+
328
+ it "should create log:implies predicate for '=>'" do
329
+ n3 = %(@prefix a: <http://foo/a#> . _:a => a:something .)
330
+ nt = %(_:a <http://www.w3.org/2000/10/swap/log#implies> <http://foo/a#something> .)
331
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
332
+ end
333
+
334
+ it "should create log:implies inverse predicate for '<='" do
335
+ n3 = %(@prefix a: <http://foo/a#> . _:a <= a:something .)
336
+ nt = %(<http://foo/a#something> <http://www.w3.org/2000/10/swap/log#implies> _:a .)
337
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
338
+ end
339
+
340
+ it "should create owl:sameAs predicate for '='" do
341
+ n3 = %(@prefix a: <http://foo/a#> . _:a = a:something .)
342
+ nt = %(_:a <http://www.w3.org/2002/07/owl#sameAs> <http://foo/a#something> .)
343
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
344
+ end
345
+
346
+ {
347
+ %(:a :b @true) => %(<http://a/b#a> <http://a/b#b> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .),
348
+ %(:a :b @false) => %(<http://a/b#a> <http://a/b#b> "false"^^<http://www.w3.org/2001/XMLSchema#boolean> .),
349
+ %(:a :b 1) => %(<http://a/b#a> <http://a/b#b> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .),
350
+ %(:a :b -1) => %(<http://a/b#a> <http://a/b#b> "-1"^^<http://www.w3.org/2001/XMLSchema#integer> .),
351
+ %(:a :b +1) => %(<http://a/b#a> <http://a/b#b> "+1"^^<http://www.w3.org/2001/XMLSchema#integer> .),
352
+ %(:a :b 1.0) => %(<http://a/b#a> <http://a/b#b> "1.0"^^<http://www.w3.org/2001/XMLSchema#decimal> .),
353
+ %(:a :b 1.0e1) => %(<http://a/b#a> <http://a/b#b> "1.0e1"^^<http://www.w3.org/2001/XMLSchema#double> .),
354
+ %(:a :b 1.0e-1) => %(<http://a/b#a> <http://a/b#b> "1.0e-1"^^<http://www.w3.org/2001/XMLSchema#double> .),
355
+ %(:a :b 1.0e+1) => %(<http://a/b#a> <http://a/b#b> "1.0e+1"^^<http://www.w3.org/2001/XMLSchema#double> .),
356
+ }.each_pair do |n3, nt|
357
+ it "should create typed literal for '#{n3}'" do
358
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
359
+ end
360
+ end
361
+
362
+ it "should accept empty localname" do
363
+ n3 = %(: : : .)
364
+ nt = %(<http://a/b#> <http://a/b#> <http://a/b#> .)
365
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
366
+ end
367
+
368
+ it "should accept prefix with empty local name" do
369
+ n3 = %(@prefix foo: <http://foo/bar#> . foo: foo: foo: .)
370
+ nt = %(<http://foo/bar#> <http://foo/bar#> <http://foo/bar#> .)
371
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
372
+ end
373
+
374
+ it "should do something for @forAll"
375
+
376
+ it "should do something for @forSome"
129
377
  end
130
- end
131
-
132
- describe "parsing real data tests" do
133
- dirs = [ 'misc', 'lcsh' ]
134
- dirs.each do |dir|
135
- dir_name = File.join(File.dirname(__FILE__), '..', 'test', 'n3_tests', dir, '*.n3')
136
- Dir.glob(dir_name).each do |n3|
137
- it "#{dir} #{n3}" do
138
- test_file(n3)
378
+
379
+ describe "namespaces" do
380
+ it "should set absolute base" do
381
+ n3 = %(@base <http://foo/bar> . <> :a <b> . <#c> :d </e>.)
382
+ nt = %(
383
+ <http://foo/bar> <http://foo/bara> <http://foo/b> .
384
+ <http://foo/bar#c> <http://foo/bard> <http://foo/e> .
385
+ )
386
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
387
+ end
388
+
389
+ it "should set absolute base (trailing /)" do
390
+ n3 = %(@base <http://foo/bar/> . <> :a <b> . <#c> :d </e>.)
391
+ nt = %(
392
+ <http://foo/bar/> <http://foo/bar/a> <http://foo/bar/b> .
393
+ <http://foo/bar/#c> <http://foo/bar/d> <http://foo/e> .
394
+ )
395
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
396
+ end
397
+
398
+ it "should set absolute base (trailing #)" do
399
+ n3 = %(@base <http://foo/bar#> . <> :a <b> . <#c> :d </e>.)
400
+ nt = %(
401
+ <http://foo/bar> <http://foo/bar#a> <http://foo/b> .
402
+ <http://foo/bar#c> <http://foo/bar#d> <http://foo/e> .
403
+ )
404
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
405
+ end
406
+
407
+ it "should set relative base" do
408
+ n3 = %(
409
+ @base <http://example.org/products/>.
410
+ <> :a <b>, <#c>.
411
+ @base <prod123/>.
412
+ <> :a <b>, <#c>.
413
+ @base <../>.
414
+ <> :a <d>, <#e>.
415
+ )
416
+ nt = %(
417
+ <http://example.org/products/> <http://example.org/products/a> <http://example.org/products/b> .
418
+ <http://example.org/products/> <http://example.org/products/a> <http://example.org/products/#c> .
419
+ <http://example.org/products/prod123/> <http://example.org/products/prod123/a> <http://example.org/products/prod123/b> .
420
+ <http://example.org/products/prod123/> <http://example.org/products/prod123/a> <http://example.org/products/prod123/#c> .
421
+ <http://example.org/products/> <http://example.org/products/a> <http://example.org/products/d> .
422
+ <http://example.org/products/> <http://example.org/products/a> <http://example.org/products/#e> .
423
+ )
424
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
425
+ end
426
+
427
+ it "should bind named namespace" do
428
+ n3doc = "@prefix ns: <http://the/namespace#> ."
429
+ @parser.parse(n3doc, "http://a/b")
430
+ @parser.graph.nsbinding.should == {"ns" => Namespace.new("http://the/namespace#", "ns")}
431
+ end
432
+
433
+ it "should bind empty prefix to <%> by default" do
434
+ n3doc = "@prefix : <#> ."
435
+ @parser.parse(n3doc, "http://the.document.itself")
436
+ @parser.graph.nsbinding.should == {"" => Namespace.new("http://the.document.itself#", "")}
437
+ end
438
+
439
+ it "should be able to bind _ as namespace" do
440
+ n3 = %(@prefix _: <http://underscore/> . _:a a _:p.)
441
+ nt = %(<http://underscore/a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://underscore/p> .)
442
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
443
+ end
444
+ end
445
+
446
+ describe "keywords" do
447
+ [
448
+ %(prefix :<>.),
449
+ %(base <>.),
450
+ %(keywords a.),
451
+ %(:a is :b of :c.),
452
+ %(:a @is :b of :c.),
453
+ %(:a is :b @of :c.),
454
+ %(:a has :b :c.),
455
+ ].each do |n3|
456
+ it "should require @ if keywords set to empty for '#{n3}'" do
457
+ lambda do
458
+ @parser.parse("@keywords . #{n3}", "http://a/b")
459
+ end.should raise_error(/unqualified keyword '\w+' used without @keyword directive/)
139
460
  end
140
461
  end
462
+
463
+ {
464
+ %(:a a :b) => %(<http://a/b#a> <http://a/b#a> <http://a/b#b> .),
465
+ %(:a :b true) => %(<http://a/b#a> <http://a/b#b> <http://a/b#true> .),
466
+ %(:a :b false) => %(<http://a/b#a> <http://a/b#b> <http://a/b#false> .),
467
+ %(c :a :t) => %(<http://a/b#c> <http://a/b#a> <http://a/b#t> .),
468
+ %(:c a :t) => %(<http://a/b#c> <http://a/b#a> <http://a/b#t> .),
469
+ %(:c :a t) => %(<http://a/b#c> <http://a/b#a> <http://a/b#t> .),
470
+ }.each_pair do |n3, nt|
471
+ it "should use default_ns for '#{n3}'" do
472
+ @parser.parse("@keywords . #{n3}", "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
473
+ end
474
+ end
475
+
476
+ {
477
+ %(@keywords true. :a :b true.) => %(<http://a/b#a> <http://a/b#b> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .),
478
+ %(@keywords false. :a :b false.) => %(<http://a/b#a> <http://a/b#b> "false"^^<http://www.w3.org/2001/XMLSchema#boolean> .),
479
+ %(@keywords a. :a a :b.) => %(<http://a/b#a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://a/b#b> .),
480
+ %(@keywords is. :a is :b @of :c.) => %(<http://a/b#c> <http://a/b#b> <http://a/b#a> .),
481
+ %(@keywords of. :a @is :b of :c.) => %(<http://a/b#c> <http://a/b#b> <http://a/b#a> .),
482
+ %(@keywords has. :a has :b :c.) => %(<http://a/b#a> <http://a/b#b> <http://a/b#c> .),
483
+ } .each_pair do |n3, nt|
484
+ it "should use keyword for '#{n3}'" do
485
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
486
+ end
487
+ end
488
+
489
+ it "should raise error if unknown keyword set" do
490
+ n3 = %(@keywords foo.)
491
+ lambda do
492
+ @parser.parse(n3, "http://a/b")
493
+ end.should raise_error(ParserException, "undefined keywords used: foo")
494
+ end
141
495
  end
142
- end
143
-
144
- it "should throw an exception when presented with a BNode as a predicate" do
145
- n3doc = "_:a _:b _:c ."
146
- lambda { @parser.parse(n3doc) }.should raise_error(RdfContext::Triple::InvalidPredicate)
147
- end
496
+
497
+ describe "declaration ordering" do
498
+ it "should process _ namespace binding after an initial use as a BNode" do
499
+ n3 = %(
500
+ _:a a :p.
501
+ @prefix _: <http://underscore/> .
502
+ _:a a :p.
503
+ )
504
+ nt = %(
505
+ <http://underscore/a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> :p .
506
+ _:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> :p .
507
+ )
508
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
509
+ end
510
+
511
+ it "should allow a prefix to be redefined" do
512
+ n3 = %(
513
+ @prefix a: <http://host/A#>.
514
+ a:b a:p a:v .
148
515
 
149
- it "should create BNodes" do
150
- n3doc = "_:a a _:c ."
151
- @parser.parse(n3doc)
152
- @parser.graph[0].subject.class.should == RdfContext::BNode
153
- @parser.graph[0].object.class.should == RdfContext::BNode
154
- end
155
-
156
- it "should create URIRefs" do
157
- n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/knows> <http://example.org/jane> ."
158
- @parser.parse(n3doc)
159
- @parser.graph[0].subject.class.should == RdfContext::URIRef
160
- @parser.graph[0].object.class.should == RdfContext::URIRef
161
- end
162
-
163
- it "should create literals" do
164
- n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/name> \"Joe\"."
165
- @parser.parse(n3doc)
166
- @parser.graph[0].object.class.should == RdfContext::Literal
167
- end
168
-
169
- it "should create typed literals" do
170
- n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/name> \"Joe\"^^<http://www.w3.org/2001/XMLSchema#string> ."
171
- @parser.parse(n3doc)
172
- @parser.graph[0].object.class.should == RdfContext::Literal
173
- end
174
-
175
- it "should map <#> to document uri" do
176
- n3doc = "@prefix : <#> ."
177
- @parser.parse(n3doc, "http://the.document.itself")
178
- @parser.graph.nsbinding.should == {"__local__", Namespace.new("http://the.document.itself", "__local__")}
516
+ @prefix a: <http://host/Z#>.
517
+ a:b a:p a:v .
518
+ )
519
+ nt = %(
520
+ <http://host/A#b> <http://host/A#p> <http://host/A#v> .
521
+ <http://host/Z#b> <http://host/Z#p> <http://host/Z#v> .
522
+ )
523
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
524
+ end
525
+
526
+ it "should process sequential @base declarations (swap base.n3)" do
527
+ n3 = %(
528
+ @base <http://example.com/ontolgies>. <a> :b <foo/bar#baz>.
529
+ @base <path/DFFERENT/>. <a2> :b2 <foo/bar#baz2>.
530
+ @prefix : <#>. <d3> :b3 <e3>.
531
+ )
532
+ nt = %(
533
+ <http://example.com/a> <http://example.com/ontolgiesb> <http://example.com/foo/bar#baz> .
534
+ <http://example.com/path/DFFERENT/a2> <http://example.com/path/DFFERENT/b2> <http://example.com/path/DFFERENT/foo/bar#baz2> .
535
+ <http://example.com/path/DFFERENT/d3> <http://example.com/path/DFFERENT/#b3> <http://example.com/path/DFFERENT/e3> .
536
+ )
537
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
538
+ end
539
+ end
540
+
541
+ describe "BNodes" do
542
+ it "should create BNode for identifier with '_' prefix" do
543
+ n3 = %(@prefix a: <http://foo/a#> . _:a a:p a:v .)
544
+ nt = %(_:bnode0 <http://foo/a#p> <http://foo/a#v> .)
545
+ end
546
+
547
+ it "should create BNode for [] as subject" do
548
+ n3 = %(@prefix a: <http://foo/a#> . [] a:p a:v .)
549
+ nt = %(_:bnode0 <http://foo/a#p> <http://foo/a#v> .)
550
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
551
+ end
552
+
553
+ it "should create BNode for [] as predicate" do
554
+ n3 = %(@prefix a: <http://foo/a#> . a:s [] a:o .)
555
+ nt = %(<http://foo/a#s> _:bnode0 <http://foo/a#o> .)
556
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
557
+ end
558
+
559
+ it "should create BNode for [] as object" do
560
+ n3 = %(@prefix a: <http://foo/a#> . a:s a:p [] .)
561
+ nt = %(<http://foo/a#s> <http://foo/a#p> _:bnode0 .)
562
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
563
+ end
564
+
565
+ it "should create BNode for [] as statement" do
566
+ n3 = %([:a :b] .)
567
+ nt = %(_:bnode0 <http://a/b#a> <http://a/b#b> .)
568
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
569
+ end
570
+
571
+ it "should create statements with BNode subjects using [ pref obj]" do
572
+ n3 = %(@prefix a: <http://foo/a#> . [ a:p a:v ] .)
573
+ nt = %(_:bnode0 <http://foo/a#p> <http://foo/a#v> .)
574
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
575
+ end
576
+
577
+ it "should create BNode as a single object" do
578
+ n3 = %(@prefix a: <http://foo/a#> . a:b a:oneRef [ a:pp "1" ; a:qq "2" ] .)
579
+ nt = %(
580
+ _:bnode0 <http://foo/a#pp> "1" .
581
+ _:bnode0 <http://foo/a#qq> "2" .
582
+ <http://foo/a#b> <http://foo/a#oneRef> _:bnode0 .
583
+ )
584
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
585
+ end
586
+
587
+ it "should create a shared BNode" do
588
+ n3 = %(
589
+ @prefix a: <http://foo/a#> .
590
+
591
+ a:b1 a:twoRef _:a .
592
+ a:b2 a:twoRef _:a .
593
+
594
+ _:a :pred [ a:pp "1" ; a:qq "2" ].
595
+ )
596
+ nt = %(
597
+ <http://foo/a#b1> <http://foo/a#twoRef> _:a .
598
+ <http://foo/a#b2> <http://foo/a#twoRef> _:a .
599
+ _:bnode0 <http://foo/a#pp> "1" .
600
+ _:bnode0 <http://foo/a#qq> "2" .
601
+ _:a :pred _:bnode0 .
602
+ )
603
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
604
+ end
605
+
606
+ it "should create nested BNodes" do
607
+ n3 = %(
608
+ @prefix a: <http://foo/a#> .
609
+
610
+ a:a a:p [ a:p2 [ a:p3 "v1" , "v2" ; a:p4 "v3" ] ; a:p5 "v4" ] .
611
+ )
612
+ nt = %(
613
+ _:bnode0 <http://foo/a#p3> "v1" .
614
+ _:bnode0 <http://foo/a#p3> "v2" .
615
+ _:bnode0 <http://foo/a#p4> "v3" .
616
+ _:bnode1 <http://foo/a#p2> _:bnode0 .
617
+ _:bnode1 <http://foo/a#p5> "v4" .
618
+ <http://foo/a#a> <http://foo/a#p> _:bnode1 .
619
+ )
620
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
621
+ end
622
+
623
+ describe "from paths" do
624
+ it "should create bnode for path x.p" do
625
+ n3 = %(:x2.:y2 :p2 "3" .)
626
+ nt = %(:x2 :y2 _:bnode0 . _:bnode0 :p2 "3" .)
627
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
628
+ end
629
+
630
+ it "should create bnode for path x!p" do
631
+ n3 = %(:x2!:y2 :p2 "3" .)
632
+ nt = %(:x2 :y2 _:bnode0 . _:bnode0 :p2 "3" .)
633
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
634
+ end
635
+
636
+ it "should create bnode for path x^p" do
637
+ n3 = %(:x2^:y2 :p2 "3" .)
638
+ nt = %(_:bnode0 :y2 :x2 . _:bnode0 :p2 "3" .)
639
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
640
+ end
641
+
642
+ it "should decode :joe!fam:mother!loc:office!loc:zip as Joe's mother's office's zipcode" do
643
+ n3 = %(
644
+ @prefix fam: <http://foo/fam#> .
645
+ @prefix loc: <http://foo/loc#> .
646
+
647
+ :joe!fam:mother!loc:office!loc:zip .
648
+ )
649
+ nt = %(
650
+ :joe <http://foo/fam#mother> _:bnode0 .
651
+ _:bnode0 <http://foo/loc#office> _:bnode1 .
652
+ _:bnode1 <http://foo/loc#zip> _:bnode2 .
653
+ )
654
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
655
+ end
656
+
657
+ it "should decode :joe!fam:mother^fam:mother Anyone whose mother is Joe's mother." do
658
+ n3 = %(
659
+ @prefix fam: <http://foo/fam#> .
660
+ @prefix loc: <http://foo/loc#> .
661
+
662
+ :joe!fam:mother^fam:mother .
663
+ )
664
+ nt = %(
665
+ :joe <http://foo/fam#mother> _:bnode0 .
666
+ _:bnode1 <http://foo/fam#mother> _:bnode0 .
667
+ )
668
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
669
+ end
670
+
671
+ it "should decode path with property list." do
672
+ n3 = %(
673
+ @prefix a: <http://a/ns#>.
674
+ :a2.a:b2.a:c2 :q1 "3" ; :q2 "4" , "5" .
675
+ )
676
+ nt = %(
677
+ :a2 <http://a/ns#b2> _:bnode0 .
678
+ _:bnode0 <http://a/ns#c2> _:bnode1 .
679
+ _:bnode1 :q1 "3" .
680
+ _:bnode1 :q2 "4" .
681
+ _:bnode1 :q2 "5" .
682
+ )
683
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
684
+ end
685
+
686
+ it "should decode path as object(1)" do
687
+ n3 = %(:a :b "lit"^:c.)
688
+ nt = %(
689
+ :a :b _:bnode .
690
+ _:bnode :c "lit" .
691
+ )
692
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
693
+ end
694
+
695
+ it "should decode path as object(2)" do
696
+ n3 = %(@prefix a: <http://a/ns#>. :r :p :o.a:p1.a:p2 .)
697
+ nt = %(
698
+ :o <http://a/ns#p1> _:bnode0 .
699
+ _:bnode0 <http://a/ns#p2> _:bnode1 .
700
+ :r :p _:bnode1 .
701
+ )
702
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
703
+ end
704
+ end
705
+ end
706
+
707
+ describe "formulae" do
708
+ it "should require that graph be formula_aware when encountering a formlua"
709
+
710
+ it "should separate triples between specified and quoted graphs"
711
+ end
712
+
713
+ describe "object lists" do
714
+ it "should create 2 statements for simple list" do
715
+ n3 = %(:a :b :c, :d)
716
+ nt = %(<http://a/b#a> <http://a/b#b> <http://a/b#c> . <http://a/b#a> <http://a/b#b> <http://a/b#d> .)
717
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
718
+ end
719
+ end
720
+
721
+ describe "property lists" do
722
+ it "should parse property list" do
723
+ n3 = %(
724
+ @prefix a: <http://foo/a#> .
725
+
726
+ a:b a:p1 "123" ; a:p1 "456" .
727
+ a:b a:p2 a:v1 ; a:p3 a:v2 .
728
+ )
729
+ nt = %(
730
+ <http://foo/a#b> <http://foo/a#p1> "123" .
731
+ <http://foo/a#b> <http://foo/a#p1> "456" .
732
+ <http://foo/a#b> <http://foo/a#p2> <http://foo/a#v1> .
733
+ <http://foo/a#b> <http://foo/a#p3> <http://foo/a#v2> .
734
+ )
735
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
736
+ end
737
+ end
738
+
739
+ describe "lists" do
740
+ it "should parse empty list" do
741
+ n3 = %(@prefix :<http://example.com/>. :empty :set ().)
742
+ nt = %(
743
+ <http://example.com/empty> <http://example.com/set> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .)
744
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
745
+ end
746
+
747
+ it "should parse list with single element" do
748
+ n3 = %(@prefix :<http://example.com/>. :gregg :wrote ("RdfContext").)
749
+ nt = %(
750
+ _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "RdfContext" .
751
+ _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
752
+ <http://example.com/gregg> <http://example.com/wrote> _:bnode0 .
753
+ )
754
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
755
+ end
756
+
757
+ it "should parse list with multiple elements" do
758
+ n3 = %(@prefix :<http://example.com/>. :gregg :name ("Gregg" "Barnum" "Kellogg").)
759
+ nt = %(
760
+ _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "Gregg" .
761
+ _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode1 .
762
+ _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "Barnum" .
763
+ _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode2 .
764
+ _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "Kellogg" .
765
+ _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
766
+ <http://example.com/gregg> <http://example.com/name> _:bnode0 .
767
+ )
768
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
769
+ end
770
+
771
+ it "should parse unattached lists" do
772
+ n3 = %(
773
+ @prefix a: <http://foo/a#> .
774
+
775
+ ("1" "2" "3") .
776
+ # This is not a statement.
777
+ () .
778
+ )
779
+ nt = %(
780
+ _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "1" .
781
+ _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode1 .
782
+ _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "2" .
783
+ _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode2 .
784
+ _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "3" .
785
+ _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
786
+ )
787
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
788
+ end
789
+
790
+ it "should add property to nil list" do
791
+ n3 = %(@prefix a: <http://foo/a#> . () a:prop "nilProp" .)
792
+ nt = %(<http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> <http://foo/a#prop> "nilProp" .)
793
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug, :compare => :array)
794
+ end
795
+ it "should parse with compound items" do
796
+ n3 = %(
797
+ @prefix a: <http://foo/a#> .
798
+
799
+ a:a a:p ( [ a:p2 "v1" ]
800
+ <http://resource1>
801
+ <http://resource2>
802
+ ("inner list") ) .
803
+
804
+ <http://resource1> a:p "value" .
805
+ )
806
+ nt = %(
807
+ _:bnode0 <http://foo/a#p2> "v1" .
808
+ _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:bnode0 .
809
+ _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode2 .
810
+ _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://resource1> .
811
+ _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode3 .
812
+ _:bnode3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://resource2> .
813
+ _:bnode4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "inner list" .
814
+ _:bnode4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
815
+ _:bnode3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode5 .
816
+ _:bnode5 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:bnode4 .
817
+ _:bnode5 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
818
+ <http://foo/a#a> <http://foo/a#p> _:bnode1 .
819
+ <http://resource1> <http://foo/a#p> "value" .
820
+ )
821
+ @parser.parse(n3, "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @parser.debug)
822
+ end
823
+
824
+ end
825
+
826
+ # n3p tests taken from http://inamidst.com/n3p/test/
827
+ describe "with real data tests" do
828
+ dirs = %w(misc lcsh rdflib n3p)
829
+ dirs.each do |dir|
830
+ dir_name = File.join(File.dirname(__FILE__), '..', 'test', 'n3_tests', dir, '*.n3')
831
+ Dir.glob(dir_name).each do |n3|
832
+ it "#{dir} #{n3}" do
833
+ test_file(n3)
834
+ end
835
+ end
836
+ end
837
+ end
838
+
839
+ describe "with AggregateGraph tests" do
840
+ subject { Graph.new }
841
+
842
+ describe "with a type" do
843
+ before(:each) do
844
+ subject.parse(%(
845
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
846
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
847
+ @prefix : <http://test/> .
848
+ :foo a rdfs:Class.
849
+ :bar :d :c.
850
+ :a :d :c.
851
+ ), "http://a/b")
852
+ end
853
+
854
+ it "should have 3 namespaces" do
855
+ subject.nsbinding.keys.length.should == 3
856
+ end
857
+ end
858
+
859
+ describe "with blank clause" do
860
+ before(:each) do
861
+ subject.parse(%(
862
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
863
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
864
+ @prefix : <http://test/> .
865
+ @prefix log: <http://www.w3.org/2000/10/swap/log#>.
866
+ :foo a rdfs:Resource.
867
+ :bar rdfs:isDefinedBy [ a log:Formula ].
868
+ :a :d :e.
869
+ ), "http://a/b")
870
+ end
871
+
872
+ it "should have 4 namespaces" do
873
+ subject.nsbinding.keys.length.should == 4
874
+ end
875
+ end
876
+
877
+ describe "with empty subject" do
878
+ before(:each) do
879
+ subject.parse(%(
880
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
881
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
882
+ @prefix log: <http://www.w3.org/2000/10/swap/log#>.
883
+ @prefix : <http://test/> .
884
+ <> a log:N3Document.
885
+ ), "http://test/")
886
+ end
887
+
888
+ it "should have 4 namespaces" do
889
+ subject.nsbinding.keys.length.should == 4
890
+ end
891
+
892
+ it "should have default subject" do
893
+ subject.size.should == 1
894
+ subject.triples.first.subject.should == "http://test/"
895
+ end
896
+ end
897
+ end
179
898
  end
180
899
 
181
- it "should parse testcase" do
900
+ it "should parse rdf_core testcase" do
182
901
  sampledoc = <<-EOF;
183
902
  <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/Manifest.rdf#test001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#PositiveParserTest> .
184
903
  <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/Manifest.rdf#test001> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#approval> <http://lists.w3.org/Archives/Public/w3c-rdfcore-wg/2002Mar/0235.html> .
@@ -190,40 +909,20 @@ describe "N3 parser" do
190
909
  <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/test001.rdf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#RDF-XML-Document> .
191
910
  EOF
192
911
  @parser.parse(sampledoc, "http://www.w3.org/2000/10/rdf-tests/rdfcore/amp-in-url/Manifest.rdf")
193
- ntriples = @parser.graph.to_ntriples
194
- ntriples = sort_ntriples(ntriples)
195
912
 
196
- nt_string = sort_ntriples(sampledoc)
197
- ntriples.should == nt_string
913
+ @parser.graph.should be_equivalent_graph(sampledoc,
914
+ :about => "http://www.w3.org/2000/10/rdf-tests/rdfcore/amp-in-url/Manifest.rdf",
915
+ :trace => @parser.debug, :compare => :array
916
+ )
198
917
  end
199
-
918
+
200
919
  def test_file(filepath)
201
- anon = "a"
202
- anon_ctx = {}
203
920
  n3_string = File.read(filepath)
204
921
  @parser.parse(n3_string, "file:#{filepath}")
205
- ntriples = @parser.graph.to_ntriples
206
- ntriples.gsub!(/_:nbn\d+[a-z]+N/, "_:") # Normalize named BNodes
207
- ntriples.gsub!(/_:bn\d+[a-z]+/) do |bn|
208
- # Normalize anon BNodes
209
- if anon_ctx[bn]
210
- anon_ctx[bn]
211
- else
212
- anon_ctx[bn] = anon
213
- anon = anon.succ
214
- end
215
- "_:#{anon_ctx[bn]}"
216
- end
217
- ntriples = sort_ntriples(ntriples)
218
922
 
219
923
  nt_string = File.read(filepath.sub('.n3', '.nt'))
220
- nt_string = sort_ntriples(nt_string)
221
-
222
- ntriples.should == nt_string
924
+ @parser.graph.should be_equivalent_graph(nt_string,
925
+ :about => "file:#{filepath}",
926
+ :trace => @parser.debug)
223
927
  end
224
-
225
- def sort_ntriples(string)
226
- string.split("\n").sort.join("\n")
227
- end
228
-
229
928
  end