# frozen_string_literal: true

return if RUBY_VERSION < "3.2"

require_relative "../test_helper"

module Prism
  class ParametersSignatureTest < TestCase
    def test_req
      assert_parameters([[:req, :a]], "a")
    end

    def test_req_destructure
      assert_parameters([[:req]], "(a, b)")
    end

    def test_opt
      assert_parameters([[:opt, :a]], "a = 1")
    end

    def test_rest
      assert_parameters([[:rest, :a]], "*a")
    end

    def test_rest_anonymous
      assert_parameters([[:rest, :*]], "*")
    end

    def test_post
      assert_parameters([[:rest, :a], [:req, :b]], "*a, b")
    end

    def test_post_destructure
      assert_parameters([[:rest, :a], [:req]], "*a, (b, c)")
    end

    def test_keyreq
      assert_parameters([[:keyreq, :a]], "a:")
    end

    def test_key
      assert_parameters([[:key, :a]], "a: 1")
    end

    def test_keyrest
      assert_parameters([[:keyrest, :a]], "**a")
    end

    def test_nokey
      assert_parameters([[:nokey]], "**nil")
    end

    def test_keyrest_anonymous
      assert_parameters([[:keyrest, :**]], "**")
    end

    if RUBY_ENGINE == "ruby"
      def test_key_ordering
        assert_parameters([[:keyreq, :a], [:keyreq, :b], [:key, :c], [:key, :d]], "a:, c: 1, b:, d: 2")
      end
    end

    def test_block
      assert_parameters([[:block, :a]], "&a")
    end

    def test_block_anonymous
      assert_parameters([[:block, :&]], "&")
    end

    def test_forwarding
      assert_parameters([[:rest, :*], [:keyrest, :**], [:block, :&]], "...")
    end

    private

    def assert_parameters(expected, source)
      # Compare against our expectation.
      assert_equal(expected, signature(source))

      # Compare against Ruby's expectation.
      object = Object.new
      eval("def object.m(#{source}); end")
      assert_equal(expected, object.method(:m).parameters)
    end

    def signature(source)
      program = Prism.parse("def m(#{source}); end").value
      program.statements.body.first.parameters.signature
    end
  end
end
