-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathreadme_spec.rb
235 lines (213 loc) · 7.22 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
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
226
227
228
229
230
231
232
233
234
235
# -*- encoding: utf-8 -*-
require_relative 'spec_helper'
require 'rdf/ntriples'
require 'fileutils'
describe 'README' do
around(:example) do |example|
tmpdir = File.join(File.dirname(__FILE__), '..', 'tmp')
FileUtils.mkdir_p(tmpdir)
Dir.chdir(tmpdir, &example)
end
context "the 'Writing RDF data using the N-Triples format' example" do
after(:each) { File.delete('hello.nt') }
{
example1: lambda {
require 'rdf/ntriples'
RDF::Writer.open("hello.nt") do |writer|
writer << RDF::Graph.new do |graph|
graph << [:hello, RDF::RDFS.label, "Hello, world!"]
end
end
},
example2: lambda {
require 'rdf/ntriples'
graph = RDF::Graph.new << [:hello, RDF::RDFS.label, "Hello, world!"]
File.open("hello.nt", "w") {|f| f << graph.dump(:ntriples)}
}
}.each do |example, code|
context example do
before(:each) {code.call}
it {expect {code.call}.not_to raise_error}
it "should not have output" do
expect {code.call}.not_to write.to(:error)
end
it "should produce a hello.nt file" do
expect(File).to exist('hello.nt')
expect(File.stat('hello.nt')).to be_file
end
it "should produce the expected data" do
expect(File.read('hello.nt')).to eq %Q(_:hello <http://www.w3.org/2000/01/rdf-schema#label> "Hello, world!" .\n)
end
end
end
end
context "the 'Reading RDF data in the N-Triples format' example" do
{
example0: lambda {
require 'rdf/ntriples'
RDF::NTriples::Reader.open("https://ruby-rdf.github.io/rdf/etc/doap.nt") do |reader|
reader.each_statement do |statement|
puts statement.inspect
end
end
},
example1: lambda {
require 'rdf/ntriples'
RDF::NTriples::Reader.open(File.expand_path("../../etc/doap.nt", __FILE__)) do |reader|
reader.each_statement do |statement|
puts statement.inspect
end
end
}
}.each do |example, code|
context example do
subject {
if example == :example0
expect(RDF::Util::File).to receive(:open_file).
with("https://ruby-rdf.github.io/rdf/etc/doap.nt", hash_including(:headers)).
at_least(1).
and_yield(Kernel.open(File.expand_path("../../etc/doap.nt", __FILE__)))
end
code.call
}
it "should have output" do
expect {subject}.to write(/^\#<RDF::Statement:0x[\da-fA-F]+\(.*?\)>\Z/)
end
end
end
end
context "the 'Reading RDF data in other formats' example" do
{
example0: lambda {
require 'rdf/nquads'
graph = RDF::Graph.load("https://ruby-rdf.github.io/rdf/etc/doap.nq", format: :nquads)
graph.each_statement do |statement|
puts statement.inspect
end
},
example1: lambda {
require 'rdf/nquads'
RDF::NQuads::Reader.open(File.expand_path("../../etc/doap.nq", __FILE__)) do |reader|
reader.each_statement do |statement|
puts statement.inspect
end
end
}
}.each do |example, code|
context example do
subject {
if example == :example0
expect(RDF::Util::File).to receive(:open_file).
with("https://ruby-rdf.github.io/rdf/etc/doap.nq", hash_including(:headers, base_uri: "https://ruby-rdf.github.io/rdf/etc/doap.nq")).
at_least(1).
and_yield(Kernel.open(File.expand_path("../../etc/doap.nq", __FILE__)))
end
code.call
}
it "should output inspected statements" do
expect {subject}.to write(/^\#<RDF::Statement:0x[\da-fA-F]+\(.*?\)>\Z/)
end
end
end
end
context "the 'Writing RDF data using other formats' example" do
after(:each) { File.delete('hello.nq') }
{
example1: lambda {
require 'rdf/nquads'
RDF::Writer.open("hello.nq") do |writer|
writer << RDF::Repository.new do |repo|
repo << RDF::Statement.new(:hello, RDF::RDFS.label, "Hello, world!", graph_name: RDF::URI("http://example/context"))
end
end
},
example2: lambda {
require 'rdf/nquads'
repo = RDF::Repository.new << RDF::Statement.new(:hello, RDF::RDFS.label, "Hello, world!", graph_name: RDF::URI("http://example/context"))
File.open("hello.nq", "w") {|f| f << repo.dump(:nquads)}
},
}.each do |example, code|
context example do
subject {code.call}
it "should not have output" do
expect {subject}.not_to write(:anything)
end
it "should produce a hello.nq file" do
subject
expect(File).to exist('hello.nq')
expect(File.stat('hello.nq')).to be_file
end
it "should produce the expected data" do
subject
expect(File.read('hello.nq')).to eq %Q(_:hello <http://www.w3.org/2000/01/rdf-schema#label> "Hello, world!" <http://example/context> .\n)
end
end
end
end
context "the 'Using pre-defined RDF vocabularies' example" do
subject do
RDF.type #=> RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
RDF::RDFS.seeAlso #=> RDF::URI("http://www.w3.org/2000/01/rdf-schema#seeAlso")
RDF::OWL.sameAs #=> RDF::URI("http://www.w3.org/2002/07/owl#sameAs")
RDF::XSD.dateTime #=> RDF::URI("http://www.w3.org/2001/XMLSchema#dateTime")
end
it "should not raise errors" do
expect { subject }.not_to raise_error
end
end
context "the 'Querying RDF data using basic graph patterns (BGPs)' example" do
require 'rdf/ntriples'
let(:graph) {RDF::Graph.load(File.expand_path("../../etc/doap.nt", __FILE__))}
let(:query) do
RDF::Query.new({
person: {
RDF.type => RDF::Vocab::FOAF.Person,
RDF::Vocab::FOAF.name => :name,
RDF::Vocab::FOAF.mbox => :email,
}
}, **{})
end
context "using query" do
subject {
query.execute(graph) do |solution|
puts "name=#{solution.name} email=#{solution.email}"
end
}
[
"name=Arto Bendiken",
"name=Ben Lavender",
"name=Gregg Kellogg",
"email="
].each do |re|
specify { expect {subject}.to write(re)}
end
end
context "using graph" do
subject {
graph.query(query) do |solution|
puts "name=#{solution.name} email=#{solution.email}"
end
}
[
"name=Arto Bendiken",
"name=Ben Lavender",
"name=Gregg Kellogg",
"email="
].each do |re|
specify { expect {subject}.to write(re)}
end
end
end
context "the 'Using ad-hoc RDF vocabularies' example" do
{
method: lambda {RDF::Vocabulary.new("http://xmlns.com/foaf/0.1/").knows},
symbol: lambda {RDF::Vocabulary.new("http://xmlns.com/foaf/0.1/")[:knows]},
string: lambda {RDF::Vocabulary.new("http://xmlns.com/foaf/0.1/")['knows']}
}.each do |example, code|
context example do
it {expect {code.call}.not_to raise_error}
it {expect(code.call).to eq RDF::Vocab::FOAF.knows}
end
end
end
end