arg-that 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ gem 'rspec-given'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Justin Searls
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # arg-that
2
+
3
+ arg-that provides a simple method to create an argument matcher in equality comparisons. This is particularly handy when writing a test to assert the equality of some complex data struct with another and only one component is difficult or unwise to assert exactly.
4
+
5
+ ## wat?
6
+
7
+ Typically, tests specify exactly what a result should be equal to. If the value of `result` were `5`, then one would probably write an assertion like `result.should == 5` and call it a day.
8
+
9
+ But sometimes:
10
+
11
+ * You don't know exactly what the result is, and the part you don't know about doesn't matter a great deal
12
+ * A looser specification is preferable to an exact one; suppose a looser specification sufficiently verifies the subject code is working, and greater specificity in the assertion would only serve to constrain changes to future implementation details
13
+
14
+ ## example
15
+
16
+ Suppose our subject returns a hefty hash of attributes following a `save` operation of a `User` entity.
17
+
18
+ ``` ruby
19
+ subject.save #=> {:name => "Bob", :age => 28, :email => "bob@example.com" :created_at => 2013-07-18 21:40:58 -0400}
20
+ ```
21
+
22
+ While authoring the test, we neither care much about the value of `created_time`, nor do we know how to specify it exactly. That means we can't just do this:
23
+
24
+ ``` ruby
25
+ result = subject.save
26
+ result.should == {:name => "Bob", :age => 28, :email => "bob@example.com" :created_at => 2013-07-18 21:40:58 -0400}
27
+ ```
28
+
29
+ This wouldn't work, because at runtime the value of `created_at` will, of course, differ.
30
+
31
+ So, one could do this:
32
+
33
+ ``` ruby
34
+ result = subject.save
35
+ result[:name].should == "Bob"
36
+ result[:age].should == 28
37
+ result[:email].should == "bob@example.com"
38
+ ```
39
+
40
+ But now we've got three assertions when before we had one. Alas, we no longer have a clear visual of the *shape* of the data being returned by `save`. Additionally, if the map grows with additional meaningful values in the future, this test would continue to pass by incident.
41
+
42
+ The `arg_that` matcher can save us this annoyance by retaining the more terse *style* of the first example, while retaining the liberal specification necessitated by the situation:
43
+
44
+ ```
45
+ result.should == {
46
+ :name => "Bob",
47
+ :age => 28,
48
+ :email => "bob@example.com",
49
+ :created_at => arg_that { true }
50
+ }
51
+ ```
52
+
53
+ Where `arg_that { true }` would literally pass any equality test. If there's *something* we want to constrain about the `created_at` value, we could do so. Perhaps a type check like `arg_that { |arg| arg.kind_of?(Time) }` would be more appropriate.
54
+
55
+ The purpose of releasing something as simple as `arg-that` as a gem is to promote the intentionality about how specific any given equality assertion needs to be. The status quo seems to be to either "always specify everything exactly, but if that gets hard, specify the remainder arbitrarily." And that's not terrific.
56
+
57
+ ## usage
58
+
59
+ Here's how you'd use it in RSpec.
60
+
61
+ In your `spec_helper.rb`, you can make arg-that available to all of your examples by telling RSpec to include it:
62
+
63
+ ``` ruby
64
+ require 'arg_that'
65
+
66
+ RSpec.configure do |config|
67
+ config.include(ArgThat)
68
+ end
69
+ ```
70
+
71
+ Once included, you can make more liberal assertions as you see fit, like so:
72
+
73
+ ``` ruby
74
+ result = {
75
+ :zip_code => 48176,
76
+ :owner => "Fred Jim",
77
+ :last_audit => Time.new(2012, 8, 12)
78
+ }
79
+
80
+ result.should == {
81
+ :zip_code => 48176,
82
+ :owner => "Fred Jim",
83
+ :last_audit => arg_that { |arg| arg > Time.new(2012, 1, 1) }
84
+ }
85
+ ```
86
+
87
+ In this way, the result will verify the two entries we want to specify exactly (`zip_code` and `owner`), but allows us the flexibility of only loosely specifying that we're okay with any value of `last_audit` so long as it was some time after January 1st, 2012.
88
+
89
+ ## known issues
90
+
91
+ `arg_that` does you no good on symbols, as the equality check short-circuits the call to `==` on the receiver.
92
+
93
+ Any ideas on how to make this pass?
94
+
95
+ ``` ruby
96
+ :foo.should == arg_that { true }
97
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/arg_that.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'arg_that/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "arg-that"
8
+ spec.version = ArgThat::VERSION
9
+ spec.authors = ["Justin Searls"]
10
+ spec.email = ["searls@gmail.com"]
11
+ spec.description = %q{arg-that provides a simple method to create an argument matcher in equality comparisons.}
12
+ spec.summary = %q{arg-that provides a simple method to create an argument matcher in equality comparisons. This is particularly handy when writing a test to assert the equality of some complex data struct with another and only one component is difficult or unwise to assert exactly.}
13
+ spec.homepage = "http://github.com/testdouble/arg-that"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ end
data/lib/arg_that.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "arg_that/version"
2
+ require "arg_that/that_arg"
3
+
4
+ module ArgThat
5
+ def arg_that(&blk)
6
+ ThatArg.new(&blk)
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ class ThatArg
2
+ def initialize(&blk)
3
+ @blk = blk
4
+ end
5
+
6
+ def ==(other)
7
+ @blk.call(other)
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ArgThat
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,64 @@
1
+ require 'rspec'
2
+ require 'rspec/given'
3
+
4
+ require 'arg_that'
5
+
6
+ RSpec.configure do |config|
7
+ config.include(ArgThat)
8
+ end
9
+
10
+ describe ArgThat do
11
+
12
+ context "catch-all" do
13
+ Then { :foo.should == arg_that { true }}
14
+
15
+ Then { 5.should == arg_that { true } }
16
+ Then { 5.should_not == arg_that { false } }
17
+ end
18
+
19
+ context "direct value equality" do
20
+ Then { 1.should == arg_that {|arg| arg.kind_of?(Fixnum) } }
21
+ Then { 1.should_not == arg_that {|arg| arg.kind_of?(String) } }
22
+ end
23
+
24
+ context "a value inside something else" do
25
+ Then { [5, 6, 1].should == [5, 6, arg_that {|arg| arg.kind_of?(Fixnum) }]}
26
+ Then { [5, 6, 1].should_not == [5, 6, arg_that {|arg| arg > 1 }]}
27
+ Then do
28
+ {
29
+ :a => 1,
30
+ :b => 99
31
+ }.should == {
32
+ :a => 1,
33
+ :b => arg_that {|arg| arg > 98 && arg < 100 }
34
+ }
35
+ end
36
+
37
+ Then do
38
+ {
39
+ :zip_code => 48176,
40
+ :owner => "Fred Jim",
41
+ :last_audit => Time.new(2012, 8, 12)
42
+ }.should == {
43
+ :zip_code => 48176,
44
+ :owner => "Fred Jim",
45
+ :last_audit => arg_that { |arg| arg > Time.new(2012, 1, 1) }
46
+ }
47
+ end
48
+
49
+ Then do
50
+ {
51
+ :name => "Bob",
52
+ :age => 28,
53
+ :email => "bob@example.com",
54
+ :created_at => Time.new(2013, 7, 18, 21, 40, 58)
55
+ }.should == {
56
+ :name => "Bob",
57
+ :age => 28,
58
+ :email => "bob@example.com",
59
+ :created_at => arg_that { |arg| arg.kind_of?(Time) }
60
+ }
61
+ end
62
+ end
63
+
64
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arg-that
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Searls
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ description: arg-that provides a simple method to create an argument matcher in equality
31
+ comparisons.
32
+ email:
33
+ - searls@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - arg_that.gemspec
44
+ - lib/arg_that.rb
45
+ - lib/arg_that/that_arg.rb
46
+ - lib/arg_that/version.rb
47
+ - spec/arg_that_spec.rb
48
+ homepage: http://github.com/testdouble/arg-that
49
+ licenses:
50
+ - MIT
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.23
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: arg-that provides a simple method to create an argument matcher in equality
73
+ comparisons. This is particularly handy when writing a test to assert the equality
74
+ of some complex data struct with another and only one component is difficult or
75
+ unwise to assert exactly.
76
+ test_files:
77
+ - spec/arg_that_spec.rb