Sha256: 4f6c20434591a600a5bdc1cc2ced36ac0c71b88ff1566d40b3c1fa19fc8f755c

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# encoding: utf-8

# Checks whether the instance is truly (deeply) immutable
#
# @example
#   expect(instance).to be_immutable
#
# @api public
#
RSpec::Matchers.define :be_immutable do
  def can_be_frozen?(value)
    return false if value.is_a? Module
    return false if value.nil?
    return false if value.equal? true
    return false if value.equal? false
    return false unless value.class.respond_to?(:new)
    true
  end

  match do |instance|
    expect(instance).to be_frozen if can_be_frozen?(instance)

    if instance.is_a? Hash
      instance.each do |k, v|
        expect(k).to be_immutable
        expect(v).to be_immutable
      end
    else
      instance.instance_variables.each do |ivar|
        expect(instance.instance_variable_get(ivar)).to be_immutable
      end

      if instance.respond_to? :each
        instance.each { |item| expect(item).to be_immutable }
      end
    end

    expect(true).to be_truthy
  end

  failure_message do |instance|
    "expected that #{instance.inspect} to be immutable"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
immutability-0.0.5 spec/support/immutable.rb
immutability-0.0.4 spec/support/immutable.rb