<commit_before>def subscribers_to_be_called(num, event = subject)
  @counter = 0

  event.subscribers.should have(num).subscribers
  event.listeners.should have(num).listeners

  event.fire "data" # fire Event, sending "data" to subscribers
  @counter.should == num
end

def should_be_defined_event(object=subject, name)
  object.should respond_to name.to_sym
  object.should respond_to "#{name}=".to_sym
  object.events.should include name.to_sym
  object.events.should_not include name.to_s
  object.class.instance_events.should include name.to_sym
  object.class.instance_events.should_not include name.to_s
  object.send(name.to_sym).should be_kind_of AMQP::Events::Event
end

def define_subscribers
  def self.subscriber_method(*args)
    args.should == ["data"]
    @counter += 1
  end

  @subscriber_proc = proc do |*args|
    args.should == ["data"]
    @counter += 1
  end
end

shared_examples_for 'evented class' do
  specify { should respond_to :instance_events }
  specify { should respond_to :event }
  its(:instance_events) { should be_a Hash }

end

shared_examples_for 'evented object' do

  specify { should respond_to :event }
  specify { should respond_to :events }
  its(:events) { should be_a Hash }

  it 'it`s events property contains Events that know about their name and host' do
    subject.events.each do |name, event|
      event.should be_kind_of AMQP::Events::Event
      event.name.should == name
      event.host.should == subject
    end
  end

  context 'with proc/method/block @subscribers and at least Bar event defined' do
    before do
      define_subscribers
      subject.event :Bar unless subject.class.instance_events.include? :Bar
    end

    context 'creating new (class-wide) Events' do
      before { @events_size = subject.events.size }

      it 'should create events on instance, with Symbol as a name' do
  # object effectively defines new Event for all similar instances... Should it be allowed?
        res = subject.event :Grumple
        res.should == subject.Grumple # returns new Event
        should_be_defined_event :Grumple
        subject.events.size.should == @events_size + 1
      end

      it 'should create events on instance, with String as a name' do
  # object effectively defines new Event for all similar instances... Should it be allowed?
        res = subject.event 'Blurp'
        res.should == subject.Blurp # returns new Event
        should_be_defined_event :Blurp
        subject.events.size.should == @events_size + 1
      end

      it 'should not redefine already defined events' do
        res = subject.event :Bar
        res.should == subject.Bar # returns existing Event
        should_be_defined_event :Bar
        subject.events.size.should == @events_size

        res = subject.event 'Bar'
        res.should == subject.Bar # returns existing Event
        should_be_defined_event :Bar
        subject.events.size.should == @events_size
      end
    end

    context '#Bar and #Bar= accessor method for defined Event' do
      it 'should respond to #Bar and #Bar=' do
        should respond_to :Bar
        should respond_to :Bar=
      end

      specify 'calling #Bar without args returns Bar Event itself' do
        subject.Bar.should == subject.events[:Bar]
      end

      specify 'calling #Bar with args fires Bar Event (like in C#)' do
        subject.Bar.should_receive :fire
        subject.Bar("whatever")
      end

      it 'allows assignment of pre-defined Event to self (+=/-=)' do
        events_size = subject.events.size
        subject.Bar = subject.Bar
        subject.Bar = subject.event(:Bar)
        subject.Bar = subject.events[:Bar]

        subject.class.instance_events.should include :Bar
        subject.events.size.should == events_size
      end

      it 'raises error if you try to assign anything else to Event' do
        events_size = subject.events.size
        other_event = AMQP::Events::Event.create(self, :Foo)
        same_name_event = AMQP::Events::Event.create(self, :Bar)
        other_object_event = AMQP::Events::Event.create(Object.new, :Bar)

        [1, 'event', :Baz, other_event, same_name_event, other_object_event, proc {}].each do |rvalue|
          expect { subject.Bar = rvalue }.to raise_error AMQP::Events::EventError, /Wrong assignment/
        end
        subject.class.instance_events.should include :Bar
        subject.events.size.should == events_size
      end

    end

    context "#subscribe to object's Event" do

      it "allows access to object's Events through its #events property" do
        subject.events[:Bar].subscribe(:bar1) { |*args| args.should == ["data"]; @counter += 1 }
        subject.events[:Bar].subscribe(:bar2, method(:subscriber_method))
        subject.events[:Bar].subscribe(:bar3, @subscriber_proc)

        subscribers_to_be_called 3, subject.Bar
      end

      it "syntax-sugars object.Event#subscribe as object.subscribe(:Event)" do
        subject.subscribe(:Bar) { |*args| args.should == ["data"]; @counter += 1 }
        subject.subscribe(:Bar, :bar1, @subscriber_proc)
        subject.subscribe(:Bar, :bar2, @subscriber_proc)
        subject.subscribe(:Bar, :bar3, &@subscriber_proc)
        subject.subscribe :Bar, @subscriber_proc
        subject.subscribe :Bar, &@subscriber_proc
        subject.listen :Bar, @subscriber_proc
        subject.subscribe(:Bar, :bar4, method(:subscriber_method))
        subject.subscribe(:Bar, :bar5, method(:subscriber_method))
        res = subject.listen(:Bar, :bar6, method(:subscriber_method))

        res.should be_an AMQP::Events::Event
        res.name.should == :Bar
        subscribers_to_be_called 10, subject.Bar
      end
    end #subscribe<commit_msg>Specs coverage extended even more<commit_after>def subscribers_to_be_called(num, event = subject)
  @counter = 0

  event.subscribers.should have(num).subscribers
  event.listeners.should have(num).listeners

  event.fire "data" # fire Event, sending "data" to subscribers
  @counter.should == num
end

