Skip to content

Commit d42c565

Browse files
committed
Minor cleanup.
1 parent 27f8065 commit d42c565

File tree

4 files changed

+58
-43
lines changed

4 files changed

+58
-43
lines changed

lib/json/ld/expand.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ def expand(input, active_property, context,
132132
unless (keys - KEYS_VALUE_LANGUAGE_TYPE_INDEX_DIRECTION).empty?
133133
# The result must not contain any keys other than @direction, @value, @language, @type, and @index. It must not contain both the @language key and the @type key. Otherwise, an invalid value object error has been detected and processing is aborted.
134134
raise JsonLdError::InvalidValueObject,
135-
"value object has unknown keys: #{output_object.inspect}"
135+
"value object has unknown keys: #{output_object.inspect}"
136136
end
137137

138138
if keys.include?('@type') && !(keys & %w(@language @direction)).empty?
139139
# @type is inconsistent with either @language or @direction
140140
raise JsonLdError::InvalidValueObject,
141-
"value object must not include @type with either @language or @direction: #{output_object.inspect}"
141+
"value object must not include @type with either @language or @direction: #{output_object.inspect}"
142142
end
143143

144144
output_object.delete('@language') if output_object.key?('@language') && Array(output_object['@language']).empty?

script/tc

100755100644
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,23 @@ def run_tc(man, tc, options)
9999
compare_results(tc, output, expected)
100100
when 'jld:FromRDFTest'
101101
repo = RDF::Repository.load(tc.input_loc, format: :nquads)
102-
output = JSON::LD::API.fromRdf(repo, validate: true, **tc.options)
102+
output = if options[:stream]
103+
JSON.parse(JSON::LD::Writer.buffer(stream: true, validate: true, **tc.options) {|w| w << repo})
104+
else
105+
JSON::LD::API.fromRdf(repo, validate: true, **tc.options)
106+
end
103107
expected = JSON.load(tc.expect) if tc.evaluationTest? && tc.positiveTest?
104108
compare_results(tc, output, expected)
105109
when 'jld:ToRDFTest'
106110
output = RDF::Repository.new.extend(RDF::Isomorphic)
107-
JSON::LD::API.toRdf(tc.input_loc, **tc.options).map do |statement|
108-
output << statement
111+
if options[:stream]
112+
JSON::LD::Reader.open(tc.input_loc, stream: true, **tc.options.merge(logger: false)) do |statement|
113+
output << statement
114+
end
115+
else
116+
JSON::LD::API.toRdf(tc.input_loc, **tc.options).map do |statement|
117+
output << statement
118+
end
109119
end
110120

111121
if tc.evaluationTest? && tc.positiveTest?
@@ -122,7 +132,7 @@ def run_tc(man, tc, options)
122132
expected = RDF::Repository.new << RDF::NQuads::Reader.new(tc.expect, validate: false, logger: [])
123133
output.isomorphic?(expected) ? 'passed' : 'failed'
124134
end
125-
rescue RDF::ReaderError
135+
rescue RDF::ReaderError, JSON::LD::JsonLdError
126136
quads = JSON::LD::API.toRdf(tc.input_loc, tc.options.merge(validate: false)).map do |statement|
127137
# Not really RDF, try different test method
128138
tc.to_quad(statement)
@@ -131,14 +141,13 @@ def run_tc(man, tc, options)
131141
# FIXME: toRDF is outputing duplicate quads
132142
output = quads.sort.uniq.join("")
133143
output == tc.expect ? 'passed' : 'failed'
134-
ensure
135-
output = output.dump(:nquads, validate: false) rescue output.to_s unless output.is_a?(String)
136144
end
137145
else
138146
output.count > 0 ? 'passed' : 'failed'
139147
end
140148
end || "untested"
141149

150+
output = output.dump(:nquads, validate: false) rescue output.to_s if output.is_a?(RDF::Enumerable)
142151
puts "\nOutput:\n" + (tc.testType == 'jld:ToRDFTest' ? output : output.to_json(JSON::LD::JSON_STATE)) if !tc.syntaxTest? && options[:verbose]
143152

144153
result = result ? 'failed' : 'passed' unless tc.positiveTest?
@@ -206,6 +215,7 @@ opts = GetoptLong.new(
206215
["--earl", GetoptLong::NO_ARGUMENT],
207216
["--quiet", "-q", GetoptLong::NO_ARGUMENT],
208217
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT],
218+
["--stream", GetoptLong::NO_ARGUMENT],
209219
["--validate", GetoptLong::NO_ARGUMENT],
210220
["--verbose", "-v", GetoptLong::NO_ARGUMENT]
211221
)
@@ -217,6 +227,7 @@ def help(options)
217227
puts " --earl: Generate EARL report"
218228
puts " --quiet: Minimal output"
219229
puts " --output: Output to specified file"
230+
puts " --stream: Use streaming RDF reader/writer"
220231
puts " --validate: Validate input"
221232
puts " --verbose: Verbose processing"
222233
puts " --help,-?: This message"
@@ -234,21 +245,25 @@ opts.each do |opt, arg|
234245
when '--quiet'
235246
options[:quiet] = true
236247
logger.level = Logger::FATAL
248+
when '--stream' then options[:stream] = true
237249
when '--validate' then options[:validate] = true
238250
when '--verbose' then options[:verbose] = true
239251
end
240252
end
241253

