json-ld 0.1.6.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -60,130 +60,49 @@ module RDF::Util
60
60
  end
61
61
 
62
62
  module Fixtures
63
- module JSONLDTest
63
+ module SuiteTest
64
64
  SUITE = RDF::URI("http://json-ld.org/test-suite/")
65
- class Test < RDF::Vocabulary("http://www.w3.org/2006/03/test-description#"); end
66
- class Jld < RDF::Vocabulary("http://json-ld.org/test-suite/vocab#"); end
65
+ class Manifest < JSON::LD::Resource
66
+ def self.open(file)
67
+ #puts "open: #{file}"
68
+ RDF::Util::File.open_file(file) do |f|
69
+ json = JSON.parse(f.read)
70
+ self.from_jsonld(json)
71
+ end
72
+ end
67
73
 
68
- class Manifest < Spira::Base
69
- type Jld.Manifest
70
- property :name, :predicate => RDF::DC.title, :type => XSD.string
71
- property :comment, :predicate => RDF::RDFS.comment, :type => XSD.string
72
- property :sequence, :predicate => Jld.sequence
73
-
74
- def entries
75
- @entries ||= begin
76
- repo = self.class.repository
77
- RDF::List.new(sequence, repo).map do |entry|
78
- results = repo.query(:subject => entry, :predicate => RDF.type)
79
- entry_types = results.map(&:object)
74
+ # @param [Hash] json framed JSON-LD
75
+ # @return [Array<Manifest>]
76
+ def self.from_jsonld(json)
77
+ Manifest.new(json)
78
+ end
80
79
 
81
- # Load entry if it is not in repo
82
- if entry_types.empty?
83
- repo.load(entry, :format => :jsonld)
84
- entry_types = repo.query(:subject => entry, :predicate => RDF.type).map(&:object)
85
- end
86
-
87
- case
88
- when entry_types.include?(Jld.Manifest) then entry.as(Manifest)
89
- when entry_types.include?(Jld.CompactTest) then entry.as(CompactTest)
90
- when entry_types.include?(Jld.ExpandTest) then entry.as(ExpandTest)
91
- when entry_types.include?(Jld.FrameTest) then entry.as(FrameTest)
92
- when entry_types.include?(Jld.NormalizeTest) then entry.as(NormalizeTest)
93
- when entry_types.include?(Jld.ToRDFTest) then entry.as(ToRDFTest)
94
- when entry_types.include?(Jld.FromRDFTest) then entry.as(FromRDFTest)
95
- when entry_types.include?(Test.TestCase) then entry.as(Entry)
96
- else raise "Unexpected entry type: #{entry_types.inspect}"
97
- end
98
- end
80
+ def entries
81
+ # Map entries to resources
82
+ attributes['sequence'].map do |e|
83
+ e.is_a?(String) ? Manifest.open("#{SUITE}#{e}") : Entry.new(e)
99
84
  end
100
85
  end
101
-
102
- def inspect
103
- "[#{self.class.to_s} " + %w(
104
- subject
105
- name
106
- ).map {|a| v = self.send(a); "#{a}='#{v}'" if v}.compact.join(", ") +
107
- ", entries=#{entries.length}" +
108
- "]"
109
- end
110
86
  end
111
87
 
112
- class Entry
88
+ class Entry < JSON::LD::Resource
113
89
  attr_accessor :debug
114
- include Spira::Resource
115
- type Test.TestCase
116
-
117
- property :name, :predicate => RDF::DC.title, :type => XSD.string
118
- property :purpose, :predicate => Test.purpose, :type => XSD.string
119
- property :expected, :predicate => Test.expectedResults
120
- property :inputDocument, :predicate => Test.informationResourceInput
121
- property :resultDocument, :predicate => Test.informationResourceResults
122
- property :extraDocument, :predicate => Test.input
123
-
124
- def information; name; end
125
90
 
126
- def input
127
- RDF::Util::File.open_file(self.inputDocument)
91
+ # Base is expanded input file
92
+ def base
93
+ "#{SUITE}tests/#{property('input')}"
128
94
  end