def should_be_defined_event(object=subject, name)
  object.should respond_to name.to_sym
  object.should respond_to "#{name}=".to_sym
  object.events.should include name.to_sym
  object.events.should_not include name.to_s
  object.class.instance_events.should include name.to_sym
  object.class.instance_events.should_not include name.to_s
  object.send(name.to_sym).should be_kind_of AMQP::Events::Event
end

def define_subscribers
  def self.subscriber_method(*args)
    args.should == ["data"]
    @counter += 1
  end

  @subscriber_proc = proc do |*args|
    args.should == ["data"]
    @counter += 1
  end
end

shared_examples_for 'evented class' do
  specify { should respond_to :instance_events }
  specify { should respond_to :event }
  its(:instance_events) { should be_a Hash }

end

shared_examples_for 'evented object' do

  specify { should respond_to :event }
  specify { should respond_to :events }
  its(:events) { should be_a Hash }

  it 'it`s events property contains Events that know about their name and host' do
    subject.events.each do |name, event|
      event.should be_kind_of AMQP::Events::Event
      event.name.should == name
      event.host.should == subject
    end
  end

  context 'with proc/method/block @subscribers and at least Bar event defined' do
    before do
      define_subscribers
      subject.event :Bar unless subject.class.instance_events.include? :Bar
    end

    context 'creating new (class-wide) Events' do
      before { @events_size = subject.events.size }

      it 'should create events on instance, with Symbol as a name' do
  # object effectively defines new Event for all similar instances... Should it be allowed?
        res = subject.event :Grumple
        res.should == subject.Grumple # returns new Event
        should_be_defined_event :Grumple
        subject.events.size.should == @events_size + 1
      end

      it 'should create events on instance, with String as a name' do
  # object effectively defines new Event for all similar instances... Should it be allowed?
        res = subject.event 'Blurp'
        res.should == subject.Blurp # returns new Event
        should_be_defined_event :Blurp
        subject.events.size.should == @events_size + 1
      end

      it 'should not redefine already defined events' do
        res = subject.event :Bar
        res.should == subject.Bar # returns existing Event
        should_be_defined_event :Bar
        subject.events.size.should == @events_size

        res = subject.event 'Bar'
        res.should == subject.Bar # returns existing Event
        should_be_defined_event :Bar
        subject.events.size.should == @events_size
      end
    end

    context '#Bar and #Bar= accessor method for defined Event' do
      it 'should respond to #Bar and #Bar=' do
        should respond_to :Bar
        should respond_to :Bar=
      end

      specify 'calling #Bar without args or block returns Bar Event itself' do
        subject.Bar.should == subject.events[:Bar]
      end

      specify 'calling #Bar without args, but WITH block subscribes block to Bar Event' do
        subject.Bar { |*args| args.should == ["data"]; @counter += 1 }

        subscribers_to_be_called 1, subject.Bar
      end

      specify 'calling #Bar with args fires Bar Event (like in C#)' do
        subject.Bar.should_receive(:fire).with("whatever")
        subject.Bar("whatever")
      end

      it 'allows assignment of pre-defined Event to self (+=/-=)' do
        events_size = subject.events.size
        subject.Bar = subject.Bar
        subject.Bar = subject.event(:Bar)
        subject.Bar = subject.events[:Bar]

        subject.class.instance_events.should include :Bar
        subject.events.size.should == events_size
      end

      it 'raises error if you try to assign anything else to Event' do
        events_size = subject.events.size
        other_event = AMQP::Events::Event.create(self, :Foo)
        same_name_event = AMQP::Events::Event.create(self, :Bar)
        other_object_event = AMQP::Events::Event.create(Object.new, :Bar)

        [1, 'event', :Baz, other_event, same_name_event, other_object_event, proc {}].each do |rvalue|
          expect { subject.Bar = rvalue }.to raise_error AMQP::Events::EventError, /Wrong assignment/
        end
        subject.class.instance_events.should include :Bar
        subject.events.size.should == events_size
      end

    end

    context "#subscribe to object's Event" do

      it "allows access to object's Events through its #events property" do
        subject.events[:Bar].subscribe(:bar1) { |*args| args.should == ["data"]; @counter += 1 }
        subject.events[:Bar].subscribe(:bar2, method(:subscriber_method))
        subject.events[:Bar].subscribe(:bar3, @subscriber_proc)

        subscribers_to_be_called 3, subject.Bar
      end

      it "allows shorthand subscription through #Bar (blocks only!)" do
        subject.Bar { |*args| args.should == ["data"]; @counter += 1 }
        subject.Bar &@subscriber_proc

        subscribers_to_be_called 2, subject.Bar
      end

      it "syntax-sugars object.Event#subscribe as object.subscribe(:Event)" do
        subject.subscribe(:Bar) { |*args| args.should == ["data"]; @counter += 1 }
        subject.subscribe(:Bar, :bar1, @subscriber_proc)
        subject.subscribe(:Bar, :bar2, @subscriber_proc)
        subject.subscribe(:Bar, :bar3, &@subscriber_proc)
        subject.subscribe :Bar, @subscriber_proc
        subject.subscribe :Bar, &@subscriber_proc
        subject.listen :Bar, @subscriber_proc
        subject.subscribe(:Bar, :bar4, method(:subscriber_method))
        subject.subscribe(:Bar, :bar5, method(:subscriber_method))
        res = subject.listen(:Bar, :bar6, method(:subscriber_method))

        res.should be_an AMQP::Events::Event
        res.name.should == :Bar
        subscribers_to_be_called 10, subject.Bar
      end
    end #subscribe
{'fraction_non_alphanumeric': 0.09942895532415183, 'fraction_numerical': 0.003694994961370507, 'mean_word_length': 3.032847951236031, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2191493', 'n_tokens_mistral': 3549, 'n_tokens_neox': 3435, 'n_words': 999}