242254
manifests = %w(expand compact flatten fromRdf html remote-doc toRdf).map do |man|
243255
"#{Fixtures::SuiteTest::SUITE}#{man}-manifest.jsonld"
244-
end + ["#{Fixtures::SuiteTest::FRAME_SUITE}frame-manifest.jsonld"]
256+
end +
257+
["#{Fixtures::SuiteTest::FRAME_SUITE}frame-manifest.jsonld"] +
258+
["#{Fixtures::SuiteTest::STREAM_SUITE}stream-toRdf-manifest.jsonld"]
245259

246260
earl_preamble(options) if options[:earl]
247261

248262
manifests.each do |man|
249263
Fixtures::SuiteTest::Manifest.open(man) do |m|
250264
m.entries.each do |tc|
251265
next unless ARGV.empty? || ARGV.any? {|n| tc.property('@id').match(/#{n}/) || tc.property('input').match(/#{n}/)}
266+
options = {stream: true}.merge(options) if man.include?('stream')
252267
run_tc(man.sub(".jsonld", ""), tc, options)
253268
end
254269
end

spec/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/uri-cache/
33
/json-ld-api
44
/json-ld-framing
5+
/json-ld-streaming

spec/reader_spec.rb

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,39 @@
4343
{
4444
plain: %q({
4545
"@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
46-
"@id": "_:bnode1",
47-
"@type": "foaf:Person",
48-
"foaf:homepage": "http://example.com/bob/",
49-
"foaf:name": "Bob"
50-
}),
51-
leading_comment: %q(
52-
// A comment before content
53-
{
54-
"@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
55-
"@id": "_:bnode1",
56-
"@type": "foaf:Person",
57-
"foaf:homepage": "http://example.com/bob/",
58-
"foaf:name": "Bob"
59-
}
60-
),
61-
script: %q(<script type="application/ld+json">
62-
{
63-
"@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
64-
"@id": "_:bnode1",
65-
"@type": "foaf:Person",
66-
"foaf:homepage": "http://example.com/bob/",
67-
"foaf:name": "Bob"
68-
}
69-
</script>),
70-
script_comments: %q(<script type="application/ld+json">
71-
// A comment before content
72-
{
73-
"@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
74-
"@id": "_:bnode1",
75-
"@type": "foaf:Person",
76-
"foaf:homepage": "http://example.com/bob/",
77-
"foaf:name": "Bob"
78-
}
79-
</script>),
46+
"@id": "_:bnode1",
47+
"@type": "foaf:Person",
48+
"foaf:homepage": "http://example.com/bob/",
49+
"foaf:name": "Bob"
50+
}),
51+
leading_comment: %q(
52+
// A comment before content
53+
{
54+
"@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
55+
"@id": "_:bnode1",
56+
"@type": "foaf:Person",
57+
"foaf:homepage": "http://example.com/bob/",
58+
"foaf:name": "Bob"
59+
}),
60+
script: %q(<script type="application/ld+json">
61+
{
62+
"@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
63+
"@id": "_:bnode1",
64+
"@type": "foaf:Person",
65+
"foaf:homepage": "http://example.com/bob/",
66+
"foaf:name": "Bob"
67+
}
68+
</script>),
69+
script_comments: %q(<script type="application/ld+json">
70+
// A comment before content
71+
{
72+
"@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
73+
"@id": "_:bnode1",
74+
"@type": "foaf:Person",
75+
"foaf:homepage": "http://example.com/bob/",
76+
"foaf:name": "Bob"
77+
}
78+
</script>),
8079
}.each do |variant, src|
8180
context variant do
8281
subject {src}

0 commit comments

Comments
 (0)