Skip to content

Commit

Permalink
Merge pull request #43 from cbeer/nil_check_on_dup
Browse files Browse the repository at this point in the history
Don't attempt to remove a non-existant variable
  • Loading branch information
jcoyne committed Jan 27, 2015
2 parents f323c1b + 9b7ecc0 commit 5f42197
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
10 changes: 6 additions & 4 deletions lib/ldp/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ def self.rdf_source? response
def dup
super.tap do |new_resp|
new_resp.send(:extend, Ldp::Response)
if ::RUBY_VERSION < '2.0'
new_resp.send(:remove_instance_variable, :@graph)
else
new_resp.remove_instance_variable(:@graph)
unless new_resp.instance_variable_get(:@graph).nil?
if ::RUBY_VERSION < '2.0'
new_resp.send(:remove_instance_variable, :@graph)
else
new_resp.remove_instance_variable(:@graph)
end
end
end
end
Expand Down
43 changes: 29 additions & 14 deletions spec/lib/ldp/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,44 @@
end

describe "#dup" do
let(:simple_container_graph) { "<> a <http://www.w3.org/ns/ldp#Container> ." }
let(:link) { ["<http://www.w3.org/ns/ldp#Resource>;rel=\"type\"","<http://www.w3.org/ns/ldp#BasicContainer>;rel=\"type\""] }

let(:mock_conn) { Faraday.new { |builder| builder.adapter :test, conn_stubs } }
let(:client) { Ldp::Client.new mock_conn }
let(:raw_response) { client.get "a_container" }
let(:conn_stubs) do
Faraday::Adapter::Test::Stubs.new do |stub|
stub.get('/a_container') { [200, {"Link" => link}, simple_container_graph] }
stub.get('/a_container') { [200, {"Link" => link}, body] }
end
end

let(:mock_conn) { Faraday.new { |builder| builder.adapter :test, conn_stubs } }
let(:client) { Ldp::Client.new mock_conn }
let(:raw_response) { client.get "a_container" }

let(:response) { Ldp::Response.wrap mock_client, raw_response }

subject { response.dup }
it { is_expected.to respond_to :links }

it "should not have duplicated the graph" do
expect(response.graph.object_id).not_to eq subject.graph.object_id
context "for a container resource" do
let(:body) { "<> a <http://www.w3.org/ns/ldp#Container> ." }
let(:link) { ["<http://www.w3.org/ns/ldp#Resource>;rel=\"type\"","<http://www.w3.org/ns/ldp#BasicContainer>;rel=\"type\""] }
it { is_expected.to respond_to :links }

it "should not have duplicated the graph" do
expect(response.graph.object_id).not_to eq subject.graph.object_id
end

it "should have duplicated the body" do
expect(response.body.object_id).to eq subject.body.object_id
end
end

it "should have duplicated the body" do
expect(response.body.object_id).to eq subject.body.object_id
context "for a non-rdf resource" do
let(:body) { "This is only a test" }
let(:link) { ["<http://www.w3.org/ns/ldp#Resource>;rel=\"type\"","<http://www.w3.org/ns/ldp#NonRDFSource>;rel=\"type\""] }
it { is_expected.to respond_to :links }

it "should not have a graph" do
expect(response.instance_variable_get(:@graph)).to be_nil
end

it "should have duplicated the body" do
expect(response.body.object_id).to eq subject.body.object_id
end
end
end

Expand Down

0 comments on commit 5f42197

Please sign in to comment.