rdf 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rdf/spec.rb ADDED
@@ -0,0 +1,138 @@
1
+ require 'spec'
2
+
3
+ module RDF
4
+ ##
5
+ # RDF extensions for RSpec.
6
+ #
7
+ # @see http://rspec.info/
8
+ module Spec
9
+ ##
10
+ # RDF matchers for RSpec.
11
+ #
12
+ # @see http://rspec.rubyforge.org/rspec/1.2.9/classes/Spec/Matchers.html
13
+ module Matchers
14
+ ##
15
+ # Defines a new RSpec matcher.
16
+ #
17
+ # @param [Symbol] name
18
+ # @return [void]
19
+ def self.define(name, &declarations)
20
+ define_method name do |*expected|
21
+ ::Spec::Matchers::Matcher.new(name, *expected, &declarations)
22
+ end
23
+ end
24
+
25
+ define :be_a_statement do
26
+ match do |statement|
27
+ statement.should be_instance_of(RDF::Statement)
28
+ statement.subject.should be_a_kind_of(RDF::Resource)
29
+ statement.predicate.should be_a_kind_of(RDF::URI)
30
+ statement.object.should be_a_kind_of(RDF::Value) unless statement.object.is_a?(String) # FIXME
31
+ true
32
+ end
33
+ end
34
+
35
+ define :be_a_triple do
36
+ match do |triple|
37
+ triple.should be_instance_of(Array)
38
+ triple.size.should == 3
39
+ triple[0].should be_a_kind_of(RDF::Resource)
40
+ triple[1].should be_a_kind_of(RDF::URI)
41
+ triple[2].should be_a_kind_of(RDF::Value) unless triple[2].is_a?(String) # FIXME
42
+ true
43
+ end
44
+ end
45
+
46
+ define :be_a_quad do
47
+ match do |quad|
48
+ quad.should be_instance_of(Array)
49
+ quad.size.should == 4
50
+ quad[0].should be_a_kind_of(RDF::Resource)
51
+ quad[1].should be_a_kind_of(RDF::URI)
52
+ quad[2].should be_a_kind_of(RDF::Value) unless quad[2].is_a?(String) # FIXME
53
+ quad[3].should be_a_kind_of(RDF::Resource) unless quad[3].nil?
54
+ true
55
+ end
56
+ end
57
+
58
+ define :be_a_resource do
59
+ match do |value|
60
+ value.should be_a_kind_of(RDF::Resource)
61
+ true
62
+ end
63
+ end
64
+
65
+ define :be_a_uri do
66
+ match do |value|
67
+ value.should be_a_kind_of(RDF::URI)
68
+ true
69
+ end
70
+ end
71
+
72
+ define :be_a_value do
73
+ match do |value|
74
+ value.should be_a_kind_of(RDF::Value) unless value.is_a?(String) # FIXME
75
+ true
76
+ end
77
+ end
78
+
79
+ define :be_a_vocabulary do |base_uri|
80
+ match do |vocabulary|
81
+ vocabulary.should be_a_kind_of(Module)
82
+ vocabulary.should respond_to(:to_uri)
83
+ vocabulary.to_uri.to_s.should == base_uri
84
+ vocabulary.should respond_to(:[])
85
+ true
86
+ end
87
+ end
88
+
89
+ define :have_properties do |base_uri, properties|
90
+ match do |vocabulary|
91
+ properties.map(&:to_sym).each do |property|
92
+ vocabulary[property].should be_a_uri
93
+ vocabulary[property].to_s.should == "#{base_uri}#{property}"
94
+ #vocabulary.should respond_to(property) # FIXME
95
+ lambda { vocabulary.send(property) }.should_not raise_error
96
+ vocabulary.send(property).should be_a_uri
97
+ vocabulary.send(property.to_s).should be_a_uri
98
+ vocabulary.send(property).to_s.should == "#{base_uri}#{property}"
99
+ end
100
+ true
101
+ end
102
+ end
103
+
104
+ define :have_subclasses do |base_uri, klasses|
105
+ match do |vocabulary|
106
+ klasses.map(&:to_sym).each do |klass|
107
+ # TODO
108
+ end
109
+ true
110
+ end
111
+ end
112
+
113
+ define :be_a_repository do
114
+ match do |repository|
115
+ repository.should be_a_kind_of(RDF::Repository)
116
+ true
117
+ end
118
+ end
119
+
120
+ define :be_a_repository_of_size do |size|
121
+ match do |repository|
122
+ repository.should be_a_repository
123
+ repository.size == size
124
+ end
125
+ end
126
+
127
+ define :have_predicate do |predicate, count|
128
+ match do |queryable|
129
+ if count.nil?
130
+ queryable.has_predicate?(predicate)
131
+ else
132
+ queryable.query([nil, predicate, nil]).size == count
133
+ end
134
+ end
135
+ end
136
+ end # module Matchers
137
+ end # module Spec
138
+ end # module RDF
@@ -0,0 +1,248 @@
1
+ require 'rdf/spec'
2
+
3
+ share_as :RDF_Enumerable do
4
+ include RDF::Spec::Matchers
5
+
6
+ before :each do
7
+ raise '+@enumerable+ must be defined in a before(:each) block' unless instance_variable_get('@enumerable')
8
+ raise '+@statements+ must be defined in a before(:each) block' unless instance_variable_get('@statements')
9
+ end
10
+
11
+ it "should support #empty?" do
12
+ @enumerable.respond_to?(:empty?).should be_true
13
+
14
+ ([].extend(RDF::Enumerable)).empty?.should be_true
15
+ @enumerable.empty?.should be_false
16
+ end
17
+
18
+ it "should support #count and #size" do
19
+ [:count, :size, :length].each do |method|
20
+ @enumerable.respond_to?(method).should be_true
21
+
22
+ @enumerable.send(method).should == @statements.size
23
+ end
24
+ end
25
+
26
+ context "statements" do
27
+ it "should support #statements" do
28
+ @enumerable.respond_to?(:statements).should be_true
29
+
30
+ @enumerable.statements.should be_instance_of(Array)
31
+ @enumerable.statements.size.should == @statements.size
32
+ @enumerable.statements.each { |statement| statement.should be_a_statement }
33
+ end
34
+
35
+ it "should support #has_statement?" do
36
+ @enumerable.respond_to?(:has_statement?).should be_true
37
+
38
+ @statements.each do |statement|
39
+ @enumerable.has_statement?(statement).should be_true
40
+ end
41
+
42
+ @enumerable.has_statement?([RDF::Node.new, RDF::URI.new("http://example.org/unknown"), RDF::Node.new]).should be_false
43
+ end
44
+
45
+ it "should support #each_statement" do
46
+ @enumerable.respond_to?(:each_statement).should be_true
47
+
48
+ @enumerable.each_statement.should be_instance_of(Enumerable::Enumerator)
49
+ @enumerable.each_statement { |statement| statement.should be_a_statement }
50
+ end
51
+
52
+ it "should support #enum_statement" do
53
+ @enumerable.respond_to?(:enum_statement).should be_true
54
+
55
+ @enumerable.enum_statement.should be_instance_of(Enumerable::Enumerator)
56
+ end
57
+ end
58
+
59
+ context "triples" do
60
+ it "should support #triples" do
61
+ @enumerable.respond_to?(:triples).should be_true
62
+
63
+ @enumerable.triples.should be_instance_of(Array)
64
+ @enumerable.triples.size.should == @statements.size
65
+ @enumerable.triples.each { |triple| triple.should be_a_triple }
66
+ end
67
+
68
+ it "should support #has_triple?" do
69
+ @enumerable.respond_to?(:has_triple?).should be_true
70
+
71
+ @statements.each do |statement|
72
+ @enumerable.has_triple?(statement.to_triple).should be_true
73
+ end
74
+ end
75
+
76
+ it "should support #each_triple" do
77
+ @enumerable.respond_to?(:each_triple).should be_true
78
+
79
+ @enumerable.each_triple.should be_instance_of(Enumerable::Enumerator)
80
+ @enumerable.each_triple { |*triple| triple.should be_a_triple }
81
+ end
82
+
83
+ it "should support #enum_triple" do
84
+ @enumerable.respond_to?(:enum_triple).should be_true
85
+
86
+ @enumerable.enum_triple.should be_instance_of(Enumerable::Enumerator)
87
+ end
88
+ end
89
+
90
+ context "quads" do
91
+ it "should support #quads" do
92
+ @enumerable.respond_to?(:quads).should be_true
93
+
94
+ @enumerable.quads.should be_instance_of(Array)
95
+ @enumerable.quads.size.should == @statements.size
96
+ @enumerable.quads.each { |quad| quad.should be_a_quad }
97
+ end
98
+
99
+ it "should support #has_quad?" do
100
+ @enumerable.respond_to?(:has_quad?).should be_true
101
+
102
+ @statements.each do |statement|
103
+ @enumerable.has_quad?(statement.to_quad).should be_true
104
+ end
105
+ end
106
+
107
+ it "should support #each_quad" do
108
+ @enumerable.respond_to?(:each_quad).should be_true
109
+
110
+ @enumerable.each_quad.should be_instance_of(Enumerable::Enumerator)
111
+ @enumerable.each_quad { |*quad| quad.should be_a_quad }
112
+ end
113
+
114
+ it "should support #enum_quad" do
115
+ @enumerable.respond_to?(:enum_quad).should be_true
116
+
117
+ @enumerable.enum_quad.should be_instance_of(Enumerable::Enumerator)
118
+ end
119
+ end
120
+
121
+ context "subjects" do
122
+ it "should support #subjects" do
123
+ @enumerable.respond_to?(:subjects).should be_true
124
+
125
+ @enumerable.subjects.should be_instance_of(Array)
126
+ @enumerable.subjects.each { |value| value.should be_a_resource }
127
+ end
128
+
129
+ it "should support #has_subject?" do
130
+ @enumerable.respond_to?(:has_subject?).should be_true
131
+
132
+ @statements.each do |statement|
133
+ @enumerable.has_subject?(statement.subject).should be_true
134
+ end
135
+ end
136
+
137
+ it "should support #each_subject" do
138
+ @enumerable.respond_to?(:each_subject).should be_true
139
+
140
+ @enumerable.each_subject.should be_instance_of(Enumerable::Enumerator)
141
+ @enumerable.each_subject { |value| value.should be_a_resource }
142
+ end
143
+
144
+ it "should support #enum_subject" do
145
+ @enumerable.respond_to?(:enum_subject).should be_true
146
+
147
+ @enumerable.enum_subject.should be_instance_of(Enumerable::Enumerator)
148
+ end
149
+ end
150
+
151
+ context "predicates" do
152
+ it "should support #predicates" do
153
+ @enumerable.respond_to?(:predicates).should be_true
154
+
155
+ @enumerable.predicates.should be_instance_of(Array)
156
+ @enumerable.predicates.each { |value| value.should be_a_uri }
157
+ end
158
+
159
+ it "should support #has_predicate?" do
160
+ @enumerable.respond_to?(:has_predicate?).should be_true
161
+
162
+ @statements.each do |statement|
163
+ @enumerable.has_predicate?(statement.predicate).should be_true
164
+ end
165
+ end
166
+
167
+ it "should support #each_predicate" do
168
+ @enumerable.respond_to?(:each_predicate).should be_true
169
+
170
+ @enumerable.each_predicate.should be_instance_of(Enumerable::Enumerator)
171
+ @enumerable.each_predicate { |value| value.should be_a_uri }
172
+ end
173
+
174
+ it "should support #enum_predicate" do
175
+ @enumerable.respond_to?(:enum_predicate).should be_true
176
+
177
+ @enumerable.enum_predicate.should be_instance_of(Enumerable::Enumerator)
178
+ end
179
+ end
180
+
181
+ context "objects" do
182
+ it "should support #objects" do
183
+ @enumerable.respond_to?(:objects).should be_true
184
+
185
+ @enumerable.objects.should be_instance_of(Array)
186
+ @enumerable.objects.each { |value| value.should be_a_value }
187
+ end
188
+
189
+ it "should support #has_object?" do
190
+ @enumerable.respond_to?(:has_object?).should be_true
191
+
192
+ @statements.each do |statement|
193
+ @enumerable.has_object?(statement.object).should be_true
194
+ end
195
+ end
196
+
197
+ it "should support #each_object" do
198
+ @enumerable.respond_to?(:each_object).should be_true
199
+
200
+ @enumerable.each_object.should be_instance_of(Enumerable::Enumerator)
201
+ @enumerable.each_object { |value| value.should be_a_value }
202
+ end
203
+
204
+ it "should support #enum_object" do
205
+ @enumerable.respond_to?(:enum_object).should be_true
206
+
207
+ @enumerable.enum_object.should be_instance_of(Enumerable::Enumerator)
208
+ end
209
+ end
210
+
211
+ context "contexts" do
212
+ it "should support #contexts" do
213
+ @enumerable.respond_to?(:contexts).should be_true
214
+
215
+ @enumerable.contexts.should be_instance_of(Array)
216
+ @enumerable.contexts.each { |value| value.should be_a_resource }
217
+ end
218
+
219
+ it "should support #has_context?" do
220
+ @enumerable.respond_to?(:has_context?).should be_true
221
+
222
+ @statements.each do |statement|
223
+ if statement.has_context?
224
+ @enumerable.has_context?(statement.context).should be_true
225
+ end
226
+ end
227
+ end
228
+
229
+ it "should support #each_context" do
230
+ @enumerable.respond_to?(:each_context).should be_true
231
+
232
+ @enumerable.each_context.should be_instance_of(Enumerable::Enumerator)
233
+ @enumerable.each_context { |value| value.should be_a_resource }
234
+ end
235
+
236
+ it "should support #enum_context" do
237
+ @enumerable.respond_to?(:enum_context).should be_true
238
+
239
+ @enumerable.enum_context.should be_instance_of(Enumerable::Enumerator)
240
+ end
241
+ end
242
+
243
+ it "should support #to_hash" do
244
+ @enumerable.respond_to?(:to_hash).should be_true
245
+
246
+ # TODO
247
+ end
248
+ end
@@ -0,0 +1,77 @@
1
+ require 'rdf/spec'
2
+
3
+ share_as :RDF_Repository do
4
+ include RDF::Spec::Matchers
5
+
6
+ before :each do
7
+ raise '+@repository+ must be defined in a before(:each) block' unless instance_variable_get('@repository')
8
+ @filename = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'etc', 'doap.nt'))
9
+ @statements = RDF::NTriples::Reader.new(File.open(@filename)).to_a
10
+ @enumerable = @repository
11
+ end
12
+
13
+ it "should be empty initially" do
14
+ @repository.empty?.should be_true
15
+ @repository.count.should be_zero
16
+ end
17
+
18
+ it "should be readable" do
19
+ @repository.readable?.should be_true
20
+ end
21
+
22
+ it "should be mutable" do
23
+ @repository.immutable?.should be_false
24
+ @repository.mutable?.should be_true
25
+ end
26
+
27
+ context "when inserting statements" do
28
+ it "should support #insert" do
29
+ @repository.should respond_to(:insert)
30
+ end
31
+
32
+ it "should not raise errors" do
33
+ lambda { @repository.insert(@statements.first) }.should_not raise_error
34
+ end
35
+
36
+ it "should support inserting one statement at a time" do
37
+ @repository.insert(@statements.first)
38
+ end
39
+
40
+ it "should support inserting multiple statements at a time" do
41
+ @repository.insert(*@statements)
42
+ end
43
+
44
+ it "should insert statements successfully" do
45
+ @repository.insert(*@statements)
46
+ @repository.count.should == @statements.size
47
+ end
48
+ end
49
+
50
+ context "when deleting statements" do
51
+ it "should support #delete" do
52
+ @repository.should respond_to(:delete)
53
+ end
54
+
55
+ it "should not raise errors"
56
+ it "should support deleting one statement at a time"
57
+ it "should support deleting multiple statements at a time"
58
+ it "should delete statements successfully"
59
+ end
60
+
61
+ context "when clearing all statements" do
62
+ it "should support #clear" do
63
+ @repository.should respond_to(:clear)
64
+ end
65
+ end
66
+
67
+ context "when enumerating statements" do
68
+ require 'rdf/spec/enumerable'
69
+
70
+ before :each do
71
+ @repository.insert(*@statements)
72
+ @enumerable = @repository
73
+ end
74
+
75
+ it_should_behave_like RDF_Enumerable
76
+ end
77
+ end