Skip to content

Commit ad737a0

Browse files
author
Tom Johnson
committed
Add shared example sets for RDF::Literal
1 parent 01f4abb commit ad737a0

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

lib/rdf/spec/literal.rb

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
##
2+
# Shared examples for RDF::Literals are broken up into multiple example sets.
3+
#
4+
# To use the core example sets that apply to most RDF::Literal implementations
5+
# include the examples for `RDF::Literal`, `RDF::Literal validation`,
6+
# `RDF::Literal canonicalization`, and `RDF::Literal lookup`.
7+
8+
shared_examples 'RDF::Literal' do |value, datatype_uri|
9+
include_examples 'RDF::Literal with datatype and grammar', value, datatype_uri
10+
include_examples 'RDF::Literal equality', value
11+
include_examples 'RDF::Literal lexical values', value
12+
end
13+
14+
shared_examples 'RDF::Literal with datatype and grammar' do |value, datatype_uri|
15+
include_examples 'RDF::Literal with grammar'
16+
include_examples 'RDF::Literal with datatype', value, datatype_uri
17+
end
18+
19+
shared_examples 'RDF::Literal lexical values' do |value|
20+
subject { described_class.new(value) }
21+
22+
describe '#humanize' do
23+
it 'gives a string representation' do
24+
expect(subject.humanize).to be_a String
25+
end
26+
end
27+
28+
describe '#to_s' do
29+
it 'gives a string representation' do
30+
expect(subject.to_s).to be_a String
31+
end
32+
end
33+
end
34+
35+
shared_examples 'RDF::Literal with grammar' do
36+
it 'has a GRAMMAR' do
37+
expect(described_class::GRAMMAR).to respond_to :=~
38+
end
39+
end
40+
41+
shared_examples 'RDF::Literal equality' do |value, other|
42+
subject { described_class.new(value) }
43+
44+
describe '#==' do
45+
it { is_expected.to eq subject }
46+
it { expect(subject.object).to eq (other || value) }
47+
it { is_expected.not_to eq described_class.new('OTHER') }
48+
it { is_expected.not_to eq nil }
49+
end
50+
end
51+
52+
shared_examples 'RDF::Literal with datatype' do |value, datatype_uri|
53+
subject { described_class.new(value) }
54+
55+
it { is_expected.to be_literal }
56+
it { is_expected.to be_typed }
57+
it { is_expected.not_to be_plain }
58+
it { is_expected.not_to be_anonymous }
59+
60+
it 'has a DATATYPE' do
61+
expect(described_class::DATATYPE).to be_a RDF::URI
62+
end
63+
64+
it 'has correct datatype' do
65+
expect(subject.datatype).to eq datatype_uri
66+
end
67+
end
68+
69+
shared_examples 'RDF::Literal lookup' do |uri_hash|
70+
uri_hash.each do |uri, klass|
71+
it "finds #{klass} for #{uri}" do
72+
expect(RDF::Literal("0", :datatype => uri).class).to eq klass
73+
end
74+
end
75+
end
76+
77+
shared_examples 'RDF::Literal canonicalization' do |datatype, pairs|
78+
pairs.each do |value, str|
79+
klass = RDF::Literal.datatyped_class(datatype.to_s)
80+
81+
it "does not normalize '#{value}' by default" do
82+
expect(RDF::Literal.new(value,
83+
datatype: datatype ,
84+
canonicalize: false).to_s)
85+
.to eq value
86+
end
87+
88+
it "normalizes double '#{value}' to '#{str}'" do
89+
expect(RDF::Literal.new(value,
90+
datatype: datatype,
91+
canonicalize: true).to_s)
92+
.to eq str
93+
end
94+
95+
it "humanizes double '#{value}' to '#{str}'" do
96+
expect(RDF::Literal.new(value,
97+
datatype: datatype,
98+
canonicalize: false).humanize)
99+
.to eq value
100+
end
101+
102+
it "instantiates '#{value}' as #{klass}" do
103+
expect(RDF::Literal.new(value,
104+
datatype: datatype,
105+
canonicalize: true))
106+
.to be_a(klass)
107+
end
108+
109+
it "causes normalized '#{value}' to be == '#{str}'" do
110+
expect(RDF::Literal.new(value,
111+
datatype: datatype,
112+
canonicalize: true))
113+
.to eq RDF::Literal.new(str, datatype: datatype, canonicalize: false)
114+
end
115+
end
116+
end
117+
118+
shared_examples 'RDF::Literal validation' do |datatype,
119+
valid_values,
120+
invalid_values|
121+
122+
klass = RDF::Literal.datatyped_class(datatype.to_s)
123+
124+
valid_values.each do |value|
125+
it "validates #{klass} '#{value}'" do
126+
expect(RDF::Literal.new(value, datatype: datatype)).to be_valid
127+
end
128+
129+
it "does not invalidate #{klass} '#{value}'" do
130+
expect(RDF::Literal.new(value, datatype: datatype)).not_to be_invalid
131+
end
132+
end
133+
134+
invalid_values.each do |value|
135+
it "invalidates #{klass} '#{value}'" do
136+
expect(RDF::Literal.new(value, datatype: datatype)).to be_invalid
137+
end
138+
139+
it "does not validate #{klass} '#{value}'" do
140+
expect(RDF::Literal.new(value, datatype: datatype)).not_to be_valid
141+
end
142+
end
143+
end

0 commit comments

Comments
 (0)