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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f65798cbb95f63baf5b4f55050286e2851afee7e
4
- data.tar.gz: 3593e76d36b0ad927a599326674e9e586ab15baa
2
+ SHA256:
3
+ metadata.gz: 24ca713c2b5c7674966ed42c6d012f6c66a61b82cc0691a7ef5b827f1043d7fc
4
+ data.tar.gz: 95ef82b51da21bc0af612ab7faab6218dbed5a1ec8aebee5c95b4457de7b1e06
5
5
  SHA512:
6
- metadata.gz: d12c3d9b8750e6c56a5eb82614fe57a65c062da82a1c3228bc4c11b2267b770b673b39fc412dca63a90cba9789f8c8acfec9fdd3d7fdded8ebc4d4e7b4861cd8
7
- data.tar.gz: 0493195b05a025b247ae87b09b437af736a7236b0da1e161845f9440e1b962bd7e2ed453e9a5154cb25ce5c3fd4ebfc72caf1609e6ef541a41ef45da3e623d6a
6
+ metadata.gz: f08b0e1e80b925f645fb26ba9e24f19c38b13f886a6237aa4e6779e441941e137f0585da10c991304d0db822f200e5d3d619b937e229d82eeb3f229cf2efe34b
7
+ data.tar.gz: '08f59271fd3f1ebbcbc2e07113e26e524e2c327c8bcffd342b22c0335cb049ecbf99654dd3366dc920fc9223ffabbb34b8c4646587d114b3e7444ce17d9ac509'
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ script:
5
+ - bundle install
6
+ - rspec
@@ -0,0 +1,6 @@
1
+
2
+ 0.0.4 / 2014-11-12
3
+ ==================
4
+
5
+ * Add RSpec matcher
6
+ * Implement basic matching APIs
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Build Status](https://travis-ci.org/mz026/hash_police.svg?branch=master)](https://travis-ci.org/mz026/hash_police)
2
+ [![Code Climate](https://codeclimate.com/github/mz026/hash_police/badges/gpa.svg)](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
@@ -0,0 +1,3 @@
1
+ # TODOs
2
+
3
+ * add an option to treat symbol-key and string-key differently
@@ -33,4 +33,8 @@ class HashPolice::CheckResult
33
33
  def passed?
34
34
  all_errors.empty?
35
35
  end
36
+
37
+ def invalid_by_proc
38
+ @errors << "`#{context_key}` is invalid given Proc fucntion"
39
+ end
36
40
  end
@@ -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
- JSON.parse(hash.to_json, :quirks_mode => true)
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
@@ -1,3 +1,3 @@
1
1
  module HashPolice
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -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.should == "`the-key`: expect String, got Fixnum"
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.should == "`key-2`: expect String, got Fixnum\n`key-3`: missing"
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.should == "`#{context_key}`: missing"
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.should == "`the-key`: missing"
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.should == "`#{context_key}`: expect String, got Array"
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.passed?.should == true
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.passed?.should == false
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.stub(:new).and_return(result)
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.stub(:new => double)
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.should_receive(:differ_type).with(:expect => String, :got => Fixnum)
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.stub(:new).and_return(result,
40
- nested_result1,
41
- nested_result2)
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.should_receive(:differ_type).with(:expect => Array, :got => Fixnum)
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.should_receive(:concat).with(nested_result1)
55
- result.should_receive(:concat).with(nested_result2)
56
- nested_result2.should_receive(:differ_type).with(:expect => Fixnum, :got => String)
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.stub(:new).and_return(result,
79
- result_name,
80
- result_married,
81
- result_nested,
82
- result_key)
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.stub(:new).and_return(result,
97
- result_married,
98
- result_nested,
99
- result_key)
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.should_receive(:missing).with("name")
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.should_receive(:differ_type).with(:expect => String, :got => NilClass)
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
- result_nested.should_receive(:missing).with("nested.key")
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
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: 2014-11-18 00:00:00.000000000 Z
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.2
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