-
Notifications
You must be signed in to change notification settings - Fork 2
/
tc
executable file
·225 lines (195 loc) · 6.99 KB
/
tc
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env ruby
require 'rubygems'
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
require "bundler/setup"
require 'logger'
require 'rdf/trig'
require 'rdf/isomorphic'
require File.expand_path(File.join(File.dirname(__FILE__), "..", 'spec', 'spec_helper'))
require File.expand_path(File.join(File.dirname(__FILE__), "..", 'spec', 'suite_helper'))
require 'getoptlong'
ASSERTOR = "https://greggkellogg.net/foaf#me"
RUN_TIME = Time.now
def earl_preamble(**options)
options[:output].write File.read(File.expand_path("../../etc/doap#{'-nquads' if options[:nquads]}.ttl", __FILE__))
options[:output].puts %(
<> foaf:primaryTopic <https://rubygems.org/gems/rdf#{'-trig' unless options[:nquads]}> ;
dc:issued "#{RUN_TIME.xmlschema}"^^xsd:dateTime ;
foaf:maker <#{ASSERTOR}> .
<#{ASSERTOR}> a foaf:Person, earl:Assertor;
foaf:name "Gregg Kellogg";
foaf:title "Implementor";
foaf:homepage <https://greggkellogg.net/> .
)
options[:output].puts options[:nquads] ? %(
<https://rubygems.org/gems/rdf>
doap:release <https://github.com/ruby-rdf/rdf/tree/#{RDF::VERSION}> .
<https://github.com/ruby-rdf/rdf/tree/#{RDF::VERSION}> a doap:Version;
doap:name "RDF.rb-#{RDF::VERSION}";
doap:revision "#{RDF::VERSION}" .
) : %(
<https://rubygems.org/gems/rdf-trig>
doap:release <https://github.com/ruby-rdf/rdf-trig/tree/#{RDF::TriG::VERSION}> .
<https://github.com/ruby-rdf/rdf-trig/tree/#{RDF::TriG::VERSION}> a doap:Version;
doap:name "rdf-turtle-#{RDF::TriG::VERSION}";
doap:created "#{File.mtime(File.expand_path('../../VERSION', __FILE__)).strftime('%Y-%m-%d')}"^^xsd:date;
doap:revision "#{RDF::TriG::VERSION}" .
)
end
def run_tc(tc, **options)
STDERR.write "run #{tc.name}"
if options[:verbose]
puts "\nTestCase: #{tc.inspect}"
puts "\nInput:\n" + tc.input
puts "\nExpected:\n" + tc.expected if tc.result
end
logger = options[:live] ? Logger.new(STDERR) : RDF::Spec.logger
logger.level = options[:level]
logger.formatter = lambda {|severity, datetime, progname, msg| "%5s %s\n" % [severity, msg]}
begin
puts "open #{tc.action}" if options[:verbose]
options = {
base_uri: tc.base,
validate: true,
logger: logger
}.merge(options)
reader = RDF::Reader.for(tc.action).new(tc.input, **options)
graph = RDF::Repository.new
result = nil
if tc.positive_test?
begin
graph << reader
rescue Exception => e
STDERR.puts "Unexpected exception: #{e.inspect}" if options[:verbose]
result = "failed"
end
else
begin
graph << reader
STDERR.puts "Expected exception" if options[:verbose]
result = "failed"
rescue RDF::ReaderError
result = "passed"
end
end
if tc.evaluate? && result.nil?
output_graph = RDF::Repository.load(tc.result, format: :nquads, base_uri: tc.base, rdfstar: true)
result = graph.isomorphic_with?(output_graph) ? "passed" : "failed"
else
result ||= "passed"
end
rescue Interrupt => e
raise e
rescue Exception => e
STDERR.puts "#{"exception:" unless options[:quiet]}: #{e}"
if options[:quiet]
return
else
raise
end
end
STDERR.puts options[:logger] if options[:verbose] && !options[:live]
if options[:earl]
options[:output].puts %{
[ a earl:Assertion;
earl:assertedBy <#{ASSERTOR}>;
earl:subject <https://rubygems.org/gems/rdf#{'-trig' unless options[:nquads]}>;
earl:test <#{tc.id}>;
earl:result [
a earl:TestResult;
earl:outcome earl:#{result};
dc:date "#{RUN_TIME.xmlschema}"^^xsd:dateTime];
earl:mode earl:automatic ] .
}
end
options[:result_count][result] ||= 0
options[:result_count][result] += 1
puts "#{"test result:" unless options[:quiet]} #{result}"
end
options = {
output: STDOUT,
level: Logger::WARN
}
OPT_ARGS = [
["--debug", GetoptLong::NO_ARGUMENT, "Debugging output"],
["--earl", GetoptLong::NO_ARGUMENT, "Generate EARL report"],
["--help", "-?", GetoptLong::NO_ARGUMENT, "print this message"],
["--info", GetoptLong::NO_ARGUMENT, "Show progress on execution"],
["--live", GetoptLong::NO_ARGUMENT, "Show live parsing results, not buffered"],
["--nquads", GetoptLong::NO_ARGUMENT, "Run N-Quads tests"],
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT, "Output to specified file"],
["--quiet", "-q", GetoptLong::NO_ARGUMENT, "Minimal output"],
["--rdfstar", GetoptLong::NO_ARGUMENT, "Run RDF-star tests"],
["--skip-slow", "-s", GetoptLong::NO_ARGUMENT, "Avoid files taking too much time"],
["--validate", GetoptLong::NO_ARGUMENT, "Validate input"],
["--verbose", "-v", GetoptLong::NO_ARGUMENT, "Verbose output"],
#["--write-manifests", GetoptLong::NO_ARGUMENT, "Write out the parsed manifests for earl reporting"],
]
def usage(**options)
STDERR.puts %{
TriG version #{RDF::TriG::VERSION}
Run TriG tests.
Usage: #{$0} [options] [test-number ...]
}.gsub(/^ /, '')
width = OPT_ARGS.map do |o|
l = o.first.length
l += o[1].length + 2 if o[1].is_a?(String)
l
end.max
OPT_ARGS.each do |o|
s = " %-*s " % [width, (o[1].is_a?(String) ? "#{o[0,2].join(', ')}" : o[0])]
s += o.last
STDERR.puts s
end
exit(1)
end
opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
opts.each do |opt, arg|
case opt
when '--help' then usage(**options)
when '--debug' then options[:level] = Logger::DEBUG
when '--earl'
options[:quiet] = options[:earl] = true
options[:level] = Logger::FATAL
when '--info' then options[:level] = Logger::INFO
when '--live' then options[:live] = true
when '--nquads' then options[:nquads] = true
when '--output' then options[:output] = File.open(arg, "w")
when '--quiet'
options[:quiet] = true
options[:level] = Logger::FATAL
when '--rdfstar' then options[:rdfstar] = true
when '--skip-slow' then options[:slow] = true
when '--validate' then options[:validate] = true
when '--verbose' then options[:verbose] = true
# when '--write-manifests' then options[:write_manifests] = true
end
end
manifests = if options[:rdfstar]
%w{nt/syntax turtle/syntax turtle/eval trig/syntax trig/eval}.map do
|man| "https://w3c.github.io/rdf-star/tests/#{man}/"
end
elsif options[:nquads]
[Fixtures::SuiteTest::BASE + "rdf11/rdf-n-quads/"]
else
[Fixtures::SuiteTest::BASE + "rdf11/rdf-trig/"]
end.map {|m| "#{m}manifest.ttl"}
earl_preamble(**options) if options[:earl]
result_count = {}
begin
manifests.each do |manifest|
Fixtures::SuiteTest::Manifest.open(manifest) do |m|
m.entries.each do |tc|
next unless ARGV.empty? || ARGV.any? {|n| tc.name.match(/#{n}/)}
run_tc(tc, result_count: result_count, **options)
end
end
end
rescue Interrupt => e
STDERR.puts "(interrupt)"
STDERR.puts "Backtrace: " + e.backtrace.join("\n ") if options[:verbose]
exit 1
end
result_count.each do |result, count|
puts "#{result}: #{count}"
end