transpec 0.2.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +1 -1
  4. data/CHANGELOG.md +10 -0
  5. data/README.md +111 -56
  6. data/README.md.erb +117 -62
  7. data/lib/transpec/ast/node.rb +41 -0
  8. data/lib/transpec/base_rewriter.rb +55 -0
  9. data/lib/transpec/cli.rb +43 -153
  10. data/lib/transpec/configuration.rb +13 -9
  11. data/lib/transpec/{rewriter.rb → converter.rb} +44 -71
  12. data/lib/transpec/dynamic_analyzer/rewriter.rb +94 -0
  13. data/lib/transpec/dynamic_analyzer/runtime_data.rb +27 -0
  14. data/lib/transpec/dynamic_analyzer.rb +166 -0
  15. data/lib/transpec/file_finder.rb +53 -0
  16. data/lib/transpec/option_parser.rb +166 -0
  17. data/lib/transpec/{context.rb → static_context_inspector.rb} +2 -2
  18. data/lib/transpec/syntax/be_close.rb +7 -9
  19. data/lib/transpec/syntax/double.rb +6 -10
  20. data/lib/transpec/syntax/expect.rb +35 -0
  21. data/lib/transpec/syntax/have.rb +195 -0
  22. data/lib/transpec/syntax/method_stub.rb +22 -27
  23. data/lib/transpec/syntax/mixin/allow_no_message.rb +73 -0
  24. data/lib/transpec/syntax/mixin/any_instance.rb +22 -0
  25. data/lib/transpec/syntax/mixin/expectizable.rb +26 -0
  26. data/lib/transpec/syntax/mixin/have_matcher.rb +23 -0
  27. data/lib/transpec/syntax/mixin/monkey_patch.rb +37 -0
  28. data/lib/transpec/syntax/mixin/send.rb +109 -0
  29. data/lib/transpec/syntax/{matcher.rb → operator_matcher.rb} +27 -14
  30. data/lib/transpec/syntax/raise_error.rb +6 -10
  31. data/lib/transpec/syntax/rspec_configure.rb +29 -28
  32. data/lib/transpec/syntax/should.rb +45 -15
  33. data/lib/transpec/syntax/should_receive.rb +44 -16
  34. data/lib/transpec/syntax.rb +29 -21
  35. data/lib/transpec/util.rb +12 -2
  36. data/lib/transpec/version.rb +3 -3
  37. data/spec/spec_helper.rb +8 -6
  38. data/spec/support/cache_helper.rb +50 -0
  39. data/spec/support/shared_context.rb +49 -1
  40. data/spec/transpec/ast/node_spec.rb +65 -0
  41. data/spec/transpec/cli_spec.rb +33 -242
  42. data/spec/transpec/commit_message_spec.rb +2 -2
  43. data/spec/transpec/configuration_spec.rb +12 -8
  44. data/spec/transpec/{rewriter_spec.rb → converter_spec.rb} +198 -148
  45. data/spec/transpec/dynamic_analyzer/rewriter_spec.rb +183 -0
  46. data/spec/transpec/dynamic_analyzer_spec.rb +164 -0
  47. data/spec/transpec/file_finder_spec.rb +118 -0
  48. data/spec/transpec/option_parser_spec.rb +185 -0
  49. data/spec/transpec/{context_spec.rb → static_context_inspector_spec.rb} +27 -12
  50. data/spec/transpec/syntax/be_close_spec.rb +8 -4
  51. data/spec/transpec/syntax/double_spec.rb +105 -12
  52. data/spec/transpec/syntax/expect_spec.rb +83 -0
  53. data/spec/transpec/syntax/have_spec.rb +599 -0
  54. data/spec/transpec/syntax/method_stub_spec.rb +276 -115
  55. data/spec/transpec/syntax/{matcher_spec.rb → operator_matcher_spec.rb} +277 -98
  56. data/spec/transpec/syntax/raise_error_spec.rb +92 -46
  57. data/spec/transpec/syntax/should_receive_spec.rb +298 -92
  58. data/spec/transpec/syntax/should_spec.rb +230 -44
  59. data/spec/transpec/util_spec.rb +2 -9
  60. data/tasks/lib/transpec_demo.rb +1 -1
  61. data/tasks/lib/transpec_test.rb +5 -7
  62. data/tasks/test.rake +5 -1
  63. data/transpec.gemspec +1 -1
  64. metadata +46 -22
  65. data/lib/transpec/syntax/able_to_allow_no_message.rb +0 -73
  66. data/lib/transpec/syntax/able_to_target_any_instance.rb +0 -24
  67. data/lib/transpec/syntax/expectizable.rb +0 -27
  68. data/lib/transpec/syntax/send_node_syntax.rb +0 -57
@@ -1,10 +1,11 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require 'transpec/context'
4
+ require 'transpec/static_context_inspector'
5
5
 
6
6
  module Transpec
7
- describe Context, :skip_on_jruby do
7
+ describe StaticContextInspector, :skip_on_jruby do
8
+ include CacheHelper
8
9
  include ::AST::Sexp
