# frozen_string_literal: true

RSpec.describe Yardcheck::MethodTracer do
  it 'traces method calls and returns for a namespace' do
    stub_const('Foo', Class.new)
    stub_const('Qux', Class.new)

    class Foo
      def self.singleton_method_example(baz)
        baz.upcase
      end

      def instance_method_example(baz)
        Foo.singleton_method_example(baz)
      end
    end # Foo

    class Qux
      def self.singleton_method_example(baz)
        baz.upcase
      end

      def instance_method_example(baz)
        Qux.singleton_method_example(baz)
      end
    end # Qux

    tracer = described_class.new(Foo)
    foo    = Foo.new # Capture the activity for this object
    qux    = Qux.new # Ignore this one
    str    = 'Hello'

    tracer.trace do
      foo.instance_method_example(str)
      qux.instance_method_example(str)
    end

    expect(tracer.events).to eq([
      Yardcheck::MethodCall::Return.process(
        scope:            :class,
        selector:         :singleton_method_example,
        namespace:        Foo.singleton_class,
        params:           { baz: 'Hello' },
        return_value:     'HELLO',
        example_metadata: { location: RSpec.current_example.location, id: RSpec.current_example.id }
      ),
      Yardcheck::MethodCall::Return.process(
        scope:            :instance,
        selector:         :instance_method_example,
        namespace:        Foo,
        params:           { baz: 'Hello' },
        return_value:     'HELLO',
        example_metadata: { location: RSpec.current_example.location, id: RSpec.current_example.id }
      )
    ])
  end
end
