|
| 1 | +# frozen_string_literal: true |
1 | 2 |
|
2 | | -# coding: utf-8 |
3 | 3 | require_relative 'spec_helper' |
4 | 4 |
|
5 | 5 | describe JSON::LD::API do |
6 | | - let(:logger) {RDF::Spec.logger} |
7 | | - before {JSON::LD::Context::PRELOADED.clear} |
| 6 | + let(:logger) { RDF::Spec.logger } |
| 7 | + |
| 8 | + before { JSON::LD::Context::PRELOADED.clear } |
8 | 9 |
|
9 | 10 | describe "#initialize" do |
10 | 11 | context "with string input" do |
11 | 12 | let(:context) do |
12 | | - JSON::LD::API::RemoteDocument.new(%q({ |
| 13 | + JSON::LD::API::RemoteDocument.new('{ |
13 | 14 | "@context": { |
14 | 15 | "xsd": "http://www.w3.org/2001/XMLSchema#", |
15 | 16 | "name": "http://xmlns.com/foaf/0.1/name", |
16 | 17 | "homepage": {"@id": "http://xmlns.com/foaf/0.1/homepage", "@type": "@id"}, |
17 | 18 | "avatar": {"@id": "http://xmlns.com/foaf/0.1/avatar", "@type": "@id"} |
18 | 19 | } |
19 | | - }), |
| 20 | + }', |
20 | 21 | documentUrl: "http://example.com/context", |
21 | | - contentType: 'application/ld+json' |
22 | | - ) |
| 22 | + contentType: 'application/ld+json') |
23 | 23 | end |
24 | 24 | let(:remote_doc) do |
25 | | - JSON::LD::API::RemoteDocument.new(%q({"@id": "", "name": "foo"}), |
| 25 | + JSON::LD::API::RemoteDocument.new('{"@id": "", "name": "foo"}', |
26 | 26 | documentUrl: "http://example.com/foo", |
27 | 27 | contentType: 'application/ld+json', |
28 | | - contextUrl: "http://example.com/context" |
29 | | - ) |
| 28 | + contextUrl: "http://example.com/context") |
30 | 29 | end |
31 | 30 |
|
32 | 31 | it "loads document with loader and loads context" do |
33 | | - expect(described_class).to receive(:documentLoader).with("http://example.com/foo", anything).and_yield(remote_doc) |
34 | | - expect(described_class).to receive(:documentLoader).with("http://example.com/context", anything).and_yield(context) |
| 32 | + expect(described_class).to receive(:documentLoader).with("http://example.com/foo", |
| 33 | + anything).and_yield(remote_doc) |
| 34 | + expect(described_class).to receive(:documentLoader).with("http://example.com/context", |
| 35 | + anything).and_yield(context) |
35 | 36 | described_class.new("http://example.com/foo", nil) |
36 | 37 | end |
37 | 38 | end |
38 | 39 | end |
39 | 40 |
|
40 | 41 | context "when validating", pending: ("JRuby support for jsonlint" if RUBY_ENGINE == "jruby") do |
41 | 42 | it "detects invalid JSON" do |
42 | | - expect {described_class.new(StringIO.new(%({"a": "b", "a": "c"})), nil, validate: true)}.to raise_error(JSON::LD::JsonLdError::LoadingDocumentFailed) |
| 43 | + expect do |
| 44 | + described_class.new(StringIO.new(%({"a": "b", "a": "c"})), nil, |
| 45 | + validate: true) |
| 46 | + end.to raise_error(JSON::LD::JsonLdError::LoadingDocumentFailed) |
43 | 47 | end |
44 | 48 | end |
45 | 49 |
|
46 | 50 | context "Test Files" do |
47 | | - %i(oj json_gem ok_json yajl).each do |adapter| |
| 51 | + %i[oj json_gem ok_json yajl].each do |adapter| |
48 | 52 | context "with MultiJson adapter #{adapter.inspect}" do |
49 | 53 | Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), 'test-files/*-input.*'))) do |filename| |
50 | 54 | test = File.basename(filename).sub(/-input\..*$/, '') |
|
54 | 58 | context = filename.sub(/-input\..*$/, '-context.jsonld') |
55 | 59 | expanded = filename.sub(/-input\..*$/, '-expanded.jsonld') |
56 | 60 | ttl = filename.sub(/-input\..*$/, '-rdf.ttl') |
57 | | - |
58 | | - context test, skip: ("Not supported in JRuby" if RUBY_ENGINE == "jruby" && %w(oj yajl).include?(adapter.to_s)) do |
| 61 | + |
| 62 | + context test, |
| 63 | + skip: ("Not supported in JRuby" if RUBY_ENGINE == "jruby" && %w[oj yajl].include?(adapter.to_s)) do |
59 | 64 | around do |example| |
60 | 65 | @file = File.open(filename) |
61 | 66 | case filename |
62 | 67 | when /.jsonld$/ |
63 | | - @file.define_singleton_method(:content_type) {'application/ld+json'} |
| 68 | + @file.define_singleton_method(:content_type) { 'application/ld+json' } |
64 | 69 | end |
65 | 70 | if context |
66 | 71 | @ctx_io = File.open(context) |
67 | 72 | case context |
68 | 73 | when /.jsonld$/ |
69 | | - @ctx_io.define_singleton_method(:content_type) {'application/ld+json'} |
| 74 | + @ctx_io.define_singleton_method(:content_type) { 'application/ld+json' } |
70 | 75 | end |
71 | 76 | end |
72 | 77 | example.run |
73 | 78 | @file.close |
74 | | - @ctx_io.close if @ctx_io |
| 79 | + @ctx_io&.close |
75 | 80 | end |
76 | 81 |
|
77 | 82 | if File.exist?(expanded) |
78 | 83 | it "expands" do |
79 | | - options = {logger: logger, adapter: adapter} |
| 84 | + options = { logger: logger, adapter: adapter } |
80 | 85 | options[:expandContext] = @ctx_io if context |
81 | 86 | jld = described_class.expand(@file, **options) |
82 | 87 | expect(jld).to produce_jsonld(JSON.parse(File.read(expanded)), logger) |
83 | 88 | end |
84 | 89 |
|
85 | 90 | it "expands with serializer" do |
86 | | - options = {logger: logger, adapter: adapter} |
| 91 | + options = { logger: logger, adapter: adapter } |
87 | 92 | options[:expandContext] = @ctx_io if context |
88 | | - jld = described_class.expand(@file, serializer: JSON::LD::API.method(:serializer), **options) |
| 93 | + jld = described_class.expand(@file, serializer: described_class.method(:serializer), **options) |
89 | 94 | expect(jld).to be_a(String) |
90 | | - expect(JSON.load(jld)).to produce_jsonld(JSON.parse(File.read(expanded)), logger) |
| 95 | + expect(JSON.parse(jld)).to produce_jsonld(JSON.parse(File.read(expanded)), logger) |
91 | 96 | end |
92 | 97 | end |
93 | | - |
| 98 | + |
94 | 99 | if File.exist?(compacted) && File.exist?(context) |
95 | 100 | it "compacts" do |
96 | 101 | jld = described_class.compact(@file, @ctx_io, adapter: adapter, logger: logger) |
97 | 102 | expect(jld).to produce_jsonld(JSON.parse(File.read(compacted)), logger) |
98 | 103 | end |
99 | 104 |
|
100 | 105 | it "compacts with serializer" do |
101 | | - jld = described_class.compact(@file, @ctx_io, serializer: JSON::LD::API.method(:serializer), adapter: adapter, logger: logger) |
| 106 | + jld = described_class.compact(@file, @ctx_io, serializer: described_class.method(:serializer), |
| 107 | + adapter: adapter, logger: logger) |
102 | 108 | expect(jld).to be_a(String) |
103 | | - expect(JSON.load(jld)).to produce_jsonld(JSON.parse(File.read(compacted)), logger) |
| 109 | + expect(JSON.parse(jld)).to produce_jsonld(JSON.parse(File.read(compacted)), logger) |
104 | 110 | end |
105 | 111 | end |
106 | | - |
| 112 | + |
107 | 113 | if File.exist?(framed) && File.exist?(frame) |
108 | 114 | it "frames" do |
109 | 115 | File.open(frame) do |frame_io| |
|
114 | 120 |
|
115 | 121 | it "frames with serializer" do |
116 | 122 | File.open(frame) do |frame_io| |
117 | | - jld = described_class.frame(@file, frame_io, serializer: JSON::LD::API.method(:serializer), adapter: adapter, logger: logger) |
| 123 | + jld = described_class.frame(@file, frame_io, serializer: described_class.method(:serializer), |
| 124 | + adapter: adapter, logger: logger) |
118 | 125 | expect(jld).to be_a(String) |
119 | | - expect(JSON.load(jld)).to produce_jsonld(JSON.parse(File.read(framed)), logger) |
| 126 | + expect(JSON.parse(jld)).to produce_jsonld(JSON.parse(File.read(framed)), logger) |
120 | 127 | end |
121 | 128 | end |
122 | 129 | end |
123 | 130 |
|
124 | | - it "toRdf" do |
125 | | - expect(RDF::Repository.load(filename, format: :jsonld, adapter: adapter, logger: logger)).to be_equivalent_graph(RDF::Repository.load(ttl), logger: logger) |
126 | | - end if File.exist?(ttl) |
| 131 | + if File.exist?(ttl) |
| 132 | + it "toRdf" do |
| 133 | + expect(RDF::Repository.load(filename, format: :jsonld, adapter: adapter, |
| 134 | + logger: logger)).to be_equivalent_graph(RDF::Repository.load(ttl), logger: logger) |
| 135 | + end |
| 136 | + end |
127 | 137 | end |
128 | 138 | end |
129 | 139 | end |
|
0 commit comments