Class: HaveUnique

Inherits:
Object
  • Object
show all
Defined in:
lib/storexplore/testing/matchers/have_unique_matcher.rb

Overview

Matcher to verify that a hash’s key is unique in a collection of other hashes a full class is required to implement the in method Use like: expect(hash).to have_unique(:id).in(hashes)

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ HaveUnique

Returns a new instance of HaveUnique.



28
29
30
# File 'lib/storexplore/testing/matchers/have_unique_matcher.rb', line 28

def initialize(key)
  @key = key
end

Instance Method Details

#descriptionObject



50
51
52
# File 'lib/storexplore/testing/matchers/have_unique_matcher.rb', line 50

def description
  "expected an hash or object with a unique #{@key} in #{@collection}"
end

#failure_message_for_shouldObject



46
47
48
# File 'lib/storexplore/testing/matchers/have_unique_matcher.rb', line 46

def failure_message_for_should
  "expected #{value_expression(actual)} (=#{value(actual)}) to be unique in #{@collection}"
end

#in(collection) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/storexplore/testing/matchers/have_unique_matcher.rb', line 32

def in(collection)
  @collection = collection
  @index = Hash.new(0)
  collection.each do |item|
    @index[value(item)] += 1
  end
  self
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/storexplore/testing/matchers/have_unique_matcher.rb', line 41

def matches?(actual)
  @actual = actual
  @index[value(actual)] == 1
end