transpec 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3d0d724597250da7c8777978b0966f79bb8ef4a
4
- data.tar.gz: d8cd06841df2954393cbe9ce26bb6d4b892fb8d7
3
+ metadata.gz: 1c33db67b6816b403a76cc42ab37c1a2fa20f0a5
4
+ data.tar.gz: 5842ca3f82cdd32eda912aabc59fe59b0b9e857f
5
5
  SHA512:
6
- metadata.gz: eaf64a911679b0b2277c3643ee3e53a2b92b8ac3b0f8d5730f6a5399e4179114b0290c32b4c308968601884a47728f52f77d8babbfdafb3309407a8ef7e8dddd
7
- data.tar.gz: 081bd5d3f7d4e0b91a7ec37754cee098c0fcc714862044304549daff2f2e263ecb3c0d3a120c1299e049d4dcdcd7750d4cc93d9d8e9b5cf548e3fd6f69b101c6
6
+ metadata.gz: 57d17382d629af2561f02042eb31f8d1f64ac6d95c63251e55699974c1340da56947d91c51dd198e19c67b26e53edc0843c8aa58eb1fc73fa405ca8f99850327
7
+ data.tar.gz: 586efdc379d22a195160a5d5e83b4f0284b23c67fdb2b74c9f5b9917167c6de5ab55037ee20720925dbf1cce082463e0f3f9ce4140c4d9b62a11fbc7ea167873
@@ -3,4 +3,4 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - jruby-19mode
6
- script: bundle exec rake all
6
+ script: bundle exec rake
@@ -1,4 +1,11 @@
1
- # CHANGELOG
1
+ # Changelog
2
+
3
+ ## Master
4
+
5
+ ## v0.0.3
6
+
7
+ * Suppress addition of superfluous parentheses when converting operator matcher that have argument in parentheses to non-operator matcher (e.g. from `== (2 - 1)` to `eq(2 - 1)`)
8
+ * Support auto-modification of syntax configuration in `RSpec.configure`
2
9
 
3
10
  ## v0.0.2
4
11
 
data/Guardfile CHANGED
@@ -14,5 +14,7 @@ guard :rubocop do
14
14
  end
15
15
 
16
16
  guard :shell do
17
- watch('README.md.erb') { `rake readme` }
17
+ watch('README.md.erb') do
18
+ system('rake', 'readme') || n('Failed to build README.md', 'README Build Result', :failed)
19
+ end
18
20
  end
data/README.md CHANGED
@@ -121,6 +121,77 @@ This will convert and overwrite all spec files in the `spec` directory.
121
121
 
122
122
  After the conversion, run `rspec` again and check if all pass.
123
123
 
124
+ ## Options
125
+
126
+ ### `-d/--disable`
127
+
128
+ Disable specific conversions.
129
+
130
+ ```bash
131
+ $ transpec --disable expect_to_receive,allow_to_receive
132
+ ```
133
+
134
+ #### Available conversion types
135
+
136
+ Conversion Type | Target Syntax | Converted Syntax
137
+ --------------------|----------------------------------|----------------------------
138
+ `expect_to_matcher` | `obj.should` | `expect(obj).to`
139
+ `expect_to_receive` | `obj.should_receive` | `expect(obj).to receive`
140
+ `allow_to_receive` | `obj.stub` | `allow(obj).to receive`
141
+ `deprecated` | `obj.stub!`, `mock('foo')`, etc. | `obj.stub`, `double('foo')`
142
+
143
+ ### `-n/--negative-form`
144
+
145
+ Specify negative form of `to` that is used in `expect` syntax.
146
+ Either `not_to` or `to_not`.
147
+ `not_to` is used by default.
148
+
149
+ ```bash
150
+ $ transpec --negative-form to_not
151
+ ```
152
+
153
+ ### `-p/--no-parentheses-matcher-arg`
154
+
155
+ Suppress parenthesizing argument of matcher when converting
156
+ `should` with operator matcher to `expect` with non-operator matcher
157
+ (`expect` syntax does not directly support the operator matchers).
158
+ Note that it will be parenthesized even if this option is specified
159
+ when parentheses are necessary to keep the meaning of the expression.
160
+
161
+ ```ruby
162
+ describe 'original spec' do
163
+ it 'is example' do
164
+ 1.should == 1
165
+ 2.should > 1
166
+ 'string'.should =~ /str/
167
+ [1, 2, 3].should =~ [2, 1, 3]
168
+ { key: value }.should == { key: value }
169
+ end
170
+ end
171
+
172
+ describe 'converted spec' do
173
+ it 'is example' do
174
+ expect(1).to eq(1)
175
+ expect(2).to be > 1
176
+ expect('string').to match(/str/)
177
+ expect([1, 2, 3]).to match_array([2, 1, 3])
178
+ expect({ key: value }).to eq({ key: value })
179
+ end
180
+ end
181
+
182
+ describe 'converted spec with -p/--no-parentheses-matcher-arg option' do
183
+ it 'is example' do
184
+ expect(1).to eq 1
185
+ expect(2).to be > 1
186
+ expect('string').to match /str/
187
+ expect([1, 2, 3]).to match_array [2, 1, 3]
188
+ # With non-operator method, the parentheses are always required
189
+ # to prevent the hash from being interpreted as a block.
190
+ expect({ key: value }).to eq({ key: value })
191
+ end
192
+ end
193
+ ```
194
+
124
195
  ## Compatibility
