Skip to content

Commit 14a46bf

Browse files
committed
Release 1.1.5.
* Added `write` expectation, to test output to $stdout/$stderr.
2 parents b548f8b + dda1dc8 commit 14a46bf

File tree

11 files changed

+215
-148
lines changed

11 files changed

+215
-148
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.3
1+
1.1.5

lib/rdf/spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'rdf' # @see http://rubygems.org/gems/rdf
22
require 'rspec' # @see http://rubygems.org/gems/rspec
3+
require 'rspec/its'
34

45
module RDF
56
##

lib/rdf/spec/countable.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ module RDF_Countable
1010
@statements = RDF::Spec.quads
1111

1212
if @countable.empty?
13-
if @countable.respond_to?(:<<) && (@countable.writable? rescue true)
13+
if (@countable.writable? rescue false)
14+
@countable.insert_statements(@statements)
15+
elsif @countable.respond_to?(:<<)
1416
@statements.each { |statement| @countable << statement }
1517
else
1618
raise "+@countable+ must respond to #<< or be pre-populated with the statements in #{RDF::Spec::TRIPLES_FILE} in a before(:each) block"

lib/rdf/spec/enumerable.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ module RDF_Enumerable
1010
@statements ||= RDF::Spec.quads
1111

1212
if @enumerable.empty?
13-
if @enumerable.respond_to?(:<<) && (@enumerable.writable? rescue true)
13+
if (@enumerable.writable? rescue false)
14+
@enumerable.insert(*@statements)
15+
elsif @enumerable.respond_to?(:<<)
1416
@statements.each { |statement| @enumerable << statement }
1517
else
1618
raise "@enumerable must respond to #<< or be pre-populated with the statements in #{RDF::Spec::TRIPLES_FILE} in a before(:each) block"
@@ -439,7 +441,7 @@ module RDF_Enumerable
439441
subject {@enumerable.enum_graph}
440442
it {should be_an_enumerator}
441443
it {should be_countable}
442-
it "enumerates the same as #each_graph", :pending => "graph inclusion issue" do
444+
it "enumerates the same as #each_graph" do
443445
expect(subject.to_a).to include(*@enumerable.each_graph.to_a) if @supports_context # expect with match problematic
444446
end
445447
end

lib/rdf/spec/matchers.rb

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,16 @@ module Matchers
141141
end
142142
end
143143

144-
RSpec::Matchers.define :have_subclasses do |base_uri, klasses|
144+
RSpec::Matchers.define :have_terms do |base_uri, klasses|
145145
match do |vocabulary|
146146
klasses.map { |k| k.to_sym }.each do |klass|
147-
pending "checks that #{base_uri} has subClassOf for #{klass}"
147+
expect(vocabulary[klass]).to be_a_uri
148+
expect(vocabulary[klass].to_s).to eq "#{base_uri}#{klass}"
149+
expect(vocabulary).to respond_to(klass)
150+
expect { vocabulary.send(klass) }.not_to raise_error
151+
expect(vocabulary.send(klass)).to be_a_uri
152+
expect(vocabulary.send(klass.to_s)).to be_a_uri
153+
expect(vocabulary.send(klass).to_s).to eq "#{base_uri}#{klass}"
148154
end
149155
true
150156
end
@@ -173,5 +179,80 @@ module Matchers
173179
end
174180
end
175181
end
182+
183+
RSpec::Matchers.define :write do |message|
184+
chain(:to) do |io|
185+
@io = io
186+
end
187+
188+
supports_block_expectations {true}
189+
190+
match do |block|
191+
@output =
192+
case io
193+
when :output then fake_stdout(&block)
194+
when :error then fake_stderr(&block)
195+
else raise("Allowed values for `to` are :output and :error, got `#{io.inspect}`")
196+
end
197+
case message
198+
when nil, :something, :anything
199+
!@output.empty?
200+
when Regexp
201+
message.match(@output)
202+
else
203+
@output.include? message
204+
end
205+
end
206+
207+
description do
208+
"write \"#{message}\" #{io_name}"
209+
end
210+
211+
failure_message do
212+
@exception ? @exception.message :
213+
"expected to include #{description.inspect} in #{@output.inspect}"
214+
end
215+
216+
failure_message_when_negated do
217+
@exception ? @exception.message :
218+
"expected to not include #{description.inspect} in #{@output.inspect}"
219+
end
220+
221+
# Fake $stderr and return a string written to it.
222+
def fake_stderr
223+
original_stderr = $stderr
224+
$stderr = StringIO.new
225+
yield
226+
$stderr.string
227+
rescue RSpec::Expectations::ExpectationNotMetError => e
228+
@exception = e
229+
raise
230+
ensure
231+
$stderr = original_stderr
232+
end
233+
234+
# Fake $stdout and return a string written to it.
235+
def fake_stdout
236+
original_stdout = $stdout
237+
$stdout = StringIO.new
238+
yield
239+
$stdout.string
240+
rescue RSpec::Expectations::ExpectationNotMetError => e
241+
@exception = e
242+
raise
243+
ensure
244+
$stdout = original_stdout
245+
end
246+
247+
# default IO is standard output
248+
def io
249+
@io ||= :output
250+
end
251+
252+
# IO name is used for description message
253+
def io_name
254+
{:output => "standard output", :error => "standard error"}[io]
255+
end
256+
end
176257
end # Matchers
177258
end; end # RDF::Spec

