# frozen_string_literal: true

require "test_helper"

class CompileCacheISeqTest < Minitest::Test
  include CompileCacheISeqHelper
  include TmpdirHelper

  def test_ruby_bug_18250
    Help.set_file("a.rb", "def foo(*); ->{ super }; end; def foo(**); ->{ super }; end", 100)
    Bootsnap::CompileCache::ISeq.fetch("a.rb")
  end

  def test_compiler_selector
    compiler_selector = Bootsnap::CompileCache::ISeq.compiler_selector

    target = Help.set_file("a.rb", "p(frozen: 'test'.frozen?)")
    out, _err = capture_io do
      load(target)
    end
    assert_equal({frozen: false}.inspect, out.strip)

    Bootsnap::CompileCache::ISeq.compiler_selector = lambda { |path|
      if path.end_with?("a.rb")
        Bootsnap::CompileCache::ISeq::FROZEN_STRING_LITERAL
      else
        Bootsnap::CompileCache::ISeq::DEFAULT
      end
    }

    target = Help.set_file("a.rb", "p(frozen: 'test'.frozen?)")
    out, _err = capture_io do
      load(target)
    end
    assert_equal({frozen: true}.inspect, out.strip)

    target = Help.set_file("b.rb", "p(frozen: 'test'.frozen?)")
    out, _err = capture_io do
      load(target)
    end
    assert_equal({frozen: false}.inspect, out.strip)
  ensure
    Bootsnap::CompileCache::ISeq.compiler_selector = compiler_selector
  end

  def test_input_to_output_encoding
    compiler = Bootsnap::CompileCache::ISeq::FROZEN_STRING_LITERAL
    source = "_a = 'fée'.encoding".b
    iseq = compiler.input_to_output(source, "a.rb", nil)
    assert_equal Encoding.default_external, iseq.eval
  end
end
