summaryrefslogtreecommitdiff
path: root/test/prism/locals_test.rb
blob: 0eb73f1b9c76b06b1b6d8e8d67e8db1e29f8cdb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

# This test is going to use the RubyVM::InstructionSequence class to compile
# local tables and compare against them to ensure we have the same locals in the
# same order. This is important to guarantee that we compile indices correctly
# on CRuby (in terms of compatibility).
#
# There have also been changes made in other versions of Ruby, so we only want
# to test on the most recent versions.
return if !defined?(RubyVM::InstructionSequence) || RUBY_VERSION < "3.4.0"

# Omit tests if running on a 32-bit machine because there is a bug with how
# Ruby is handling large ISeqs on 32-bit machines
return if RUBY_PLATFORM =~ /i686/

require_relative "test_helper"

module Prism
  class LocalsTest < TestCase
    base = File.join(__dir__, "fixtures")
    Dir["**/*.txt", base: base].each do |relative|
      # Skip this fixture because it has a different number of locals because
      # CRuby is eliminating dead code.
      next if relative == "whitequark/ruby_bug_10653.txt"

      filepath = File.join(base, relative)
      define_method("test_#{relative}") { assert_locals(filepath) }
    end

    def setup
      @previous_default_external = Encoding.default_external
      ignore_warnings { Encoding.default_external = Encoding::UTF_8 }
    end

    def teardown
      ignore_warnings { Encoding.default_external = @previous_default_external }
    end

    private

    def assert_locals(filepath)
      source = File.read(filepath)

      expected = Debug.cruby_locals(source)
      actual = Debug.prism_locals(source)

      assert_equal(expected, actual)
    end

    def ignore_warnings
      previous_verbosity = $VERBOSE
      $VERBOSE = nil
      yield
    ensure
      $VERBOSE = previous_verbosity
    end
  end
end