Class: Shindo::Tests

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/test_helpers/formats_helper.rb,
lib/fog/test_helpers/succeeds_helper.rb,
lib/fog/test_helpers/responds_to_helper.rb

Instance Method Summary collapse

Instance Method Details

#data_matches_schema(schema, options = {}) { ... } ⇒ Boolean

Generates a Shindo test that compares a hash schema to the result of the passed in block returning true if they match.

The schema that is passed in is a Hash or Array of hashes that have Classes in place of values. When checking the schema the value should match the Class.

Strict mode will fail if the data has additional keys. Setting strict to false will allow additional keys to appear.

Examples:

Using in a test

Shindo.tests("comparing welcome data against schema") do
  data = {:welcome => "Hello" }
  data_matches_schema(:welcome => String) { data }
end

comparing welcome data against schema
+ data matches schema

Example schema

{
  "id" => String,
  "ram" => Integer,
  "disks" => [
    {
      "size" => Float
    }
  ],
  "dns_name" => Fog::Nullable::String,
  "active" => Fog::Boolean,
  "created" => DateTime
}

Parameters:

  • schema (Hash)

    A Hash schema

  • options (Hash) (defaults to: {})

    Options to change validation rules

Options Hash (options):

  • :allow_extra_keys (Boolean)

    If true does not fail when keys are in the data that are not specified in the schema. This allows new values to appear in API output without breaking the check.

  • :allow_optional_rules (Boolean)

    If true does not fail if extra keys are in the schema that do not match the data. Not recommended!

Yields:

  • Data to check with schema

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
# File 'lib/fog/test_helpers/formats_helper.rb', line 75

def data_matches_schema(schema, options = {})
  test('data matches schema') do
    validator = Fog::Schema::DataValidator.new
    valid = validator.validate(yield, schema, options)
    @message = validator.message unless valid
    valid
  end
end

#formats(format, strict = true) ⇒ Object

Deprecated.

#formats is deprecated. Use #data_matches_schema instead



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fog/test_helpers/formats_helper.rb', line 85

def formats(format, strict = true)
  test('has proper format') do
    if strict
      options = {:allow_extra_keys => false, :allow_optional_rules => true}
    else
      options = {:allow_extra_keys => true, :allow_optional_rules => true}
    end
    validator = Fog::Schema::DataValidator.new
    valid = validator.validate(yield, format, options)
    @message = validator.message unless valid
    valid
  end
end

#responds_to(method_names) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/fog/test_helpers/responds_to_helper.rb', line 4

def responds_to(method_names)
  for method_name in [*method_names]
    tests("#respond_to?(:#{method_name})").returns(true) do
      @instance.respond_to?(method_name)
    end
  end
end

#succeedsObject



4
5
6
7
8
# File 'lib/fog/test_helpers/succeeds_helper.rb', line 4

def succeeds
  test('succeeds') do
    !!instance_eval(&Proc.new)
  end
end