named_let 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +5 -0
- data/LICENSE +20 -0
- data/README.md +122 -0
- data/Rakefile +1 -0
- data/lib/named_let/version.rb +3 -0
- data/lib/named_let.rb +72 -0
- data/named_let.gemspec +24 -0
- metadata +102 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Tomohito Ozaki
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
named_let
|
2
|
+
==================================
|
3
|
+
|
4
|
+
The `named_let can be used to make the rspec's output easier to read.
|
5
|
+
It's wrapper function of `let`.
|
6
|
+
|
7
|
+
`named_let(:name){ obj }` changes the value which returns 'obj#to_s' and
|
8
|
+
'obj#inspect' to :name, then output message of 'rspec -format d' be improved more readable.
|
9
|
+
|
10
|
+
# Usage
|
11
|
+
|
12
|
+
describe 'named_let' do
|
13
|
+
context 'symbol only' do
|
14
|
+
named_let(:foo) { Object.new }
|
15
|
+
it { foo.to_s should == "foo" }
|
16
|
+
it { foo.inspect.should == "foo" }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'with label strings' do
|
20
|
+
named_let(:foo,"label for display"){ Object.new }
|
21
|
+
it { foo.to_s should == "label for display" }
|
22
|
+
it { foo.inspect.should == "label for display" }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
You can use `name_let!` to force the method's invocation before each example, like original `let!`.
|
27
|
+
|
28
|
+
# Why named_let?
|
29
|
+
|
30
|
+
RSpec uses 'Object#inspect' for generating output message from value of specified by `let`.
|
31
|
+
This will generates unexpected output like 'should == #<Object:0x2aaaaf8a0870A>', it's not human readable.
|
32
|
+
|
33
|
+
Now let's use `named_let` instead of `let`.The generaed output will be more readable like 'should == "label for display'.
|
34
|
+
|
35
|
+
# Example
|
36
|
+
|
37
|
+
For Example, now writing specs for CanCan like bellow,
|
38
|
+
|
39
|
+
require 'spec_helper'
|
40
|
+
require "cancan/matchers"
|
41
|
+
|
42
|
+
describe Ability do
|
43
|
+
context 'an user' do
|
44
|
+
let(:user) { Factory.create(:user) }
|
45
|
+
|
46
|
+
let(:article) { Factory.create(:article) }
|
47
|
+
let(:own_article) { Factory.create(:article, :user => user) }
|
48
|
+
|
49
|
+
subject { Ability.new(user) }
|
50
|
+
|
51
|
+
it { should be_able_to(:read, article) }
|
52
|
+
it { should be_able_to(:update, own_article) }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
This specs generates outputs is ...
|
58
|
+
|
59
|
+
|
60
|
+
$ bundle exec rspec -c --format d spec/models/ability_spec.rb
|
61
|
+
|
62
|
+
Ability
|
63
|
+
an user
|
64
|
+
should be able to :read #<Article id: 44, title: "The Test Article 1", body: "This is test article!!", created_at: "2012-02-23 14:19:26", updated_at: "2012-02-23 14:19:26", user_id: nil>
|
65
|
+
should be able to :update #<Article id: 45, title: "The Test Article 2", body: "This is test article!!", created_at: "2012-02-23 14:19:26", updated_at: "2012-02-23 14:19:26", user_id: 31>
|
66
|
+
|
67
|
+
Finished in 0.26158 seconds
|
68
|
+
2 examples, 0 failures
|
69
|
+
|
70
|
+
|
71
|
+
OMG, It's not human readable. so,let's change `let` to `named_let`.
|
72
|
+
|
73
|
+
named_let(:article) { Factory.create(:article) }
|
74
|
+
named_let(:own_article) { Factory.create(:article, :user => user) }
|
75
|
+
|
76
|
+
|
77
|
+
again, execute `rspec --format d ...`
|
78
|
+
|
79
|
+
|
80
|
+
$ bundle exec rspec -c --format d spec/models/ability_spec.rb
|
81
|
+
|
82
|
+
Ability
|
83
|
+
an user
|
84
|
+
should be able to :read article
|
85
|
+
should be able to :update own article
|
86
|
+
|
87
|
+
Finished in 0.25375 seconds
|
88
|
+
2 examples, 0 failures
|
89
|
+
|
90
|
+
|
91
|
+
okay, it's readable!!!
|
92
|
+
|
93
|
+
# For debugging
|
94
|
+
|
95
|
+
If the specs is fail, You will want to know original outputs of 'Object#inspect'.
|
96
|
+
But named_let hides actual outputs of "Object#inspect".
|
97
|
+
|
98
|
+
Failures:
|
99
|
+
|
100
|
+
1) Ability an user
|
101
|
+
Failure/Error: it { should_not be_able_to(:update, own_article) }
|
102
|
+
expected not to be able to :update own article
|
103
|
+
|
104
|
+
|
105
|
+
For debugging spec, if given `-d` option to rspec command or `$DEBUG` flag is true,
|
106
|
+
named_let append orignal result of `Object#inspect` to returns value.
|
107
|
+
|
108
|
+
|
109
|
+
given `-d` option, then...
|
110
|
+
|
111
|
+
Failures:
|
112
|
+
|
113
|
+
1) Ability an user
|
114
|
+
Failure/Error: it { should_not be_able_to(:update, own_article) }
|
115
|
+
expected not to be able to :update own article (#<Article id: 113, title: "The Test Article 3", body: "This is test article!!", created_at: "2012-02-24 05:53:17", updated_at: "2012-02-24 05:53:17", user_id: 90>)
|
116
|
+
|
117
|
+
|
118
|
+
NOTE:
|
119
|
+
|
120
|
+
Requires `ruby-debug` to using `-d` option.
|
121
|
+
run `gem install ruby-debug`.
|
122
|
+
If your Ruby-Runtime is 1.9+, see "https://github.com/mark-moseley/ruby-debug".
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/named_let.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# -------------------------------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# named_let can be used to make the rspec's output easier to read.
|
4
|
+
#
|
5
|
+
# `named_let(:name){ obj }` changes the value which returns 'obj#to_s' and
|
6
|
+
# 'obj#inspect' to givne :name,
|
7
|
+
# then output of 'rspec -format d' will be improved more readable.
|
8
|
+
#
|
9
|
+
# -------------------------------------------------------------------------------------------------
|
10
|
+
#
|
11
|
+
# rspecのlet(:name)で定義したオブジェクトのto_sとinspectの値を、:nameに変更する
|
12
|
+
#
|
13
|
+
# named_let(:foo){ Object.new } ってやると、
|
14
|
+
# foo.to_sが"foo"になる
|
15
|
+
#
|
16
|
+
# named_let(:foo,"label for display"){ ... } って第二引数に別名を渡すと、
|
17
|
+
# その別名が、to_s/inspectの値になる
|
18
|
+
#
|
19
|
+
# subject should == fooとか書いたときの出力が
|
20
|
+
# ふつうは
|
21
|
+
# should == #<Object:0x2aaaaf8a0870>
|
22
|
+
# とかで汚いけど、これをつかうと
|
23
|
+
# should == "label for display"
|
24
|
+
# のようにキレイになる
|
25
|
+
#
|
26
|
+
# -------------------------------------------------------------------------------------------------
|
27
|
+
|
28
|
+
require "named_let/version"
|
29
|
+
|
30
|
+
module NamedLet
|
31
|
+
# In RSpec 2.8, RSpec::Core::Let::ExampleGroupMethods
|
32
|
+
if RSpec::Core::Version::STRING < "2.8.0"
|
33
|
+
klass = RSpec::Core::Let::ClassMethods
|
34
|
+
else
|
35
|
+
klass = RSpec::Core::Let::ExampleGroupMethods
|
36
|
+
end
|
37
|
+
|
38
|
+
klass.class_eval do
|
39
|
+
def named_let(name, label = nil, &block)
|
40
|
+
define_method(name) do
|
41
|
+
__memoized.fetch(name) {|k| __memoized[k] = instance_eval(&block).tap{|o|
|
42
|
+
return o if o.nil?
|
43
|
+
|
44
|
+
the_name = label || name
|
45
|
+
|
46
|
+
# if given -d/--debug option, append calling original ones.(ruby-debug required)
|
47
|
+
call_super = begin ;$DEBUG or Debugger.started? rescue LoadError; nil; end
|
48
|
+
|
49
|
+
inject_code = lambda{|obj, code| begin; obj.instance_eval code; rescue TypeError; end }
|
50
|
+
escape = lambda{|str| str.gsub(/\"/, '\\"')}
|
51
|
+
|
52
|
+
genereate_wrapper_code = lambda{|obj, method|
|
53
|
+
original_result = escape.call(obj.send(method)) if call_super
|
54
|
+
code = "def #{method}; \"#{the_name}\" #{call_super ? " + \" (#{original_result})\"" : ''} ;end"
|
55
|
+
}
|
56
|
+
|
57
|
+
to_s_code = genereate_wrapper_code.call(o, :to_s)
|
58
|
+
inspect_code = genereate_wrapper_code.call(o, :inspect)
|
59
|
+
|
60
|
+
inject_code.call(o, to_s_code)
|
61
|
+
inject_code.call(o, inspect_code)
|
62
|
+
o
|
63
|
+
}}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def named_let!(name, label = nil, &block)
|
68
|
+
named_let(name, label, &block)
|
69
|
+
before { __send__(name) }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/named_let.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "named_let/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "named_let"
|
7
|
+
s.version = NamedLet::VERSION
|
8
|
+
s.authors = ["Tomohito Ozaki"]
|
9
|
+
s.email = ["ozaki@yuroyoro.com"]
|
10
|
+
s.homepage = "https://github.com/yuroyoro/named_let"
|
11
|
+
s.summary = %q{named_let can be used to make the rspec's output easier to read.}
|
12
|
+
s.description = %q{'named_let(:name){ obj }' changes the value which returns 'obj#to_s' and 'obj#inspect' to :name, then output of 'rspec -format d' be improved more readable.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "named_let"
|
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
|
+
# dependencies
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "rspec-core", ">= 0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: named_let
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tomohito Ozaki
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-02-24 00:00:00 +09:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec-core
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: "'named_let(:name){ obj }' changes the value which returns 'obj#to_s' and 'obj#inspect' to :name, then output of 'rspec -format d' be improved more readable."
|
50
|
+
email:
|
51
|
+
- ozaki@yuroyoro.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
61
|
+
- LICENSE
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- lib/named_let.rb
|
65
|
+
- lib/named_let/version.rb
|
66
|
+
- named_let.gemspec
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: https://github.com/yuroyoro/named_let
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 3
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
requirements: []
|
95
|
+
|
96
|
+
rubyforge_project: named_let
|
97
|
+
rubygems_version: 1.5.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: named_let can be used to make the rspec's output easier to read.
|
101
|
+
test_files: []
|
102
|
+
|