require "test_helper"
require "support/mock_decoder"

# Tests for ResolveParserTest
class ResolveParserTest < Minitest::Test
  cover "MultiXML*"

  def test_resolve_parser_with_module
    require "multi_xml/parsers/nokogiri"
    result = MultiXML.send(:resolve_parser, MultiXML::Parsers::Nokogiri)

    assert_equal MultiXML::Parsers::Nokogiri, result
  end

  def test_resolve_parser_with_class
    result = MultiXML.send(:resolve_parser, MockDecoder)

    assert_equal MockDecoder, result
  end

  def test_resolve_parser_raises_for_invalid_spec
    error = assert_raises(MultiXML::ParserLoadError) do
      MultiXML.send(:resolve_parser, 123)
    end

    assert_match(/expected parser to be a Symbol, String, or Module/, error.message)
  end
end

# Tests for ResolveParserDetailedTest
class ResolveParserDetailedTest < Minitest::Test
  cover "MultiXML*"

  def test_resolve_parser_accepts_module
    require "multi_xml/parsers/nokogiri"
    result = MultiXML.send(:resolve_parser, MultiXML::Parsers::Nokogiri)

    assert_equal MultiXML::Parsers::Nokogiri, result
  end

  def test_resolve_parser_accepts_class
    result = MultiXML.send(:resolve_parser, MockDecoder)

    assert_equal MockDecoder, result
  end

  def test_resolve_parser_raises_for_integer
    error = assert_raises(MultiXML::ParserLoadError) do
      MultiXML.send(:resolve_parser, 123)
    end

    assert_match(/expected parser to be/, error.message)
  end

  def test_resolve_parser_raises_for_nil
    error = assert_raises(MultiXML::ParserLoadError) do
      MultiXML.send(:resolve_parser, nil)
    end

    assert_match(/expected parser to be/, error.message)
  end
end

# Tests resolve_parser case statement branches
class ResolveParserCaseTest < Minitest::Test
  cover "MultiXML*"

  def test_resolve_parser_handles_string
    result = MultiXML.send(:resolve_parser, "nokogiri")

    assert_equal MultiXML::Parsers::Nokogiri, result
  end

  def test_resolve_parser_handles_symbol
    result = MultiXML.send(:resolve_parser, :nokogiri)

    assert_equal MultiXML::Parsers::Nokogiri, result
  end

  def test_resolve_parser_handles_module
    require "multi_xml/parsers/nokogiri"
    result = MultiXML.send(:resolve_parser, MultiXML::Parsers::Nokogiri)

    assert_equal MultiXML::Parsers::Nokogiri, result
  end

  def test_resolve_parser_handles_class
    result = MultiXML.send(:resolve_parser, MockDecoder)

    assert_equal MockDecoder, result
  end
end
