File: same_elements.rb

package info (click to toggle)
ruby-riot 0.12.7-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, forky, sid, trixie
  • size: 512 kB
  • sloc: ruby: 2,557; makefile: 2
file content (29 lines) | stat: -rw-r--r-- 1,203 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Riot
  # Asserts that two arrays contain the same elements, the same number of times.
  #
  #   asserts("test") { ["foo", "bar"] }.same_elements(["bar", "foo"])
  #   should("test") { ["foo", "bar"] }.same_elements(["bar", "foo"])
  #
  # Maybe you just want to make sure two sets arent't the same:
  #
  #  denies("test") { ["foo", "bar"] }.same_elements(["baz", "boo"])
  class SameElementsMacro < AssertionMacro
    register :same_elements
    require 'set'
    
    # (see Riot::AssertionMacro#evaluate)
    # @param [Object] expected the collection of elements that actual should be equivalent to
    def evaluate(actual, expected)
      same = (Set.new(expected) == Set.new(actual))
      same ? pass(new_message.has_same_elements_as(expected)) : fail(expected_message.elements(expected).to_match(actual))
    end

    # (see Riot::AssertionMacro#devaluate)
    # @param [Object] expected the collection of elements that actual should not be equivalent to
    def devaluate(actual, expected)
      same = (Set.new(expected) == Set.new(actual))
      same ? fail(expected_message.elements(expected).not_to_match(actual)) : pass(new_message.has_same_elements_as(expected))
    end

  end
end