Sha256: 358c212bac2792139b30a23311f602467109cb8927d390a6c17f24c9b70d7a4c

Contents?: true

Size: 1.57 KB

Versions: 12

Compression:

Stored size: 1.57 KB

Contents

#!/usr/bin/env ruby
# encoding: UTF-8

require File.expand_path("../test_helper", __FILE__)

class DynamicMethodTest < TestCase

  class FruitMedley
    define_method(:apple) do
      sleep(0.1)
      "I'm a peach"
    end

    define_method(:orange) do
      sleep(0.2)
      "I'm an orange"
    end

    [:banana, :peach].each_with_index do |fruit,i|
      define_method(fruit) do
        sleep(i == 0 ? 0.3 : 0.4)
        "I'm a #{fruit}"
      end
    end
  end

  def test_dynamic_method
    medley = FruitMedley.new
    result = RubyProf::Profile.profile(measure_mode: RubyProf::WALL_TIME) do
      medley.apple
      medley.orange
      medley.banana
      medley.peach
    end

    methods = result.threads.first.methods.sort.reverse

    if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1')
      expected_method_names = %w(
        DynamicMethodTest#test_dynamic_method
        Kernel#sleep
        DynamicMethodTest::FruitMedley#peach
        DynamicMethodTest::FruitMedley#banana
        DynamicMethodTest::FruitMedley#orange
        DynamicMethodTest::FruitMedley#apple
        Symbol#to_s
      )
    else
      expected_method_names = %w(
        DynamicMethodTest#test_dynamic_method
        Kernel#sleep
        DynamicMethodTest::FruitMedley#peach
        DynamicMethodTest::FruitMedley#banana
        DynamicMethodTest::FruitMedley#orange
        DynamicMethodTest::FruitMedley#apple
        Integer#==
      )
    end

    assert_equal expected_method_names.join("\n"), methods.map(&:full_name).join("\n")
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
ruby-prof-1.7.1-x64-mingw-ucrt test/dynamic_method_test.rb
ruby-prof-1.7.1 test/dynamic_method_test.rb
ruby-prof-1.7.0-x64-mingw-ucrt test/dynamic_method_test.rb
ruby-prof-1.7.0 test/dynamic_method_test.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/ruby-prof-1.6.3/test/dynamic_method_test.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/ruby-prof-1.6.3/test/dynamic_method_test.rb
ruby-prof-1.6.3-x64-mingw-ucrt test/dynamic_method_test.rb
ruby-prof-1.6.3 test/dynamic_method_test.rb
ruby-prof-1.6.2-x64-mingw-ucrt test/dynamic_method_test.rb
ruby-prof-1.6.2 test/dynamic_method_test.rb
ruby-prof-1.6.1 test/dynamic_method_test.rb
ruby-prof-1.6.1-x64-mingw-ucrt test/dynamic_method_test.rb