interactive_rspec 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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in interactive_rspec.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Akira Matsuda
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.rdoc ADDED
@@ -0,0 +1,88 @@
1
+ = Interactive RSpec
2
+
3
+ RSpec on IRB
4
+
5
+
6
+ == Features
7
+
8
+ === Execute IRB matchers in the console
9
+
10
+ irb> >> (1+1).should == 3
11
+ F
12
+
13
+ Failures:
14
+
15
+ 1)
16
+ Failure/Error: Unable to find matching line from backtrace
17
+ expected: 3
18
+ got: 2 (using ==)
19
+ # (irb):2
20
+
21
+ Finished in 18.53 seconds
22
+ 1 example, 1 failure
23
+
24
+ Failed examples:
25
+
26
+ rspec ./.rvm/gems/ruby-1.8.7-p334/gems/interactive_rspec-0.0.1/lib/interactive_rspec.rb:44 #
27
+ => false
28
+
29
+ Let's call this the "interactive RSpec".
30
+
31
+ === Execute spec file(s) via running IRB session
32
+
33
+ > irspec 'path/to/spec_file_spec.rb'
34
+
35
+ The filename parameter matches to various shortcuts such as:
36
+
37
+ 'spec/foo' => spec/foo_spec.rb
38
+ 'models/user' => spec/models/user_spec.rb
39
+ 'controllers' => spec/controllers/**/*_spec.rb
40
+ :all => **/*_spec.rb
41
+
42
+
43
+ == How to start
44
+
45
+ === Launch an interactive RSpec console via CLI
46
+
47
+ % irspec
48
+
49
+ === Launch an interactive RSpec console via running IRB session
50
+
51
+ > require 'rubygems'
52
+ > require 'interacive-rspec'
53
+ > irspec
54
+
55
+ == With Rails
56
+
57
+ === Bundle interactive-rspec gem
58
+
59
+ gem 'interactive-rspec'
60
+
61
+ % bundle
62
+
63
+ === Start up your Rails console
64
+
65
+ % rails c
66
+
67
+ === Then it's done!
68
+
69
+ You can check how the matchers work:
70
+
71
+ > irspec
72
+ > User.new(:name => 'matz').should_not be_valid
73
+
74
+ You can run any of the existing spec files:
75
+
76
+ > irspec 'spec/requests/users_spec.rb'
77
+
78
+ You might notice that it runs suuuuper fast since irspec uses already loaded Rails process.
79
+
80
+
81
+ == Contributing to Interactive RSpec
82
+
83
+ * Fork, fix, then send me a pull request.
84
+
85
+
86
+ == Copyright
87
+
88
+ Copyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/irspec ADDED
@@ -0,0 +1,3 @@
1
+ require 'interactive_rspec'
2
+
3
+ InteractiveRspec.start
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'interactive_rspec/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'interactive_rspec'
7
+ s.version = InteractiveRspec::VERSION
8
+ s.authors = ['Akira Matsuda']
9
+ s.email = ['ronnie@dio.jp']
10
+ s.homepage = 'https://github.com/amatsuda/interactive_rspec'
11
+ s.summary = %q{RSpec on IRB}
12
+ s.description = %q{RSpec on IRB}
13
+
14
+ s.rubyforge_project = 'interactive_rspec'
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
+ # specify any dependencies here; for example:
22
+ s.add_runtime_dependency 'rspec'
23
+ end
@@ -0,0 +1,3 @@
1
+ module InteractiveRspec
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,126 @@
1
+ require 'interactive_rspec/version'
2
+ require 'rspec'
3
+ require 'irb'
4
+ require 'irb/completion'
5
+
6
+ module InteractiveRspec
7
+ class << self
8
+ attr_accessor :rspec_mode
9
+ end
10
+ end
11
+
12
+ require File.join(File.dirname(__FILE__), 'monkey/irb')
13
+ require File.join(File.dirname(__FILE__), 'monkey/rspec')
14
+
15
+ module InteractiveRspec
16
+ def self.start(options = {})
17
+ configure if {:configure => true}.merge(options)
18
+
19
+ switch_rspec_mode do
20
+ switch_rails_env do
21
+ IRB.start_with_context new_extended_example_group
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.configure
27
+ #TODO load spec_helper.rb?
28
+ RSpec.configure do |c|
29
+ c.output_stream = STDOUT
30
+ c.color_enabled = true
31
+ end
32
+ end
33
+
34
+ def self.new_extended_example_group
35
+ eg = describe
36
+ RSpec.configuration.expectation_frameworks.each do |framework|
37
+ eg.extend framework
38
+ end
39
+ eg.extend RSpec.configuration.mock_framework
40
+ end
41
+
42
+ # @param [Exception or true]
43
+ def self.report(result)
44
+ e = describe.example
45
+
46
+ ret = RSpec.configuration.reporter.report(1) do |r|
47
+ r.instance_variable_set '@example_count', 1
48
+ if result.is_a? Exception
49
+ e.send :record, :status => 'failed', :finished_at => Time.now, :run_time => 0, :exception => result
50
+ r.example_failed e
51
+ false
52
+ else
53
+ r.example_passed e
54
+ true
55
+ end
56
+ end
57
+ RSpec.reset
58
+ ret
59
+ end
60
+
61
+ def self.run_specs(specs)
62
+ # to avoid auto_run at_exit
63
+ RSpec::Core::Runner.instance_variable_set '@autorun_disabled', true
64
+ config_options = RSpec::Core::ConfigurationOptions.new ['--color', fuzzy_match(specs)]
65
+ config_options.parse_options
66
+
67
+ RSpec::Core::CommandLine.new(config_options, RSpec.configuration, RSpec.world).run(STDERR, STDOUT)
68
+ end
69
+
70
+ def self.fuzzy_match(specs)
71
+ return Dir.glob '**/*_spec.rb' if specs == :all
72
+ [specs, "spec/#{specs}", "#{specs}.rb", "#{specs}_spec.rb", "spec/#{specs}.rb", "spec/#{specs}_spec.rb", "#{specs}/**/*_spec.rb", "spec/#{specs}/**/*_spec.rb"].each do |pattern|
73
+ files = Dir.glob pattern
74
+ return files if files.any?
75
+ end
76
+ specs
77
+ end
78
+
79
+ def self.switch_rspec_mode(&block)
80
+ begin
81
+ InteractiveRspec.rspec_mode = true
82
+ block.call
83
+ ensure
84
+ InteractiveRspec.rspec_mode = false
85
+ end
86
+ end
87
+
88
+ def self.switch_rails_env(&block)
89
+ begin
90
+ original_env_rails_env, original_rails_rails_env = nil, nil
91
+ if defined? Rails
92
+ unless Rails.env.test?
93
+ original_env_rails_env = ENV['RAILS_ENV']
94
+ ENV['RAILS_ENV'] = 'test'
95
+ original_rails_rails_env = Rails.env
96
+ load Rails.root.join 'config/environments/test.rb'
97
+ Rails.env = 'test'
98
+ reconnect_active_record
99
+ Bundler.require :test if defined? Bundler
100
+ end
101
+ end
102
+
103
+ block.call
104
+ ensure
105
+ if original_env_rails_env || original_rails_rails_env
106
+ ENV['RAILS_ENV'] = original_env_rails_env
107
+ Rails.env = original_rails_rails_env
108
+ load Rails.root.join "config/environments/#{Rails.env}.rb"
109
+ reconnect_active_record
110
+ end
111
+ end
112
+ end
113
+
114
+ def self.reconnect_active_record
115
+ if defined? ActiveRecord::Base
116
+ if ActiveRecord::Base.respond_to? :clear_cache
117
+ ActiveRecord::Base.clear_cache!
118
+ else
119
+ end
120
+ ActiveRecord::Base.clear_all_connections!
121
+ ActiveRecord::Base.establish_connection
122
+ end
123
+ end
124
+ end
125
+
126
+ InteractiveRSpec = InteractiveRspec
data/lib/monkey/irb.rb ADDED
@@ -0,0 +1,47 @@
1
+ module IRB
2
+ def IRB.start_with_context(context = nil)
3
+ IRB.setup nil
4
+
5
+ if @CONF[:SCRIPT]
6
+ irb = Irb.new(nil, @CONF[:SCRIPT])
7
+ else
8
+ irb = Irb.new WorkSpace.new(TOPLEVEL_BINDING, context)
9
+ end
10
+
11
+ @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
12
+ @CONF[:MAIN_CONTEXT] = irb.context
13
+
14
+ trap("SIGINT") do
15
+ irb.signal_handle
16
+ end
17
+
18
+ begin
19
+ catch(:IRB_EXIT) do
20
+ irb.eval_input
21
+ end
22
+ ensure
23
+ irb_at_exit
24
+ end
25
+ end
26
+
27
+ module ExtendCommandBundle
28
+ def irspec(specs = nil)
29
+ #TODO check if already in irspec
30
+ InteractiveRspec.configure
31
+ if specs
32
+ InteractiveRspec.switch_rails_env do
33
+ InteractiveRspec.run_specs specs
34
+ end
35
+ else
36
+ InteractiveRspec.switch_rspec_mode do
37
+ InteractiveRspec.switch_rails_env do
38
+ # pushws InteractiveRspec.new_extended_example_group
39
+ irb InteractiveRspec.new_extended_example_group
40
+ end
41
+ end
42
+ end
43
+ RSpec.reset
44
+ nil
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,44 @@
1
+ module RSpec
2
+ module Matchers
3
+ class OperatorMatcher
4
+ def eval_match_with_reporting(actual, operator, expected)
5
+ return eval_match_without_reporting(actual, operator, expected) unless InteractiveRspec.rspec_mode
6
+
7
+ InteractiveRspec.report begin
8
+ eval_match_without_reporting(actual, operator, expected)
9
+ rescue RSpec::Expectations::ExpectationNotMetError => err
10
+ err
11
+ end
12
+ end
13
+ alias_method :eval_match_without_reporting, :eval_match
14
+ alias_method :eval_match, :eval_match_with_reporting
15
+ end
16
+ end
17
+
18
+ module Expectations
19
+ class PositiveExpectationHandler
20
+ class << self
21
+ unless method_defined? :handle_matcher_with_reporting
22
+ def handle_matcher_with_reporting(actual, matcher, message=nil, &block)
23
+ return handle_matcher_without_reporting actual, matcher, message, &block unless InteractiveRspec.rspec_mode
24
+
25
+ begin
26
+ result = handle_matcher_without_reporting actual, matcher, message, &block
27
+ if result.class == TrueClass
28
+ InteractiveRspec.report result
29
+ else
30
+ result
31
+ end
32
+ rescue RSpec::Expectations::ExpectationNotMetError => err
33
+ InteractiveRspec.report err
34
+ false
35
+ end
36
+ end
37
+
38
+ alias_method :handle_matcher_without_reporting, :handle_matcher
39
+ alias_method :handle_matcher, :handle_matcher_with_reporting
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: interactive_rspec
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Akira Matsuda
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-01 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: RSpec on IRB
35
+ email:
36
+ - ronnie@dio.jp
37
+ executables:
38
+ - irspec
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - MIT-LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - bin/irspec
50
+ - interactive_rspec.gemspec
51
+ - lib/interactive_rspec.rb
52
+ - lib/interactive_rspec/version.rb
53
+ - lib/monkey/irb.rb
54
+ - lib/monkey/rspec.rb
55
+ homepage: https://github.com/amatsuda/interactive_rspec
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: interactive_rspec
84
+ rubygems_version: 1.8.11
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: RSpec on IRB
88
+ test_files: []
89
+
90
+ has_rdoc: