-
Notifications
You must be signed in to change notification settings - Fork 11
/
tc
executable file
·150 lines (131 loc) · 4.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
#!/usr/bin/env ruby
require 'rubygems'
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
require "bundler/setup"
require 'rdf/rdfa'
require 'sparql/grammar'
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'
def run_tc(tc, **options)
input_uri = tc.input(options[:host_language], options[:version])
results_uri = tc.results(options[:host_language], options[:version])
res = nil
STDOUT.write "run #{tc.num}"
if options[:verbose]
puts("\nTestCase: #{tc.inspect}")
puts("\nInput:\n" + RDF::Util::File.open_file(input_uri) {|f| f.read})
puts("\nQuery:\n" + RDF::Util::File.open_file(results_uri) {|f| f.read})
end
pg = RDF::Graph.new if options[:processor_graph]
begin
puts "open #{input_uri}" if options[:verbose]
options = {
base_uri: input_uri,
processor_graph: pg,
format: :rdfa,
}.merge(options)
if tc.queryParam
opt, arg = tc.queryParam.split('=').map(&:to_sym)
options[opt] = arg
end
reader = RDF::RDFa::Reader.open(input_uri, **options)
graph = RDF::Repository.new << reader
rescue Exception => e
options[:result_count]["exception"] ||= 0
options[:result_count]["exception"] += 1
puts "#{"exception:" unless options[:quiet]}: #{e}"
if options[:quiet]
return
else
raise
end
end
puts("\nResult:\n" + graph.dump(options[:format], validate: false)) unless options[:quiet]
begin
result = SPARQL::Grammar.open(results_uri).execute(graph)
rescue Exception => e
options[:result_count]["exception"] ||= 0
options[:result_count]["exception"] += 1
puts "#{"exception:" unless options[:quiet]}: #{e}"
return
end
res = (result == (tc.expectedResults || true)) ? 'pass' : 'fail'
puts "#{"test result:" unless options[:quiet]} #{res}"
options[:result_count][res] ||= 0
options[:result_count][res] += 1
if pg && !options[:quiet]
puts "\nProcessor Graph:\n"
puts pg.inspect
RDF::Writer.for(options[:format]).new do |writer|
writer << pg
end
end
end
logger = Logger.new(STDERR)
logger.level = Logger::WARN
logger.formatter = lambda {|severity, datetime, progname, msg| "#{severity}: #{msg}\n"}
options = {
verbose: false,
quite: false,
validate: false,
format: :ntriples,
library: nil,
processor_graph: nil,
host_language: "xhtml1",
version: "rdfa1.1",
logger: logger
}
opts = GetoptLong.new(
["--help", "-?", GetoptLong::NO_ARGUMENT],
["--debug", GetoptLong::NO_ARGUMENT],
["--format", GetoptLong::REQUIRED_ARGUMENT],
["--host-language", "-h", GetoptLong::REQUIRED_ARGUMENT],
["--library", GetoptLong::REQUIRED_ARGUMENT],
["--processor-graph", GetoptLong::NO_ARGUMENT],
["--quiet", GetoptLong::NO_ARGUMENT],
["--validate", GetoptLong::NO_ARGUMENT],
["--verbose", GetoptLong::NO_ARGUMENT],
["--version", "-v", GetoptLong::OPTIONAL_ARGUMENT]
)
def help(options)
puts "Usage: #{$0} [options] [test-number ...]"
puts "Options:"
puts " --dump: Dump raw output, otherwise serialize to Ruby"
puts " --debug: Display detailed debug output"
puts " --expand Expand graph with vocab_expansion option"
puts " --format: Format for output, defaults to #{options[:format].inspect}"
puts " --host-language: Run for specified host language, defaults to #{options[:host_language]}"
puts " --library: XML parsing library, defaults to :nokogiri if available, otherwise :rexml"
puts " --rdfagraph: output, processor or output,processor"
puts " --quiet: Minimal output"
puts " --validate: Validate input"
puts " --verbose: Verbose processing"
puts " --version: Version of processor to use (rdfa1.0, rdfa1.1). Defaults to #{options[:version]}"
puts " --help,-?: This message"
exit(0)
end
opts.each do |opt, arg|
case opt
when '--help' then help(options)
when '--debug' then logger.level = Logger::DEBUG
when '--format' then options[:format] = arg.to_sym
when '--host-language' then options[:host_language] = arg
when '--library' then options[:library] = arg.to_sym
when '--quiet'
options[:quiet] = true
logger.level = Logger::FATAL
when '--validate' then options[:validate] = true
when '--verbose' then options[:verbose] = true
when '--version' then options[:version] = arg
end
end
result_count = {}
Fixtures::TestCase.for_specific(options[:host_language], options[:version]) do |tc|
next unless ARGV.empty? || ARGV.any? {|n| tc.num.match(/#{n}/)}
next if tc.classification.to_s =~ /deprecated/
run_tc(tc, **options.merge(result_count: result_count))
end
result_count.each do |result, count|
puts "#{result}: #{count}"
end