Class: CemAcpt::Bolt::Tests::TestData

Inherits:
Object
  • Object
show all
Defined in:
lib/cem_acpt/bolt/tests.rb

Overview

Provides an abstraction for the specific Bolt test data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**prop_validators) ⇒ TestData

Returns a new instance of TestData.



18
19
20
21
22
# File 'lib/cem_acpt/bolt/tests.rb', line 18

def initialize(**prop_validators)
  @prop_validators = { params: {}, status: 'success' }.merge(prop_validators.transform_keys(&:to_sym))
  @params = @prop_validators[:params]
  @status = @prop_validators[:status]
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



16
17
18
# File 'lib/cem_acpt/bolt/tests.rb', line 16

def params
  @params
end

#prop_validatorsObject (readonly)

Returns the value of attribute prop_validators.



16
17
18
# File 'lib/cem_acpt/bolt/tests.rb', line 16

def prop_validators
  @prop_validators
end

#statusObject (readonly)

Returns the value of attribute status.



16
17
18
# File 'lib/cem_acpt/bolt/tests.rb', line 16

def status
  @status
end

Instance Method Details

#to_hObject



24
25
26
# File 'lib/cem_acpt/bolt/tests.rb', line 24

def to_h
  prop_validators
end

#validate_props(other) ⇒ Array<Hash>

Compares prop_validators with another object that implements any of the properties as methods.

Parameters:

  • other (Object)

    the object to compare against

Returns:

  • (Array<Hash>)

    an array of the hashes representing the results of the validation

Raises:

  • (ArgumentError)

    if other does not implement any of the properties



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cem_acpt/bolt/tests.rb', line 36

def validate_props(other)
  if (other.methods & validation_properties).empty?
    raise ArgumentError, "Object does not implement any of the properties: #{validation_properties}"
  end

  prop_validators.each_with_object([]) do |(k, v), ary|
    unless other.respond_to?(k)
      ary << validation_hash(k, 'skipped', 'property_not_implemented', v, nil)
      next
    end

    other_val = other.send(k)
    begin
      case v
      when String
        ary << validation_hash(k, result_from_bool(other_val == v), 'string_eq', v, other_val)
      when Hash
        v.transform_keys!(&:to_sym)
        unknown_validators_val = validate_validators(v, :match, :not_match)
        match_val = validate_match(k, v[:match], other_val)
        not_match_val = validate_match(k, v[:not_match], other_val, negate: true)
        [match_val, not_match_val, unknown_validators_val].compact.each { |h| ary << h }
      else
        ary << validation_hash(k, result_from_bool(other_val == v), 'simple_eq', v, other_val)
      end
    rescue StandardError => e
      ary << validation_hash(k, 'failure', 'ruby_error', e, nil)
    end
  end
end

#validation_propertiesObject



28
29
30
# File 'lib/cem_acpt/bolt/tests.rb', line 28

def validation_properties
  @validation_properties ||= prop_validators.keys
end