9
10
  include_context 'parsed objects'
10
11
  include_context 'isolated environment'
@@ -89,23 +90,28 @@ module Transpec
89
90
 
90
91
  next unless expected_scopes
91
92
 
92
- context_object = Context.new(ancestor_nodes)
93
- context_object.scopes.should == expected_scopes
93
+ context_inspector = StaticContextInspector.new(ancestor_nodes)
94
+ context_inspector.scopes.should == expected_scopes
94
95
  end
95
96
  end
96
97
  end
97
98
 
98
99
  shared_examples 'context inspection methods' do
99
- let(:context_object) do
100
+ let(:context_inspector) do
100
101
  AST::Scanner.scan(ast) do |node, ancestor_nodes|
101
102
  next unless node == s(:send, nil, :target)
102
- return Context.new(ancestor_nodes)
103
+ return StaticContextInspector.new(ancestor_nodes)
103
104
  end
104
105
 
105
106
  fail 'Target node not found!'
106
107
  end
107
108
 
108
- def eval_with_rspec_in_context(eval_source)
109
+ def eval_with_rspec_in_context(eval_source, spec_source)
110
+ # Clear SPEC_OPTS environment variable so that this spec does not fail
111
+ # with dynamic analysis in self-testing.
112
+ original_spec_opts = ENV['SPEC_OPTS']
113
+ ENV['SPEC_OPTS'] = nil
114
+
109
115
  result_path = 'result'
110
116
 
111
117
  helper_source = <<-END
@@ -117,28 +123,37 @@ module Transpec
117
123
  END
118
124
 
119
125
  source_path = 'spec.rb'
120
- File.write(source_path, helper_source + source)
126
+
127
+ File.write(source_path, helper_source + spec_source)
121
128
 
122
129
  `rspec #{source_path}`
123
130
 
124
131
  Marshal.load(File.read(result_path))
132
+ ensure
133
+ ENV['SPEC_OPTS'] = original_spec_opts
125
134
  end
126
135
 
127
136
  describe '#non_monkey_patch_expectation_available?' do
128
- subject { context_object.non_monkey_patch_expectation_available? }
137
+ subject { context_inspector.non_monkey_patch_expectation_available? }
129
138
 
130
139
  let(:expected) do
131
- eval_with_rspec_in_context('respond_to?(:expect)')
140
+ eval_source = 'respond_to?(:expect)'
141
+ with_cache(eval_source + source) do
142
+ eval_with_rspec_in_context(eval_source, source)
143
+ end
132
144
  end
133
145
 
134
146
  it { should == expected }
135
147
  end
136
148
 
137
149
  describe '#non_monkey_patch_mock_available?' do
138
- subject { context_object.non_monkey_patch_mock_available? }
150
+ subject { context_inspector.non_monkey_patch_mock_available? }
139
151
 
140
152
  let(:expected) do
141
- eval_with_rspec_in_context('respond_to?(:allow) && respond_to?(:receive)')
153
+ eval_source = 'respond_to?(:allow) && respond_to?(:receive)'
154
+ with_cache(eval_source + source) do
155
+ eval_with_rspec_in_context(eval_source, source)
156
+ end
142
157
  end
143
158
 
144
159
  it { should == expected }
@@ -28,16 +28,20 @@ module Transpec
28
28
  context 'when it is `be_close(expected, delta)` form' do
29
29
  let(:source) do
30
30
  <<-END
31
- it 'is close to 0.333' do
32
- (1.0 / 3.0).should be_close(0.333, 0.001)
31
+ describe 'example' do
32
+ it 'is close to 0.333' do
33
+ (1.0 / 3.0).should be_close(0.333, 0.001)
34
+ end
33
35
  end
34
36
  END
35
37
  end
36
38
 
37
39
  let(:expected_source) do
38
40
  <<-END
39
- it 'is close to 0.333' do
40
- (1.0 / 3.0).should be_within(0.001).of(0.333)
41
+ describe 'example' do
42
+ it 'is close to 0.333' do
43
+ (1.0 / 3.0).should be_within(0.001).of(0.333)
44
+ end
41
45
  end
42
46
  END
43
47
  end
@@ -20,12 +20,99 @@ module Transpec
20
20
  fail 'No double node is found!'
21
21
  end
22
22
 