125
196
 
126
197
  Tested on MRI 1.9, MRI 2.0 and JRuby in 1.9 mode.
@@ -94,6 +94,100 @@ This will convert and overwrite all spec files in the `spec` directory.
94
94
 
95
95
  After the conversion, run `rspec` again and check if all pass.
96
96
 
97
+ ## Options
98
+
99
+ ### `-d/--disable`
100
+
101
+ Disable specific conversions.
102
+
103
+ ```bash
104
+ $ transpec --disable expect_to_receive,allow_to_receive
105
+ ```
106
+
107
+ #### Available conversion types
108
+
109
+ Conversion Type | Target Syntax | Converted Syntax
110
+ --------------------|----------------------------------|----------------------------
111
+ <%=
112
+ conversion_type_table = <<END
113
+ `expect_to_matcher` | `obj.should` | `expect(obj).to`
114
+ `expect_to_receive` | `obj.should_receive` | `expect(obj).to receive`
115
+ `allow_to_receive` | `obj.stub` | `allow(obj).to receive`
116
+ `deprecated` | `obj.stub!`, `mock('foo')`, etc. | `obj.stub`, `double('foo')`
117
+ END
118
+
119
+ types_in_readme = conversion_type_table.lines.map do |line|
120
+ first_column = line.split('|').first
121
+ first_column.gsub(/[^\w]/, '').to_sym
122
+ end.sort
123
+
124
+ types_in_code = Transpec::CLI::CONFIG_ATTRS_FOR_CLI_TYPES.keys.sort
125
+
126
+ unless types_in_readme == types_in_code
127
+ types_missing_description = types_in_code - types_in_readme
128
+ fail "No descriptions for conversion types #{types_missing_description}"
129
+ end
130
+
131
+ conversion_type_table
132
+ -%>
133
+
134
+ ### `-n/--negative-form`
135
+
136
+ Specify negative form of `to` that is used in `expect` syntax.
137
+ Either `not_to` or `to_not`.
138
+ `not_to` is used by default.
139
+
140
+ ```bash
141
+ $ transpec --negative-form to_not
142
+ ```
143
+
144
+ ### `-p/--no-parentheses-matcher-arg`
145
+
146
+ Suppress parenthesizing argument of matcher when converting
147
+ `should` with operator matcher to `expect` with non-operator matcher
148
+ (`expect` syntax does not directly support the operator matchers).
149
+ Note that it will be parenthesized even if this option is specified
150
+ when parentheses are necessary to keep the meaning of the expression.
151
+
152
+ ```ruby
153
+ <%=
154
+ parenthesizing_example = <<END
155
+ describe 'original spec' do
156
+ it 'is example' do
157
+ 1.should == 1
158
+ 2.should > 1
159
+ 'string'.should =~ /str/
160
+ [1, 2, 3].should =~ [2, 1, 3]
161
+ { key: value }.should == { key: value }
162
+ end
163
+ end
164
+ END
165
+ -%>
166
+
167
+ <%=
168
+ Transpec::Rewriter.new.rewrite(parenthesizing_example).gsub(
169
+ 'original spec',
170
+ 'converted spec'
171
+ )
172
+ -%>
173
+
174
+ <%=
175
+ configuration = Transpec::Configuration.new
176
+ configuration.parenthesize_matcher_arg = false
177
+ rewriter = Transpec::Rewriter.new(configuration)
178
+ rewriter.rewrite(parenthesizing_example)
179
+ .gsub(
180
+ 'original spec',
181
+ 'converted spec with -p/--no-parentheses-matcher-arg option'
182
+ )
183
+ .gsub(/^.+\{ key: value \}/) do |match|
184
+ " # With non-operator method, the parentheses are always required\n" +
185
+ " # to prevent the hash from being interpreted as a block.\n" +
186
+ match
187
+ end
188
+ -%>
189
+ ```
190
+
97
191
  ## Compatibility
98
192
 
99
193
  Tested on MRI 1.9, MRI 2.0 and JRuby in 1.9 mode.
data/Rakefile CHANGED
@@ -18,13 +18,10 @@ namespace :ci do
18
18
  end
19
19
  end
20
20
 
21
- desc 'Run RSpec and RuboCop'
22
- task all: [:spec, :style]
23
-
24
21
  desc 'Generate README.md'
25
22
  task :readme do
26
23
  require 'erb'
27
- require 'transpec'
24
+ require 'transpec/cli'
28
25
 
29
26
  gem_specification = Gem::Specification.load('transpec.gemspec')
30
27
  rspec_dependency = gem_specification.dependencies.find { |d| d.name == 'rspec' }
@@ -45,4 +42,4 @@ end
45
42
 
46
43
  Rake::Task[:release].enhance([:abort_unless_latest_readme_is_committed])
47
44
 
48
- task default: :all
45
+ task default: [:spec, :style, :readme]
@@ -13,6 +13,7 @@ module Transpec
13
13
  def self.scan(origin_node, &block)
14
14
  instance = new(&block)
15
15
  instance.scan(origin_node, true)
16
+ nil
16
17
  end
17
18
 
18
19
  def initialize(&block)
@@ -7,6 +7,7 @@ require 'transpec/syntax/be_close'
7
7
  require 'transpec/syntax/double'
