Sha256: 3af9f9dc0b60a83642a8a8be2760c37966da9226550c6a1d80ff73888e63bb44

Contents?: true

Size: 1.33 KB

Versions: 35

Compression:

Stored size: 1.33 KB

Contents

RSpec::Matchers.define :validate_nilness_of do |attribute|
  description do
    "require nil for #{attribute}"
  end

  failure_message_method = :failure_message_for_should

  # RSpec compatibility without deprecation warnings
  if respond_to?(:failure_message)
    failure_message_method = :failure_message
  end

  send(failure_message_method) do |instance|
    "Expected errors to include 'must be nil' when #{attribute} is set to an arbitrary string"
  end

  failure_message_when_negated_method = :failure_message_for_should_not

  # RSpec compatibility without deprecation warnings
  if respond_to?(:failure_message_when_negated)
    failure_message_when_negated_method = :failure_message_when_negated
  end

  send(failure_message_when_negated_method) do |instance|
    "Expected errors not to include 'must be nil' when #{attribute} is set"
  end

  match do |instance|
    writer = :"#{attribute}="
    instance.send(writer, nil)
    instance.valid?
    allow_nil = instance.errors[attribute].empty?

    empty = ''
    instance.send(writer, empty)
    instance.valid?
    disallow_empty = instance.errors[attribute].include?('must be nil')

    present = 'present'
    instance.send(writer, present)
    instance.valid?
    disallow_present = instance.errors[attribute].include?('must be nil')

    allow_nil && disallow_empty && disallow_present
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
metasploit-model-5.0.2 spec/support/matchers/validate_nilness_of.rb
metasploit-model-5.0.1 spec/support/matchers/validate_nilness_of.rb
metasploit-model-5.0.0 spec/support/matchers/validate_nilness_of.rb
metasploit-model-4.0.6 spec/support/matchers/validate_nilness_of.rb
metasploit-model-4.0.5 spec/support/matchers/validate_nilness_of.rb
metasploit-model-4.0.4 spec/support/matchers/validate_nilness_of.rb
metasploit-model-4.0.3 spec/support/matchers/validate_nilness_of.rb
metasploit-model-4.0.2 spec/support/matchers/validate_nilness_of.rb
metasploit-model-4.0.1 spec/support/matchers/validate_nilness_of.rb
metasploit-model-4.0.0 spec/support/matchers/validate_nilness_of.rb
metasploit-model-3.1.4 spec/support/matchers/validate_nilness_of.rb
metasploit-model-3.1.3 spec/support/matchers/validate_nilness_of.rb
metasploit-model-3.1.2 spec/support/matchers/validate_nilness_of.rb
metasploit-model-3.1.1 spec/support/matchers/validate_nilness_of.rb
metasploit-model-3.1.0 spec/support/matchers/validate_nilness_of.rb
metasploit-model-3.0.0 spec/support/matchers/validate_nilness_of.rb
metasploit-model-2.0.4 spec/support/matchers/validate_nilness_of.rb
metasploit-model-2.0.3 spec/support/matchers/validate_nilness_of.rb
metasploit-model-2.0.2 spec/support/matchers/validate_nilness_of.rb
metasploit-model-2.0.1 spec/support/matchers/validate_nilness_of.rb