rspec 0.5.10 → 0.5.11

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,5 +1,12 @@
1
1
  = RSpec Changelog
2
2
 
3
+ == Version 0.5.11
4
+ This release makes test2spec usable with Rails (with some manual steps).
5
+ See http://rspec.rubyforge.org/tools/rails.html for more details
6
+
7
+ * test2spec now correctly translates bodies of helper methods (non- test_*, setup and teardown ones).
8
+ * Added more documentation about how to get test2spec to work with Rails.
9
+
3
10
  == Version 0.5.10
4
11
  This version features a second rewrite of test2spec - hopefully better than the previous one.
5
12
 
@@ -84,3 +84,49 @@ Model:
84
84
 
85
85
  Controller:
86
86
  <ruby file="../vendor/rspec_on_rails/spec/controllers/person_controller_spec.rb"/>
87
+
88
+ h3. Translating existing Test::Unit tests
89
+
90
+ The test2spec tool that ships with RSpec translates existing tests into RSpec specs.
91
+ Translating tests to specs in a Rails environment requires some manual steps...
92
+
93
+ h4. Install the rspec_generator
94
+ How to do this is described above
95
+
96
+ h4. Modify your test/test_helper.rb
97
+ In order to be able to translate any Rails tests, you must modify your test/test_helper.rb file:
98
+ <pre>
99
+ # This line must be commented out in order for test2spec to work.
100
+ # require 'test_help'
101
+ require 'test2spec_help'
102
+ </pre>
103
+
104
+ The reason for this is that the <tt>test_help</tt> mixin confuses test2spec to the point
105
+ where it's unable to perform the translation. The <tt>test2spec_help</tt> addresses this
106
+ shortcoming.
107
+
108
+ h4. Perform the translations
109
+ Now you can translate your unit tests (model tests) with:
110
+ <pre>
111
+ test2spec --template spec/test2spec.erb --specdir spec/models test/unit
112
+ </pre>
113
+ and your functional tests (controller tests) with:
114
+ <pre>
115
+ test2spec --template spec/test2spec.erb --specdir spec/controllers test/functional
116
+ </pre>
117
+
118
+ h4. Edit your translated specs
119
+ test2spec currently doesn't translate class-level statements such as <tt>fixtures</tt>, so you have to do this yourself.
120
+ Copy all the <tt>fixtures</tt> statements in your tests to the corresponding contexts. Example:
121
+
122
+ <ruby>
123
+ context "The Foo Model" do
124
+ fixtures :foo
125
+ end
126
+ </ruby>
127
+
128
+ h4. Make sure fixtures are found.
129
+ By default, RSpec on Rails expects to find fixtures under <tt>spec/fixtures</tt>. You should either move your
130
+ existing <tt>test/fixtures/*.yml</tt> files to <tt>spec/fixtures</tt> or edit your <tt>spec/spec_helper.rb</tt>
131
+ to point to the old <tt>test/fixtures</tt> location. Beware that every time you do a <tt>script/generate rspec_model</tt>,
132
+ new fixstures will always be written to <tt>spec/fixtures</tt>.
@@ -59,12 +59,23 @@ h2. Sample translation
59
59
  h2. What's translated?
60
60
 
61
61
  test2spec only translates classes that extend from Test::Unit - and their methods. It will not bring over any
62
- code that isn't inside a Test::Unit class. This means that require statements at the top of your file, any other
63
- classes in the same file as the test class etc *will not be translated*.
62
+ code that isn't inside a Test::Unit class. This means that require statements at the top of your file, any
63
+ non-Test::Unit classes (even if they are in in the same file as the test class) *will not be translated*.
64
+
65
+ We recommend that you manually write a spec_helper.rb file with require statements and put it at the top
66
+ of your spec directory. Also, write a test2spec.erb file that will put a <tt>require</tt> of that file at
67
+ the top of each translated spec. Use the <tt>--template</tt> option to specify this erb file when you run test2spec.
68
+
69
+ Sometimes, test classes have helper methods that don't start with test_ (and that are not setup or teardown).
70
+ These methods will be moved inside the setup block, and will also have any core Test::Unit assert_* methods
71
+ translated. (Non-Test::Unit core assert_ methods will not be translated.)
64
72
 
65
73
  So, there is a fair chance that you will have to do some manual editing after test2spec has run. We're only
66
74
  aiming to get most of the translation job done for you - not all of it.
67
75
 
76
+ If you're translating Rails tests, please refer to the RSpec on Rails page for more detail of what needs to
77
+ be done.
78
+
68
79
  h2. Troubleshooting
69
80
 
70
81
  In some cases, test2spec will not be able to translate a test class. In that case, please submit a bug report
@@ -1,5 +1,6 @@
1
1
  # Taken from http://dark.fhtr.org/ruby2ruby.rb
2
2
 
3
+ require 'pp'
3
4
  require 'rubygems'
4
5
  begin
5
6
  require 'parse_tree'
@@ -484,7 +485,8 @@ class RubyToRuby < SexpProcessor
484
485
  end
485
486
 
486
487
  def process_bmethod(exp)
487
- raise "FIXME"
488
+ exp.clear
489
+ ""
488
490
  end
489
491
 
490
492
  end
@@ -30,11 +30,7 @@ module Spec
30
30
  def initialize
31
31
  super
32
32
  self.expected = Array
33
- end
34
-
35
- def process(exp)
36
- #puts "PROCESS:#{exp[0]}"
37
- super
33
+ @regular_method = false
38
34
  end
39
35
 
40
36
  def process_class(exp)
@@ -61,10 +57,15 @@ super
61
57
  unless setup.empty?
62
58
  setup_block = process(setup.shift)
63
59
  unless methods.empty?
60
+ translated_methods = []
61
+ # At this stage we don't want to translate :lvar to :dvar
62
+ @regular_method = true
63
+ translated_methods << process(methods.shift) until methods.empty?
64
+ @regular_method = false
64
65
  if setup_block.length == 3
65
- setup_block += methods
66
+ setup_block += translated_methods
66
67
  else
67
- setup_block[3] += methods
68
+ setup_block[3] += translated_methods
68
69
  end
69
70
  end
70
71
  context_body << setup_block
@@ -109,20 +110,26 @@ super
109
110
  result[-1] += @dasgn_decl unless @dasgn_decl.empty?
110
111
  result[-1] += block_body unless block_body == [[:nil]]
111
112
  result
113
+ else
114
+ # Return the same method, but process the innards
115
+ result = [exp.shift, exp.shift] + process(exp)
116
+ result
112
117
  end
113
118
  end
114
119
 
115
120
  def process_lasgn(exp)
116
121
  result = exp.dup
117
- result[0] = :dasgn_curr
118
- decl = result[0..1].dup
119
- if @dasgn_decl.empty?
120
- @dasgn_decl += [decl]
121
- else
122
- @dasgn_decl_tail << decl
123
- end
124
- @dasgn_decl_tail = decl
125
122
  exp.clear
123
+ unless @regular_method
124
+ result[0] = :dasgn_curr
125
+ decl = result[0..1].dup
126
+ if @dasgn_decl.empty?
127
+ @dasgn_decl += [decl]
128
+ else
129
+ @dasgn_decl_tail << decl
130
+ end
131
+ @dasgn_decl_tail = decl
132
+ end
126
133
  result
127
134
  end
128
135
 
@@ -180,8 +187,10 @@ super
180
187
 
181
188
  def process_lvar(exp)
182
189
  result = exp.dup
183
- result[0] = :dvar
184
190
  exp.clear
191
+ unless @regular_method
192
+ result[0] = :dvar
193
+ end
185
194
  result
186
195
  end
187
196
  end
@@ -30,8 +30,10 @@ module Spec
30
30
  else
31
31
  log "Successfully translated #{klass}"
32
32
  end
33
- rescue SexpProcessorError => e
33
+ rescue => e
34
34
  log "Failed to translate #{klass}"
35
+ log "Message: #{e.message}"
36
+ log e.backtrace.join("\n")
35
37
  end
36
38
  end
37
39
  end
@@ -3,7 +3,7 @@ module Spec
3
3
  unless defined? MAJOR
4
4
  MAJOR = 0
5
5
  MINOR = 5
6
- TINY = 10
6
+ TINY = 11
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
9
  TAG = "REL_" + [MAJOR, MINOR, TINY].join('_')
@@ -168,6 +168,32 @@ module Spec
168
168
  end
169
169
  end
170
170
  end
171
+
172
+ class NinthTest < Test::Unit::TestCase
173
+ def test_2_should_be_pair
174
+ foo = 1
175
+ assert_pair(2)
176
+ end
177
+
178
+ def assert_pair(n)
179
+ assert_equal 0, n%2
180
+ end
181
+ end
182
+ class NinthContext
183
+ def wrapper
184
+ context "Ninth" do
185
+ setup do
186
+ def assert_pair(n)
187
+ (n%2).should_equal 0
188
+ end
189
+ end
190
+ specify "2 should be pair" do
191
+ foo = 1
192
+ assert_pair(2)
193
+ end
194
+ end
195
+ end
196
+ end
171
197
 
172
198
  class SexpTransformerTest < Test::Unit::TestCase
173
199
  def test_first
@@ -202,13 +228,59 @@ module Spec
202
228
  should_translate_class_to_context('Eighth')
203
229
  end
204
230
 
205
- def should_translate_class_to_context(name)
206
- t = test_class_exp(eval("#{name}Test"))
207
- c = context_exp(eval("#{name}Context"))
231
+ def test_ninth
232
+ should_translate_class_to_context('Ninth')
233
+ end
234
+
235
+ class Something
236
+ def method_with_asserts
237
+ assert_equal 2, 3
238
+ end
239
+ end
240
+ class SomethingTranslated
241
+ def method_with_asserts
242
+ 3.should_equal 2
243
+ end
244
+ end
245
+ def test_translates_regular_method_bodies
246
+ something = ParseTree.new.parse_tree_for_method(Something, :method_with_asserts)
247
+ expected_something_translated = ParseTree.new.parse_tree_for_method(SomethingTranslated, :method_with_asserts)
248
+ actual_something_translated = @t.process(something)
249
+ verify_sexp_equal expected_something_translated, actual_something_translated
250
+ end
251
+
252
+ def should_translate_class_to_context(name, debug=false)
253
+ test_class_name = "#{name}Test"
254
+ context_class_name = "#{name}Context"
255
+ t = test_class_exp(eval(test_class_name))
256
+ if(debug)
257
+ puts "ORIGINAL"
258
+ pp t
259
+ end
260
+ c = wrapper_exp(eval(context_class_name))
208
261
 
209
262
  trans = @t.process(t)
263
+
210
264
  verify_sexp_equal c, trans
211
-
265
+
266
+ =begin
267
+ if c != trans
268
+ # Try to print out the Ruby2Ruby of the trans
269
+ begin
270
+ trans2ruby = @r2r.process(trans.dup[0])
271
+ # Parse the translation again
272
+ retranslated_class_name = "#{context_class_name}Retranslated"
273
+ eval "class #{retranslated_class_name}\ndef wrapper\n#{trans2ruby}\nend\nend"
274
+ retranslated_class = eval(retranslated_class_name)
275
+ retranslated_tree = wrapper_exp(retranslated_class)
276
+ retranslated_tree
277
+ verify_sexp_equal c, retranslated_tree
278
+ rescue SexpProcessorError => e
279
+ # That didn't work, just print the tree
280
+ verify_sexp_equal c, trans
281
+ end
282
+ end
283
+ =end
212
284
  # Verify that we can evaluate it after translated by R2R
213
285
  eval(@r2r.process(trans[0]))
214
286
  end
@@ -217,9 +289,7 @@ module Spec
217
289
  ParseTree.new.parse_tree(klass)[0]
218
290
  end
219
291
 
220
- def context_exp(klass)
221
- #pp ParseTree.new.parse_tree_for_method(klass, :wrapper)
222
- #exit
292
+ def wrapper_exp(klass)
223
293
  ParseTree.new.parse_tree_for_method(klass, :wrapper)[2][1][2..-1]
224
294
  end
225
295
 
@@ -9,10 +9,16 @@ class TestUnitApiTest < Test::Unit::TestCase
9
9
  def teardown
10
10
  end
11
11
 
12
+ def assert_pair(n)
13
+ assert_equal 0, n%2
14
+ end
15
+
12
16
  def test_can_be_translated_to_rspec
13
17
  a_float = 123.45
14
18
  a_nil = nil
15
19
 
20
+ assert_pair(2)
21
+
16
22
  assert true
17
23
  assert_not_nil @an_int
18
24
  assert_block { true }
metadata CHANGED
@@ -3,9 +3,9 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: rspec
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.10
7
- date: 2006-06-14 00:00:00 -05:00
8
- summary: RSpec-0.5.10 - BDD for Ruby http://rspec.rubyforge.org/
6
+ version: 0.5.11
7
+ date: 2006-06-15 00:00:00 -05:00
8
+ summary: RSpec-0.5.11 - BDD for Ruby http://rspec.rubyforge.org/
9
9
  require_paths:
10
10
  - lib
11
11
  email: rspec-devel@rubyforge.org