require 'polyfill'

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    # This option will default to `true` in RSpec 4.
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
  # have no way to turn it off -- the option exists only for backwards
  # compatibility in RSpec 3). It causes shared context metadata to be
  # inherited by the metadata hash of host groups and examples, rather than
  # triggering implicit auto-inclusion in groups with matching metadata.
  config.shared_context_metadata_behavior = :apply_to_host_groups

  config.filter_run_when_matching :focus

  config.disable_monkey_patching!

  config.warnings = true

  config.default_formatter = 'doc' if config.files_to_run.one?

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = :random

  # Seed global randomization in this process using the `--seed` CLI option.
  # Setting this allows you to use `--seed` to deterministically reproduce
  # test failures related to randomization by passing the same `--seed` value
  # as the one that triggered the failure.
  Kernel.srand config.seed
end

def ignore_warnings
  Polyfill::InternalUtils.ignore_warnings(&::Proc.new)
end

def when_ruby(version)
  yield if Polyfill::InternalUtils.current_ruby_version == version
end

def when_ruby_above(version)
  yield if Polyfill::InternalUtils.current_ruby_version > version
end

def when_ruby_below(version)
  yield if Polyfill::InternalUtils.current_ruby_version < version
end

def frozen_error_class
  if Polyfill::InternalUtils.current_ruby_version >= '2.5'
    FrozenError
  else
    RuntimeError
  end
end
