# frozen_string_literal: true

require 'test_helper'
require 'stringio'

class EventWithConflictingHelpersBeforeDefinitionTest < StateMachinesTest
  def setup
    @original_stderr = $stderr
    $stderr = StringIO.new

    @superclass = Class.new do
      def can_ignite?
        0
      end

      def ignite_transition
        0
      end

      def ignite
        0
      end

      def ignite!
        0
      end
    end
    @klass = Class.new(@superclass)
    @machine = StateMachines::Machine.new(@klass)
    @machine.events << @event = StateMachines::Event.new(@machine, :ignite)
    @object = @klass.new
  end

  def teardown
    $stderr = @original_stderr
  end

  def test_should_not_redefine_predicate
    assert_equal 0, @object.can_ignite?
  end

  def test_should_not_redefine_transition_accessor
    assert_equal 0, @object.ignite_transition
  end

  def test_should_not_redefine_action
    assert_equal 0, @object.ignite
  end

  def test_should_not_redefine_bang_action
    assert_equal 0, @object.ignite!
  end

  def test_should_output_warning
    expected = %w[can_ignite? ignite_transition ignite ignite!].map do |method|
      location = @superclass.instance_method(method).source_location.join(':')
      "Instance method \"#{method}\" is already defined in #{@superclass} at #{location}, use generic helper instead or set StateMachines::Machine.ignore_method_conflicts = true. Defining :state state machine on #{@klass}.\n"
    end.join

    assert_equal expected, $stderr.string
  end
end
