From b1ab6537be0ec033864512df56902eee768dab88 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 12 May 2026 16:00:30 +0900 Subject: [PATCH] Fix ViolationError#object_class being always nil. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @object_class was never assigned in initialize — it's now set to target.class so that both the attr_reader and as_json serialisation return the actual class of the offending object. Updated tests to assert the correct value rather than nil. --- lib/async/safe/violation_error.rb | 1 + test/async/safe/violation_error.rb | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/async/safe/violation_error.rb b/lib/async/safe/violation_error.rb index bd0a9f1..e95c331 100644 --- a/lib/async/safe/violation_error.rb +++ b/lib/async/safe/violation_error.rb @@ -16,6 +16,7 @@ class ViolationError < StandardError # @parameter current [Fiber] The fiber that attempted to access the object. def initialize(message = nil, target:, method:, owner:, current:) @target = target + @object_class = target.class @method = method @owner = owner @current = current diff --git a/test/async/safe/violation_error.rb b/test/async/safe/violation_error.rb index 91855e1..dff24c4 100644 --- a/test/async/safe/violation_error.rb +++ b/test/async/safe/violation_error.rb @@ -6,20 +6,35 @@ require "async/safe" describe Async::Safe::ViolationError do - it "can be serialized to JSON" do - owner_fiber = Fiber.current - current_fiber = Fiber.new{}.tap(&:resume) - - error = Async::Safe::ViolationError.new( + let(:owner_fiber) {Fiber.current} + let(:current_fiber) {Fiber.new{}.tap(&:resume)} + + let(:error) do + Async::Safe::ViolationError.new( target: "test_object", method: :test_method, owner: owner_fiber, current: current_fiber ) - + end + + it "exposes object_class as the class of the target" do + expect(error.object_class).to be == String + end + + it "exposes the method name" do + expect(error.method).to be == :test_method + end + + it "exposes owner and current fibers" do + expect(error.owner).to be == owner_fiber + expect(error.current).to be == current_fiber + end + + it "serializes object_class correctly in as_json" do json = error.as_json - expect(json[:object_class]).to be == nil # Not set + expect(json[:object_class]).to be == String expect(json[:method]).to be == :test_method expect(json[:owner]).to be_a(Hash) expect(json[:current]).to be_a(Hash)