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.

Defined Under Namespace

Modules: ExpectExpressionGenerator, ShouldExpressionGenerator

Instance Method Summary (collapse)

Instance 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.

42
43
44
# File 'lib/rspec/expectations/syntax.rb', line 42
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.

93
94
95
96
97
98
99
100
101
# File 'lib/rspec/expectations/syntax.rb', line 93
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

- (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.

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

- (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.

79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rspec/expectations/syntax.rb', line 79
def enable_expect(syntax_host = ::RSpec::Matchers)
  return if expect_enabled?(syntax_host)
  syntax_host.module_exec do
    def expect(value=::RSpec::Expectations::ExpectationTarget::UndefinedValue, &block)
      ::RSpec::Expectations::ExpectationTarget.for(value, block)
    end
  end
  ::RSpec::Expectations::ExpectationTarget.enable_deprecated_should if should_enabled?
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.

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

- (ExpectationTarget) expect

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

Examples:

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

Returns:

See Also:


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

- (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)
111
112
113
# File 'lib/rspec/expectations/syntax.rb', line 111
def expect_enabled?(syntax_host = ::RSpec::Matchers)
  syntax_host.method_defined?(:expect)
end

- (Object) expression_generator

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.

Selects which expression generator to use based on the configured syntax.

129
130
131
132
133
134
135
# File 'lib/rspec/expectations/syntax.rb', line 129
def expression_generator
  if expect_enabled?
    ExpectExpressionGenerator
  else
    ShouldExpressionGenerator
  end
end

- (Object) negative_expression(target_expression, matcher_expression)

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 negative expectation expression.

123
124
125
# File 'lib/rspec/expectations/syntax.rb', line 123
def negative_expression(target_expression, matcher_expression)
  expression_generator.negative_expression(target_expression, matcher_expression)
end

- (Object) positive_expression(target_expression, matcher_expression)

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 positive expectation expression.

117
118
119
# File 'lib/rspec/expectations/syntax.rb', line 117
def positive_expression(target_expression, matcher_expression)
  expression_generator.positive_expression(target_expression, matcher_expression)
end

- (Boolean) should

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

- (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)
105
106
107
# File 'lib/rspec/expectations/syntax.rb', line 105
def should_enabled?(syntax_host = default_should_host)
  syntax_host.method_defined?(:should)
end

- (Boolean) should_not

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