8
8
  require 'transpec/syntax/matcher'
9
9
  require 'transpec/syntax/method_stub'
10
+ require 'transpec/syntax/rspec_configure'
10
11
  require 'transpec/syntax/should'
11
12
  require 'transpec/syntax/should_receive'
12
13
  require 'parser/current'
@@ -117,5 +118,26 @@ module Transpec
117
118
  def process_be_close(be_close)
118
119
  be_close.convert_to_be_within! if @configuration.replace_deprecated_method?
119
120
  end
121
+
122
+ def process_rspec_configure(rspec_configure)
123
+ if need_to_modify_expectation_syntax_configuration?(rspec_configure)
124
+ rspec_configure.modify_expectation_syntaxes!(:expect)
125
+ end
126
+
127
+ if need_to_modify_mock_syntax_configuration?(rspec_configure)
128
+ rspec_configure.modify_mock_syntaxes!(:expect)
129
+ end
130
+ end
131
+
132
+ def need_to_modify_expectation_syntax_configuration?(rspec_configure)
133
+ return false unless @configuration.convert_to_expect_to_matcher?
134
+ rspec_configure.expectation_syntaxes == [:should]
135
+ end
136
+
137
+ def need_to_modify_mock_syntax_configuration?(rspec_configure)
138
+ return false if !@configuration.convert_to_expect_to_receive? &&
139
+ !@configuration.convert_to_allow_to_receive?
140
+ rspec_configure.mock_syntaxes == [:should]
141
+ end
120
142
  end
121
143
  end
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require 'transpec/syntax/send_node_syntax'
4
+ require 'transpec/util'
4
5
 
5
6
  module Transpec
6
7
  class Syntax
@@ -8,7 +9,7 @@ module Transpec
8
9
  include SendNodeSyntax
9
10
 
10
11
  def wrap_subject_in_expect!
11
- if subject_range.source[0] == '('
12
+ if Util.in_parentheses?(subject_range)
12
13
  insert_before(subject_range, 'expect')
13
14
  else
14
15
  insert_before(subject_range, 'expect(')
@@ -41,7 +41,9 @@ module Transpec
41
41
 
42
42
  case left_parenthesis_range.source
43
43
  when ' '
44
- if always || arg_node.type == :hash
44
+ if in_parentheses?(arg_node)
45
+ remove(left_parenthesis_range)
46
+ elsif always || arg_node.type == :hash
45
47
  replace(left_parenthesis_range, '(')
46
48
  insert_after(expression_range, ')')
47
49
  end
