Class: BeSorted

Inherits:
Object
  • Object
show all
Defined in:
lib/itesttool/custom_matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ BeSorted

Returns a new instance of BeSorted.



53
54
55
# File 'lib/itesttool/custom_matchers.rb', line 53

def initialize(order)
  @order = order
end

Instance Method Details

#failure_message_for_shouldObject



75
76
77
78
79
80
81
82
83
# File 'lib/itesttool/custom_matchers.rb', line 75

def failure_message_for_should
  if @not_aligned
    "expected #{@list.inspect} type is not available."
  elsif @order_invalid
    "specified order is invalid. valid order in [:asc, :desc]"
  else
    "expected #{@list.inspect} to be sorted #{@order}"
  end
end

#matches?(list) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/itesttool/custom_matchers.rb', line 56

def matches?(list)
  @list = list
  type = list.first.class
  unless list.all?{|x| x.class == type }
    @not_aligned = true
    false
  else
    sorted_list = list.sort
    if @order == :asc
      list == sorted_list
    elsif @order == :desc
      list == sorted_list.reverse
    else
      @order_invalid = true
      false
    end
  end
end