Module: RSpec::Expectations::Syntax Private

Defined in:
lib/rspec/expectations/syntax.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Provides methods for enabling and disabling the available syntaxes provided by rspec-expectations.

Class Method Summary (collapse)

Class Method Details

+ (Object) default_should_host

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines where we add should and should_not.



11
12
13
# File 'lib/rspec/expectations/syntax.rb', line 11

def default_should_host
  @default_should_host ||= ::Object.ancestors.last
end

+ (Object) disable_expect(syntax_host = ::RSpec::Matchers)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Disables the expect syntax.



82
83
84
85
86
87
88
# File 'lib/rspec/expectations/syntax.rb', line 82

def disable_expect(syntax_host = ::RSpec::Matchers)
  return unless expect_enabled?(syntax_host)

  syntax_host.module_exec do
    undef expect
  end
end

+ (Object) disable_should(syntax_host = default_should_host)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Disables the should syntax.



57
58
59
60
61
62
63
64
# File 'lib/rspec/expectations/syntax.rb', line 57

def disable_should(syntax_host = default_should_host)
  return unless should_enabled?(syntax_host)

  syntax_host.module_exec do
    undef should
    undef should_not
  end
end

+ (Object) enable_expect(syntax_host = ::RSpec::Matchers)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enables the expect syntax.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rspec/expectations/syntax.rb', line 68

def enable_expect(syntax_host = ::RSpec::Matchers)
  return if expect_enabled?(syntax_host)

  syntax_host.module_exec do
    def expect(*target, &target_block)
      target << target_block if block_given?
      raise ArgumentError.new("You must pass an argument or a block to #expect but not both.") unless target.size == 1
      ::RSpec::Expectations::ExpectationTarget.new(target.first)
    end
  end
end

+ (Object) enable_should(syntax_host = default_should_host)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enables the should syntax.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/expectations/syntax.rb', line 38

def enable_should(syntax_host = default_should_host)
  @warn_about_should = false if syntax_host == default_should_host
  return if should_enabled?(syntax_host)

  syntax_host.module_exec do
    def should(matcher=nil, message=nil, &block)
      ::RSpec::Expectations::Syntax.warn_about_should_unless_configured(__method__)
      ::RSpec::Expectations::PositiveExpectationHandler.handle_matcher(self, matcher, message, &block)
    end

    def should_not(matcher=nil, message=nil, &block)
      ::RSpec::Expectations::Syntax.warn_about_should_unless_configured(__method__)
      ::RSpec::Expectations::NegativeExpectationHandler.handle_matcher(self, matcher, message, &block)
    end
  end
end

+ (Boolean) expect_enabled?(syntax_host = ::RSpec::Matchers)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indicates whether or not the expect syntax is enabled.

Returns:

  • (Boolean)


98
99
100
# File 'lib/rspec/expectations/syntax.rb', line 98

def expect_enabled?(syntax_host = ::RSpec::Matchers)
  syntax_host.method_defined?(:expect)
end

+ (Boolean) should_enabled?(syntax_host = default_should_host)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indicates whether or not the should syntax is enabled.

Returns:

  • (Boolean)


92
93
94
# File 'lib/rspec/expectations/syntax.rb', line 92

def should_enabled?(syntax_host = default_should_host)
  syntax_host.method_defined?(:should)
end

+ (Object) warn_about_should!

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instructs rspec-expectations to warn on first usage of should or should_not. Enabled by default. This is largely here to facilitate testing.



18
19
20
# File 'lib/rspec/expectations/syntax.rb', line 18

def warn_about_should!
  @warn_about_should = true
end

+ (Object) warn_about_should_unless_configured(method_name)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a deprecation warning for the given method if no warning has already been issued.



25
26
27
28
29
30
31
32
33
34
# File 'lib/rspec/expectations/syntax.rb', line 25

def warn_about_should_unless_configured(method_name)
  if @warn_about_should
    RSpec.deprecate(
      "Using `#{method_name}` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax",
      :replacement => "the new `:expect` syntax or explicitly enable `:should`"
    )

    @warn_about_should = false
  end
end