A gem to check whether given to hashes are of the same format
Add this line to your application's Gemfile:
gem 'hash_police'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hash_police
rule = {
:name => "a string",
:age => 28,
:favorites => [ "a string" ],
:locations => [
{ :name => "string", :duration => 3 }
]
}
valid = {
:name => "Jack",
:age => 28,
:favorites => [ "sport", "music" ],
:locations => [
{ :name => "Taiwan", :duration => 25 },
{ :name => "US", :duration => 5 }
]
}
invalid = {
:name => [],
:age => "not a number",
:locations => [
{ :name => "Taiwan", :duration => 25 },
{ :name => 23 }
]
}
police = HashPolice::Police.new(rule)
result = police.check(valid)
result.passed? # => true
result.error_messages # => ""
result = police.check(invalid)
result.passed? # => false
result.error_messages #=> "`name`: expect String, got Array; `favorites`: missing; `locations.1.name`: expect String, got Array; `locations.1.duration`: missing"
HashPolice
provides a RSpec matcher have_the_same_hash_format_as
checking the formats of two hashes.
require 'hash_police/rspec_matcher'
it "should be able to use the matcher `have_the_same_hash_format_as`" do
expected = { 'str' => '', 'an_arr' => [ 1 ] }
to_be_checked = { 'str' => 'hola', :an_arr => [1,3,4] }
expect(to_be_checked).to have_the_same_hash_format_as(expected)
end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request