lib/rdf/spec/mutable.rb

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,27 @@ module RDF_Mutable
5353
end
5454

5555
it "should accept a string filename argument" do
56-
pending("mutability", :unless => subject.mutable?) do
57-
expect { subject.load(RDF::Spec::TRIPLES_FILE) }.not_to raise_error
58-
end
56+
skip("mutability") unless subject.mutable?
57+
expect { subject.load(RDF::Spec::TRIPLES_FILE) }.not_to raise_error
5958
end
6059

6160
it "should accept an optional hash argument" do
62-
pending("mutability", :unless => subject.mutable?) do
63-
expect { subject.load(RDF::Spec::TRIPLES_FILE, {}) }.not_to raise_error
64-
end
61+
skip("mutability") unless subject.mutable?
62+
expect { subject.load(RDF::Spec::TRIPLES_FILE, {}) }.not_to raise_error
6563
end
6664

6765
it "should load statements" do
68-
pending("mutability", :unless => subject.mutable?) do
69-
subject.load RDF::Spec::TRIPLES_FILE
70-
expect(subject.size).to eq File.readlines(RDF::Spec::TRIPLES_FILE).size
71-
expect(subject).to have_subject(resource)
72-
end
66+
skip("mutability") unless subject.mutable?
67+
subject.load RDF::Spec::TRIPLES_FILE
68+
expect(subject.size).to eq File.readlines(RDF::Spec::TRIPLES_FILE).size
69+
expect(subject).to have_subject(resource)
7370
end
7471

7572
it "should load statements with a context override" do
76-
pending("mutability and contextuality", :unless => (subject.mutable? && @supports_context)) do
77-
subject.load RDF::Spec::TRIPLES_FILE, :context => context
78-
expect(subject).to have_context(context)
79-
expect(subject.query(:context => context).size).to eq subject.size
80-
end
73+
skip("mutability and contextuality") unless (subject.mutable? && @supports_context)
74+
subject.load RDF::Spec::TRIPLES_FILE, :context => context
75+
expect(subject).to have_context(context)
76+
expect(subject.query(:context => context).size).to eq subject.size
8177
end
8278
end
8379

@@ -97,62 +93,57 @@ module RDF_Mutable
9793
end
9894

9995
it "should not raise errors" do
100-
pending("mutability", :unless => subject.mutable?) do
101-
expect { subject.delete(@statements.first) }.not_to raise_error
102-
end
96+
skip("mutability") unless subject.mutable?
97+
expect { subject.delete(@statements.first) }.not_to raise_error
10398
end
10499

105100
it "should support deleting one statement at a time" do
106-
pending("mutability", :unless => subject.mutable?) do
107-
subject.delete(@statements.first)
108-
expect(subject).not_to have_statement(@statements.first)
109-
end
101+
skip("mutability") unless subject.mutable?
102+
subject.delete(@statements.first)
103+
expect(subject).not_to have_statement(@statements.first)
110104
end
111105

112106
it "should support deleting multiple statements at a time" do
113-
pending("mutability", :unless => subject.mutable?) do
114-
subject.delete(*@statements)
115-
expect(subject.find { |s| subject.has_statement?(s) }).to be_false
116-
end
107+
skip("mutability") unless subject.mutable?
108+
subject.delete(*@statements)
109+
expect(subject.find { |s| subject.has_statement?(s) }).to be_nil
117110
end
118111

119112
it "should support wildcard deletions" do
120-
pending("mutability", :unless => subject.mutable?) do
121-
# nothing deleted
122-
require 'digest/sha1'
123-
count = subject.count
124-
subject.delete([nil, nil, random = Digest::SHA1.hexdigest(File.read(__FILE__))])
125-
expect(subject).not_to be_empty
126-
expect(subject.count).to eq count
127-
128-
# everything deleted
129-
subject.delete([nil, nil, nil])
130-
expect(subject).to be_empty
131-
end
113+
skip("mutability") unless subject.mutable?
114+
# nothing deleted
115+
require 'digest/sha1'
116+
count = subject.count
117+
subject.delete([nil, nil, Digest::SHA1.hexdigest(File.read(__FILE__))])
118+
expect(subject).not_to be_empty
119+
expect(subject.count).to eq count
120+
121+
# everything deleted
122+
subject.delete([nil, nil, nil])
123+
expect(subject).to be_empty
132124
end
133125

