-
Notifications
You must be signed in to change notification settings - Fork 9
/
suite_helper.rb
183 lines (161 loc) · 5.53 KB
/
suite_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Spira class for manipulating test-manifest style test suites.
# Used for Turtle tests
require 'rdf/turtle'
require 'json/ld'
# For now, override RDF::Utils::File.open_file to look for the file locally before attempting to retrieve it
module RDF::Util
module File
REMOTE_PATH = "https://w3c.github.io/rdf-tests/rdf/"
LOCAL_PATH = ::File.expand_path("../rdf-tests/rdf/", __FILE__) + '/'
class << self
alias_method :original_open_file, :open_file
end
##
# Override to use Patron for http and https, Kernel.open otherwise.
#
# @param [String] filename_or_url to open
# @param [Hash{Symbol => Object}] options
# @option options [Array, String] :headers
# HTTP Request headers.
# @return [IO] File stream
# @yield [IO] File stream
def self.open_file(filename_or_url, **options, &block)
case
when filename_or_url.to_s =~ /^file:/
path = filename_or_url[5..-1]
Kernel.open(path.to_s, options, &block)
when (filename_or_url.to_s =~ %r{^#{REMOTE_PATH}} && Dir.exist?(LOCAL_PATH))
#puts "attempt to open #{filename_or_url} locally"
localpath = filename_or_url.to_s.sub(REMOTE_PATH, LOCAL_PATH)
response = begin
::File.open(localpath)
rescue Errno::ENOENT => e
raise IOError, e.message
end
document_options = {
base_uri: RDF::URI(filename_or_url),
charset: Encoding::UTF_8,
code: 200,
headers: {}
}
#puts "use #{filename_or_url} locally"
document_options[:headers][:content_type] = case filename_or_url.to_s
when /\.ttl$/ then 'text/turtle'
when /\.nt$/ then 'application/n-triples'
when /\.jsonld$/ then 'application/ld+json'
else 'unknown'
end
document_options[:headers][:content_type] = response.content_type if response.respond_to?(:content_type)
# For overriding content type from test data
document_options[:headers][:content_type] = options[:contentType] if options[:contentType]
remote_document = RDF::Util::File::RemoteDocument.new(response.read, **document_options)
if block_given?
yield remote_document
else
remote_document
end
else
original_open_file(filename_or_url, **options, &block)
end
rescue IOError => e
raise IOError, "Error opening #{filename_or_url.inspect}: #{e.message}"
end
end
end
module Fixtures
module SuiteTest
BASE = "https://w3c.github.io/rdf-tests/rdf/"
FRAME = JSON.parse(%q({
"@context": {
"xsd": "http://www.w3.org/2001/XMLSchema#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"mf": "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#",
"mq": "http://www.w3.org/2001/sw/DataAccess/tests/test-query#",
"rdft": "http://www.w3.org/ns/rdftest#",
"label": "rdfs:label",
"baseIri": {"@id": "mf:assumedTestBase", "@type": "@id"},
"comment": "rdfs:comment",
"entries": {"@id": "mf:entries", "@container": "@list"},
"name": "mf:name",
"action": {"@id": "mf:action", "@type": "@id"},
"result": {"@id": "mf:result", "@type": "@id"}
},
"@type": "mf:Manifest",
"entries": {
"@type": [
"rdft:TestTurtlePositiveSyntax",
"rdft:TestTurtleNegativeSyntax",
"rdft:TestNTriplesPositiveSyntax",
"rdft:TestNTriplesNegativeSyntax",
"rdft:TestNTriplesPositiveC14N",
"rdft:TestNTriplesNegativeC14N",
"rdft:TestTurtleEval",
"rdft:TestTurtleNegativeEval"
]
}
}))
class Manifest < JSON::LD::Resource
def self.open(file)
#puts "open: #{file}"
g = RDF::Repository.load(file, format: :ttl)
JSON::LD::API.fromRDF(g) do |expanded|
JSON::LD::API.frame(expanded, FRAME) do |framed|
yield Manifest.new(framed)
end
end
end
# @param [Hash] json framed JSON-LD
# @return [Array<Manifest>]
def self.from_jsonld(json)
json['@graph'].map {|e| Manifest.new(e)}
end
def entries
# Map entries to resources
attributes['entries'].map {|e| Entry.new(e)}
end
end
class Entry < JSON::LD::Resource
attr_accessor :logger
def base
RDF::URI(@base_iri || action)
end
# Alias data and query
def input
@input ||= RDF::Util::File.open_file(action) {|f| f.read}
end
def expected
@expected ||= RDF::Util::File.open_file(result) {|f| f.read} if result
end
def evaluate?
Array(attributes['@type']).join(" ").match(/Eval/)
end
def syntax?
!!Array(attributes['@type']).join(" ").match(/Syntax/)
end
def entailment?
!!Array(attributes['@type']).join(" ").match(/Entailment/)
end
def ntriples?
!!Array(attributes['@type']).join(" ").match(/NTriples/)
end
def positive_test?
!Array(attributes['@type']).join(" ").match(/Negative/)
end
def negative_test?
!positive_test?
end
def c14n?
!!Array(attributes['@type']).join(" ").match(/C14N/)
end
def inspect
super.sub('>', "\n" +
" syntax?: #{syntax?.inspect}\n" +
" positive?: #{positive_test?.inspect}\n" +
" evaluate?: #{evaluate?.inspect}\n" +
" entailment?: #{entailment?.inspect}\n" +
">"
)
end
end
end
end