contrast 0.2.2 → 1.0.0
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.
- data/README.md +30 -16
- data/Rakefile +2 -1
- data/lib/contrast/detective.rb +10 -9
- data/lib/contrast/matching_exception.rb +6 -0
- data/lib/contrast/version.rb +1 -1
- data/lib/contrast_with.rb +1 -5
- data/spec/contrast/matching_exception_spec.rb +2 -4
- data/spec/contrast_with_spec.rb +2 -2
- metadata +8 -2
data/README.md
CHANGED
|
@@ -1,29 +1,43 @@
|
|
|
1
1
|
# Contrast [](http://travis-ci.org/darrencauthon/contrast)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This gem is meant to assist in comparing objects to others, most notably in tests and specs.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Usage
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Require contrast.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
````ruby
|
|
10
|
+
require 'contrast'
|
|
11
|
+
````
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
Let's say you have two objects, like so:
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
````ruby
|
|
16
|
+
john = Person.new first_name: 'John', last_name: 'Galt', gender: 'm'
|
|
17
|
+
howard = Person.new first_name: 'Howard', last_name: 'Roark', gender: 'm'
|
|
18
|
+
````
|
|
14
19
|
|
|
15
|
-
|
|
20
|
+
You can contrast the two objects, providing the attributes by which to compare them:
|
|
16
21
|
|
|
17
|
-
|
|
22
|
+
````ruby
|
|
23
|
+
john.contrast_with howard, [:first_name, :last_name, :gender]
|
|
24
|
+
# {:first_name=>{:actual_value=>"Howard", :expected_value=>"John"},
|
|
25
|
+
# :last_name=>{:actual_value=>"Roark", :expected_value=>"Galt"}}
|
|
26
|
+
````
|
|
18
27
|
|
|
19
|
-
|
|
28
|
+
Note that it did not show a result for gender, as they matched.
|
|
29
|
+
|
|
30
|
+
If you call contrast_with!, an exception will be thrown if the two objects do not match based on the provided fields.
|
|
31
|
+
|
|
32
|
+
````ruby
|
|
33
|
+
john.contrast_with! howard, [:first_name, :last_name, :gender]
|
|
34
|
+
# An exception was just thrown, showing the fields that do not match in the message.
|
|
35
|
+
````
|
|
20
36
|
|
|
21
|
-
|
|
37
|
+
You can also compare objects to hashes, and hashes to hashes:
|
|
22
38
|
|
|
23
|
-
|
|
39
|
+
````ruby
|
|
40
|
+
john.contrast_with { first_name: 'Dagny', gender: 'f' }, [:gender]
|
|
41
|
+
# {:gender=>{:actual_value=>"m", :expected_value=>"f"}}
|
|
42
|
+
````
|
|
24
43
|
|
|
25
|
-
1. Fork it
|
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
27
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
|
29
|
-
5. Create new Pull Request
|
data/Rakefile
CHANGED
data/lib/contrast/detective.rb
CHANGED
|
@@ -7,21 +7,22 @@ module Contrast
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def examine(a, b)
|
|
10
|
-
|
|
10
|
+
Hash[fields_that_do_not_match(a, b).map do |key|
|
|
11
|
+
[key, { :actual_value => get_the_value(a, key),
|
|
12
|
+
:expected_value => get_the_value(b, key) }]
|
|
13
|
+
end]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def fields_that_do_not_match(a, b)
|
|
19
|
+
@fields.select do |field|
|
|
11
20
|
first = get_the_value(a, field)
|
|
12
21
|
second = get_the_value(b, field)
|
|
13
22
|
the_values_the_do_not_match(first, second)
|
|
14
23
|
end
|
|
15
|
-
result = {}
|
|
16
|
-
keys.each do |key|
|
|
17
|
-
result[key] = {:actual_value => get_the_value(a, key),
|
|
18
|
-
:expected_value => get_the_value(b, key)}
|
|
19
|
-
end
|
|
20
|
-
result
|
|
21
24
|
end
|
|
22
25
|
|
|
23
|
-
private
|
|
24
|
-
|
|
25
26
|
def get_the_value(object, field)
|
|
26
27
|
begin
|
|
27
28
|
object[field]
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
require 'awesome_print'
|
|
2
2
|
module Contrast
|
|
3
3
|
class MatchingException < StandardError
|
|
4
|
+
|
|
5
|
+
def initialize(results)
|
|
6
|
+
@results = results
|
|
7
|
+
end
|
|
8
|
+
|
|
4
9
|
attr_accessor :results
|
|
10
|
+
|
|
5
11
|
def to_s
|
|
6
12
|
return 'The results did not match, but I cannot tell you why.' if results.nil? || results.empty?
|
|
7
13
|
return self.results.ai
|
data/lib/contrast/version.rb
CHANGED
data/lib/contrast_with.rb
CHANGED
|
@@ -6,10 +6,6 @@ class Object
|
|
|
6
6
|
|
|
7
7
|
def contrast_with!(other, fields)
|
|
8
8
|
results = self.contrast_with(other, fields)
|
|
9
|
-
if results.any?
|
|
10
|
-
exception = Contrast::MatchingException.new
|
|
11
|
-
exception.results = results
|
|
12
|
-
raise exception
|
|
13
|
-
end
|
|
9
|
+
raise Contrast::MatchingException.new(results) if results.any?
|
|
14
10
|
end
|
|
15
11
|
end
|
|
@@ -4,15 +4,13 @@ describe Contrast::MatchingException do
|
|
|
4
4
|
describe "#to_s" do
|
|
5
5
|
it 'no results: The results did not match, but I cannot tell you why.' do
|
|
6
6
|
[nil, []]. each do |value|
|
|
7
|
-
error = Contrast::MatchingException.new
|
|
8
|
-
error.results = value
|
|
7
|
+
error = Contrast::MatchingException.new value
|
|
9
8
|
get_message_for(error).must_equal 'The results did not match, but I cannot tell you why.'
|
|
10
9
|
end
|
|
11
10
|
end
|
|
12
11
|
|
|
13
12
|
it "results: Run them through awesome print" do
|
|
14
|
-
error = Contrast::MatchingException.new
|
|
15
|
-
error.results = {:result => nil}
|
|
13
|
+
error = Contrast::MatchingException.new( {:result => nil} )
|
|
16
14
|
error.results.expects(:ai).returns('expected results')
|
|
17
15
|
get_message_for(error).must_equal 'expected results'
|
|
18
16
|
end
|
data/spec/contrast_with_spec.rb
CHANGED
|
@@ -34,7 +34,7 @@ describe 'contrast with!' do
|
|
|
34
34
|
Contrast::Detective.any_instance.expects(:examine).
|
|
35
35
|
with(first, second).returns(result)
|
|
36
36
|
|
|
37
|
-
exception_thrown =
|
|
37
|
+
exception_thrown = lambda { first.contrast_with!(second, fields) }.
|
|
38
38
|
call_safely { true }
|
|
39
39
|
|
|
40
40
|
exception_thrown.must_equal true
|
|
@@ -48,7 +48,7 @@ describe 'contrast with!' do
|
|
|
48
48
|
Contrast::Detective.any_instance.expects(:examine).
|
|
49
49
|
with(first, second).returns(result)
|
|
50
50
|
|
|
51
|
-
exception_thrown =
|
|
51
|
+
exception_thrown = lambda { first.contrast_with!(second, fields); false }.
|
|
52
52
|
call_safely { true }
|
|
53
53
|
|
|
54
54
|
exception_thrown.must_equal false
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: contrast
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-08-
|
|
12
|
+
date: 2012-08-17 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: subtle
|
|
@@ -113,12 +113,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
113
113
|
- - ! '>='
|
|
114
114
|
- !ruby/object:Gem::Version
|
|
115
115
|
version: '0'
|
|
116
|
+
segments:
|
|
117
|
+
- 0
|
|
118
|
+
hash: -1837560623878563679
|
|
116
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
120
|
none: false
|
|
118
121
|
requirements:
|
|
119
122
|
- - ! '>='
|
|
120
123
|
- !ruby/object:Gem::Version
|
|
121
124
|
version: '0'
|
|
125
|
+
segments:
|
|
126
|
+
- 0
|
|
127
|
+
hash: -1837560623878563679
|
|
122
128
|
requirements: []
|
|
123
129
|
rubyforge_project:
|
|
124
130
|
rubygems_version: 1.8.24
|