ruby/spec
Jeremy Evans 98286e9850 Ensure origins for all included, prepended, and refined modules
This fixes various issues when a module is included in or prepended
to a module or class, and then refined, or refined and then included
or prepended to a module or class.

Implement by renaming ensure_origin to rb_ensure_origin, making it
non-static, and calling it when refining a module.

Fix Module#initialize_copy to handle origins correctly.  Previously,
Module#initialize_copy did not handle origins correctly.  For example,
this code:

```ruby
module B; end
class A
  def b; 2 end
  prepend B
end
a = A.dup.new
class A
  def b; 1 end
end
p a.b
```

Printed 1 instead of 2.  This is because the super chain for
a.singleton_class was:

```
a.singleton_class
A.dup
B(iclass)
B(iclass origin)
A(origin) # not A.dup(origin)
```

The B iclasses would not be modified, so the includer entry would be
still be set to A and not A.dup.

This modifies things so that if the class/module has an origin,
all iclasses between the class/module and the origin are duplicated
and have the correct includer entry set, and the correct origin
is created.

This requires other changes to make sure all tests still pass:

* rb_undef_methods_from doesn't automatically handle classes with
  origins, so pass it the origin for Comparable when undefing
  methods in Complex. This fixed a failure in the Complex tests.

* When adding a method, the method cache was not cleared
  correctly if klass has an origin.  Clear the method cache for
  the klass before switching to the origin of klass.  This fixed
  failures in the autoload tests related to overridding require,
  without breaking the optimization tests.  Also clear the method
  cache for both the module and origin when removing a method.

* Module#include? is fixed to skip origin iclasses.

* Refinements are fixed to use the origin class of the module that
  has an origin.

* RCLASS_REFINED_BY_ANY is removed as it was only used in a single
  place and is no longer needed.

* Marshal#dump is fixed to skip iclass origins.

* rb_method_entry_make is fixed to handled overridden optimized
  methods for modules that have origins.

Fixes [Bug #16852]
2020-06-03 09:50:37 -07:00
..
bundler Fix failures in ./spec/bundler/quality_spec.rb 2020-05-25 20:12:41 +09:00
mspec Update to ruby/mspec@e3abf6b 2020-05-31 18:22:47 +02:00
ruby Ensure origins for all included, prepended, and refined modules 2020-06-03 09:50:37 -07:00
README.md Run specs against the latest release of 2.4 2020-01-28 18:12:24 +01:00
default.mspec Adapt tools to follow spec/rubyspec => spec/ruby rename 2017-09-20 20:19:54 +00:00

README.md

spec/bundler

spec/bundler is rspec examples for bundler library(lib/bundler.rb, lib/bundler/*).

Running spec/bundler

To run rspec for bundler:

make test-bundler

spec/ruby

ruby/spec (https://github.com/ruby/spec/) is a test suite for the Ruby language.

Once a month, @eregon merges the in-tree copy under spec/ruby with the upstream repository, preserving the commits and history. The same happens for other implementations such as JRuby and TruffleRuby.

Feel welcome to modify the in-tree spec/ruby. This is the purpose of the in-tree copy, to facilitate contributions to ruby/spec for MRI developers.

New features, additional tests for existing features and regressions tests are all welcome in ruby/spec. There is very little behavior that is implementation-specific, as in the end user programs tend to rely on every behavior MRI exhibits. In other words: If adding a spec might reveal a bug in another implementation, then it is worth adding it. Currently, the only module which is MRI-specific is RubyVM.

Changing behavior and versions guards

Version guards (ruby_version_is) must be added for new features or features which change behavior or are removed. This is necessary for other Ruby implementations to still be able to run the specs and contribute new specs.

For example, change:

describe "Some spec" do
  it "some example" do
    # Old behavior for Ruby < 2.7
  end
end

to:

describe "Some spec" do
  ruby_version_is ""..."2.7" do
    it "some example" do
      # Old behavior for Ruby < 2.7
    end
  end

  ruby_version_is "2.7" do
    it "some example" do
      # New behavior for Ruby >= 2.7
    end
  end
end

See spec/ruby/CONTRIBUTING.md for more documentation about guards.

To verify specs are compatible with older Ruby versions:

cd spec/ruby
$RUBY_MANAGER use 2.4.9
../mspec/bin/mspec -j

Running ruby/spec

To run all specs:

make test-spec

Extra arguments can be added via MSPECOPT. For instance, to show the help:

make test-spec MSPECOPT=-h

You can also run the specs in parallel, which is currently experimental. It takes around 10s instead of 60s on a quad-core laptop.

make test-spec MSPECOPT=-j

To run a specific test, add its path to the command:

make test-spec MSPECOPT=spec/ruby/language/for_spec.rb

If ruby trunk is your current ruby in $PATH, you can also run mspec directly:

# change ruby to trunk
ruby -v # => trunk
spec/mspec/bin/mspec spec/ruby/language/for_spec.rb

ruby/spec and test/

The main difference between a "spec" under spec/ruby/ and a test under test/ is that specs are documenting what they test. This is extremely valuable when reading these tests, as it helps to quickly understand what specific behavior is tested, and how a method should behave. Basic English is fine for spec descriptions. Specs also tend to have few expectations (assertions) per spec, as they specify one aspect of the behavior and not everything at once. Beyond that, the syntax is slightly different but it does the same thing: assert_equal 3, 1+2 is just (1+2).should == 3.

Example:

describe "The for expression" do
  it "iterates over an Enumerable passing each element to the block" do
    j = 0
    for i in 1..3
      j += i
    end
    j.should == 6
  end
end

For more details, see spec/ruby/CONTRIBUTING.md.