rubyrdf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/History.txt +4 -0
  2. data/License.txt +24 -0
  3. data/Manifest.txt +52 -0
  4. data/README.txt +79 -0
  5. data/Rakefile +4 -0
  6. data/config/hoe.rb +70 -0
  7. data/config/requirements.rb +15 -0
  8. data/lib/rdf/blank_node.rb +41 -0
  9. data/lib/rdf/exceptions.rb +26 -0
  10. data/lib/rdf/format/ntriples.rb +493 -0
  11. data/lib/rdf/graph/base.rb +118 -0
  12. data/lib/rdf/graph/memory.rb +146 -0
  13. data/lib/rdf/graph/tests.rb +137 -0
  14. data/lib/rdf/namespace.rb +90 -0
  15. data/lib/rdf/plain_literal_node.rb +36 -0
  16. data/lib/rdf/query/binding.rb +68 -0
  17. data/lib/rdf/query/executer.rb +42 -0
  18. data/lib/rdf/query/result.rb +54 -0
  19. data/lib/rdf/query.rb +54 -0
  20. data/lib/rdf/triple.rb +61 -0
  21. data/lib/rdf/typed_literal_node.rb +39 -0
  22. data/lib/rdf/uri_node.rb +35 -0
  23. data/lib/rdf/version.rb +9 -0
  24. data/lib/rubyrdf.rb +59 -0
  25. data/log/debug.log +0 -0
  26. data/script/console +11 -0
  27. data/script/destroy +14 -0
  28. data/script/generate +14 -0
  29. data/script/txt2html +74 -0
  30. data/setup.rb +1585 -0
  31. data/tasks/deployment.rake +34 -0
  32. data/tasks/environment.rake +7 -0
  33. data/tasks/website.rake +17 -0
  34. data/test/helper.rb +14 -0
  35. data/test/test_blank_node.rb +67 -0
  36. data/test/test_format_ntriples.rb +247 -0
  37. data/test/test_graph_base.rb +71 -0
  38. data/test/test_graph_memory.rb +146 -0
  39. data/test/test_namespace.rb +130 -0
  40. data/test/test_plain_literal_node.rb +83 -0
  41. data/test/test_query.rb +49 -0
  42. data/test/test_query_binding.rb +84 -0
  43. data/test/test_query_result.rb +111 -0
  44. data/test/test_rdf.rb +56 -0
  45. data/test/test_triple.rb +147 -0
  46. data/test/test_typed_literal_node.rb +61 -0
  47. data/test/test_uri_node.rb +45 -0
  48. data/website/index.html +169 -0
  49. data/website/index.txt +92 -0
  50. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  51. data/website/stylesheets/screen.css +138 -0
  52. data/website/template.html.erb +48 -0
  53. metadata +139 -0
