-
Notifications
You must be signed in to change notification settings - Fork 14
/
readme_spec.rb
72 lines (67 loc) · 2.45 KB
/
readme_spec.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
$:.unshift File.expand_path("..", __FILE__)
require 'spec_helper'
require 'strscan'
describe "README" do
def self.read_examples
examples = []
readme = File.join(File.expand_path(File.dirname(__FILE__)), "..", "README.md")
scanner = StringScanner.new(File.read(readme))
scanner.skip_until(/^## Examples$/)
until scanner.eos?
break if scanner.matched =~ /## Adding SPARQL content negotiation/
title = scanner.matched.match(/^### (.*)$/) && $1
code = scanner.scan_until(/^##.*$/)
code = code.match(/^(.*)^##.*$/m) && $1
case title
when "Command line processing"
code.split("\n").reject {|c| c =~ /^\s*(?:#.*)?$/}.each do |command|
examples << {title: "#{title}: #{command.strip}", sh: command}
end
else
examples << {title: title, eval_true: code}
end
end
examples
end
read_examples.each do |example|
it "Example #{example[:title] || 'require'}" do
if example[:eval_true]
cmd = example[:eval_true].
gsub('etc', File.join(File.dirname(__FILE__), '..', 'etc'))
eval(cmd)
elsif example[:sh] =~ /sparql server/m
skip "running server"
else
cmd = example[:sh].
sub('sparql', File.join(File.dirname(__FILE__), '..', 'bin', 'sparql')).
sub('etc', File.join(File.dirname(__FILE__), '..', 'etc'))
expect(IO.popen(cmd) {|io| io.read}).to be_truthy
end
end
end
context "Extension Functions" do
let(:repo) {RDF::Repository.new << RDF::Statement.new(:s, RDF::URI("http://schema.org/email"), "gregg@greggkellogg.com")}
let(:query) {%{
PREFIX rsp: <https://rubygems#>
PREFIX schema: <http://schema.org/>
SELECT ?crypted
{
[ schema:email ?email]
BIND(rsp:crypt(?email) AS ?crypted)
}
}}
before(:all) {SPARQL::Algebra::Expression.extensions.clear}
it "returns encrypted string" do
# Register a function using the IRI <https://rubygems#crypt>
crypt_iri = RDF::URI("https://rubygems#crypt")
SPARQL::Algebra::Expression.register_extension(crypt_iri) do |literal|
raise TypeError, "argument must be a literal" unless literal.literal?
RDF::Literal(literal.to_s.crypt("salt"))
end
results = SPARQL.execute(query, repo)
expect(results).to describe_solutions([
RDF::Query::Solution.new({crypted: RDF::Literal("gregg@greggkellogg.net".crypt("salt"))})
], nil)
end
end
end