134126
it "should only delete statements when the context matches" do
135-
pending("mutability", :unless => subject.mutable?) do
136-
# Setup three statements identical except for context
137-
count = subject.count + (@supports_context ? 3 : 1)
138-
s1 = RDF::Statement.new(resource, RDF::URI.new("urn:predicate:1"), RDF::URI.new("urn:object:1"))
139-
s2 = s1.dup
140-
s2.context = RDF::URI.new("urn:context:1")
141-
s3 = s1.dup
142-
s3.context = RDF::URI.new("urn:context:2")
143-
subject.insert(s1)
144-
subject.insert(s2)
145-
subject.insert(s3)
146-
expect(subject.count).to eq count
147-
148-
# Delete one by one
149-
subject.delete(s1)
150-
expect(subject.count).to eq count - (@supports_context ? 1 : 1)
151-
subject.delete(s2)
152-
expect(subject.count).to eq count - (@supports_context ? 2 : 1)
153-
subject.delete(s3)
154-
expect(subject.count).to eq count - (@supports_context ? 3 : 1)
155-
end
127+
skip("mutability") unless subject.mutable?
128+
# Setup three statements identical except for context
129+
count = subject.count + (@supports_context ? 3 : 1)
130+
s1 = RDF::Statement.new(resource, RDF::URI.new("urn:predicate:1"), RDF::URI.new("urn:object:1"))
131+
s2 = s1.dup
132+
s2.context = RDF::URI.new("urn:context:1")
133+
s3 = s1.dup
134+
s3.context = RDF::URI.new("urn:context:2")
135+
subject.insert(s1)
136+
subject.insert(s2)
137+
subject.insert(s3)
138+
expect(subject.count).to eq count
139+
140+
# Delete one by one
141+
subject.delete(s1)
142+
expect(subject.count).to eq count - (@supports_context ? 1 : 1)
143+
subject.delete(s2)
144+
expect(subject.count).to eq count - (@supports_context ? 2 : 1)
145+
subject.delete(s3)
146+
expect(subject.count).to eq count - (@supports_context ? 3 : 1)
156147
end
157148
end
158149
end

lib/rdf/spec/queryable.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module RDF_Queryable
1111
@statements = RDF::Spec.quads
1212

1313
if @queryable.empty?
14-
if @queryable.respond_to?(:<<) && (@queryable.writable? rescue true)
14+
if (@queryable.writable? rescue false)
15+
@queryable.insert(*@statements)
16+
elsif @queryable.respond_to?(:<<)
1517
@statements.each { |statement| @queryable << statement }
1618
else
1719
raise "@queryable must respond to #<< or be pre-populated with the statements in #{@doap} in a before(:each) block"
@@ -188,11 +190,11 @@ module RDF_Queryable
188190
its(:count) {should == 2}
189191

190192
it "has two solutions" do
191-
expect(subject.any? {|s| s.subject == RDF::URI("http://example.org/xi1")}).to be_true
193+
expect(subject.any? {|s| s.subject == RDF::URI("http://example.org/xi1")}).to be_truthy
192194
end
193195

194196
it "has xi2 as a solution" do
195-
expect(subject.any? {|s| s.subject == RDF::URI("http://example.org/xi2")}).to be_true
197+
expect(subject.any? {|s| s.subject == RDF::URI("http://example.org/xi2")}).to be_truthy
196198
end
197199
end
198200

@@ -201,7 +203,7 @@ module RDF_Queryable
201203
its(:count) {should == 1}
202204

203205
it "has xd1 as a solution" do
204-
expect(subject.any? {|s| s.subject == RDF::URI("http://example.org/xd1")}).to be_true
206+
expect(subject.any? {|s| s.subject == RDF::URI("http://example.org/xd1")}).to be_truthy
205207
end
206208
end
207209
end
@@ -213,7 +215,7 @@ module RDF_Queryable
213215
# @see RDF::Queryable#query_execute
214216
describe "#query_execute" do
215217
it "defines a protected #query_execute method" do
216-
expect(subject.class.protected_method_defined?(:query_execute)).to be_true
218+
expect(subject.class.protected_method_defined?(:query_execute)).to be_truthy
217219
end
218220

219221
context "when called" do
@@ -237,7 +239,7 @@ module RDF_Queryable
237239
# @see RDF::Queryable#query_pattern
238240
describe "#query_pattern" do
239241
it "defines a protected #query_pattern method" do
240-
expect(subject.class.protected_method_defined?(:query_pattern)).to be_true
242+
expect(subject.class.protected_method_defined?(:query_pattern)).to be_truthy
241243
end
242244

243245
context "when called" do

lib/rdf/spec/reader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module RDF_Reader
2222

2323
describe ".open" do
2424
before(:each) do
25-
RDF::Util::File.stub(:open_file).and_yield(StringIO.new(@reader_input))
25+
allow(RDF::Util::File).to receive(:open_file).and_yield(StringIO.new(@reader_input))
2626
end
2727

2828
it "yields reader given file_name" do

0 commit comments

Comments
 (0)