Skip to content

Commit 562a1de

Browse files
committed
Add inspects and matchers from sparql.
1 parent 444a51e commit 562a1de

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

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 'rdf/spec/inspects'
34
require 'rspec/its'
45

56
module RDF

lib/rdf/spec/inspects.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'rdf'
2+
require 'rdf/ntriples'
3+
# override several inspect functions to improve output for what we're doing
4+
5+
class RDF::Literal
6+
def inspect
7+
RDF::NTriples::Writer.serialize(self) + " R:L:(#{self.class.to_s.match(/([^:]*)$/)})"
8+
end
9+
end
10+
11+
class RDF::URI
12+
def inspect
13+
RDF::NTriples::Writer.serialize(self)
14+
end
15+
end
16+
17+
class RDF::Node
18+
def inspect
19+
RDF::NTriples::Writer.serialize(self) + "(#{object_id})"
20+
end
21+
end
22+
23+
class RDF::Graph
24+
def inspect
25+
"\n" + dump(:n3) + "\n"
26+
end
27+
end
28+
29+
class RDF::Query
30+
def inspect
31+
"RDF::Query(#{graph_name ? graph_name.to_sxp : 'nil'})#{patterns.inspect}"
32+
end
33+
end
34+
35+
class Array
36+
alias_method :inspect_without_formatting, :inspect
37+
def inspect_with_formatting
38+
if all? { |item| item.is_a?(Hash) }
39+
string = "[\n"
40+
each do |item|
41+
string += " {\n"
42+
item.keys.map(&:to_s).sort.each do |key|
43+
string += " #{key}: #{item[key.to_sym].inspect}\n"
44+
end
45+
string += " },\n"
46+
end
47+
string += "]"
48+
string
49+
elsif all? { |item| item.is_a?(RDF::Query::Solution)}
50+
string = "[\n"
51+
each do |item|
52+
string += " {\n"
53+
item.bindings.keys.map(&:to_s).sort.each do |key|
54+
string += " #{key}: #{item.bindings[key.to_sym].inspect}\n"
55+
end
56+
string += " },\n"
57+
end
58+
string += "]"
59+
string
60+
else
61+
inspect_without_formatting
62+
end
63+
end
64+
alias_method :inspect, :inspect_with_formatting
65+
end
66+
67+
class RDF::Query::Solutions
68+
def inspect
69+
string = "vars: #{variable_names.join(",")}\n#{to_a.inspect}"
70+
end
71+
end

lib/rdf/spec/matchers.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,20 @@ def io_name
260260
end
261261
end # Matchers
262262
end; end # RDF::Spec
263+
264+
265+
module RSpec
266+
module Matchers
267+
class MatchArray
268+
private
269+
def safe_sort(array)
270+
case
271+
when array.all?{|item| item.respond_to?(:<=>) && !item.is_a?(Hash)}
272+
array.sort
273+
else
274+
array
275+
end
276+
end
277+
end
278+
end
279+
end

0 commit comments

Comments
 (0)