129
95
 
130
- def extra
131
- RDF::Util::File.open_file(self.extraDocument)
132
- end
133
-
134
- def expect
135
- RDF::Util::File.open_file(self.resultDocument)
96
+ # Alias input, context, expect and frame
97
+ %w(input context expect frame).each do |m|
98
+ define_method(m.to_sym) {RDF::Util::File.open_file "#{SUITE}tests/#{property(m)}"}
136
99
  end
137
100
 
138
- def base_uri
139
- inputDocument.to_s
101
+ def positiveTest
102
+ property('positiveTest') == 'true'
140
103
  end
141
104
 
142
- def trace
143
- @debug.to_a.join("\n")
144
- end
145
-
146
- def inspect
147
- "[#{self.class.to_s} " + %w(
148
- subject
149
- name
150
- inputDocument
151
- resultDocument
152
- extraDocument
153
- ).map {|a| v = self.send(a); "#{a}='#{v}'" if v}.compact.join(", ") +
154
- "]"
155
- end
156
- end
157
-
158
- class CompactTest < Entry
159
- type Jld.CompactTest
160
- end
161
-
162
- class ExpandTest < Entry
163
- type Jld.ExpandTest
164
- end
165
-
166
- class FrameTest < Entry
167
- type Jld.FameTest
168
- end
169
-
170
- class NormalizeTest < Entry
171
- type Jld.NormalizeTest
172
- end
173
-
174
- class FromRDFTest < Entry
175
- type Jld.FromRDFTest
176
- end
177
-
178
- class ToRDFTest < Entry
179
- type Jld.ToRDFTest
180
-
181
- def quads
182
- RDF::Util::File.open_file(self.expected)
183
- end
105
+ def trace; @debug.join("\n"); end
184
106
  end
185
-
186
- repo = RDF::Repository.load(SUITE.join("manifest.jsonld"), :format => :jsonld)
187
- Spira.add_repository! :default, repo
188
107
  end
189
108
  end
@@ -0,0 +1,76 @@
1
+ # coding: utf-8
2
+ $:.unshift "."
3
+ require 'spec_helper'
4
+
5
+ describe JSON::LD do
6
+ describe "test suite" do
7
+ require 'suite_helper'
8
+ m = Fixtures::SuiteTest::Manifest.open('http://json-ld.org/test-suite/tests/toRdf-manifest.jsonld')
9
+ describe m.name do
10
+ m.entries.each do |t|
11
+ specify "#{t.property('input')}: #{t.name}" do
12
+ begin
13
+ t.debug = ["test: #{t.inspect}", "source: #{t.input.read}"]
14
+ quads = []
15
+ JSON::LD::API.toRDF(t.input, nil, nil,
16
+ :base => t.base,
17
+ :debug => t.debug) do |statement|
18
+ quads << to_quad(statement)
19
+ end
20
+
21
+ sorted_expected = t.expect.readlines.sort.join("")
22
+ quads.sort.join("").should produce(sorted_expected, t.debug)
23
+ rescue JSON::LD::ProcessingError => e
24
+ fail("Processing error: #{e.message}")
25
+ rescue JSON::LD::InvalidContext => e
26
+ fail("Invalid Context: #{e.message}")
27
+ rescue JSON::LD::InvalidFrame => e
28
+ fail("Invalid Frame: #{e.message}")
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ # Don't use NQuads writer so that we don't escape Unicode
36
+ def to_quad(thing)
37
+ case thing
38
+ when RDF::URI
39
+ "<#{escaped(thing.to_s)}>"
40
+ when RDF::Node
41
+ escaped(thing.to_s)
42
+ when RDF::Literal::Double
43
+ case
44
+ when thing.object.nan?, thing.object.infinite?, thing.object.zero?
45
+ thing.canonicalize.to_ntriples
46
+ else
47
+ i, f, e = ('%.15E' % thing.object.to_f).split(/[\.E]/)
48
+ f.sub!(/0*$/, '') # remove any trailing zeroes
49
+ f = '0' if f.empty? # ...but there must be a digit to the right of the decimal point
50
+ e.sub!(/^\+?0+(\d)$/, '\1') # remove the optional leading '+' sign and any extra leading zeroes
51
+ %("#{i}.#{f}E#{e}"^^<http://www.w3.org/2001/XMLSchema#double>)
52
+ end
53
+ when RDF::Literal
54
+ quoted(escaped(thing.value)) +
55
+ (thing.datatype? ? "^^<#{thing.datatype}>" : "") +
56
+ (thing.language? ? "@#{thing.language}" : "")
57
+ when RDF::Statement
58
+ thing.to_quad.map {|r| to_quad(r)}.compact.join(" ") + " .\n"
59
+ end
60
+ end
61
+
62
+ ##
63
+ # @param [String] string
64
+ # @return [String]
65
+ def quoted(string)
66
+ "\"#{string}\""
67
+ end
68
+
69
+ ##
70
+ # @param [String] string
71
+ # @return [String]
72
+ def escaped(string)
73
+ string.gsub('\\', '\\\\').gsub("\t", '\\t').
74
+ gsub("\n", '\\n').gsub("\r", '\\r').gsub('"', '\\"')
75
+ end
76
+ end unless ENV['CI']
@@ -96,6 +96,12 @@ describe JSON::LD::API do
96
96
  }),
97
97
  %q([ a <http://example.com/foo>, <http://example.com/baz> ] .)
98
98
  ],
99
+ "blank node type" => [
100
+ %q({
101
+ "@type": "_:foo"
102
+ }),
103
+ %q([ a _:a ] .)
104
+ ]
99
105
  }.each do |title, (js, nt)|
100
106
  it title do
101
107
  parse(js).should be_equivalent_graph(nt, :trace => @debug, :inputDocument => js)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-ld
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-24 00:00:00.000000000 Z
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdf
@@ -242,6 +242,7 @@ files:
242
242
  - lib/json/ld/frame.rb
243
243
  - lib/json/ld/from_rdf.rb
244
244
  - lib/json/ld/reader.rb
245
+ - lib/json/ld/resource.rb
245
246
  - lib/json/ld/to_rdf.rb
246
247
  - lib/json/ld/utils.rb
247
248
  - lib/json/ld/version.rb
@@ -257,9 +258,14 @@ files:
257
258
  - spec/from_rdf_spec.rb
258
259
  - spec/matchers.rb
259
260
  - spec/reader_spec.rb
261
+ - spec/resource_spec.rb
260
262
  - spec/spec_helper.rb
263
+ - spec/suite_compact_spec.rb
264
+ - spec/suite_expand_spec.rb
265
+ - spec/suite_frame_spec.rb
266
+ - spec/suite_from_rdf_spec.rb
261
267
  - spec/suite_helper.rb
262
- - spec/suite_spec.rb
268
+ - spec/suite_to_rdf_spec.rb
263
269
  - spec/support/extensions.rb
264
270
  - spec/to_rdf_spec.rb
265
271
  - spec/writer_spec.rb
@@ -356,9 +362,14 @@ test_files:
356
362
  - spec/from_rdf_spec.rb
357
363
  - spec/matchers.rb
358
364
  - spec/reader_spec.rb
365
+ - spec/resource_spec.rb
359
366
  - spec/spec_helper.rb
367
+ - spec/suite_compact_spec.rb
368
+ - spec/suite_expand_spec.rb
369
+ - spec/suite_frame_spec.rb
370
+ - spec/suite_from_rdf_spec.rb
360
371
  - spec/suite_helper.rb
361
- - spec/suite_spec.rb
372
+ - spec/suite_to_rdf_spec.rb
362
373
  - spec/support/extensions.rb
363
374
  - spec/to_rdf_spec.rb
364
375
  - spec/writer_spec.rb