23
+ describe '.target_node?' do
24
+ let(:send_node) do
25
+ ast.each_descendent_node do |node|
26
+ next unless node.type == :send
27
+ method_name = node.children[1]
28
+ next unless method_name == :double
29
+ return node
30
+ end
31
+ fail 'No #double node is found!'
32
+ end
33
+
34
+ context 'when #double node is passed' do
35
+ let(:source) do
36
+ <<-END
37
+ describe 'example' do
38
+ it 'includes something' do
39
+ something = double('something')
40
+ [1, something].should include(something)
41
+ end
42
+ end
43
+ END
44
+ end
45
+
46
+ it 'returns true' do
47
+ Double.target_node?(send_node).should be_true
48
+ end
49
+ end
50
+
51
+ context 'with runtime information' do
52
+ include_context 'dynamic analysis objects'
53
+
54
+ context "when RSpec's #double node is passed" do
55
+ let(:source) do
56
+ <<-END
57
+ describe 'example' do
58
+ it 'includes something' do
59
+ something = double('something')
60
+ [1, something].should include(something)
61
+ end
62
+ end
63
+ END
64
+ end
65
+
66
+ it 'returns true' do
67
+ Double.target_node?(send_node).should be_true
68
+ end
69
+ end
70
+
71
+ context 'when another #double node is passed' do
72
+ let(:source) do
73
+ <<-END
74
+ module AnotherMockFramework
75
+ def setup_mocks_for_rspec
76
+ def double(arg)
77
+ arg.upcase
78
+ end
79
+ end
80
+
81
+ def verify_mocks_for_rspec
82
+ end
83
+
84
+ def teardown_mocks_for_rspec
85
+ end
86
+ end
87
+
88
+ RSpec.configure do |config|
89
+ config.mock_framework = AnotherMockFramework
90
+ end
91
+
92
+ describe 'example' do
93
+ it "is not RSpec's #double" do
94
+ something = double('something')
95
+ [1, something].should include('SOMETHING')
96
+ end
97
+ end
98
+ END
99
+ end
100
+
101
+ it 'returns false' do
102
+ Double.target_node?(send_node, runtime_data).should be_false
103
+ end
104
+ end
105
+ end
106
+ end
107
+
23
108
  describe '#method_name' do
24
109
  let(:source) do
25
110
  <<-END
26
- it 'includes something' do
27
- something = double('something')
28
- [1, something].should include(something)
111
+ describe 'example' do
112
+ it 'includes something' do
113
+ something = double('something')
114
+ [1, something].should include(something)
115
+ end
29
116
  end
30
117
  END
31
118
  end
@@ -44,18 +131,22 @@ module Transpec
44
131
  context "when it is ##{method}" do
45
132
  let(:source) do
46
133
  <<-END
47
- it 'includes something' do
48
- something = #{method}('something')
49
- [1, something].should include(something)
134
+ describe 'example' do
135
+ it 'includes something' do
136
+ something = #{method}('something')
137
+ [1, something].should include(something)
138
+ end
50
139
  end
51
140
  END
52
141
  end
53
142
 
54
143
  let(:expected_source) do
55
144
  <<-END
56
- it 'includes something' do
57
- something = double('something')
58
- [1, something].should include(something)
145
+ describe 'example' do
146
+ it 'includes something' do
147
+ something = double('something')
148
+ [1, something].should include(something)
149
+ end
59
150
  end
60
151
  END
61
152
  end
@@ -75,9 +166,11 @@ module Transpec
75
166
  context 'when it is #double' do
76
167
  let(:source) do
77
168
  <<-END
78
- it 'includes something' do
79
- something = double('something')
80
- [1, something].should include(something)
169
+ describe 'example' do
170
+ it 'includes something' do
171
+ something = double('something')
172
+ [1, something].should include(something)
173
+ end
81
174
  end
82
175
  END
83
176
  end
@@ -0,0 +1,83 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'transpec/syntax/expect'
5
+
6
+ module Transpec
7
+ class Syntax
8
+ describe Expect do
9
+ include_context 'parsed objects'
10
+ include_context 'expect object'
11
+
12
+ describe '#subject_node' do
13
+ let(:source) do
14
+ <<-END
15
+ describe 'example' do
16
+ it 'is empty' do
17
+ expect(subject).to be_empty
18
+ end
19
+ end
20
+ END
21
+ end
22
+
23
+ it 'returns subject node' do
24
+ method_name = expect_object.subject_node.children[1]
25
+ method_name.should == :subject
26
+ end
27
+ end
28
+
29
+ describe '#matcher_node' do
30
+ let(:source) do
31
+ <<-END
32
+ describe 'example' do
33
+ it 'is empty' do
34
+ expect(subject).to be_empty
35
+ end
36
+ end
37
+ END
38
+ end
39
+
40
+ it 'returns matcher node' do
41
+ method_name = expect_object.matcher_node.children[1]
42
+ method_name.should == :be_empty
43
+ end
44
+ end
45
+
46
+ describe '#have_matcher' do
47
+ subject { expect_object.have_matcher }
48
+
49
+ context 'when it is taking #have matcher' do
50
+ let(:source) do
51
+ <<-END
52
+ describe 'example' do
53
+ it 'has 2 items' do
54
+ expect(subject).to have(2).items
55
+ end
56
+ end
57
+ END
58
+ end
59
+
60
+ it 'returns an instance of Have' do
61
+ should be_an(Have)
62
+ end
63
+ end
64
+
65
+ context 'when it is taking any other matcher' do
66
+ let(:source) do
67
+ <<-END
68
+ describe 'example' do
69
+ it 'is empty' do
70
+ expect(subject).to be_empty
71
+ end
72
+ end
73
+ END
74
+ end
75
+
76
+ it 'returns nil' do
77
+ should be_nil
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end