@@ -0,0 +1,113 @@
1
+ # coding: utf-8
2
+
3
+ require 'transpec/syntax'
4
+ require 'transpec/util'
5
+ require 'transpec/ast/scanner'
6
+
7
+ module Transpec
8
+ class Syntax
9
+ class RSpecConfigure < Syntax
10
+ class FrameworkConfiguration
11
+ include ::AST::Sexp
12
+
13
+ def initialize(rspec_configure_node, framework_config_method_name, source_rewriter)
14
+ @rspec_configure_node = rspec_configure_node
15
+ @framework_config_method_name = framework_config_method_name
16
+ @source_rewriter = source_rewriter
17
+ end
18
+
19
+ def syntaxes
20
+ return [] unless syntaxes_node
21
+
22
+ case syntaxes_node.type
23
+ when :sym
24
+ [syntaxes_node.children.first]
25
+ when :array
26
+ syntaxes_node.children.map do |child_node|
27
+ child_node.children.first
28
+ end
29
+ else
30
+ fail
31
+ end
32
+ end
33
+
34
+ def modify_syntaxes!(syntaxes)
35
+ unless [Array, Symbol].include?(syntaxes.class)
36
+ fail ArgumentError, 'Syntaxes must be either an array or a symbol.'
37
+ end
38
+
39
+ @source_rewriter.replace(syntaxes_node.loc.expression, syntaxes.inspect)
40
+ end
41
+
42
+ private
43
+
44
+ def syntaxes_node
45
+ return nil unless framework_block_node
46
+
47
+ @syntaxes_node ||= begin
48
+ framework_config_variable_name = first_block_arg_name(framework_block_node)
49
+
50
+ AST::Scanner.scan(framework_block_node) do |descendent_node|
51
+ next unless descendent_node.type == :send
52
+ receiver_node, method_name, arg_node, *_ = *descendent_node
53
+ next unless receiver_node == s(:lvar, framework_config_variable_name)
54
+ next unless method_name == :syntax=
55
+ break arg_node
56
+ end
57
+ end
58
+ end
59
+
60
+ def framework_block_node
61
+ @framework_block_node ||= begin
62
+ AST::Scanner.scan(@rspec_configure_node) do |descendent_node|
63
+ next unless descendent_node.type == :block
64
+ send_node = descendent_node.children.first
65
+ receiver_node, method_name, *_ = *send_node
66
+ next unless receiver_node == s(:lvar, rspec_configure_block_arg_name)
67
+ next unless method_name == @framework_config_method_name
68
+ # TODO: Check expectation framework.
69
+ break descendent_node
70
+ end
71
+ end
72
+ end
73
+
74
+ def rspec_configure_block_arg_name
75
+ first_block_arg_name(@rspec_configure_node)
76
+ end
77
+
78
+ def first_block_arg_name(block_node)
79
+ args_node = block_node.children[1]
80
+ first_arg_node = args_node.children.first
81
+ first_arg_node.children.first
82
+ end
83
+ end
84
+
85
+ def self.target_node?(node)
86
+ return false unless node.type == :block
87
+ send_node = node.children.first
88
+ receiver_node, method_name, *_ = *send_node
89
+ Util.const_name(receiver_node) == 'RSpec' && method_name == :configure
90
+ end
91
+
92
+ def self.add_framework_configuration(type, config_method_name)
93
+ class_eval <<-END
94
+ def #{type}_syntaxes
95
+ #{type}_framework_configuration.syntaxes
96
+ end
97
+
98
+ def modify_#{type}_syntaxes!(syntaxes)
99
+ #{type}_framework_configuration.modify_syntaxes!(syntaxes)
100
+ end
101
+
102
+ def #{type}_framework_configuration
103
+ @#{type}_framework_configuration ||=
104
+ FrameworkConfiguration.new(node, :#{config_method_name}, source_rewriter)
105
+ end
106
+ END
107
+ end
108
+
109
+ add_framework_configuration :expectation, :expect_with
110
+ add_framework_configuration :mock, :mock_with
111
+ end
112
+ end
113
+ end
@@ -41,6 +41,21 @@ module Transpec
41
41
  node.loc.begin.source.start_with?('<<')
42
42
  end
43
43
 
44
+ def in_parentheses?(subject)
45
+ source = case subject
46
+ when String
47
+ subject
48
+ when Parser::Source::Range
49
+ subject.source
50
+ when Parser::AST::Node
51
+ subject.loc.expression.source
52
+ else
53
+ fail ArgumentError
54
+ end
55
+
56
+ source[0] == '('
57
+ end
58
+
44
59
  def indentation_of_line(node)
45
60
  line = node.loc.expression.source_line
46
61
  /^(?<indentation>\s*)\S/ =~ line
@@ -5,7 +5,7 @@ module Transpec
5
5
  module Version
6
6
  MAJOR = 0
7
7
  MINOR = 0
8
- PATCH = 2
8
+ PATCH = 3
9
9
 
10
10
  def self.to_s
11
11
  [MAJOR, MINOR, PATCH].join('.')
@@ -11,23 +11,32 @@ RSpec.configure do |config|
11
11
  end
12
12
 
13
13
  config.treat_symbols_as_metadata_keys_with_true_values = true
14
- config.filter_run_excluding do_not_run_in_transpeced_spec: ENV['TRANSPECED_SPEC']
14
+ config.filter_run_excluding do_not_run_in_converted_spec: ENV['TRANSPEC_CONVERTED_SPEC']
15
+
16
+ if ENV['TRAVIS']
17
+ config.before(:all) do
18
+ system('git config --global user.email "you@example.com"')
19
+ system('git config --global user.name "Your Name"')
20
+ end
21
+ end
15
22
  end
16
23
 
17
- require 'simplecov'
18
- SimpleCov.coverage_dir(File.join('spec', 'coverage'))
24
+ unless ENV['TRANSPEC_CONVERTED_SPEC']
25
+ require 'simplecov'
26
+ SimpleCov.coverage_dir(File.join('spec', 'coverage'))
19
27
 
20
- if ENV['TRAVIS']
21
- require 'coveralls'
22
- SimpleCov.formatter = Coveralls::SimpleCov::Formatter
23
- elsif ENV['CI']
24
- require 'simplecov-rcov'
25
- SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
26
- end
28
+ if ENV['TRAVIS']
29
+ require 'coveralls'
30
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
31
+ elsif ENV['CI']
32
+ require 'simplecov-rcov'
33
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
34
+ end
27
35
 
28
- SimpleCov.start do
29
- add_filter '/spec/'
30
- add_filter '/vendor/bundle/'
36
+ SimpleCov.start do
37
+ add_filter '/spec/'
38
+ add_filter '/vendor/bundle/'
39
+ end
31
40
  end
32
41
 
33
42
  Dir[File.join(File.dirname(__FILE__), 'support', '*')].each do |path|
@@ -4,23 +4,22 @@ require 'spec_helper'
4
4
  require 'tmpdir'
5
5
  require 'English'
6
6
 
7
- describe 'Transpec project spec', :do_not_run_in_transpeced_spec do
8
- around do |example|
7
+ describe 'Transpec project spec', :do_not_run_in_converted_spec do
8
+ copied_project_root = Dir.mktmpdir
9
+
10
+ before(:all) do
11
+ project_root = File.expand_path('..', File.dirname(__FILE__))
9
12
  Dir.chdir(project_root) do
10
- example.run
13
+ FileUtils.cp_r(Dir['*'], copied_project_root)
11
14
  end
12
15
  end
13
16
 
14
- let(:project_root) do
15
- File.expand_path('..', File.dirname(__FILE__))
16
- end
17
-
18
- let(:spec_dir) do
19
- File.join(project_root, 'spec')
17
+ around do |example|
18
+ Dir.chdir(copied_project_root) do
19
+ example.run
20
+ end
20
21
  end
21
22
 
22
- TRANSPECED_SPEC_DIR = File.join(Dir.mktmpdir, 'transpeced_spec')
23
-
24
23
  def silent_system(*args)
25
24
  original_env = ENV.to_hash
26
25
 
@@ -40,15 +39,13 @@ describe 'Transpec project spec', :do_not_run_in_transpeced_spec do
40
39
  end
41
40
 
42
41
  it 'can be converted by Transpec itself without error' do
43
- FileUtils.cp_r(spec_dir, TRANSPECED_SPEC_DIR)
44
- silent_system('./bin/transpec', '--force', TRANSPECED_SPEC_DIR).should be_true
42
+ silent_system('./bin/transpec', '--force').should be_true
45
43
  end
46
44
 
47
45
  describe 'converted spec' do
48
46
  it 'passes all' do
49
- pending 'Need to rewrite syntax configuration in RSpec.configure'
50
- env = { 'TRANSPECED_SPEC' => 'true' }
51
- silent_system(env, 'rspec', TRANSPECED_SPEC_DIR).should be_true
47
+ env = { 'TRANSPEC_CONVERTED_SPEC' => 'true' }
48
+ silent_system(env, 'rspec').should be_true
52
49
  end
53
50
  end
54
51
  end
@@ -46,21 +46,8 @@ end
46
46
  shared_context 'isolated environment' do
47
47
  around do |example|
48
48
  Dir.mktmpdir do |tmpdir|
49
- original_home = ENV['HOME']
50
-
51
- begin
52
- virtual_home = File.expand_path(File.join(tmpdir, 'home'))
53
- Dir.mkdir(virtual_home)
54
- ENV['HOME'] = virtual_home
55
-
56
- working_dir = File.join(tmpdir, 'work')
57
- Dir.mkdir(working_dir)
58
-
59
- Dir.chdir(working_dir) do
60
- example.run
61
- end
62
- ensure
63
- ENV['HOME'] = original_home
49
+ Dir.chdir(tmpdir) do
50
+ example.run
64
51
  end
65
52
  end
66
53
  end
@@ -7,13 +7,6 @@ module Transpec
7
7
  describe Git do
8
8
  include_context 'isolated environment'
9
9
 
10
- if ENV['TRAVIS']
11
- before do
12
- system('git config --global user.email "you@example.com"')
13
- system('git config --global user.name "Your Name"')
14
- end
15
- end
16
-
17
10
  describe '.command_available?' do
18
11
  subject { Git.command_available? }
19
12
 
@@ -29,6 +29,16 @@ module Transpec
29
29
 
30
30
  let(:source) do
31
31
  <<-END
32
+ RSpec.configure do |config|
33
+ config.expect_with :rspec do |c|
34
+ c.syntax = :should
35
+ end
36
+
37
+ config.mock_with :rspec do |c|
38
+ c.syntax = :should
39
+ end
40
+ end
41
+
32
42
  describe 'example group' do
33
43
  it 'is an example' do
34
44
  something = mock('something')
@@ -41,10 +51,10 @@ module Transpec
41
51
  END
42
52
  end
43
53
 
44
- context 'when the configuration #convert_to_expect_to_matcher? is true' do
54
+ context 'when Configuration#convert_to_expect_to_matcher? is true' do
45
55
  before { configuration.convert_to_expect_to_matcher = true }
46
56
 
47
- context 'and configuration #negative_form_of_to is "not_to"' do
57
+ context 'and Configuration#negative_form_of_to is "not_to"' do
48
58
  before { configuration.negative_form_of_to = 'not_to' }
49
59
 
50
60
  it 'invokes Should#expectize! with "not_to"' do
@@ -53,7 +63,7 @@ module Transpec
53
63
  end
54
64
  end
55
65
 
56
- context 'and configuration #negative_form_of_to is "to_not"' do
66
+ context 'and Configuration#negative_form_of_to is "to_not"' do
57
67
  before { configuration.negative_form_of_to = 'to_not' }
58
68
 
59
69
  it 'invokes Should#expectize! with "to_not"' do
@@ -62,7 +72,7 @@ module Transpec
62
72
  end
63
73
  end
64
74
 
65
- context 'and configuration #parenthesize_matcher_arg is true' do
75
+ context 'and Configuration#parenthesize_matcher_arg is true' do
66
76
  before { configuration.parenthesize_matcher_arg = true }
67
77
 
68
78
  it 'invokes Should#expectize! with true as second argument' do
@@ -71,7 +81,7 @@ module Transpec
71
81
  end
72
82
  end
73
83
 
74
- context 'and configuration #parenthesize_matcher_arg is false' do
84
+ context 'and Configuration#parenthesize_matcher_arg is false' do
75
85
  before { configuration.parenthesize_matcher_arg = false }
76
86
 
77
87
  it 'invokes Should#expectize! with false as second argument' do
@@ -81,7 +91,7 @@ module Transpec
81
91
  end
82
92
  end
83
93
 
84
- context 'when the configuration #convert_to_expect_to_matcher? is false' do
94
+ context 'when Configuration#convert_to_expect_to_matcher? is false' do
85
95
  before { configuration.convert_to_expect_to_matcher = false }
86
96
 
87
97
  it 'does not invoke Should#expectize!' do
@@ -90,10 +100,10 @@ module Transpec
90
100
  end
91
101
  end
92
102
 
93
- context 'when the configuration #convert_to_expect_to_receive? is true' do
103
+ context 'when Configuration#convert_to_expect_to_receive? is true' do
94
104
  before { configuration.convert_to_expect_to_receive = true }
95
105
 
96
- context 'and configuration #negative_form_of_to is "not_to"' do
106
+ context 'and Configuration#negative_form_of_to is "not_to"' do
97
107
  before { configuration.negative_form_of_to = 'not_to' }
98
108
 
99
109
  it 'invokes ShouldReceive#expectize! with "not_to"' do
@@ -102,7 +112,7 @@ module Transpec
102
112
  end
103
113
  end
104
114
 
105
- context 'and configuration #negative_form_of_to is "to_not"' do
115
+ context 'and Configuration#negative_form_of_to is "to_not"' do
106
116
  before { configuration.negative_form_of_to = 'to_not' }
107
117
 
108
118
  it 'invokes ShouldReceive#expectize! with "to_not"' do
@@ -112,7 +122,7 @@ module Transpec
112
122
  end
113
123
  end
114
124
 
115
- context 'when the configuration #convert_to_expect_to_receive? is false' do
125
+ context 'when Configuration#convert_to_expect_to_receive? is false' do
116
126
  before { configuration.convert_to_expect_to_receive = false }
117
127
 
118
128
  it 'does not invoke ShouldReceive#expectize!' do
@@ -121,7 +131,7 @@ module Transpec
121
131
  end
122
132
  end
123
133
 
124
- context 'when the configuration #convert_to_allow_to_receive? is true' do
134
+ context 'when Configuration#convert_to_allow_to_receive? is true' do
125
135
  before { configuration.convert_to_allow_to_receive = true }
126
136
 
127
137
  it 'invokes MethodStub#allowize!' do
@@ -130,7 +140,7 @@ module Transpec
130
140
  end
131
141
  end
132
142
 
133
- context 'when the configuration #convert_to_allow_to_receive? is false' do
143
+ context 'when Configuration#convert_to_allow_to_receive? is false' do
134
144
  before { configuration.convert_to_allow_to_receive = false }
135
145
 
136
146
  it 'does not invoke MethodStub#allowize!' do
@@ -139,7 +149,7 @@ module Transpec
139
149
  end
140
150
  end
141
151
 
142
- context 'when the configuration #replace_deprecated_method? is true' do
152
+ context 'when Configuration#replace_deprecated_method? is true' do
143
153
  before { configuration.replace_deprecated_method = true }
144
154
 
145
155
  it 'invokes Double#convert_to_double!' do
@@ -153,7 +163,7 @@ module Transpec
153
163
  end
154
164
  end
155
165
 
156
- context 'when the configuration #replace_deprecated_method? is true' do
166
+ context 'when Configuration#replace_deprecated_method? is true' do
157
167
  before { configuration.replace_deprecated_method = false }
158
168
 
159
169
  it 'does not invoke Double#convert_to_double!' do
@@ -167,6 +177,52 @@ module Transpec
167
177
  end
168
178
  end
169
179
 
180
+ context 'when #need_to_modify_expectation_syntax_configuration? returns true' do
181
+ before do
182
+ rewriter.stub(:need_to_modify_expectation_syntax_configuration?).and_return(true)
183
+ end
184
+
185
+ it 'invokes RSpecConfigure#modify_expectation_syntaxes! with :expect' do
186
+ Syntax::RSpecConfigure.any_instance
187
+ .should_receive(:modify_expectation_syntaxes!).with(:expect)
188
+ rewriter.rewrite(source)
189
+ end
190
+ end
191
+
192
+ context 'when #need_to_modify_expectation_syntax_configuration? returns false' do
193
+ before do
194
+ rewriter.stub(:need_to_modify_expectation_syntax_configuration?).and_return(false)
195
+ end
196
+
197
+ it 'does not invoke RSpecConfigure#modify_expectation_syntaxes!' do
198
+ Syntax::RSpecConfigure.any_instance.should_not_receive(:modify_expectation_syntaxes!)
199
+ rewriter.rewrite(source)
200
+ end
201
+ end
202
+
203
+ context 'when #need_to_modify_mock_syntax_configuration? returns true' do
204
+ before do
205
+ rewriter.stub(:need_to_modify_mock_syntax_configuration?).and_return(true)
206
+ end
207
+
208
+ it 'invokes RSpecConfigure#modify_mock_syntaxes! with :expect' do
209
+ Syntax::RSpecConfigure.any_instance
210
+ .should_receive(:modify_mock_syntaxes!).with(:expect)
211
+ rewriter.rewrite(source)
212
+ end
213
+ end
214
+
215
+ context 'when #need_to_modify_mock_syntax_configuration? returns false' do
216
+ before do
217
+ rewriter.stub(:need_to_modify_mock_syntax_configuration?).and_return(false)
218
+ end
219
+
220
+ it 'does not invoke RSpecConfigure#modify_mock_syntaxes!' do
221
+ Syntax::RSpecConfigure.any_instance.should_not_receive(:modify_mock_syntaxes!)
222
+ rewriter.rewrite(source)
223
+ end
224
+ end
225
+
170
226
  context 'when the source have overlapped rewrite targets' do
171
227
  let(:source) do
172
228
  <<-END
@@ -229,5 +285,103 @@ module Transpec
229
285
  end
230
286
  end
231
287
  end
288
+
289
+ shared_examples 'syntaxes' do |syntaxes_reader, expectations|
290
+ expectations.each do |current_syntaxes, return_value|
291
+ context "and RSpecConfigure##{syntaxes_reader} returns #{current_syntaxes.inspect}" do
292
+ before do
293
+ rspec_configure.stub(syntaxes_reader).and_return(current_syntaxes)
294
+ end
295
+
296
+ it "returns #{return_value}" do
297
+ should == return_value
298
+ end
299
+ end
300
+ end
301
+ end
302
+
303
+ describe '#need_to_modify_expectation_syntax_configuration?' do
304
+ subject { rewriter.need_to_modify_expectation_syntax_configuration?(rspec_configure) }
305
+ let(:rspec_configure) { double('rspec_configure') }
306
+
307
+ context 'when Configuration#convert_to_expect_to_matcher? is true' do
308
+ before { configuration.convert_to_expect_to_matcher = true }
309
+
310
+ include_examples 'syntaxes', :expectation_syntaxes, {
311
+ [] => false,
312
+ [:should] => true,
313
+ [:expect] => false,
314
+ [:should, :expect] => false
315
+ }
316
+ end
317
+
318
+ context 'when Configuration#convert_to_expect_to_matcher? is false' do
319
+ before { configuration.convert_to_expect_to_matcher = false }
320
+
321
+ include_examples 'syntaxes', :expectation_syntaxes, {
322
+ [] => false,
323
+ [:should] => false,
324
+ [:expect] => false,
325
+ [:should, :expect] => false
326
+ }
327
+ end
328
+ end
329
+
330
+ describe '#need_to_modify_mock_syntax_configuration?' do
331
+ subject { rewriter.need_to_modify_mock_syntax_configuration?(rspec_configure) }
332
+ let(:rspec_configure) { double('rspec_configure') }
333
+
334
+ context 'when Configuration#convert_to_expect_to_receive? is true' do
335
+ before { configuration.convert_to_expect_to_receive = true }
336
+
337
+ context 'and Configuration#convert_to_allow_to_receive? is true' do
338
+ before { configuration.convert_to_allow_to_receive = true }
339
+
340
+ include_examples 'syntaxes', :mock_syntaxes, {
341
+ [] => false,
342
+ [:should] => true,
343
+ [:expect] => false,
344
+ [:should, :expect] => false
345
+ }
346
+ end
347
+
348
+ context 'and Configuration#convert_to_allow_to_receive? is false' do
349
+ before { configuration.convert_to_allow_to_receive = false }
350
+
351
+ include_examples 'syntaxes', :mock_syntaxes, {
352
+ [] => false,
353
+ [:should] => true,
354
+ [:expect] => false,
355
+ [:should, :expect] => false
356
+ }
357
+ end
358
+ end
359
+
360
+ context 'when Configuration#convert_to_expect_to_receive? is false' do
361
+ before { configuration.convert_to_expect_to_receive = false }
362
+
363
+ context 'and Configuration#convert_to_allow_to_receive? is true' do
364
+ before { configuration.convert_to_allow_to_receive = true }
365
+
366
+ include_examples 'syntaxes', :mock_syntaxes, {
367
+ [] => false,
368
+ [:should] => true,
369
+ [:expect] => false,
370
+ [:should, :expect] => false
371
+ }
372
+ end
373
+
374
+ context 'and Configuration#convert_to_allow_to_receive? is false' do
375
+ before { configuration.convert_to_allow_to_receive = false }
376
+
377
+ include_examples 'syntaxes', :mock_syntaxes, {
378
+ [] => false,
379
+ [:should] => false,
380
+ [:expect] => false,
381
+ [:should, :expect] => false
382
+ }
383
+ end
384
+ end
385
+ end
232
386
  end
233
387
  end
@@ -133,6 +133,28 @@ module Transpec
133
133
  end
134
134
  end
135
135
 
136
+ context 'when it is `== (2 - 1)` form' do
137
+ let(:source) do
138
+ <<-END
139
+ it 'is 1' do
140
+ subject.should == (2 - 1)
141
+ end
142
+ END
143
+ end
144
+
145
+ let(:expected_source) do
146
+ <<-END
147
+ it 'is 1' do
148
+ subject.should eq(2 - 1)
149
+ end
150
+ END
151
+ end
152
+
153
+ it 'converts into `eq(2 - 1)` form without superfluous parentheses' do
154
+ rewritten_source.should == expected_source
155
+ end
156
+ end
157
+
136
158
  context "when it is `== { 'key' => 'value' }` form" do
137
159
  let(:source) do
138
160
  <<-END
@@ -0,0 +1,161 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'transpec/syntax/rspec_configure'
5
+
6
+ module Transpec
7
+ class Syntax
8
+ describe RSpecConfigure do
9
+ include_context 'parsed objects'
10
+
11
+ subject(:rspec_configure) do
12
+ AST::Scanner.scan(ast) do |node, ancestor_nodes|
13
+ next unless RSpecConfigure.target_node?(node)
14
+ return RSpecConfigure.new(
15
+ node,
16
+ ancestor_nodes,
17
+ in_example_group_context?,
18
+ source_rewriter
19
+ )
20
+ end
21
+ fail 'No RSpec.configure node is found!'
22
+ end
23
+
24
+ let(:in_example_group_context?) { true }
25
+
26
+ [
27
+ [:expectation_syntaxes, :expect_with, 'RSpec::Matchers::Configuration'],
28
+ [:mock_syntaxes, :mock_with, 'RSpec::Mocks::Configuration']
29
+ ].each do |subject_method, config_block_method, framework_config_class|
30
+ describe "##{subject_method}" do
31
+ subject { rspec_configure.send(subject_method) }
32
+
33
+ context 'when :should is enabled' do
34
+ let(:source) do
35
+ <<-END
36
+ RSpec.configure do |config|
37
+ config.#{config_block_method} :rspec do |c|
38
+ c.syntax = :should
39
+ end
40
+ end
41
+ END
42
+ end
43
+
44
+ it 'returns [:should]' do
45
+ should == [:should]
46
+ end
47
+ end
48
+
49
+ context 'when :should and :expect are enabled' do
50
+ let(:source) do
51
+ <<-END
52
+ RSpec.configure do |config|
53
+ config.#{config_block_method} :rspec do |c|
54
+ c.syntax = [:should, :expect]
55
+ end
56
+ end
57
+ END
58
+ end
59
+
60
+ it 'returns [:should, :expect]' do
61
+ should == [:should, :expect]
62
+ end
63
+ end
64
+
65
+ context "when RSpec::Core::Configuration##{config_block_method} block does not exist" do
66
+ let(:source) do
67
+ <<-END
68
+ RSpec.configure do |config|
69
+ end
70
+ END
71
+ end
72
+
73
+ it 'returns empty array' do
74
+ should == []
75
+ end
76
+ end
77
+
78
+ context "when #{framework_config_class}#syntax= does not exist" do
79
+ let(:source) do
80
+ <<-END
81
+ RSpec.configure do |config|
82
+ config.#{config_block_method} :rspec do |c|
83
+ end
84
+ end
85
+ END
86
+ end
87
+
88
+ it 'returns empty array' do
89
+ should == []
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ [
96
+ [:modify_expectation_syntaxes!, :expect_with, 'RSpec::Matchers::Configuration'],
97
+ [:modify_mock_syntaxes!, :mock_with, 'RSpec::Mocks::Configuration']
98
+ ].each do |subject_method, config_block_method, framework_config_class|
99
+ describe "##{subject_method}" do
100
+ before do
101
+ rspec_configure.send(subject_method, syntaxes)
102
+ end
103
+
104
+ let(:source) do
105
+ <<-END
106
+ RSpec.configure do |config|
107
+ config.#{config_block_method} :rspec do |c|
108
+ c.syntax = :should
109
+ end
110
+ end
111
+ END
112
+ end
113
+
114
+ context 'when :expect is passed' do
115
+ let(:syntaxes) { :expect }
116
+
117
+ let(:expected_source) do
118
+ <<-END
119
+ RSpec.configure do |config|
120
+ config.#{config_block_method} :rspec do |c|
121
+ c.syntax = :expect
122
+ end
123
+ end
124
+ END
125
+ end
126
+
127
+ it 'rewrites syntax specification to `c.syntax = :expect`' do
128
+ rewritten_source.should == expected_source
129
+ end
130
+ end
131
+
132
+ context 'when [:should, :expect] is passed' do
133
+ let(:syntaxes) { [:should, :expect] }
134
+
135
+ let(:expected_source) do
136
+ <<-END
137
+ RSpec.configure do |config|
138
+ config.#{config_block_method} :rspec do |c|
139
+ c.syntax = [:should, :expect]
140
+ end
141
+ end
142
+ END
143
+ end
144
+
145
+ it 'rewrites syntax specification to `c.syntax = [:should, :expect]`' do
146
+ rewritten_source.should == expected_source
147
+ end
148
+ end
149
+
150
+ context 'when RSpec::Core::Configuration#expect_with block does not exist' do
151
+ pending
152
+ end
153
+
154
+ context "when #{framework_config_class}#syntax= does not exist" do
155
+ pending
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transpec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Nakayama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-28 00:00:00.000000000 Z
11
+ date: 2013-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -183,6 +183,7 @@ files:
183
183
  - lib/transpec/syntax/expectizable.rb
184
184
  - lib/transpec/syntax/matcher.rb
185
185
  - lib/transpec/syntax/method_stub.rb
186
+ - lib/transpec/syntax/rspec_configure.rb
186
187
  - lib/transpec/syntax/send_node_syntax.rb
187
188
  - lib/transpec/syntax/should.rb
188
189
  - lib/transpec/syntax/should_receive.rb
@@ -203,6 +204,7 @@ files:
203
204
  - spec/transpec/syntax/double_spec.rb
204
205
  - spec/transpec/syntax/matcher_spec.rb
205
206
  - spec/transpec/syntax/method_stub_spec.rb
207
+ - spec/transpec/syntax/rspec_configure_spec.rb
206
208
  - spec/transpec/syntax/should_receive_spec.rb
207
209
  - spec/transpec/syntax/should_spec.rb
208
210
  - spec/transpec/util_spec.rb
@@ -247,6 +249,7 @@ test_files:
247
249
  - spec/transpec/syntax/double_spec.rb
248
250
  - spec/transpec/syntax/matcher_spec.rb
249
251
  - spec/transpec/syntax/method_stub_spec.rb
252
+ - spec/transpec/syntax/rspec_configure_spec.rb
250
253
  - spec/transpec/syntax/should_receive_spec.rb
251
254
  - spec/transpec/syntax/should_spec.rb
252
255
  - spec/transpec/util_spec.rb