rdf-rdfa 0.3.1.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +2 -1
- data/Gemfile +18 -0
- data/History.md +14 -1
- data/README +291 -0
- data/README.md +236 -36
- data/Rakefile +7 -27
- data/UNLICENSE +24 -0
- data/VERSION +1 -1
- data/etc/basic.html +1 -1
- data/etc/profile.html +40 -0
- data/example-files/payswarm.html +449 -0
- data/example-files/payswarm.n3 +86 -0
- data/lib/rdf/rdfa.rb +5 -0
- data/lib/rdf/rdfa/format.rb +8 -4
- data/lib/rdf/rdfa/patches/graph_properties.rb +34 -0
- data/lib/rdf/rdfa/patches/nokogiri_hacks.rb +6 -0
- data/lib/rdf/rdfa/patches/string_hacks.rb +9 -0
- data/lib/rdf/rdfa/profile.rb +65 -41
- data/lib/rdf/rdfa/profile/xhtml.rb +36 -0
- data/lib/rdf/rdfa/profile/xml.rb +45 -0
- data/lib/rdf/rdfa/reader.rb +168 -92
- data/lib/rdf/rdfa/writer.rb +822 -0
- data/lib/rdf/rdfa/writer/haml_templates.rb +306 -0
- data/rdf-rdfa.gemspec +77 -23
- data/script/intern_vocabulary +83 -0
- data/script/parse +62 -27
- data/script/tc +87 -37
- data/spec/.gitignore +1 -0
- data/spec/matchers.rb +89 -154
- data/spec/profile_spec.rb +36 -21
- data/spec/{rdfa_reader_spec.rb → reader_spec.rb} +86 -159
- data/spec/spec_helper.rb +6 -29
- data/spec/test_helper.rb +97 -0
- data/spec/writer_spec.rb +385 -0
- metadata +203 -37
- data/spec/html4-manifest.yml +0 -1749
- data/spec/html5-manifest.yml +0 -1749
- data/spec/rdfa_helper.rb +0 -257
- data/spec/svgtiny-manifest.yml +0 -37
- data/spec/xhtml-manifest.yml +0 -1749
@@ -0,0 +1,83 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Generate a Ruby version of an RDFa vocabulary to be directly accessible from RDF::RDFa::Profile
|
4
|
+
require 'rubygems'
|
5
|
+
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
|
6
|
+
require 'linkeddata'
|
7
|
+
require 'getoptlong'
|
8
|
+
require 'open-uri'
|
9
|
+
|
10
|
+
def help
|
11
|
+
puts "Usage: #{$0} [options] file-or-uri ..."
|
12
|
+
puts "Options:"
|
13
|
+
puts " --dump: Dump raw output, otherwise serialize to Ruby"
|
14
|
+
puts " --debug: Display detailed debug output"
|
15
|
+
puts " --verbose: Verbose processing"
|
16
|
+
puts " --output: Write to file"
|
17
|
+
puts " --help,-?: This message"
|
18
|
+
exit(0)
|
19
|
+
end
|
20
|
+
|
21
|
+
dump = false
|
22
|
+
output = STDOUT
|
23
|
+
|
24
|
+
opts = GetoptLong.new(
|
25
|
+
["--debug", GetoptLong::NO_ARGUMENT],
|
26
|
+
["--dump", GetoptLong::NO_ARGUMENT],
|
27
|
+
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT],
|
28
|
+
["--verbose", GetoptLong::NO_ARGUMENT],
|
29
|
+
["--help", "-?", GetoptLong::NO_ARGUMENT]
|
30
|
+
)
|
31
|
+
opts.each do |opt, arg|
|
32
|
+
case opt
|
33
|
+
when '--verbose' then $verbose = true
|
34
|
+
when '--debug' then ::RDF::RDFa::debug = true
|
35
|
+
when '--dump' then dump = true
|
36
|
+
when '--output' then output = File.open(arg, "w")
|
37
|
+
when '--help' then help
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
help if ARGV.empty?
|
42
|
+
|
43
|
+
ARGV.each do |file|
|
44
|
+
uri = RDF::URI(file)
|
45
|
+
if RDF::RDFa::Profile.cache[uri]
|
46
|
+
STDERR.puts "Remove cached entry for #{uri} before attempting to parse"
|
47
|
+
exit(1)
|
48
|
+
end
|
49
|
+
|
50
|
+
profile = RDF::RDFa::Profile.find(uri)
|
51
|
+
if dump
|
52
|
+
puts profile.inspect
|
53
|
+
else
|
54
|
+
output.puts <<START
|
55
|
+
# This file is automatically generated by #{__FILE__}
|
56
|
+
# RDFa vocabulary for #{file}
|
57
|
+
|
58
|
+
profile = RDF::RDFa::Profile.new(RDF::URI("#{file}"), {
|
59
|
+
START
|
60
|
+
|
61
|
+
output.puts %{ :vocabulary => "#{profile.vocabulary},"} if profile.vocabulary
|
62
|
+
|
63
|
+
[:prefixes, :terms].each do |t|
|
64
|
+
hash = profile.send(t)
|
65
|
+
key_width = hash.keys.inject(0) {|width, k| k.inspect.length > width ? k.inspect.length : width}
|
66
|
+
unless hash.empty?
|
67
|
+
output.puts %( :#{t} => {)
|
68
|
+
hash.keys.map(&:to_s).sort.each do |k|
|
69
|
+
v = hash[k.to_sym]
|
70
|
+
output.printf(%( %*s => %s,\n), -key_width, k.to_sym.inspect, v.inspect)
|
71
|
+
end
|
72
|
+
output.puts %( },)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
output.puts <<END
|
77
|
+
})
|
78
|
+
|
79
|
+
RDF::RDFa::Profile.cache[RDF::URI("#{file}")] = profile
|
80
|
+
END
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
data/script/parse
CHANGED
@@ -2,55 +2,90 @@
|
|
2
2
|
require 'rubygems'
|
3
3
|
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
|
4
4
|
require 'rdf/rdfa'
|
5
|
-
require 'rdf/rdfxml' rescue nil
|
6
5
|
require 'rdf/n3' rescue nil
|
6
|
+
require 'rdf/rdfxml' rescue nil
|
7
7
|
require 'getoptlong'
|
8
8
|
require 'open-uri'
|
9
9
|
|
10
|
-
def
|
11
|
-
reader_class = RDF::Reader.for(
|
12
|
-
writer_class = RDF::Writer.for($output_format.to_sym)
|
10
|
+
def run(input, options)
|
11
|
+
reader_class = RDF::Reader.for(options[:input_format].to_sym)
|
13
12
|
raise "Reader not found for #{$input_format}" unless reader_class
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
|
14
|
+
start = Time.new
|
15
|
+
num = 0
|
16
|
+
|
17
|
+
if options[:output_format] == :ntriples || options[:quiet]
|
18
|
+
reader_class.new(input, :base_uri => options[:base_uri], :strict => true).each do |statement|
|
19
|
+
num += 1
|
20
|
+
if options[:quiet]
|
21
|
+
#print "."
|
22
|
+
else
|
23
|
+
options[:output].puts statement.to_ntriples
|
24
|
+
end
|
25
|
+
end
|
26
|
+
elsif options[:output_format] == :inspect
|
27
|
+
reader_class.new(input, :base_uri => options[:base_uri], :strict => true).each do |statement|
|
28
|
+
num += 1
|
29
|
+
options[:output].puts statement.inspect
|
18
30
|
end
|
19
|
-
|
31
|
+
else
|
32
|
+
r = reader_class.new(input, options)
|
33
|
+
g = RDF::Graph.new << r
|
34
|
+
num = g.count
|
35
|
+
options[:output].puts g.dump(options[:output_format], options.merge(:prefixes => r.prefixes))
|
36
|
+
end
|
37
|
+
STDERR.puts
|
38
|
+
secs = Time.new - start
|
39
|
+
STDERR.puts "Parsed #{num} statements in #{secs} seconds @ #{num/secs} statements/second."
|
40
|
+
rescue
|
41
|
+
fname = input.respond_to?(:path) ? input.path : "-stdin-"
|
42
|
+
STDERR.puts("Error in #{fname}")
|
43
|
+
raise
|
20
44
|
end
|
21
45
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
46
|
+
options = {
|
47
|
+
:verbose => false,
|
48
|
+
:validate => false,
|
49
|
+
:strict => true,
|
50
|
+
:output => STDOUT,
|
51
|
+
:output_format => :ntriples,
|
52
|
+
:input_format => :rdfa,
|
53
|
+
:base_uri => "http://example.com",
|
54
|
+
}
|
26
55
|
input = nil
|
27
56
|
|
28
57
|
opts = GetoptLong.new(
|
29
58
|
["--debug", GetoptLong::NO_ARGUMENT],
|
30
|
-
["--
|
31
|
-
["--quiet", GetoptLong::NO_ARGUMENT],
|
59
|
+
["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT],
|
32
60
|
["--format", GetoptLong::REQUIRED_ARGUMENT],
|
33
61
|
["--input-format", GetoptLong::REQUIRED_ARGUMENT],
|
34
|
-
["--
|
35
|
-
["--
|
62
|
+
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT],
|
63
|
+
["--quiet", GetoptLong::NO_ARGUMENT],
|
64
|
+
["--template", GetoptLong::REQUIRED_ARGUMENT],
|
65
|
+
["--uri", GetoptLong::REQUIRED_ARGUMENT],
|
66
|
+
["--validate", GetoptLong::NO_ARGUMENT],
|
67
|
+
["--verbose", GetoptLong::NO_ARGUMENT]
|
36
68
|
)
|
37
69
|
opts.each do |opt, arg|
|
38
70
|
case opt
|
39
|
-
when '--
|
40
|
-
when '--
|
41
|
-
when '--
|
42
|
-
when '--
|
43
|
-
when '--
|
44
|
-
when '--
|
45
|
-
when '--
|
71
|
+
when '--debug' then ::RDF::RDFa::debug = true
|
72
|
+
when '--execute' then input = arg
|
73
|
+
when '--format' then options[:output_format] = arg.to_sym
|
74
|
+
when '--input-format' then options[:input_format] = arg.to_sym
|
75
|
+
when '--quiet' then options[:quiet] = true
|
76
|
+
when '--output' then options[:output] = File.open(arg, "w")
|
77
|
+
when '--template' then options[:haml] = arg.to_sym
|
78
|
+
when '--uri' then options[:base_uri] = arg
|
79
|
+
when '--verbose' then options[:verbose] = true
|
80
|
+
when '--validate' then options[:validate] = true
|
46
81
|
end
|
47
82
|
end
|
48
83
|
|
49
84
|
if ARGV.empty?
|
50
85
|
s = input ? input : $stdin.read
|
51
|
-
|
86
|
+
run(StringIO.new(s), options)
|
52
87
|
else
|
53
88
|
ARGV.each do |test_file|
|
54
|
-
|
89
|
+
run(Kernel.open(test_file), options)
|
55
90
|
end
|
56
|
-
end
|
91
|
+
end
|
data/script/tc
CHANGED
@@ -1,65 +1,115 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'rubygems'
|
3
3
|
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
|
4
|
-
require 'rdf/n3'
|
5
|
-
require 'rdf/rdfxml'
|
6
4
|
require 'rdf/rdfa'
|
7
|
-
require
|
5
|
+
require 'sparql/grammar'
|
6
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "..", 'spec', 'spec_helper'))
|
7
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "..", 'spec', 'test_helper'))
|
8
8
|
require 'getoptlong'
|
9
9
|
|
10
|
-
def run_tc(tc)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
def run_tc(tc, options)
|
11
|
+
input_uri = tc.input(options[:host_language], options[:version])
|
12
|
+
results_uri = tc.results(options[:host_language], options[:version])
|
13
|
+
|
14
|
+
STDOUT.write "run #{tc.name}"
|
15
|
+
|
16
|
+
if options[:verbose]
|
17
|
+
puts("\nTestCase: #{tc.inspect}")
|
18
|
+
puts("\nInput:\n" + Kernel.open(input_uri) {|f| f.read})
|
19
|
+
puts("\nQuery:\n" + Kernel.open(results_uri) {|f| f.read})
|
20
|
+
end
|
21
|
+
|
22
|
+
pg = RDF::Graph.new if options[:processor_graph]
|
23
|
+
|
24
|
+
begin
|
25
|
+
reader = RDF::RDFa::Reader.new(Kernel.open(input_uri),
|
26
|
+
:base_uri => input_uri,
|
27
|
+
:validate => options[:validate],
|
18
28
|
:processor_graph => pg,
|
19
|
-
:version =>
|
20
|
-
|
29
|
+
:version => options[:version])
|
30
|
+
|
31
|
+
graph = RDF::Graph.new << reader
|
32
|
+
rescue Exception => e
|
33
|
+
puts "#{"exception:" unless options[:quiet]}: #{e}"
|
34
|
+
if options[:quiet]
|
35
|
+
return
|
36
|
+
else
|
37
|
+
raise
|
21
38
|
end
|
22
39
|
end
|
23
|
-
puts ""
|
24
40
|
|
25
|
-
|
41
|
+
puts("\nResult:\n" + graph.dump(options[:format])) unless options[:quiet]
|
42
|
+
|
43
|
+
begin
|
44
|
+
result = SPARQL::Grammar.parse(Kernel.open(results_uri)).execute(graph)
|
45
|
+
rescue Exception => e
|
46
|
+
puts "#{"exception:" unless options[:quiet]}: #{e}"
|
47
|
+
return
|
48
|
+
end
|
49
|
+
puts "#{"test result:" unless options[:quiet]} #{(result == (tc.expectedResults || true)) ? 'ok' : 'fail'}"
|
50
|
+
|
51
|
+
if pg && !options[:quiet]
|
26
52
|
puts "\nProcessor Graph:\n"
|
27
53
|
puts pg.inspect
|
28
|
-
RDF::Writer.for(
|
54
|
+
RDF::Writer.for(options[:format]).new do |writer|
|
29
55
|
writer << pg
|
30
56
|
end
|
31
57
|
end
|
32
58
|
end
|
33
59
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
60
|
+
options = {
|
61
|
+
:verbose => false,
|
62
|
+
:quite => false,
|
63
|
+
:validate => false,
|
64
|
+
:format => :ntriples,
|
65
|
+
:processor_graph => nil,
|
66
|
+
:host_language => "xhtml1",
|
67
|
+
:version => "rdfa1.1"
|
68
|
+
}
|
69
|
+
|
39
70
|
opts = GetoptLong.new(
|
40
|
-
["--
|
41
|
-
["--
|
71
|
+
["--help", "-?", GetoptLong::NO_ARGUMENT],
|
72
|
+
["--dbg", GetoptLong::NO_ARGUMENT],
|
73
|
+
["--format", GetoptLong::REQUIRED_ARGUMENT],
|
74
|
+
["--host-language", "-h", GetoptLong::OPTIONAL_ARGUMENT],
|
75
|
+
["--processor-graph", GetoptLong::NO_ARGUMENT],
|
42
76
|
["--quiet", GetoptLong::NO_ARGUMENT],
|
43
|
-
["--suite", GetoptLong::OPTIONAL_ARGUMENT],
|
44
77
|
["--validate", GetoptLong::NO_ARGUMENT],
|
45
|
-
["--
|
46
|
-
["--
|
78
|
+
["--verbose", GetoptLong::NO_ARGUMENT],
|
79
|
+
["--version", "-v", GetoptLong::OPTIONAL_ARGUMENT],
|
47
80
|
)
|
81
|
+
|
82
|
+
def help(options)
|
83
|
+
puts "Usage: #{$0} [options] [test-number ...]"
|
84
|
+
puts "Options:"
|
85
|
+
puts " --dump: Dump raw output, otherwise serialize to Ruby"
|
86
|
+
puts " --debug: Display detailed debug output"
|
87
|
+
puts " --format: Format for output, defaults to #{options[:format].inspect}"
|
88
|
+
puts " --host-language: Run for specified host language, defaults to #{options[:host_language]}"
|
89
|
+
puts " --processor-graph: Output processor graph"
|
90
|
+
puts " --quiet: Minimal output"
|
91
|
+
puts " --validate: Validate input"
|
92
|
+
puts " --verbose: Verbose processing"
|
93
|
+
puts " --version: Version of processor to use (rdf1.0, rdf1.1). Defaults to #{options[:version]}"
|
94
|
+
puts " --help,-?: This message"
|
95
|
+
exit(0)
|
96
|
+
end
|
97
|
+
|
48
98
|
opts.each do |opt, arg|
|
49
99
|
case opt
|
50
|
-
when '--
|
51
|
-
when '--
|
52
|
-
when '--
|
53
|
-
when '--
|
54
|
-
when '--
|
55
|
-
when '--
|
56
|
-
when '--
|
100
|
+
when '--help' then help(options)
|
101
|
+
when '--verbose' then options[:verbose] = true
|
102
|
+
when '--quiet' then options[:quiet] = true
|
103
|
+
when '--dbg' then ::RDF::RDFa::debug = true
|
104
|
+
when '--format' then options[:format] = arg.to_sym
|
105
|
+
when '--processor-graph' then options[:processor_graph] = true
|
106
|
+
when '--host-language' then options[:host_language] = arg
|
107
|
+
when '--version' then options[:version] = arg
|
108
|
+
when '--validate' then options[:validate] = true
|
57
109
|
end
|
58
110
|
end
|
59
111
|
|
60
|
-
|
61
|
-
|
62
|
-
test_cases.each do |tc|
|
112
|
+
Fixtures::TestCase.for_specific(options[:host_language], options[:version]) do |tc|
|
63
113
|
next unless ARGV.empty? || ARGV.any? {|n| tc.name.match(/#{n}/)}
|
64
|
-
run_tc(tc)
|
114
|
+
run_tc(tc, options)
|
65
115
|
end
|
data/spec/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/uri-cache/
|
data/spec/matchers.rb
CHANGED
@@ -1,171 +1,106 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Info = Struct.new(:about, :information, :trace, :compare, :inputDocument, :outputDocument)
|
4
|
-
def normalize(graph)
|
5
|
-
case graph
|
6
|
-
when RDF::Graph then graph
|
7
|
-
when IO, StringIO
|
8
|
-
RDF::Graph.new.load(graph, :base_uri => @info.about)
|
9
|
-
else
|
10
|
-
# Figure out which parser to use
|
11
|
-
g = RDF::Graph.new
|
12
|
-
reader_class = detect_format(graph)
|
13
|
-
reader_class.new(graph, :base_uri => @info.about).each {|s| g << s}
|
14
|
-
g
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def initialize(expected, info)
|
19
|
-
@info = if info.respond_to?(:about)
|
20
|
-
info
|
21
|
-
elsif info.is_a?(Hash)
|
22
|
-
identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.context : info[:about]
|
23
|
-
trace = info[:trace]
|
24
|
-
trace = trace.join("\n") if trace.is_a?(Array)
|
25
|
-
Info.new(identifier, info[:information] || "", trace, info[:compare])
|
26
|
-
else
|
27
|
-
Info.new(expected.is_a?(RDF::Graph) ? expected.context : info, info.to_s)
|
28
|
-
end
|
29
|
-
@expected = normalize(expected)
|
30
|
-
end
|
31
|
-
|
32
|
-
def matches?(actual)
|
33
|
-
@actual = normalize(actual)
|
34
|
-
@actual == @expected
|
35
|
-
end
|
1
|
+
require 'rspec/matchers'
|
2
|
+
require 'sparql/grammar'
|
36
3
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
"Graphs do not differ\n"
|
4
|
+
RSpec::Matchers.define :have_xpath do |xpath, value|
|
5
|
+
match do |actual|
|
6
|
+
@doc = Nokogiri::XML.parse(actual)
|
7
|
+
@doc.should be_a(Nokogiri::XML::Document)
|
8
|
+
@doc.root.should be_a(Nokogiri::XML::Element)
|
9
|
+
@namespaces = @doc.namespaces.merge("xhtml" => "http://www.w3.org/1999/xhtml", "xml" => "http://www.w3.org/XML/1998/namespace")
|
10
|
+
case value
|
11
|
+
when false
|
12
|
+
@doc.root.at_xpath(xpath, @namespaces).should be_nil
|
13
|
+
when true
|
14
|
+
@doc.root.at_xpath(xpath, @namespaces).should_not be_nil
|
15
|
+
when Array
|
16
|
+
@doc.root.at_xpath(xpath, @namespaces).to_s.split(" ").should include(*value)
|
17
|
+
when Regexp
|
18
|
+
@doc.root.at_xpath(xpath, @namespaces).to_s.should =~ value
|
19
|
+
else
|
20
|
+
@doc.root.at_xpath(xpath, @namespaces).to_s.should == value
|
55
21
|
end
|
56
22
|
end
|
57
23
|
|
58
|
-
|
59
|
-
|
24
|
+
failure_message_for_should do |actual|
|
25
|
+
msg = "expected that #{xpath.inspect} would be #{value.inspect} in:\n" + actual.to_s
|
26
|
+
msg += "was: #{@doc.root.at_xpath(xpath, @namespaces)}"
|
60
27
|
end
|
28
|
+
end
|
61
29
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
@query = Redland::Query.new(expected)
|
68
|
-
@info = info
|
69
|
-
#puts "PassQuery: expected #{expected.inspect}"
|
70
|
-
end
|
71
|
-
def matches?(actual)
|
72
|
-
@actual = actual
|
73
|
-
@expected_results = @info.respond_to?(:expectedResults) ? @info.expectedResults : true
|
74
|
-
model = Redland::Model.new
|
75
|
-
ntriples_parser = Redland::Parser.ntriples
|
76
|
-
ntriples_parser.parse_string_into_model(model, actual.to_ntriples, "http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/")
|
77
|
-
|
78
|
-
@results = @query.execute(model)
|
79
|
-
#puts "Redland query results: #{@results.inspect}"
|
80
|
-
if @expected_results && @results
|
81
|
-
@results.is_boolean? && @results.get_boolean?
|
82
|
-
else
|
83
|
-
@results.nil? || @results.is_boolean? && !@results.get_boolean?
|
84
|
-
end
|
85
|
-
end
|
86
|
-
def failure_message_for_should
|
87
|
-
info = @info.respond_to?(:information) ? @info.information : ""
|
88
|
-
"#{info + "\n" unless info.empty?}" +
|
89
|
-
if @results.nil?
|
90
|
-
"Query failed to return results"
|
91
|
-
elsif !@results.is_boolean?
|
92
|
-
"Query returned non-boolean results"
|
93
|
-
elsif @expected_results
|
94
|
-
"Query returned false"
|
95
|
-
else
|
96
|
-
"Query returned true (expected false)"
|
97
|
-
end +
|
98
|
-
"\n#{@expected}" +
|
99
|
-
"\n#{@info.input}" +
|
100
|
-
"\nResults:\n#{@actual.to_ntriples}" +
|
101
|
-
"\nDebug:\n#{@info.trace}"
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
def pass_query(expected, info = "")
|
106
|
-
PassQuery.new(expected, info)
|
107
|
-
end
|
30
|
+
def normalize(graph)
|
31
|
+
case graph
|
32
|
+
when RDF::Graph then graph
|
33
|
+
when IO, StringIO
|
34
|
+
RDF::Graph.new.load(graph, :base_uri => @info.about)
|
108
35
|
else
|
109
|
-
|
36
|
+
# Figure out which parser to use
|
37
|
+
g = RDF::Graph.new
|
38
|
+
reader_class = detect_format(graph)
|
39
|
+
reader_class.new(graph, :base_uri => @info.about).each {|s| g << s}
|
40
|
+
g
|
110
41
|
end
|
42
|
+
end
|
111
43
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
"#{@info + "\n" unless @info.empty?}" +
|
126
|
-
if @doc.nil?
|
127
|
-
"did not parse"
|
128
|
-
else
|
129
|
-
"\n#{@results}" +
|
130
|
-
"\nParsed:\n#{@doc}"
|
131
|
-
end +
|
132
|
-
"\nActual:\n#{@actual}"
|
44
|
+
Info = Struct.new(:about, :information, :trace, :compare, :inputDocument, :outputDocument)
|
45
|
+
|
46
|
+
RSpec::Matchers.define :be_equivalent_graph do |expected, info|
|
47
|
+
match do |actual|
|
48
|
+
@info = if info.respond_to?(:about)
|
49
|
+
info
|
50
|
+
elsif info.is_a?(Hash)
|
51
|
+
identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.context : info[:about]
|
52
|
+
trace = info[:trace]
|
53
|
+
trace = trace.join("\n") if trace.is_a?(Array)
|
54
|
+
Info.new(identifier, info[:information] || "", trace, info[:compare])
|
55
|
+
else
|
56
|
+
Info.new(expected.is_a?(RDF::Graph) ? expected.context : info, info.to_s)
|
133
57
|
end
|
58
|
+
@expected = normalize(expected)
|
59
|
+
@actual = normalize(actual)
|
60
|
+
@actual.isomorphic_with?(@expected)
|
134
61
|
end
|
135
62
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
end
|
145
|
-
|
146
|
-
|
147
|
-
|
63
|
+
failure_message_for_should do |actual|
|
64
|
+
info = @info.respond_to?(:information) ? @info.information : @info.inspect
|
65
|
+
if @expected.is_a?(RDF::Graph) && @actual.size != @expected.size
|
66
|
+
"Graph entry count differs:\nexpected: #{@expected.size}\nactual: #{@actual.size}"
|
67
|
+
elsif @expected.is_a?(Array) && @actual.size != @expected.length
|
68
|
+
"Graph entry count differs:\nexpected: #{@expected.length}\nactual: #{@actual.size}"
|
69
|
+
else
|
70
|
+
"Graph differs"
|
71
|
+
end +
|
72
|
+
"\n#{info + "\n" unless info.empty?}" +
|
73
|
+
(@info.inputDocument ? "Input file: #{@info.inputDocument}\n" : "") +
|
74
|
+
(@info.outputDocument ? "Output file: #{@info.outputDocument}\n" : "") +
|
75
|
+
"Unsorted Expected:\n#{@expected.dump(:ntriples)}" +
|
76
|
+
"Unsorted Results:\n#{@actual.dump(:ntriples)}" +
|
77
|
+
(@info.trace ? "\nDebug:\n#{@info.trace}" : "")
|
78
|
+
end
|
79
|
+
end
|
148
80
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
rescue
|
155
|
-
puts $!
|
156
|
-
@fault = $!.message
|
157
|
-
false
|
158
|
-
end
|
81
|
+
RSpec::Matchers.define :pass_query do |expected, info|
|
82
|
+
match do |actual|
|
83
|
+
@expected = expected.read
|
84
|
+
query = SPARQL::Grammar.parse(@expected)
|
85
|
+
@results = query.execute(actual)
|
159
86
|
|
160
|
-
|
161
|
-
"#{@info + "\n" unless @info.empty?}" +
|
162
|
-
"Fault: #{@fault + "\n" if @fault}" +
|
163
|
-
"Expected:#{@expected}\n" +
|
164
|
-
"Actual:#{@actual}"
|
165
|
-
end
|
87
|
+
@results.should == info.expectedResults
|
166
88
|
end
|
167
89
|
|
168
|
-
|
169
|
-
|
170
|
-
|
90
|
+
failure_message_for_should do |actual|
|
91
|
+
information = info.respond_to?(:information) ? info.information : ""
|
92
|
+
"#{information + "\n" unless information.empty?}" +
|
93
|
+
if @results.nil?
|
94
|
+
"Query failed to return results"
|
95
|
+
elsif !@results.is_a?(RDF::Literal::Boolean)
|
96
|
+
"Query returned non-boolean results"
|
97
|
+
elsif info.expectedResults
|
98
|
+
"Query returned false"
|
99
|
+
else
|
100
|
+
"Query returned true (expected false)"
|
101
|
+
end +
|
102
|
+
"\n#{@expected}" +
|
103
|
+
"\nResults:\n#{@actual.dump(:ntriples)}" +
|
104
|
+
"\nDebug:\n#{info.trace}"
|
105
|
+
end
|
171
106
|
end
|