Skip to content

Commit 4a24507

Browse files
committed
have_constant should use inherit=false
1 parent 3298f49 commit 4a24507

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

spec/mspec/lib/mspec/matchers/have_class_variable.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
require 'mspec/matchers/variable'
22

33
class HaveClassVariableMatcher < VariableMatcher
4-
self.variables_method = :class_variables
5-
self.description = 'class variable'
4+
self.description = 'class variable'
5+
6+
private def check(object, variable)
7+
object.class_variable_defined?(variable)
8+
end
69
end
710

811
module MSpecMatchers

spec/mspec/lib/mspec/matchers/have_constant.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
require 'mspec/matchers/variable'
22

33
class HaveConstantMatcher < VariableMatcher
4-
self.variables_method = :constants
5-
self.description = 'constant'
4+
self.description = 'constant'
5+
6+
private def check(object, variable)
7+
# Differs from object.const_defined?(variable, false) for undefined constants
8+
object.constants(false).include?(variable)
9+
end
610
end
711

812
module MSpecMatchers

spec/mspec/lib/mspec/matchers/have_instance_variable.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
require 'mspec/matchers/variable'
22

33
class HaveInstanceVariableMatcher < VariableMatcher
4-
self.variables_method = :instance_variables
5-
self.description = 'instance variable'
4+
self.description = 'instance variable'
5+
6+
private def check(object, variable)
7+
object.instance_variable_defined?(variable)
8+
end
69
end
710

811
module MSpecMatchers

spec/mspec/lib/mspec/matchers/variable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class VariableMatcher
22
class << self
3-
attr_accessor :variables_method, :description
3+
attr_accessor :description
44
end
55

66
def initialize(variable)
@@ -9,7 +9,7 @@ def initialize(variable)
99

1010
def matches?(object)
1111
@object = object
12-
@object.send(self.class.variables_method).include? @variable
12+
check(@object, @variable)
1313
end
1414

1515
def failure_message

spec/ruby/core/module/autoload_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,11 @@ class LexicalScope
573573
-> { DeclaredInParentDefinedInCurrent }.should raise_error(NameError)
574574
ruby_version_is ""..."3.1" do
575575
# Basically, the parent autoload constant remains in a "undefined" state
576-
self.should have_constant(:DeclaredInParentDefinedInCurrent)
576+
self.constants(false).should.include?(:DeclaredInParentDefinedInCurrent)
577577
end
578578
ruby_version_is "3.1" do
579579
# The autoload constant has been removed
580-
self.should_not have_constant(:DeclaredInParentDefinedInCurrent)
580+
self.constants(false).should_not.include?(:DeclaredInParentDefinedInCurrent)
581581
end
582582

583583
ModuleSpecs::Autoload::LexicalScope.send(:remove_const, :DeclaredInParentDefinedInCurrent)
@@ -627,7 +627,7 @@ class LexicalScope
627627
# The autoload constant has been removed
628628
self.autoload?(:DeclaredInCurrentDefinedInParent).should == nil
629629
const_defined?(:DeclaredInCurrentDefinedInParent).should == false
630-
self.should_not have_constant(:DeclaredInCurrentDefinedInParent)
630+
self.constants(false).should_not.include?(:DeclaredInCurrentDefinedInParent)
631631
-> { const_get(:DeclaredInCurrentDefinedInParent) }.should raise_error(NameError)
632632
end
633633

@@ -650,7 +650,7 @@ class LexicalScope
650650
# Basically, the autoload constant remains in a "undefined" state
651651
self.autoload?(:DeclaredInCurrentDefinedInParent).should == nil
652652
const_defined?(:DeclaredInCurrentDefinedInParent).should == false
653-
self.should have_constant(:DeclaredInCurrentDefinedInParent)
653+
self.constants(false).should.include?(:DeclaredInCurrentDefinedInParent)
654654
-> { const_get(:DeclaredInCurrentDefinedInParent) }.should raise_error(NameError)
655655
end
656656

0 commit comments

Comments
 (0)