-
Notifications
You must be signed in to change notification settings - Fork 7
/
suite_helper.rb
165 lines (146 loc) · 4.88 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
# For manipulating test-manifest style test suites.
# Used for RDF/XML 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("../w3c-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 /\.html$/ then 'text/html'
when /\.xhtml$/ then 'application/xhtml+xml'
when /\.xml$/ then 'application/xml'
when /\.svg$/ then 'image/svg+xml'
when /\.ttl$/ then 'text/turtle'
when /\.ttl$/ then 'text/turtle'
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
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#",
"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:TestXMLEval",
"rdft:TestXMLNegativeSyntax"
]
}
}))
class Manifest < JSON::LD::Resource
def self.open(file)
#puts "open: #{file}"
prefixes = {}
g = RDF::Repository.load(file, format: :turtle)
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(action)
end
# Alias data and query
def input
RDF::Util::File.open_file(action) {|f| f.read}
end
def expected
RDF::Util::File.open_file(result) {|f| f.read}
end
def evaluate?
attributes['@type'].to_s.match(/Eval/)
end
def syntax?
attributes['@type'].to_s.match(/Syntax/)
end
def positive_test?
!attributes['@type'].to_s.match(/Negative/)
end
def negative_test?
!positive_test?
end
def inspect
super.sub('>', "\n" +
" syntax?: #{syntax?.inspect}\n" +
" positive?: #{positive_test?.inspect}\n" +
" evaluate?: #{evaluate?.inspect}\n" +
">"
)
end
end
end
end