@@ -0,0 +1,34 @@
1
+ desc 'Release the website and new gem version'
2
+ task :deploy => [:check_version, :website, :release] do
3
+ puts "Remember to create SVN tag:"
4
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
5
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
6
+ puts "Suggested comment:"
7
+ puts "Tagging release #{CHANGES}"
8
+ end
9
+
10
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
11
+ task :local_deploy => [:website_generate, :install_gem]
12
+
13
+ task :check_version do
14
+ unless ENV['VERSION']
15
+ puts 'Must pass a VERSION=x.y.z release version'
16
+ exit
17
+ end
18
+ unless ENV['VERSION'] == VERS
19
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
20
+ exit
21
+ end
22
+ end
23
+
24
+ desc 'Install the package as a gem, without generating documentation(ri/rdoc)'
25
+ task :install_gem_no_doc => [:clean, :package] do
26
+ sh "#{'sudo ' unless Hoe::WINDOZE }gem install pkg/*.gem --no-rdoc --no-ri"
27
+ end
28
+
29
+ namespace :manifest do
30
+ desc 'Recreate Manifest.txt to include ALL files'
31
+ task :refresh do
32
+ `rake check_manifest | patch -p0 > Manifest.txt`
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ task :ruby_env do
2
+ RUBY_APP = if RUBY_PLATFORM =~ /java/
3
+ "jruby"
4
+ else
5
+ "ruby"
6
+ end unless defined? RUBY_APP
7
+ end
@@ -0,0 +1,17 @@
1
+ desc 'Generate website files'
2
+ task :website_generate => :ruby_env do
3
+ (Dir['website/**/*.txt'] - Dir['website/version*.txt']).each do |txt|
4
+ sh %{ #{RUBY_APP} script/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
5
+ end
6
+ end
7
+
8
+ desc 'Upload website files to rubyforge'
9
+ task :website_upload do
10
+ host = "#{rubyforge_username}@rubyforge.org"
11
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
12
+ local_dir = 'website'
13
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
14
+ end
15
+
16
+ desc 'Generate and upload website files'
17
+ task :website => [:website_generate, :website_upload, :publish_docs]
data/test/helper.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ gem 'mocha'
5
+ require 'mocha'
6
+
7
+ RDF_LIB = File.join(File.dirname(__FILE__), '..', 'lib') unless defined?(RDF_LIB)
8
+ $:.unshift(RDF_LIB) unless $:.include?(RDF_LIB)
9
+
10
+ TEST_LIB = File.join(File.dirname(__FILE__)) unless defined?(TEST_LIB)
11
+ $:.unshift(TEST_LIB) unless $:.include?(TEST_LIB)
12
+
13
+ require 'rubyrdf'
14
+ RDF::Namespace.register(:ex, 'http://example.org/')
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/helper.rb'
2
+
3
+ class TestBlankNode < Test::Unit::TestCase
4
+ def setup
5
+ @graph = RDF::Graph::Memory.new
6
+ end
7
+
8
+ def test_new_should_initialize_graph
9
+ assert_equal @graph, RDF::BlankNode.new('test', @graph).graph
10
+ end
11
+
12
+ def test_new_should_raise_error_for_nil_graph
13
+ assert_raise(ArgumentError) {
14
+ RDF::BlankNode.new('test')
15
+ }
16
+ end
17
+
18
+ def test_new_should_initialize_name
19
+ assert_equal 'test', RDF::BlankNode.new('test', @graph).name
20
+ end
21
+
22
+ def test_new_should_raise_error_for_empty_name
23
+ assert_raise(ArgumentError) {
24
+ RDF::BlankNode.new(nil, @graph)
25
+ }
26
+ end
27
+
28
+ def test_equal_should_be_true_for_same_graph_and_name
29
+ assert_equal RDF::BlankNode.new('test', @graph), RDF::BlankNode.new('test', @graph)
30
+ end
31
+
32
+ def test_equal_should_be_false_for_same_graph_and_different_name
33
+ assert_not_equal RDF::BlankNode.new('test', @graph), RDF::BlankNode.new('test2', @graph)
34
+ end
35
+
36
+ def test_equal_should_be_false_for_different_graph_and_same_name
37
+ assert_not_equal RDF::BlankNode.new('test', @graph), RDF::BlankNode.new('test', RDF::Graph::Memory.new)
38
+ end
39
+
40
+ def test_hash_should_be_equal_for_same_graph_and_name
41
+ assert_equal RDF::BlankNode.new('test', @graph).hash, RDF::BlankNode.new('test', @graph).hash
42
+ end
43
+
44
+ def test_hash_should_not_be_equal_for_same_graph_and_different_name
45
+ assert_not_equal RDF::BlankNode.new('test', @graph).hash, RDF::BlankNode.new('test2', @graph).hash
46
+ end
47
+
48
+ def test_hash_should_not_be_equal_for_different_graph_and_same_name
49
+ assert_not_equal RDF::BlankNode.new('test', @graph).hash, RDF::BlankNode.new('test', RDF::Graph::Memory.new).hash
50
+ end
51
+
52
+ def test_eql_should_be_true_for_same_graph_and_name
53
+ assert RDF::BlankNode.new('test', @graph).eql?(RDF::BlankNode.new('test', @graph))
54
+ end
55
+
56
+ def test_eql_should_be_false_for_same_graph_and_different_name
57
+ assert !RDF::BlankNode.new('test', @graph).eql?(RDF::BlankNode.new('test2', @graph))
58
+ end
59
+
60
+ def test_eql_should_be_false_for_different_graph_and_same_name
61
+ assert !RDF::BlankNode.new('test', @graph).eql?(RDF::BlankNode.new('test', RDF::Graph::Memory.new))
62
+ end
63
+
64
+ def test_to_s_should_be_ntriples_format
65
+ assert_equal "_:a", RDF::BlankNode.new('a', @graph).to_s
66
+ end
67
+ end
@@ -0,0 +1,247 @@
1
+ require File.dirname(__FILE__) + '/helper.rb'
2
+
3
+ class TestFormatNTriples < Test::Unit::TestCase
4
+ def setup
5
+ @graph = RDF::Graph::Memory.new
6
+ end
7
+
8
+ def test_import_comment_line
9
+ RDF::Format::NTriples.import('# comment lines', @graph)
10
+ assert @graph.empty?
11
+ end
12
+
13
+ def test_import_comment_line_after_whitespace
14
+ RDF::Format::NTriples.import(" \t \t # comment line after whitespace", @graph)
15
+ assert @graph.empty?
16
+ end
17
+
18
+ def test_import_empty_blank_line
19
+ RDF::Format::NTriples.import('', @graph)
20
+ assert @graph.empty?
21
+ end
22
+
23
+ def test_import_blank_line_with_tabs_and_spaces
24
+ RDF::Format::NTriples.import(" \t", @graph)
25
+ assert @graph.empty?
26
+ end
27
+
28
+ def test_import_normal_triple
29
+ RDF::Format::NTriples.import('<http://example.org/resource1> <http://example.org/property> <http://example.org/resource2> .', @graph)
30
+ assert @graph.include?(EX::resource1, EX::property, EX::resource2)
31
+ end
32
+
33
+ def test_import_should_create_graph
34
+ assert RDF::Format::NTriples.import('<http://example.org/resource1> <http://example.org/property> <http://example.org/resource2> .').is_a?(RDF::Graph::Memory)
35
+ end
36
+
37
+ def test_import_triple_with_blank_subject
38
+ RDF::Format::NTriples.import('_:anon <http://example.org/property> <http://example.org/resource2> .', @graph)
39
+ assert @graph.include?(RDF::BlankNode.new('anon', @graph), EX::property, EX::resource2)
40
+ end
41
+
42
+ def test_import_triple_with_blank_object
43
+ RDF::Format::NTriples.import('<http://example.org/resource2> <http://example.org/property> _:anon .', @graph)
44
+ assert @graph.include?(EX::resource2, EX::property, RDF::BlankNode.new('anon', @graph))
45
+ end
46
+
47
+ def test_import_triple_with_spaces_and_tabs_throughout
48
+ RDF::Format::NTriples.import(" \t <http://example.org/resource3> \t <http://example.org/property>\t <http://example.org/resource2> \t.\t ", @graph)
49
+ assert @graph.include?(EX::resource3, EX::property, EX::resource2)
50
+ end
51
+
52
+ def test_import_triple_with_cr_nl_line_ending
53
+ RDF::Format::NTriples.import("<http://example.org/resource4> <http://example.org/property> <http://example.org/resource2> .\15\12", @graph)
54
+ assert @graph.include?(EX::resource4, EX::property, EX::resource2)
55
+ end
56
+
57
+ def test_import_two_lines_separated_by_cr
58
+ RDF::Format::NTriples.import("<http://example.org/resource5> <http://example.org/property> <http://example.org/resource2> .\15<http://example.org/resource6> <http://example.org/property> <http://example.org/resource2> .", @graph)
59
+ assert @graph.include?(EX::resource5, EX::property, EX::resource2)
60
+ assert @graph.include?(EX::resource6, EX::property, EX::resource2)
61
+ end
62
+
63
+ def test_import_simple_literal
64
+ RDF::Format::NTriples.import('<http://example.org/resource7> <http://example.org/property> "simple literal" .', @graph)
65
+ assert @graph.include?(EX::resource7, EX::property, RDF::PlainLiteralNode.new('simple literal'))
66
+ end
67
+
68
+ def test_import_literal_with_backslashed_backslash
69
+ RDF::Format::NTriples.import('<http://example.org/resource8> <http://example.org/property> "backslash:\\\\" .', @graph)
70
+ assert @graph.include?(EX::resource8, EX::property, RDF::PlainLiteralNode.new('backslash:\\'))
71
+ end
72
+
73
+ def test_import_literal_with_backslashed_quote
74
+ RDF::Format::NTriples.import('<http://example.org/resource9> <http://example.org/property> "dquote:\"" .', @graph)
75
+ assert @graph.include?(EX::resource9, EX::property, RDF::PlainLiteralNode.new('dquote:"'))
76
+ end
77
+
78
+ def test_import_literal_with_backslashed_n
79
+ RDF::Format::NTriples.import('<http://example.org/resource10> <http://example.org/property> "newline:\\n" .', @graph)
80
+ assert @graph.include?(EX::resource10, EX::property, RDF::PlainLiteralNode.new("newline:\n"))
81
+ end
82
+
83
+ def test_import_literal_with_backslashed_r
84
+ RDF::Format::NTriples.import('<http://example.org/resource11> <http://example.org/property> "return\\r" .', @graph)
85
+ assert @graph.include?(EX::resource11, EX::property, RDF::PlainLiteralNode.new("return\r"))
86
+ end
87
+
88
+ def test_import_literal_with_backslashed_t
89
+ RDF::Format::NTriples.import('<http://example.org/resource12> <http://example.org/property> "tab:\\t" .', @graph)
90
+ assert @graph.include?(EX::resource12, EX::property, RDF::PlainLiteralNode.new("tab:\t"))
91
+ end
92
+
93
+ def test_import_triple_with_uri_and_no_space_before_period
94
+ RDF::Format::NTriples.import('<http://example.org/resource13> <http://example.org/property> <http://example.org/resource2>.', @graph)
95
+ assert @graph.include?(EX::resource13, EX::property, EX::resource2)
96
+ end
97
+
98
+ def test_import_triple_with_literal_and_no_space_before_period
99
+ RDF::Format::NTriples.import('<http://example.org/resource14> <http://example.org/property> "x".', @graph)
100
+ assert @graph.include?(EX::resource14, EX::property, RDF::PlainLiteralNode.new('x'))
101
+ end
102
+
103
+ def test_import_triple_with_blank_node_and_no_space_before_period
104
+ RDF::Format::NTriples.import('<http://example.org/resource15> <http://example.org/property> _:anon.', @graph)
105
+ assert @graph.include?(EX::resource15, EX::property, RDF::BlankNode.new('anon', @graph))
106
+ end
107
+
108
+ def test_import_triple_with_u_escape
109
+ # latin small letter e with acute symbol \u00E9 - 2 UTF-8 bytes #C3 #A9
110
+ RDF::Format::NTriples.import('<http://example.org/resource16> <http://example.org/property> "\u00E9" .', @graph)
111
+ assert @graph.include?(EX::resource16, EX::property, RDF::PlainLiteralNode.new("\xC3\xA9"))
112
+ end
113
+
114
+ def test_import_triple_with_three_byte_u_escape
115
+ # Euro symbol \u20ac - 3 UTF-8 bytes #xE2 #x82 #xAC
116
+ RDF::Format::NTriples.import('<http://example.org/resource17> <http://example.org/property> "\u20AC" .', @graph)
117
+ assert @graph.include?(EX::resource17, EX::property, RDF::PlainLiteralNode.new("\xE2\x82\xAC"))
118
+ end
119
+
120
+ def test_import_triple_with_cap_u_escape
121
+ # ? \U1ABDC - 4 UTF-8 bytes #xF0 #x9A #xAF #x8D
122
+ RDF::Format::NTriples.import('<http://example.org/resource18> <http://example.org/property> "\U1ABCD" .', @graph)
123
+ assert @graph.include?(EX::resource18, EX::property, RDF::PlainLiteralNode.new("\xF0\x9A\xAF\x8D"))
124
+ end
125
+
126
+ def test_import_blank_xml_literal
127
+ RDF::Format::NTriples.import('<http://example.org/resource21> <http://example.org/property> ""^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .', @graph)
128
+ assert @graph.include?(EX::resource21, EX::property, RDF::TypedLiteralNode.new('', RDFS::XMLLiteral))
129
+ end
130
+
131
+ def test_import_space_xml_literal
132
+ RDF::Format::NTriples.import('<http://example.org/resource22> <http://example.org/property> " "^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .', @graph)
133
+ assert @graph.include?(EX::resource22, EX::property, RDF::TypedLiteralNode.new(' ', RDFS::XMLLiteral))
134
+ end
135
+
136
+ def test_import_x_xml_literal
137
+ RDF::Format::NTriples.import('<http://example.org/resource23> <http://example.org/property> "x"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .', @graph)
138
+ assert @graph.include?(EX::resource23, EX::property, RDF::TypedLiteralNode.new('x', RDFS::XMLLiteral))
139
+ end
140
+
141
+ def test_import_backslashed_quote_xml_literal
142
+ RDF::Format::NTriples.import('<http://example.org/resource23> <http://example.org/property> "\""^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .', @graph)
143
+ assert @graph.include?(EX::resource23, EX::property, RDF::TypedLiteralNode.new('"', RDFS::XMLLiteral))
144
+ end
145
+
146
+ def test_import_well_formed_xml_literal
147
+ RDF::Format::NTriples.import('<http://example.org/resource24> <http://example.org/property> "<a></a>"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .', @graph)
148
+ assert @graph.include?(EX::resource24, EX::property, RDF::TypedLiteralNode.new('<a></a>', RDFS::XMLLiteral))
149
+ end
150
+
151
+ def test_import_well_formed_xml_literal_text_before
152
+ RDF::Format::NTriples.import('<http://example.org/resource25> <http://example.org/property> "a <b></b>"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .', @graph)
153
+ assert @graph.include?(EX::resource25, EX::property, RDF::TypedLiteralNode.new('a <b></b>', RDFS::XMLLiteral))
154
+ end
155
+
156
+ def test_import_well_formed_xml_literal_text_before_and_after
157
+ RDF::Format::NTriples.import('<http://example.org/resource26> <http://example.org/property> "a <b></b> c"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .', @graph)
158
+ assert @graph.include?(EX::resource26, EX::property, RDF::TypedLiteralNode.new('a <b></b> c', RDFS::XMLLiteral))
159
+ end
160
+
161
+ def test_import_well_formed_xml_literal_text_and_newline_before_and_after
162
+ RDF::Format::NTriples.import('<http://example.org/resource26> <http://example.org/property> "a\n<b></b>\nc"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .', @graph)
163
+ assert @graph.include?(EX::resource26, EX::property, RDF::TypedLiteralNode.new("a\n<b></b>\nc", RDFS::XMLLiteral))
164
+ end
165
+
166
+ def test_import_text_typed_as_xml_literal
167
+ RDF::Format::NTriples.import('<http://example.org/resource27> <http://example.org/property> "chat"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .', @graph)
168
+ assert @graph.include?(EX::resource27, EX::property, RDF::TypedLiteralNode.new('chat', RDFS::XMLLiteral))
169
+ end
170
+
171
+ def test_import_plain_literal_with_french_language_tag
172
+ RDF::Format::NTriples.import('<http://example.org/resource30> <http://example.org/property> "chat"@fr .', @graph)
173
+ assert @graph.include?(EX::resource30, EX::property, RDF::PlainLiteralNode.new('chat', 'fr'))
174
+ end
175
+
176
+ def test_import_plain_literal_with_english_language_tag
177
+ RDF::Format::NTriples.import('<http://example.org/resource31> <http://example.org/property> "chat"@en .', @graph)
178
+ assert @graph.include?(EX::resource31, EX::property, RDF::PlainLiteralNode.new('chat', 'en'))
179
+ end
180
+
181
+ def test_import_typed_literal
182
+ RDF::Format::NTriples.import('<http://example.org/resource32> <http://example.org/property> "abc"^^<http://example.org/datatype1> .', @graph)
183
+ assert @graph.include?(EX::resource32, EX::property, RDF::TypedLiteralNode.new('abc', EX::datatype1))
184
+ end
185
+
186
+ def test_export_simple_triple
187
+ @graph.add(EX::a, EX::b, EX::c)
188
+ io = StringIO.new('')
189
+ RDF::Format::NTriples.export(@graph, io)
190
+ assert_equal "<http://example.org/a> <http://example.org/b> <http://example.org/c>.\n", io.string
191
+ end
192
+
193
+ def test_export_triple_with_blank_subject
194
+ @graph.add(@graph.new_blank_node('a'), EX::b, EX::c)
195
+ io = StringIO.new('')
196
+ RDF::Format::NTriples.export(@graph, io)
197
+ assert_equal "_:a <http://example.org/b> <http://example.org/c>.\n", io.string
198
+ end
199
+
200
+ def test_export_triple_with_blank_object
201
+ @graph.add(EX::a, EX::b, @graph.new_blank_node('c'))
202
+ io = StringIO.new('')
203
+ RDF::Format::NTriples.export(@graph, io)
204
+ assert_equal "<http://example.org/a> <http://example.org/b> _:c.\n", io.string
205
+ end
206
+
207
+ def test_export_triple_with_plain_literal_object
208
+ @graph.add(EX::a, EX::b, RDF::PlainLiteralNode.new('test'))
209
+ io = StringIO.new('')
210
+ RDF::Format::NTriples.export(@graph, io)
211
+ assert_equal "<http://example.org/a> <http://example.org/b> \"test\".\n", io.string
212
+ end
213
+
214
+ def test_export_triple_with_plain_literal_object_with_language_tag
215
+ @graph.add(EX::a, EX::b, RDF::PlainLiteralNode.new('test', 'en'))
216
+ io = StringIO.new('')
217
+ RDF::Format::NTriples.export(@graph, io)
218
+ assert_equal "<http://example.org/a> <http://example.org/b> \"test\"@en.\n", io.string
219
+ end
220
+
221
+ def test_export_triple_with_typed_literal_object
222
+ @graph.add(EX::a, EX::b, RDF::TypedLiteralNode.new('test', EX::d))
223
+ io = StringIO.new('')
224
+ RDF::Format::NTriples.export(@graph, io)
225
+ assert_equal "<http://example.org/a> <http://example.org/b> \"test\"^^<http://example.org/d>.\n", io.string
226
+ end
227
+
228
+ def test_import_node_should_parse_blank_node
229
+ assert_equal RDF::BlankNode.new('x', @graph), RDF::Format::NTriples.import_node('_:x', @graph)
230
+ end
231
+
232
+ def test_import_node_should_parse_uri_node
233
+ assert_equal RDF::UriNode.new('http://paul.stadig.name/'), RDF::Format::NTriples.import_node('<http://paul.stadig.name/>')
234
+ end
235
+
236
+ def test_import_node_should_parse_plain_literal_node
237
+ assert_equal RDF::PlainLiteralNode.new('test'), RDF::Format::NTriples.import_node('"test"')
238
+ end
239
+
240
+ def test_import_node_should_parse_plain_literal_node_with_language_tag
241
+ assert_equal RDF::PlainLiteralNode.new('test', 'en'), RDF::Format::NTriples.import_node('"test"@en')
242
+ end
243
+
244
+ def test_import_node_should_parse_typed_literal_node
245
+ assert_equal RDF::TypedLiteralNode.new('test', EX::a), RDF::Format::NTriples.import_node("\"test\"^^<#{EX::a.uri}>")
246
+ end
247
+ end
@@ -0,0 +1,71 @@
1
+ require File.dirname(__FILE__) + '/helper.rb'
2
+
3
+ class TestGraphBase < Test::Unit::TestCase
4
+ def setup
5
+ @graph = RDF::Graph::Base.new
6
+ end
7
+
8
+ ##== export ==##
9
+ def test_export_should_use_string_io_for_nil_file
10
+ RDF::Format::NTriples.expects(:export).with(@graph, is_a(StringIO))
11
+ StringIO.any_instance.expects(:string)
12
+ @graph.export(:ntriples)
13
+ end
14
+
15
+ def test_export_should_default_to_ntriples
16
+ RDF::Format::NTriples.expects(:export)
17
+ @graph.stubs(:each)
18
+ @graph.export
19
+ end
20
+
21
+ def test_export_should_raise_unsupported_format_error
22
+ assert_raises(RDF::UnsupportedFormatError) do
23
+ @graph.export(:bogey)
24
+ end
25
+ end
26
+
27
+ ##== import ==##
28
+ def test_import_should_raise_error_for_nil_file
29
+ assert_raises(ArgumentError) do
30
+ @graph.import(nil)
31
+ end
32
+ end
33
+
34
+ def test_import_should_default_to_ntriples
35
+ RDF::Format::NTriples.expects(:import).with(anything, @graph)
36
+ @graph.import(stub())
37
+ end
38
+
39
+ def test_import_should_raise_unsupported_format_error
40
+ assert_raises(RDF::UnsupportedFormatError) do
41
+ @graph.import(stub(), :bogey)
42
+ end
43
+ end
44
+
45
+ ##== include? ==##
46
+ def test_include_should_raise_error_for_symbol_subject
47
+ assert_raises(ArgumentError) do
48
+ @graph.include?(:a, EX::b, EX::c)
49
+ end
50
+ end
51
+
52
+ def test_include_should_be_false_for_unassociated_bnode_subject
53
+ assert !@graph.include?(RDF::Graph::Memory.new.new_blank_node('a'), EX::b, EX::c)
54
+ end
55
+
56
+ def test_include_should_raise_error_for_symbol_predicate
57
+ assert_raises(ArgumentError) do
58
+ @graph.include?(EX::a, :a, EX::c)
59
+ end
60
+ end
61
+
62
+ def test_include_should_raise_error_for_symbol_object
63
+ assert_raises(ArgumentError) do
64
+ @graph.include?(EX::a, EX::b, :a)
65
+ end
66
+ end
67
+
68
+ def test_include_should_be_false_for_unassociated_bnode_object
69
+ assert !@graph.include?(EX::a, EX::b, RDF::Graph::Memory.new.new_blank_node('a'))
70
+ end
71
+ end
@@ -0,0 +1,146 @@
1
+ require File.dirname(__FILE__) + '/helper.rb'
2
+ require 'rdf/graph/tests'
3
+
4
+ class TestGraphMemory < Test::Unit::TestCase
5
+ include RDF::Graph::Tests
6
+
7
+ def setup
8
+ @graph = RDF::Graph::Memory.new
9
+ end
10
+
11
+ ##== match ==##
12
+ def test_match_should_return_result
13
+ assert @graph.match(EX::a, EX::b, EX::c).is_a?(RDF::Query::Result)
14
+ end
15
+
16
+ def test_match_should_succeed
17
+ @graph.add(EX::a, EX::b, EX::c)
18
+ assert @graph.match(EX::a, EX::b, EX::c).success?
19
+ end
20
+
21
+ def test_match_should_succeed_with_blank_node_subject_when_in_graph
22
+ bn = @graph.new_blank_node('x')
23
+ @graph.add(bn, EX::b, EX::c)
24
+ assert @graph.match(bn, EX::b, EX::c).success?
25
+ end
26
+
27
+ def test_match_should_not_bind_blank_node_subject_when_in_graph
28
+ bn = @graph.new_blank_node('x')
29
+ @graph.add(bn, EX::b, EX::c)
30
+ assert @graph.match(bn, EX::b, EX::c).bindings.empty?
31
+ end
32
+
33
+ def test_match_should_succeed_with_blank_node_subject_when_not_in_graph
34
+ @graph.add(EX::a, EX::b, EX::c)
35
+ bn = @graph.new_blank_node('x')
36
+ assert EX::a, @graph.match(bn, EX::b, EX::c).success?
37
+ end
38
+
39
+ def test_match_should_bind_blank_node_subject_when_not_in_graph
40
+ @graph.add(EX::a, EX::b, EX::c)
41
+ bn = @graph.new_blank_node('x')
42
+ assert_equal EX::a, @graph.match(bn, EX::b, EX::c).bindings.to_a.first[bn]
43
+ end
44
+
45
+ def test_match_should_succeed_with_blank_node_object_when_in_graph
46
+ bn = @graph.new_blank_node('x')
47
+ @graph.add(bn, EX::b, EX::c)
48
+ assert @graph.match(bn, EX::b, EX::c).success?
49
+ end
50
+
51
+ def test_match_should_not_bind_blank_node_object_when_in_graph
52
+ bn = @graph.new_blank_node('x')
53
+ @graph.add(EX::a, EX::b, bn)
54
+ assert @graph.match(EX::a, EX::b, bn).bindings.empty?
55
+ end
56
+
57
+ def test_match_should_succeed_with_blank_node_object_when_not_in_graph
58
+ @graph.add(EX::a, EX::b, EX::c)
59
+ bn = @graph.new_blank_node('x')
60
+ assert EX::c, @graph.match(EX::a, EX::b, bn).success?
61
+ end
62
+
63
+ def test_match_should_bind_blank_node_object_when_not_in_graph
64
+ @graph.add(EX::a, EX::b, EX::c)
65
+ bn = @graph.new_blank_node('x')
66
+ assert_equal EX::c, @graph.match(EX::a, EX::b, bn).bindings.to_a.first[bn]
67
+ end
68
+
69
+ def test_match_should_succeed_with_variable_subject
70
+ @graph.add(EX::a, EX::b, EX::c)
71
+ assert @graph.match(:x, EX::b, EX::c).success?
72
+ end
73
+
74
+ def test_match_should_bind_variable_subject
75
+ @graph.add(EX::a, EX::b, EX::c)
76
+ assert_equal EX::a, @graph.match(:x, EX::b, EX::c).bindings.to_a.first[:x]
77
+ end
78
+
79
+ def test_match_should_fail_with_bound_subject_not_in_graph
80
+ @graph.add(EX::a, EX::b, EX::c)
81
+ result = RDF::Query::Result.new
82
+ result.bindings << RDF::Query::Binding.new(:x => EX::d)
83
+ assert @graph.match(:x, EX::b, EX::c, result).failure?
84
+ end
85
+
86
+ def test_match_should_bind_predicate_with_bound_subject
87
+ @graph.add(EX::a, EX::b, EX::c)
88
+ result = RDF::Query::Result.new
89
+ result.bindings << RDF::Query::Binding.new(:x => EX::a)
90
+ assert_equal EX::b, @graph.match(:x, :y, EX::c, result).bindings.to_a.first[:y]
91
+ end
92
+
93
+ def test_match_should_fail_with_bound_predicate_not_in_graph
94
+ @graph.add(EX::a, EX::b, EX::c)
95
+ result = RDF::Query::Result.new
96
+ result.bindings << RDF::Query::Binding.new(:x => EX::d)
97
+ assert @graph.match(EX::a, :x, EX::c, result).failure?
98
+ end
99
+
100
+ def test_match_should_fail_with_bound_object_not_in_graph
101
+ @graph.add(EX::a, EX::b, EX::c)
102
+ result = RDF::Query::Result.new
103
+ result.bindings << RDF::Query::Binding.new(:x => EX::d)
104
+ assert @graph.match(EX::a, EX::b, :x, result).failure?
105
+ end
106
+
107
+ def test_match_should_bind_variable_predicate
108
+ @graph.add(EX::a, EX::b, EX::c)
109
+ assert_equal EX::b, @graph.match(EX::a, :x, EX::c).bindings.to_a.first[:x]
110
+ end
111
+
112
+ def test_match_should_bind_variable_object
113
+ @graph.add(EX::a, EX::b, EX::c)
114
+ assert_equal EX::c, @graph.match(EX::a, EX::b, :x).bindings.to_a.first[:x]
115
+ end
116
+
117
+ def test_match_should_fail
118
+ @graph.add(EX::a, EX::b, EX::d)
119
+ assert @graph.match(EX::a, EX::b, EX::c).failure?
120
+ end
121
+
122
+ def test_match_should_fail_when_empty
123
+ assert @graph.match(EX::a, EX::b, EX::c).failure?
124
+ end
125
+
126
+ def test_match_should_not_find_deleted_triple
127
+ @graph.add(EX::a, EX::b, EX::c)
128
+ @graph.delete(EX::a, EX::b, EX::c)
129
+ assert @graph.match(@graph.new_blank_node('x'), EX::b, @graph.new_blank_node('z')).failure?
130
+ end
131
+
132
+ def test_match_should_bind_with_existing_subject_and_non_existent_predicate
133
+ @graph.add(EX::a, EX::d, EX::e)
134
+ assert @graph.match(@graph.new_blank_node(UUID.timestamp_create), EX::b, @graph.new_blank_node('x')).failure?
135
+ end
136
+
137
+ ##== delete ==##
138
+ def test_delete_should_not_leak_memory
139
+ t = RDF::Triple.new(@graph.new_blank_node('x'), EX::b, @graph.new_blank_node('y'))
140
+ @graph.add(t)
141
+ @graph.delete(t)
142
+ assert @graph.instance_variable_get('@idx_s').empty?
143
+ assert @graph.instance_variable_get('@idx_sp').empty?
144
+ assert @graph.instance_variable_get('@idx_blank').empty?
145
+ end
146
+ end