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.



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

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

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


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

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.



131
132
133
134
135
136
137
# File 'lib/rspec/expectations/syntax.rb', line 131

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.



125
126
127
# File 'lib/rspec/expectations/syntax.rb', line 125

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.



119
120
121
# File 'lib/rspec/expectations/syntax.rb', line 119

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)


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

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