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)