hash_police 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.travis.yml +6 -0
- data/CHANGELOG.md +6 -0
- data/README.md +3 -0
- data/TODO.md +3 -0
- data/lib/hash_police/check_result.rb +4 -0
- data/lib/hash_police/police.rb +12 -2
- data/lib/hash_police/rspec_matcher.rb +2 -2
- data/lib/hash_police/version.rb +1 -1
- data/spec/hash_police/check_result_spec.rb +16 -9
- data/spec/hash_police/police_spec.rb +43 -22
- metadata +16 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 24ca713c2b5c7674966ed42c6d012f6c66a61b82cc0691a7ef5b827f1043d7fc
|
4
|
+
data.tar.gz: 95ef82b51da21bc0af612ab7faab6218dbed5a1ec8aebee5c95b4457de7b1e06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f08b0e1e80b925f645fb26ba9e24f19c38b13f886a6237aa4e6779e441941e137f0585da10c991304d0db822f200e5d3d619b937e229d82eeb3f229cf2efe34b
|
7
|
+
data.tar.gz: '08f59271fd3f1ebbcbc2e07113e26e524e2c327c8bcffd342b22c0335cb049ecbf99654dd3366dc920fc9223ffabbb34b8c4646587d114b3e7444ce17d9ac509'
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
[](https://travis-ci.org/mz026/hash_police)
|
2
|
+
[](https://codeclimate.com/github/mz026/hash_police)
|
3
|
+
|
1
4
|
# HashPolice
|
2
5
|
A gem to check whether given to hashes are of the same format
|
3
6
|
|
data/TODO.md
ADDED
data/lib/hash_police/police.rb
CHANGED
@@ -12,6 +12,11 @@ module HashPolice
|
|
12
12
|
|
13
13
|
def check target
|
14
14
|
result = CheckResult.new(context_key)
|
15
|
+
if rule.kind_of?(Proc) && ! rule.call(target)
|
16
|
+
result.invalid_by_proc
|
17
|
+
return result
|
18
|
+
end
|
19
|
+
|
15
20
|
unless type_matched?(rule, target)
|
16
21
|
result.differ_type(:expect => rule.class, :got => target.class)
|
17
22
|
return result
|
@@ -47,6 +52,9 @@ module HashPolice
|
|
47
52
|
private
|
48
53
|
def type_matched? rule, target
|
49
54
|
return true if bool?(rule) && bool?(target)
|
55
|
+
if rule.kind_of?(Proc)
|
56
|
+
return !!rule.call(target)
|
57
|
+
end
|
50
58
|
rule.class == target.class
|
51
59
|
end
|
52
60
|
|
@@ -59,8 +67,10 @@ module HashPolice
|
|
59
67
|
end
|
60
68
|
|
61
69
|
def stringify_keys hash
|
62
|
-
|
70
|
+
hash.reduce({}) do |memo,(k,v)|
|
71
|
+
memo[k.to_s] = v
|
72
|
+
memo
|
73
|
+
end
|
63
74
|
end
|
64
|
-
|
65
75
|
end
|
66
76
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
RSpec::Matchers.define :have_the_same_hash_format_as do |expected|
|
2
2
|
result = nil
|
3
|
-
|
3
|
+
|
4
4
|
match do |actual|
|
5
5
|
police = HashPolice::Police.new(expected)
|
6
6
|
result = police.check(actual)
|
7
7
|
result.passed?
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
failure_message do |actual|
|
11
11
|
result.error_messages
|
12
12
|
end
|
data/lib/hash_police/version.rb
CHANGED
@@ -14,14 +14,14 @@ describe HashPolice::CheckResult do
|
|
14
14
|
context "when without children" do
|
15
15
|
it "returns plain reason" do
|
16
16
|
result.differ_type(:expect => String, :got => Fixnum)
|
17
|
-
result.error_messages.
|
17
|
+
expect(result.error_messages).to eq("`the-key`: expect String, got Fixnum")
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
context "when with children" do
|
22
22
|
it "returns message with children's ones" do
|
23
23
|
children = [1,2,3].map do |i|
|
24
|
-
child = HashPolice::CheckResult.new("key-#{i}")
|
24
|
+
child = HashPolice::CheckResult.new("key-#{i}")
|
25
25
|
result.concat(child)
|
26
26
|
child
|
27
27
|
end
|
@@ -29,42 +29,42 @@ describe HashPolice::CheckResult do
|
|
29
29
|
children[1].differ_type(:expect => String, :got => Fixnum)
|
30
30
|
children[2].missing
|
31
31
|
|
32
|
-
result.error_messages
|
32
|
+
expect(result.error_messages)
|
33
|
+
.to eq( "`key-2`: expect String, got Fixnum\n`key-3`: missing")
|
33
34
|
end
|
34
35
|
end
|
35
|
-
|
36
36
|
end
|
37
37
|
|
38
38
|
describe "#missing(key = nil)" do
|
39
39
|
it "adds missing message to errors" do
|
40
40
|
result.missing
|
41
41
|
|
42
|
-
result.error_messages.
|
42
|
+
expect(result.error_messages).to eq("`#{context_key}`: missing")
|
43
43
|
end
|
44
44
|
|
45
45
|
it "assigns missing key if given" do
|
46
46
|
result.missing("the-key")
|
47
47
|
|
48
|
-
result.error_messages.
|
48
|
+
expect(result.error_messages).to eq("`the-key`: missing")
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
52
|
describe "#differ_type(:expect => Class1, :got => Class2)" do
|
53
53
|
it "adds message to error_messages" do
|
54
54
|
result.differ_type(:expect => String, :got => Array)
|
55
|
-
result.error_messages.
|
55
|
+
expect(result.error_messages).to eq("`#{context_key}`: expect String, got Array")
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
describe "passed?" do
|
60
60
|
it "returns true if all_errors empty" do
|
61
|
-
result.
|
61
|
+
expect(result).to be_passed
|
62
62
|
end
|
63
63
|
|
64
64
|
it "returns false if all_errors not empty" do
|
65
65
|
result.missing
|
66
66
|
|
67
|
-
result.
|
67
|
+
expect(result).not_to be_passed
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -74,4 +74,11 @@ describe HashPolice::CheckResult do
|
|
74
74
|
result.concat(child)
|
75
75
|
end
|
76
76
|
end
|
77
|
+
|
78
|
+
describe "#invalid_by_proc" do
|
79
|
+
it "adds message to error_messages" do
|
80
|
+
result.invalid_by_proc
|
81
|
+
expect(result.error_messages).to eq("`#{context_key}` is invalid given Proc fucntion")
|
82
|
+
end
|
83
|
+
end
|
77
84
|
end
|
@@ -15,17 +15,36 @@ describe HashPolice::Police do
|
|
15
15
|
let(:result) { double(:result, :concat => nil ) }
|
16
16
|
|
17
17
|
before(:each) do
|
18
|
-
HashPolice::CheckResult.
|
18
|
+
allow(HashPolice::CheckResult).to receive(:new).and_return(result)
|
19
19
|
end
|
20
20
|
|
21
21
|
context "when rule is scalar value" do
|
22
22
|
it "passes if a string" do
|
23
|
-
HashPolice::CheckResult.
|
23
|
+
allow(HashPolice::CheckResult).to receive(:new).and_return(double)
|
24
24
|
police.check "another string"
|
25
25
|
end
|
26
26
|
|
27
27
|
it "failed if type not match" do
|
28
|
-
result.
|
28
|
+
expect(result).to receive(:differ_type).with(:expect => String, :got => Fixnum)
|
29
|
+
police.check 12345
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when rule is scalar value and rule is kind of Proc" do
|
34
|
+
let(:rule) { lambda { |str| str.nil? || str.kind_of?(String) } }
|
35
|
+
|
36
|
+
it "passes if a string" do
|
37
|
+
allow(HashPolice::CheckResult).to receive(:new).and_return(double)
|
38
|
+
police.check('yo')
|
39
|
+
end
|
40
|
+
|
41
|
+
it "passes if a nil" do
|
42
|
+
allow(HashPolice::CheckResult).to receive(:new).and_return(double)
|
43
|
+
police.check(nil)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "fails if lambda return false" do
|
47
|
+
expect(result).to receive(:invalid_by_proc)
|
29
48
|
police.check 12345
|
30
49
|
end
|
31
50
|
end
|
@@ -36,9 +55,9 @@ describe HashPolice::Police do
|
|
36
55
|
let(:nested_result2) { double(:nested_result2) }
|
37
56
|
|
38
57
|
before(:each) do
|
39
|
-
HashPolice::CheckResult.
|
40
|
-
|
41
|
-
|
58
|
+
allow(HashPolice::CheckResult).to receive(:new).and_return(result,
|
59
|
+
nested_result1,
|
60
|
+
nested_result2)
|
42
61
|
end
|
43
62
|
|
44
63
|
it "passes if target is an of the same type" do
|
@@ -46,14 +65,14 @@ describe HashPolice::Police do
|
|
46
65
|
end
|
47
66
|
|
48
67
|
it "failed if target is not an array" do
|
49
|
-
result.
|
68
|
+
expect(result).to receive(:differ_type).with(:expect => Array, :got => Fixnum)
|
50
69
|
police.check 12345
|
51
70
|
end
|
52
71
|
|
53
72
|
it "faild if not all elements are of the same type" do
|
54
|
-
result.
|
55
|
-
result.
|
56
|
-
nested_result2.
|
73
|
+
expect(result).to receive(:concat).with(nested_result1)
|
74
|
+
expect(result).to receive(:concat).with(nested_result2)
|
75
|
+
expect(nested_result2).to receive(:differ_type).with(:expect => Fixnum, :got => String)
|
57
76
|
|
58
77
|
police.check [ 123, "string" ]
|
59
78
|
end
|
@@ -75,11 +94,11 @@ describe HashPolice::Police do
|
|
75
94
|
let(:result_key) { double(:result_key) }
|
76
95
|
|
77
96
|
before(:each) do
|
78
|
-
HashPolice::CheckResult.
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
97
|
+
allow(HashPolice::CheckResult).to receive(:new).and_return(result,
|
98
|
+
result_name,
|
99
|
+
result_married,
|
100
|
+
result_nested,
|
101
|
+
result_key)
|
83
102
|
end
|
84
103
|
|
85
104
|
it "passes if all keys are matched" do
|
@@ -93,26 +112,28 @@ describe HashPolice::Police do
|
|
93
112
|
end
|
94
113
|
|
95
114
|
it "failed if missing key" do
|
96
|
-
HashPolice::CheckResult.
|
97
|
-
|
98
|
-
|
99
|
-
|
115
|
+
allow(HashPolice::CheckResult).to receive(:new).and_return(result,
|
116
|
+
result_married,
|
117
|
+
result_nested,
|
118
|
+
result_key)
|
119
|
+
|
100
120
|
target.delete :name
|
101
|
-
result.
|
121
|
+
expect(result).to receive(:missing).with("name")
|
102
122
|
|
103
123
|
police.check(target)
|
104
124
|
end
|
105
125
|
|
106
126
|
it "failed if expect string but nil" do
|
107
127
|
target[:nested] = { :key => nil }
|
108
|
-
result_key.
|
128
|
+
expect(result_key).to receive(:differ_type).with(:expect => String, :got => NilClass)
|
109
129
|
|
110
130
|
police.check(target)
|
111
131
|
end
|
112
132
|
|
113
133
|
it "failed if nested key missing" do
|
114
134
|
target[:nested] = {}
|
115
|
-
|
135
|
+
|
136
|
+
expect(result_nested).to receive(:missing).with("nested.key")
|
116
137
|
|
117
138
|
police.check(target)
|
118
139
|
end
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_police
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yang-Hsing Lin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: a gem to check whether given to hashes are of the same format
|
@@ -59,12 +59,15 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .gitignore
|
63
|
-
- .rspec
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
64
66
|
- Gemfile
|
65
67
|
- LICENSE.txt
|
66
68
|
- README.md
|
67
69
|
- Rakefile
|
70
|
+
- TODO.md
|
68
71
|
- hash_police.gemspec
|
69
72
|
- lib/hash_police.rb
|
70
73
|
- lib/hash_police/check_result.rb
|
@@ -84,17 +87,17 @@ require_paths:
|
|
84
87
|
- lib
|
85
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
86
89
|
requirements:
|
87
|
-
- -
|
90
|
+
- - ">="
|
88
91
|
- !ruby/object:Gem::Version
|
89
92
|
version: '0'
|
90
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
94
|
requirements:
|
92
|
-
- -
|
95
|
+
- - ">="
|
93
96
|
- !ruby/object:Gem::Version
|
94
97
|
version: '0'
|
95
98
|
requirements: []
|
96
99
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.4
|
100
|
+
rubygems_version: 2.7.4
|
98
101
|
signing_key:
|
99
102
|
specification_version: 4
|
100
103
|
summary: a gem to check whether given to hashes are of the same format
|