Module: Applitools::ArgumentGuard

Extended by:
ArgumentGuard
Included in:
ArgumentGuard
Defined in:
lib/applitools/core/argument_guard.rb

Instance Method Summary collapse

Instance Method Details

#greater_than_or_equal_to_zero(param, param_name) ⇒ Object



23
24
25
# File 'lib/applitools/core/argument_guard.rb', line 23

def greater_than_or_equal_to_zero(param, param_name)
  raise Applitools::EyesIllegalArgument.new "#{param_name} < 0" if 0 > param
end

#greater_than_zero(param, param_name) ⇒ Object



27
28
29
# File 'lib/applitools/core/argument_guard.rb', line 27

def greater_than_zero(param, param_name)
  raise Applitools::EyesIllegalArgument.new "#{param_name} <= 0" if 0 >= param
end

#hash(param, param_name, required_fields = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/applitools/core/argument_guard.rb', line 10

def hash(param, param_name, required_fields = [])
  if param.is_a? Hash
    missed_keys = required_fields - param.keys
    error_message = "Expected #{param_name} to include keys #{missed_keys.join ', '}"
    raise Applitools::EyesIllegalArgument.new error_message if missed_keys.any?
  else
    error_message = "#{param_name} expected to be a Hash"
    end_of_message = required_fields.any? ? " containing keys #{required_fields.join(', ')}." : '.'
    error_message << end_of_message
    raise Applitools::EyesIllegalArgument.new error_message
  end
end

#is_a?(param, param_name, klass) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/applitools/core/argument_guard.rb', line 31

def is_a?(param, param_name, klass)
  return true if param.is_a? klass
  raise Applitools::EyesIllegalArgument.new "Expected #{param_name} to be" \
    " instance of #{klass}, but got #{param.class.name} instead"
end

#not_nil(param, param_name) ⇒ Object



6
7
8
# File 'lib/applitools/core/argument_guard.rb', line 6

def not_nil(param, param_name)
  raise Applitools::EyesIllegalArgument.new "#{param_name} is nil!" if param.nil?
end

#one_of?(param, param_name, klasses) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/applitools/core/argument_guard.rb', line 37

def one_of?(param, param_name, klasses)
  return true if klasses.detect { |a| param.is_a? a }
  raise Applitools::EyesIllegalArgument.new "Expected #{param_name} to be" \
    " instance of one of this classes: [#{klasses}], but got #{param.class.name} instead"
end

#raise_argument_error(error) ⇒ Object



43
44
45
# File 'lib/applitools/core/argument_guard.rb', line 43

def raise_argument_error(error)
  raise Applitools::EyesIllegalArgument.new error
end