rspec_explain 0.0.1

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/.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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems #'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec_explain.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alexander K
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,54 @@
1
+ # RSpecExplain
2
+
3
+ The idea was to write something like scenario outlines in cucumber but without semicolons or vertical bars or other syntax trash.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rspec_explain'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rspec_explain
18
+
19
+ ## Usage
20
+
21
+ describe 'Usage' do
22
+ subject { Usage.new }
23
+
24
+ explain :square, %w[
25
+ 2 4
26
+ 10 100
27
+ 25 625
28
+ 30 900
29
+ ].map(&:to_i)
30
+
31
+ explain :no_spaces, <<-EX
32
+ one string => one_string
33
+ a b c => a_b_c
34
+ a_b c_d => a_b_c_d
35
+ EX
36
+
37
+ end
38
+
39
+ ### output:
40
+
41
+ Usage
42
+ #square
43
+ 2 => 4
44
+ 10 => 100
45
+ ...
46
+
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :spec
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,5 @@
1
+ require "rspec_explain/version"
2
+
3
+ module RspecExplain
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,37 @@
1
+ module RSpecExplain
2
+ def explain method, examples, run='subject.%s(%s)'
3
+
4
+ examples = prepare examples
5
+
6
+ context "##{method}" do
7
+ examples.each do |input,output|
8
+ specify "#{input} => #{output}" do
9
+ eval(run % [method, input.inspect]).should == output #subject.send(:method, input)
10
+ end
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ private
17
+ def prepare table
18
+ case table
19
+ when Array then table.map { |x| prepare_one x }.each_slice(2)
20
+ when String then prepare table.lines.map { |line| line.split '=>' }.flatten
21
+ else
22
+ raise
23
+ end
24
+ end
25
+
26
+ COMMAND = %r"^`(.*)`$"
27
+ def prepare_one element
28
+
29
+ element = element.strip if element.is_a? String
30
+
31
+ case element
32
+ when COMMAND then eval(element[COMMAND,1])
33
+ else
34
+ element
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module RSpecExplain
2
+ VERSION = "0.0.1"
3
+ end
4
+
5
+ require_relative 'explain'
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rspec_explain/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rspec_explain"
8
+ gem.version = RSpecExplain::VERSION
9
+ gem.authors = ["Alexander K"]
10
+ gem.email = ["xpyro@ya.ru"]
11
+
12
+ gem.description = <<-DESC
13
+ I'd like to write simple methods with simple specs
14
+ so this gem gonna be easy to use
15
+ with syntactic sugar to simplify the addition of examples
16
+ but not so much configurable to shape a way you write methods
17
+ DESC
18
+
19
+ gem.summary = %q[helpers to describe methods by concise input/output tables]
20
+ gem.homepage = ""
21
+
22
+ gem.files = `git ls-files`.split($/)
23
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
24
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
25
+ gem.require_paths = ["lib"]
26
+
27
+ gem.add_development_dependency "rspec", "~> 2.0"
28
+ end
@@ -0,0 +1,160 @@
1
+ require 'spec_helper'
2
+
3
+ class Usage
4
+ include RSpecExplain
5
+
6
+
7
+ #####################
8
+ # Methods we test #
9
+ #####################
10
+
11
+ def square x
12
+ x**2
13
+ end
14
+
15
+ def no_spaces x
16
+ x.gsub(' ','_')
17
+ end
18
+
19
+ DROP_LAST = [%r|^(.*/)[^/]+/$|, 1]
20
+
21
+ def level_up x
22
+ x[*DROP_LAST]
23
+ end
24
+
25
+ def equal x
26
+ x
27
+ end
28
+
29
+
30
+ ##############
31
+ # Examples #
32
+ ##############
33
+
34
+ def usage_1
35
+ explain :square, [2,4, 3,9, 5,25, 10,100] # 4 examples
36
+ end
37
+
38
+ def usage_2
39
+ explain :square, %w[
40
+ 2 4
41
+ 3 9
42
+ 5 25
43
+ 6 36
44
+ 7 49
45
+ ].map(&:to_i)
46
+ end
47
+
48
+ def usage_3
49
+ explain :no_spaces, <<-EX
50
+ one string => one_string
51
+ a b c => a_b_c
52
+ a_b c_d => a_b_c_d
53
+ EX
54
+ end
55
+
56
+ def usage_4
57
+ explain :level_up, <<-EX
58
+ /x/y/z/ => /x/y/
59
+ /x/y/ => /x/
60
+ /one/ => /
61
+ / => `nil`
62
+ EX
63
+ end
64
+
65
+ def usage_5
66
+ explain :equal, <<-EX
67
+ `2*2` => `2+2`
68
+ `3*2` => `2*3`
69
+ `1*1` => `1**1`
70
+ EX
71
+ end
72
+
73
+ def usage_6
74
+ explain :equal, <<-EX.split(/$|=>/).map { |x| '`%s`' % x.strip }
75
+ 2*2 => 2+2
76
+ 3*2 => 2*3
77
+ 1*1 => 1**1
78
+ EX
79
+ end
80
+
81
+ def should_be_red
82
+ explain :equal, <<-EX
83
+ 2*2 => `2*2`
84
+ EX
85
+ end
86
+
87
+ private
88
+ def context method
89
+ yield
90
+ end
91
+
92
+ def specify method
93
+ yield
94
+ end
95
+
96
+ def subject # dont forget to set subject in your specs! #
97
+ self #
98
+ end #
99
+ end #
100
+ #
101
+ describe RSpecExplain do #
102
+ subject { Usage.new } # like this! <= #
103
+
104
+ context '.explain' do
105
+
106
+ it 'creates context block for given method' do
107
+ subject.should_receive(:context).with('#square').once
108
+ subject.usage_1
109
+ end
110
+
111
+ it 'specifies each example inside context block' do
112
+ subject.should_receive(:specify).exactly(4).times
113
+ subject.usage_1
114
+ end
115
+
116
+ it 'invokes subject inside specify block' do
117
+ subject.should_receive(:subject).exactly(4).times { subject }
118
+ subject.usage_1
119
+ end
120
+
121
+ it 'invokes :method on subject' do
122
+ subject.should_receive(:square).exactly(4).times { |x| x**2 }
123
+ subject.usage_1
124
+ end
125
+
126
+ # specify 'someday I will test .should==output part' do
127
+ # pending
128
+ # end
129
+
130
+
131
+ context 'obvious testing' do
132
+
133
+ it 'good examples should pass' do
134
+ subject.methods.grep(/usage_\d/).size.should be >= 4
135
+ subject.methods.grep(/usage_\d/).each { |method| subject.send method }
136
+ end
137
+
138
+ it 'bad examples should not pass' do
139
+ expect { subject.should_be_red }.to raise_error
140
+ end
141
+
142
+ end
143
+
144
+ context 'the most obvious usage example:' do
145
+ extend RSpecExplain
146
+
147
+ describe 'Usage' do
148
+ subject { Usage.new }
149
+
150
+ explain :square, %w[
151
+ 2 4
152
+ 10 100
153
+ 25 625
154
+ 30 900
155
+ ].map(&:to_i)
156
+
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,20 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require "bundler"
8
+ Bundler.require :default, :development
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec_explain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander K
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
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: '2.0'
30
+ description: ! " I'd like to write simple methods with simple specs\n so this
31
+ gem gonna be easy to use\n with syntactic sugar to simplify the addition of examples\n
32
+ \ but not so much configurable to shape a way you write methods\n"
33
+ email:
34
+ - xpyro@ya.ru
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - .rspec
41
+ - Gemfile
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - lib/rspec_explain.rb
46
+ - lib/rspec_explain/explain.rb
47
+ - lib/rspec_explain/version.rb
48
+ - rspec_explain.gemspec
49
+ - spec/explain_spec.rb
50
+ - spec/spec_helper.rb
51
+ homepage: ''
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.23
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: helpers to describe methods by concise input/output tables
75
+ test_files:
76
+ - spec/explain_spec.rb
77
+ - spec/spec_helper.rb
78
+ has_rdoc: