its 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/README.md +62 -0
- data/Rakefile +6 -0
- data/its.gemspec +24 -0
- data/lib/its.rb +102 -0
- data/lib/its/version.rb +3 -0
- data/spec/its_spec.rb +97 -0
- metadata +90 -0
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
"Its" makes testing methods with multiple arguments much easier
|
2
|
+
==================================================
|
3
|
+
|
4
|
+
|
5
|
+
![Build Status](https://secure.travis-ci.org/dnagir/its.png)
|
6
|
+
|
7
|
+
|
8
|
+
Have you ever written something like this in your specs?
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
it "should be US currency" do
|
12
|
+
subject.currency(:us).should == 'US dollar'
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should be AU currency" do
|
16
|
+
subject.currency(:us).should == 'AU dollar'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should be UK currency" do
|
20
|
+
subject.currency(:uk).should == 'UK pound'
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
If yes, then this what you really wanted:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
its(:currency, :us) { should == 'US dollar' }
|
28
|
+
its(:currency, :au) { should == 'AU dollar' }
|
29
|
+
its(:currency, :uk) { should == 'UK pound' }
|
30
|
+
```
|
31
|
+
|
32
|
+
That's what this gem is for!
|
33
|
+
|
34
|
+
Installation and use
|
35
|
+
==================================================
|
36
|
+
|
37
|
+
Add to your `Gemfile`:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
gem 'its'
|
41
|
+
```
|
42
|
+
|
43
|
+
Then require it somewhere:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'its'
|
47
|
+
```
|
48
|
+
|
49
|
+
And you are done.
|
50
|
+
|
51
|
+
|
52
|
+
Help
|
53
|
+
==================================================
|
54
|
+
|
55
|
+
Please report any issues here or better submit a Pull Request.
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
License:
|
60
|
+
==================================================
|
61
|
+
|
62
|
+
MIT by me and RSpec guys where this code was extracted from.
|
data/Rakefile
ADDED
data/its.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "its/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "its"
|
7
|
+
s.version = Its::VERSION
|
8
|
+
s.authors = ["Dmytrii Nagirniak"]
|
9
|
+
s.email = ["dnagir@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/dnagir/its"
|
11
|
+
s.summary = %q{Testing methods with multiple arguments much easier with RSpec}
|
12
|
+
s.description = %q{You can write `its(:currency, :us) \{ should == 'US dollars' \}`}
|
13
|
+
|
14
|
+
s.rubyforge_project = "its"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_runtime_dependency "rspec-core", "~> 2.8"
|
22
|
+
s.add_development_dependency "rspec-mocks"
|
23
|
+
s.add_development_dependency "rspec-expectations"
|
24
|
+
end
|
data/lib/its.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require "its/version"
|
2
|
+
require "rspec/core"
|
3
|
+
|
4
|
+
module Its
|
5
|
+
module ExampleGroupMethods
|
6
|
+
|
7
|
+
# Creates a nested example group named by the submitted +attribute+,
|
8
|
+
# and then generates an example using the submitted block.
|
9
|
+
#
|
10
|
+
# # This ...
|
11
|
+
# describe Array do
|
12
|
+
# its(:size) { should eq(0) }
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # ... generates the same runtime structure as this:
|
16
|
+
# describe Array do
|
17
|
+
# describe "size" do
|
18
|
+
# it "should eq(0)" do
|
19
|
+
# subject.size.should eq(0)
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# The attribute can be a +Symbol+ or a +String+. Given a +String+
|
25
|
+
# with dots, the result is as though you concatenated that +String+
|
26
|
+
# onto the subject in an expression.
|
27
|
+
#
|
28
|
+
# describe Person do
|
29
|
+
# subject do
|
30
|
+
# Person.new.tap do |person|
|
31
|
+
# person.phone_numbers << "555-1212"
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# its("phone_numbers.first") { should eq("555-1212") }
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# When the subject is a +Hash+, you can refer to the Hash keys by
|
39
|
+
# specifying a +Symbol+ or +String+ in an array.
|
40
|
+
#
|
41
|
+
# describe "a configuration Hash" do
|
42
|
+
# subject do
|
43
|
+
# { :max_users => 3,
|
44
|
+
# 'admin' => :all_permissions }
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# its([:max_users]) { should eq(3) }
|
48
|
+
# its(['admin']) { should eq(:all_permissions) }
|
49
|
+
#
|
50
|
+
# # You can still access to its regular methods this way:
|
51
|
+
# its(:keys) { should include(:max_users) }
|
52
|
+
# its(:count) { should eq(2) }
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# You can also pass any additional arguments the target method can accept:
|
56
|
+
#
|
57
|
+
# describe Person do
|
58
|
+
# subject do
|
59
|
+
# Person.new.tap do |person|
|
60
|
+
# person.phone_numbers << "123-123"
|
61
|
+
# person.phone_numbers << "234-234"
|
62
|
+
# person.phone_numbers << "xxx-xxx"
|
63
|
+
# end
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# its("phone_numbers.first", 2) { should == ["123-123", "234-234"]
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
#
|
70
|
+
# This is extraction for the RSpec Core.
|
71
|
+
# For reference, see:
|
72
|
+
# - https://github.com/rspec/rspec-core/blob/7ce078e4948e8f0d1745a50bb83dd87a68b2e50e/lib/rspec/core/subject.rb#L120
|
73
|
+
# This modifies the following behaviour:
|
74
|
+
# - calls the target with arguments passed it
|
75
|
+
# - changes the description to include the args (if any)
|
76
|
+
def its(attribute, *args, &block)
|
77
|
+
desc = attribute.to_s
|
78
|
+
desc += "(#{args.map{|a| a.nil? ? 'nil' : a.to_s}.join(', ')})" unless args.empty?
|
79
|
+
|
80
|
+
describe(desc) do
|
81
|
+
example do
|
82
|
+
self.class.class_eval do
|
83
|
+
define_method(:subject) do
|
84
|
+
@_subject ||= if attribute.is_a?(Array)
|
85
|
+
super()[*attribute]
|
86
|
+
else
|
87
|
+
attribute.to_s.split('.').inject(super()) do |target, method|
|
88
|
+
target.send(method, *args)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
instance_eval(&block)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
RSpec.configure { |c| c.extend Its::ExampleGroupMethods }
|
data/lib/its/version.rb
ADDED
data/spec/its_spec.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'its'
|
2
|
+
|
3
|
+
describe "#its" do
|
4
|
+
subject do
|
5
|
+
Class.new do
|
6
|
+
def initialize
|
7
|
+
@call_count = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def call_count
|
11
|
+
@call_count += 1
|
12
|
+
end
|
13
|
+
end.new
|
14
|
+
end
|
15
|
+
|
16
|
+
context "with a call counter" do
|
17
|
+
its(:call_count) { should eq(1) }
|
18
|
+
end
|
19
|
+
|
20
|
+
context "with nil value" do
|
21
|
+
subject do
|
22
|
+
Class.new do
|
23
|
+
def nil_value
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end.new
|
27
|
+
end
|
28
|
+
its(:nil_value) { should be_nil }
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with nested attributes" do
|
32
|
+
subject do
|
33
|
+
Class.new do
|
34
|
+
def name
|
35
|
+
"John"
|
36
|
+
end
|
37
|
+
end.new
|
38
|
+
end
|
39
|
+
its("name") { should eq("John") }
|
40
|
+
its("name.size") { should eq(4) }
|
41
|
+
its("name.size.class") { should eq(Fixnum) }
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when it responds to #[]" do
|
45
|
+
subject do
|
46
|
+
Class.new do
|
47
|
+
def [](*objects)
|
48
|
+
objects.map do |object|
|
49
|
+
"#{object.class}: #{object.to_s}"
|
50
|
+
end.join("; ")
|
51
|
+
end
|
52
|
+
|
53
|
+
def name
|
54
|
+
"George"
|
55
|
+
end
|
56
|
+
end.new
|
57
|
+
end
|
58
|
+
its([:a]) { should eq("Symbol: a") }
|
59
|
+
its(['a']) { should eq("String: a") }
|
60
|
+
its([:b, 'c', 4]) { should eq("Symbol: b; String: c; Fixnum: 4") }
|
61
|
+
its(:name) { should eq("George") }
|
62
|
+
context "when referring to an attribute without the proper array syntax" do
|
63
|
+
context "it raises an error" do
|
64
|
+
its(:age) do
|
65
|
+
expect do
|
66
|
+
should eq(64)
|
67
|
+
end.to raise_error(NoMethodError)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "when it does not respond to #[]" do
|
74
|
+
subject { Object.new }
|
75
|
+
|
76
|
+
context "it raises an error" do
|
77
|
+
its([:a]) do
|
78
|
+
expect do
|
79
|
+
should eq("Symbol: a")
|
80
|
+
end.to raise_error(NoMethodError)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "with arguments passed in" do
|
86
|
+
subject do
|
87
|
+
Class.new do
|
88
|
+
def currency(code)
|
89
|
+
"$#{code}"
|
90
|
+
end
|
91
|
+
end.new
|
92
|
+
end
|
93
|
+
its(:currency, :us) { should eq("$us") }
|
94
|
+
its(:currency, :uk) { should eq("$uk") }
|
95
|
+
its(:currency, :ua) { should eq("$ua") }
|
96
|
+
end
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: its
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dmytrii Nagirniak
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec-core
|
16
|
+
requirement: &70343863742980 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.8'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70343863742980
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec-mocks
|
27
|
+
requirement: &70343863741980 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70343863741980
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec-expectations
|
38
|
+
requirement: &70343863741000 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70343863741000
|
47
|
+
description: You can write `its(:currency, :us) { should == 'US dollars' }`
|
48
|
+
email:
|
49
|
+
- dnagir@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rspec
|
56
|
+
- .travis.yml
|
57
|
+
- Gemfile
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- its.gemspec
|
61
|
+
- lib/its.rb
|
62
|
+
- lib/its/version.rb
|
63
|
+
- spec/its_spec.rb
|
64
|
+
homepage: https://github.com/dnagir/its
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project: its
|
84
|
+
rubygems_version: 1.8.10
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Testing methods with multiple arguments much easier with RSpec
|
88
|
+
test_files:
|
89
|
+
- spec/its_spec.rb
|
90
|
+
has_rdoc:
|