Module: RSpec::Expectations::Syntax Private

Extended by:
Syntax
Included in:
Syntax
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.

Instance Method Summary collapse

Instance Method Details

#default_should_hostObject

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.



42
43
44
# File 'lib/rspec/expectations/syntax.rb', line 42

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

#disable_expect(syntax_host = ::RSpec::Matchers) ⇒ Object

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.



95
96
97
98
99
100
101
102
103
# File 'lib/rspec/expectations/syntax.rb', line 95

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

  syntax_host.module_eval do
    undef expect
  end

  ::RSpec::Expectations::ExpectationTarget.disable_deprecated_should
end

#disable_should(syntax_host = default_should_host) ⇒ Object

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.



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

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

  syntax_host.module_eval do
    undef should
    undef should_not
  end

  ::RSpec::Expectations::ExpectationTarget.disable_deprecated_should
end

#enable_expect(syntax_host = ::RSpec::Matchers) ⇒ Object

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.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rspec/expectations/syntax.rb', line 79

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

  syntax_host.module_eval 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

  ::RSpec::Expectations::ExpectationTarget.enable_deprecated_should if should_enabled?
end

#enable_should(syntax_host = default_should_host) ⇒ Object

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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rspec/expectations/syntax.rb', line 48

def enable_should(syntax_host = default_should_host)
  return if should_enabled?(syntax_host)

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

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

  ::RSpec::Expectations::ExpectationTarget.enable_deprecated_should if expect_enabled?
end

#expectExpectationTarget

Supports expect(actual).to matcher syntax by wrapping actual in an ExpectationTarget.

Examples:

expect(actual).to eq(expected)
expect(actual).to_not eq(expected)

Returns:

See Also:



# File 'lib/rspec/expectations/syntax.rb', line 30

#expect_enabled?(syntax_host = ::RSpec::Matchers) ⇒ Boolean

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)


113
114
115
# File 'lib/rspec/expectations/syntax.rb', line 113

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

#shouldBoolean

Passes if matcher returns true. Available on every Object.

Examples:

actual.should eq expected
actual.should match /expression/

Parameters:

  • matcher (Matcher)
  • message (String)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    true if the expectation succeeds (else raises)

See Also:



# File 'lib/rspec/expectations/syntax.rb', line 9

#should_enabled?(syntax_host = default_should_host) ⇒ Boolean

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)


107
108
109
# File 'lib/rspec/expectations/syntax.rb', line 107

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

#should_notBoolean

Passes if matcher returns false. Available on every Object.

Examples:

actual.should_not eq expected

Parameters:

  • matcher (Matcher)
  • message (String)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    false if the negative expectation succeeds (else raises)

See Also:



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