|
1 | 1 | # coding: utf-8 |
2 | | -require 'rdf/isomorphic' |
3 | 2 | require 'json' |
4 | 3 | JSON_STATE = JSON::State.new( |
5 | 4 | indent: " ", |
|
9 | 8 | array_nl: "\n" |
10 | 9 | ) |
11 | 10 |
|
12 | | -def normalize(graph) |
13 | | - case graph |
14 | | - when RDF::Queryable then graph |
15 | | - when IO, StringIO |
16 | | - RDF::Graph.new.load(graph, base_uri: @info.about) |
17 | | - else |
18 | | - # Figure out which parser to use |
19 | | - g = RDF::Repository.new |
20 | | - reader_class = detect_format(graph) |
21 | | - reader_class.new(graph, base_uri: @info.about).each {|s| g << s} |
22 | | - g |
23 | | - end |
24 | | -end |
25 | | - |
26 | | -Info = Struct.new(:about, :coment, :trace, :input, :result, :action, :expected) |
27 | | - |
28 | | -RSpec::Matchers.define :be_equivalent_graph do |expected, info| |
29 | | - match do |actual| |
30 | | - @info = if info.respond_to?(:input) |
31 | | - info |
32 | | - elsif info.is_a?(Hash) |
33 | | - identifier = info[:identifier] || expected.is_a?(RDF::Enumerable) ? expected.context : info[:about] |
34 | | - trace = info[:trace] |
35 | | - if trace.is_a?(Array) |
36 | | - trace = trace.map {|s| s.dup.force_encoding(Encoding::UTF_8)}.join("\n") |
37 | | - end |
38 | | - Info.new(identifier, info[:comment] || "", trace) |
39 | | - else |
40 | | - Info.new(expected.is_a?(RDF::Enumerable) ? expected.context : info, info.to_s) |
41 | | - end |
42 | | - @expected = normalize(expected) |
43 | | - @actual = normalize(actual) |
44 | | - @actual.isomorphic_with?(@expected) rescue false |
45 | | - end |
46 | | - |
47 | | - failure_message do |actual| |
48 | | - info = @info.respond_to?(:comment) ? @info.comment : @info.inspect |
49 | | - if @expected.is_a?(RDF::Graph) && @actual.size != @expected.size |
50 | | - "Graph entry count differs:\nexpected: #{@expected.size}\nactual: #{@actual.size}" |
51 | | - elsif @expected.is_a?(Array) && @actual.size != @expected.length |
52 | | - "Graph entry count differs:\nexpected: #{@expected.length}\nactual: #{@actual.size}" |
53 | | - else |
54 | | - "Graph differs" |
55 | | - end + |
56 | | - "\n#{info + "\n" unless info.empty?}" + |
57 | | - (@info.action ? "Input file: #{@info.action}\n" : "") + |
58 | | - (@info.result ? "Result file: #{@info.result}\n" : "") + |
59 | | - "Unsorted Expected:\n#{@expected.dump(:ttl, standard_prefixes: true)}" + |
60 | | - "Unsorted Results:\n#{@actual.dump(:ttl, standard_prefixes: true)}" + |
61 | | - (@info.trace ? "\nDebug:\n#{@info.trace}" : "") |
62 | | - end |
63 | | -end |
64 | | - |
65 | 11 | RSpec::Matchers.define :generate do |expected, options = {}| |
66 | 12 | def parser(options = {}) |
67 | 13 | @debug = options[:progress] ? 2 : [] |
|
0 commit comments