Skip to content

Commit 70f9a43

Browse files
authored
Merge pull request #2 from w3c/travis
Sync common files and add testing files.
2 parents 44dab22 + 4e7352b commit 70f9a43

File tree

6 files changed

+350
-0
lines changed

6 files changed

+350
-0
lines changed

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
jobs:
2+
include:
3+
- stage: common
4+
script:
5+
- git remote add -f b https://github.com/w3c/json-ld-wg.git
6+
- git remote update
7+
- git diff --exit-code remotes/b/master -- common
8+
- stage: examples
9+
language: ruby
10+
rvm: 2.5
11+
bundler_args: --without debug
12+
script: "rake test"

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source "https://rubygems.org"
2+
3+
gem 'nokogiri'
4+
gem 'linkeddata'
5+
gem 'colorize'
6+
gem 'rake'

Gemfile.lock

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
addressable (2.5.2)
5+
public_suffix (>= 2.0.2, < 4.0)
6+
bcp47 (0.3.3)
7+
i18n
8+
builder (3.2.3)
9+
colorize (0.8.1)
10+
concurrent-ruby (1.0.5)
11+
connection_pool (2.2.2)
12+
ebnf (1.1.2)
13+
rdf (>= 2.2, < 4.0)
14+
sxp (~> 1.0)
15+
equivalent-xml (0.6.0)
16+
nokogiri (>= 1.4.3)
17+
haml (5.0.4)
18+
temple (>= 0.8.0)
19+
tilt
20+
hamster (3.0.0)
21+
concurrent-ruby (~> 1.0)
22+
htmlentities (4.3.4)
23+
i18n (1.0.1)
24+
concurrent-ruby (~> 1.0)
25+
json-ld (2.2.1)
26+
multi_json (~> 1.12)
27+
rdf (>= 2.2.8, < 4.0)
28+
json-ld-preloaded (2.2.3)
29+
json-ld (>= 2.2, < 4.0)
30+
multi_json (~> 1.12)
31+
rdf (>= 2.2, < 4.0)
32+
ld-patch (0.3.3)
33+
ebnf (~> 1.1)
34+
rdf (>= 2.2, < 4.0)
35+
rdf-xsd (>= 2.2, < 4.0)
36+
sparql (>= 2.2, < 4.0)
37+
sxp (~> 1.0)
38+
link_header (0.0.8)
39+
linkeddata (3.0.1)
40+
equivalent-xml (~> 0.6)
41+
json-ld (>= 2.2.1, < 4.0)
42+
ld-patch (~> 0.3, >= 0.3.3)
43+
nokogiri (~> 1.8)
44+
nokogumbo (~> 1.5)
45+
rdf (~> 3.0)
46+
rdf-aggregate-repo (>= 2.2.1, < 4.0)
47+
rdf-isomorphic (~> 3.0)
48+
rdf-json (>= 2.2, < 4.0)
49+
rdf-microdata (>= 2.2.3, < 4.0)
50+
rdf-n3 (~> 3.0)
51+
rdf-normalize (~> 0.3, >= 0.3.3)
52+
rdf-rdfa (~> 3.0)
53+
rdf-rdfxml (>= 2.2.1, < 4.0)
54+
rdf-reasoner (~> 0.5.0)
55+
rdf-tabular (>= 2.2.1, < 4.0)
56+
rdf-trig (>= 2.2, < 4.0)
57+
rdf-trix (>= 2.2.1, < 4.0)
58+
rdf-turtle (~> 3.0, >= 3.0.1)
59+
rdf-vocab (~> 3.0)
60+
rdf-xsd (~> 3.0)
61+
shex (~> 0.5, >= 0.5.2)
62+
sparql (~> 3.0)
63+
sparql-client (~> 3.0)
64+
mini_portile2 (2.3.0)
65+
multi_json (1.13.1)
66+
net-http-persistent (3.0.0)
67+
connection_pool (~> 2.2)
68+
nokogiri (1.8.2)
69+
mini_portile2 (~> 2.3.0)
70+
nokogumbo (1.5.0)
71+
nokogiri
72+
public_suffix (3.0.2)
73+
rake (12.3.1)
74+
rdf (3.0.2)
75+
hamster (~> 3.0)
76+
link_header (~> 0.0, >= 0.0.8)
77+
rdf-aggregate-repo (2.2.1)
78+
rdf (>= 2.2, < 4.0)
79+
rdf-isomorphic (3.0.0)
80+
rdf (~> 3.0)
81+
rdf-json (2.2.0)
82+
rdf (>= 2.2, < 4.0)
83+
rdf-microdata (2.2.3)
84+
htmlentities (~> 4.3)
85+
nokogiri (~> 1.8)
86+
rdf (>= 2.2.8, < 4.0)
87+
rdf-xsd (>= 2.2, < 4.0)
88+
rdf-n3 (3.0.0)
89+
rdf (~> 3.0)
90+
rdf-normalize (0.3.3)
91+
rdf (>= 2.2, < 4.0)
92+
rdf-rdfa (3.0.1)
93+
haml (~> 5.0)
94+
htmlentities (~> 4.3)
95+
rdf (~> 3.0)
96+
rdf-aggregate-repo (>= 2.2, < 4.0)
97+
rdf-xsd (~> 3.0)
98+
rdf-rdfxml (2.2.1)
99+
htmlentities (~> 4.3)
100+
rdf (>= 2.2, < 4.0)
101+
rdf-rdfa (>= 2.2, < 4.0)
102+
rdf-xsd (>= 2.2, < 4.0)
103+
rdf-reasoner (0.5.1)
104+
rdf (~> 3.0)
105+
rdf-vocab (~> 3.0)
106+
rdf-xsd (~> 3.0)
107+
rdf-tabular (2.2.1)
108+
addressable (~> 2.3)
109+
bcp47 (~> 0.3, >= 0.3.3)
110+
json-ld (>= 2.1, < 4.0)
111+
rdf (>= 2.2, < 4.0)
112+
rdf-vocab (>= 2.2, < 4.0)
113+
rdf-xsd (>= 2.2, < 4.0)
114+
rdf-trig (2.2.0)
115+
ebnf (~> 1.0, >= 1.0.1)
116+
rdf (>= 2.2, < 4.0)
117+
rdf-turtle (>= 2.2, < 4.0)
118+
rdf-trix (2.2.1)
119+
rdf (>= 2.2, < 4.0)
120+
rdf-turtle (3.0.1)
121+
ebnf (~> 1.1)
122+
rdf (~> 3.0)
123+
rdf-vocab (3.0.2)
124+
rdf (~> 3.0)
125+
rdf-xsd (3.0.0)
126+
rdf (~> 3.0)
127+
shex (0.5.2)
128+
ebnf (~> 1.1)
129+
json-ld (>= 2.2, < 4.0)
130+
json-ld-preloaded (>= 2.2, < 4.0)
131+
rdf (>= 2.2, < 4.0)
132+
rdf-xsd (>= 2.2, < 4.0)
133+
sparql (>= 2.2, < 4.0)
134+
sxp (~> 1.0)
135+
sparql (3.0.1)
136+
builder (~> 3.2)
137+
ebnf (~> 1.1)
138+
rdf (~> 3.0)
139+
rdf-aggregate-repo (>= 2.2, < 4.0)
140+
rdf-xsd (~> 3.0)
141+
sparql-client (~> 3.0)
142+
sxp (~> 1.0)
143+
sparql-client (3.0.0)
144+
net-http-persistent (>= 2.9, < 4)
145+
rdf (~> 3.0)
146+
sxp (1.0.1)
147+
rdf (>= 2.2, < 4.0)
148+
temple (0.8.0)
149+
tilt (2.0.8)
150+
151+
PLATFORMS
152+
ruby
153+
154+
DEPENDENCIES
155+
colorize
156+
linkeddata
157+
nokogiri
158+
rake
159+
160+
BUNDLED WITH
161+
1.16.1

Rakefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
task default: :test
2+
3+
desc "Test examples in spec files"
4+
task :test do
5+
sh %(bundle exec common/extract-examples.rb index.html)
6+
end
7+
8+
desc "Extract Examples"
9+
task :examples do
10+
sh %(bundle exec common/extract-examples.rb --example-dir examples index.html)
11+
end

common/README.rb

Whitespace-only changes.

common/extract-examples.rb

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#!/usr/bin/env ruby
2+
# Extracts examples from a ReSpec document, verifies that example titles are unique. Numbering attempts to replicate that used by ReSpec. Examples in script elements, which are not visibile, may be used for describing the results of related examples
3+
require 'getoptlong'
4+
require 'json'
5+
require 'nokogiri'
6+
require 'linkeddata'
7+
require 'fileutils'
8+
require 'colorize'
9+
10+
example_dir = nil
11+
opts = GetoptLong.new(
12+
["--example-dir", GetoptLong::REQUIRED_ARGUMENT]
13+
)
14+
opts.each do |opt, arg|
15+
case opt
16+
when '--example-dir' then example_dir = arg
17+
end
18+
end
19+
20+
num_errors = 0
21+
22+
ARGV.each do |input|
23+
$stdout.puts "\ninput: #{input}"
24+
example_number = 0
25+
examples = {}
26+
errors = []
27+
warnings = []
28+
29+
File.open(input, "r") do |f|
30+
doc = Nokogiri::HTML.parse(f.read)
31+
doc.css(".example").each do |element|
32+
warn = false
33+
example_number += 1 if element.name == "pre"
34+
35+
if element.attr('data-ignore') || element.name == "table"
36+
$stdout.write "i".colorize(:yellow)
37+
next
38+
end
39+
40+
if (title = element.attr('title')).to_s.empty?
41+
errors << "Example #{example_number} at line #{element.line} has no title"
42+
$stdout.write "F".colorize(:red)
43+
next
44+
end
45+
46+
if examples.any? {|n, ex| ex[:title] == title}
47+
warnings << "Example #{example_number} at line #{element.line} uses duplicate title: #{title}"
48+
warn = true
49+
end
50+
51+
content = element.inner_html.
52+
sub(/^\s*<!--\s*/m, '').
53+
sub(/s*-->\s*$/m, '').
54+
gsub('****', '').
55+
gsub(/####([^#]*)####/, '')
56+
57+
ext = case element.attr('data-content-type')
58+
when nil, '' then "json"
59+
when 'application/n-quads', 'nq' then 'nq'
60+
when 'text/html', 'html' then 'html'
61+
when 'text/turtle', 'ttl' then 'ttl'
62+
else 'txt'
63+
end
64+
65+
# Perform example syntactic validation based on extension
66+
case ext
67+
when 'json'
68+
begin
69+
::JSON.parse(content)
70+
rescue JSON::ParserError => e
71+
errors << "Example #{example_number} at line #{element.line} parse error: #{e.message}"
72+
$stdout.write "F".colorize(:red)
73+
next
74+
end
75+
when 'html'
76+
begin
77+
doc = Nokogiri::HTML.parse(content) {|c| c.strict}
78+
doc.errors.each do |e|
79+
errors << "Example #{example_number} at line #{element.line} parse error: #{e}"
80+
end
81+
unless doc.errors.empty?
82+
$stdout.write "F".colorize(:red)
83+
next
84+
end
85+
rescue Nokogiri::XML::SyntaxError => e
86+
errors << "Example #{example_number} at line #{element.line} parse error: #{e.message}"
87+
$stdout.write "F".colorize(:red)
88+
next
89+
end
90+
when 'ttl'
91+
begin
92+
reader_errors = []
93+
RDF::Turtle::Reader.new(content, logger: reader_errors) {|r| r.validate!}
94+
rescue
95+
reader_errors.each do |e|
96+
errors << "Example #{example_number} at line #{element.line} parse error: #{e}"
97+
end
98+
$stdout.write "F".colorize(:red)
99+
next
100+
end
101+
when 'nq'
102+
begin
103+
reader_errors = []
104+
RDF::NQuads::Reader.new(content, logger: reader_errors) {|r| r.validate!}
105+
rescue
106+
reader_errors.each do |e|
107+
errors << "Example #{example_number} at line #{element.line} parse error: #{e}"
108+
end
109+
$stdout.write "F".colorize(:red)
110+
next
111+
end
112+
end
113+
114+
case element.name
115+
when "pre"
116+
fn = "example-#{"%03d" % example_number}-#{title.gsub(/[^\w]+/, '-')}.#{ext}"
117+
examples[example_number] = {
118+
title: title,
119+
filename: fn,
120+
content: content
121+
}
122+
#puts "example #{example_number}: #{content}"
123+
when "script"
124+
# Validate the previous example appropriately
125+
else
126+
errors << "Example #{example_number} at line #{element.line} has unknown element type #{element.name}"
127+
$stdout.write "F".colorize(:red)
128+
next
129+
end
130+
131+
if warn
132+
$stdout.write "w".colorize(:yellow)
133+
else
134+
$stdout.write ".".colorize(:green)
135+
end
136+
end
137+
end
138+
139+
$stdout.puts "\nWarnings:" unless warnings.empty?
140+
warnings.each {|e| $stdout.puts " #{e}".colorize(:yellow)}
141+
$stdout.puts "\nErrors:" unless errors.empty?
142+
errors.each {|e| $stdout.puts " #{e}".colorize(:red)}
143+
num_errors += errors.length
144+
145+
if example_dir
146+
# Make examples directory
147+
FileUtils::mkdir_p(example_dir)
148+
examples.each do |num, ex|
149+
File.open(File.join(example_dir, ex[:filename]), 'w') {|f| f.write(ex[:content])}
150+
end
151+
end
152+
end
153+
154+
if num_errors == 0
155+
$stdout.puts "\nok".colorize(:green)
156+
else
157+
exit(1)
158+
end
159+
160+
exit(0)

0 commit comments

Comments
 (0)