@@ -1,105 +0,0 @@
1
- # coding: utf-8
2
- $:.unshift "."
3
- require 'spec_helper'
4
-
5
- describe JSON::LD do
6
- describe "test suite" do
7
- require 'suite_helper'
8
-
9
- if m = Fixtures::JSONLDTest::Manifest.each.to_a.first
10
- describe m.name do
11
- m.entries.each do |m2|
12
- describe m2.name do
13
- m2.entries.each do |t|
14
- next if t.is_a?(Fixtures::JSONLDTest::NormalizeTest)
15
- specify "#{File.basename(t.inputDocument.to_s)}: #{t.name}" do
16
- begin
17
- t.debug = ["test: #{t.inspect}", "source: #{t.input.read}"]
18
- case t
19
- when Fixtures::JSONLDTest::CompactTest
20
- t.debug << "context: #{t.extra.read}" if t.extraDocument
21
- result = JSON::LD::API.compact(t.input, t.extra, nil,
22
- :base => t.base_uri,
23
- :debug => t.debug)
24
- expected = JSON.load(t.expect)
25
- result.should produce(expected, t.debug)
26
- when Fixtures::JSONLDTest::ExpandTest
27
- t.debug << "context: #{t.extra.read}" if t.extraDocument
28
- result = JSON::LD::API.expand(t.input, nil, nil,
29
- :base => t.base_uri,
30
- :debug => t.debug)
31
- expected = JSON.load(t.expect)
32
- result.should produce(expected, t.debug)
33
- when Fixtures::JSONLDTest::FrameTest
34
- t.debug << "frame: #{t.extra.read}" if t.extraDocument
35
- result = JSON::LD::API.frame(t.input, t.extra, nil,
36
- :base => t.inputDocument,
37
- :debug => t.debug)
38
- expected = JSON.load(t.expect)
39
- result.should produce(expected, t.debug)
40
- when Fixtures::JSONLDTest::FromRDFTest
41
- repo = RDF::Repository.load(t.inputDocument)
42
- result = JSON::LD::API.fromRDF(repo.each_statement.to_a, nil,
43
- :debug => t.debug)
44
- expected = JSON.load(t.expect)
45
- result.should produce(expected, t.debug)
46
- when Fixtures::JSONLDTest::ToRDFTest
47
- quads = []
48
- JSON::LD::API.toRDF(t.input, nil, nil,
49
- :base => t.inputDocument,
50
- :debug => t.debug) do |statement|
51
- quads << to_quad(statement)
52
- end
53
-
54
- quads.sort.join("").should produce(t.expect.read, t.debug)
55
- else
56
- pending("unkown test type #{t.inspect}")
57
- end
58
- rescue JSON::LD::ProcessingError => e
59
- fail("Processing error: #{e.message}")
60
- rescue JSON::LD::InvalidContext => e
61
- fail("Invalid Context: #{e.message}")
62
- rescue JSON::LD::InvalidFrame => e
63
- fail("Invalid Frame: #{e.message}")
64
- end
65
- end
66
- end
67
- end
68
- end
69
- end
70
- end
71
- end
72
-
73
- # Don't use NQuads writer so that we don't escape Unicode
74
- def to_quad(thing)
75
- case thing
76
- when RDF::URI
77
- "<#{escaped(thing.to_s)}>"
78
- when RDF::Node
79
- escaped(thing.to_s)
80
- when RDF::Literal::Double
81
- quoted("%1.15e" % thing.value) + "^^<#{RDF::XSD.double}>"
82
- when RDF::Literal
83
- quoted(escaped(thing.value)) +
84
- (thing.datatype? ? "^^<#{thing.datatype}>" : "") +
85
- (thing.language? ? "@#{thing.language}" : "")
86
- when RDF::Statement
87
- thing.to_quad.map {|r| to_quad(r)}.compact.join(" ") + " .\n"
88
- end
89
- end
90
-
91
- ##
92
- # @param [String] string
93
- # @return [String]
94
- def quoted(string)
95
- "\"#{string}\""
96
- end
97
-
98
- ##
99
- # @param [String] string
100
- # @return [String]
101
- def escaped(string)
102
- string.gsub('\\', '\\\\').gsub("\t", '\\t').
103
- gsub("\n", '\\n').gsub("\r", '\\r').gsub('"', '\\"')
104
- end
105
- end