rspec 0.5.9 → 0.5.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'parse_tree'
3
+ require 'spec/test_to_spec/sexp_transformer'
4
+ require 'spec/test_to_spec/ruby2ruby'
5
+
6
+ module Test
7
+ module Unit
8
+ class TestCase
9
+ # Returns a String representing the RSpec translation of this class
10
+ def self.to_rspec
11
+ tree = ParseTree.new.parse_tree(self).first
12
+ rspec_tree = Spec::TestToSpec::SexpTransformer.new.process(tree)
13
+ modules = self.name.split("::")[0..-2]
14
+ result = ""
15
+ result += modules.collect{|m| "module #{m}\n"}.join("")
16
+ result += RubyToRuby.new.process(rspec_tree[0])
17
+ result += modules.collect{|m| "\nend"}.join("")
18
+ result
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,9 +1,9 @@
1
- require 'spec/tool/test_unit_translator'
1
+ require 'spec/test_to_spec/test_case_ext'
2
2
  require 'fileutils'
3
3
  require 'erb'
4
4
 
5
5
  module Spec
6
- module Tool
6
+ module TestToSpec
7
7
  # A Test::Unit runner that doesn't run tests, but translates them instead!
8
8
  class TranslationTestRunner
9
9
  include FileUtils
@@ -14,11 +14,10 @@ module Spec
14
14
 
15
15
  def initialize(suite)
16
16
  log "Writing translated specs to #{$test2spec_options[:specdir]}"
17
- translator = TestUnitTranslator.new
18
17
  ObjectSpace.each_object(Class) do |klass|
19
18
  if klass < ::Test::Unit::TestCase
20
19
  begin
21
- translation = translator.translate(klass)
20
+ translation = klass.to_rspec
22
21
 
23
22
  unless $test2spec_options[:dry_run]
24
23
  relative_path = underscore(klass.name)
@@ -3,7 +3,7 @@ module Spec
3
3
  unless defined? MAJOR
4
4
  MAJOR = 0
5
5
  MINOR = 5
6
- TINY = 9
6
+ TINY = 10
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
9
  TAG = "REL_" + [MAJOR, MINOR, TINY].join('_')
@@ -29,8 +29,8 @@ module Spec
29
29
  end
30
30
 
31
31
  def test_should_produce_standard_summary
32
- @formatter.dump_summary(4,3,2,1)
33
- assert_equal("\nFinished in 4 seconds\n\n3 contexts, 2 specifications, 1 failure\n", @io.string)
32
+ @formatter.dump_summary(3,2,1)
33
+ assert_equal("\nFinished in 3 seconds\n\n2 specifications, 1 failure\n", @io.string)
34
34
  end
35
35
 
36
36
  def test_should_produce_line_break_on_start_dump
@@ -46,7 +46,7 @@ module Spec
46
46
  end
47
47
 
48
48
  def test_should_not_produce_summary_on_dry_run
49
- @formatter.dump_summary(4,3,2,1)
49
+ @formatter.dump_summary(3,2,1)
50
50
  assert_equal("", @io.string)
51
51
  end
52
52
  end
@@ -24,7 +24,7 @@ module Spec
24
24
  end
25
25
 
26
26
  def test_should_produce_no_summary
27
- @formatter.dump_summary(nil,nil,nil,nil)
27
+ @formatter.dump_summary(nil,nil,nil)
28
28
  assert(@io.string.empty?)
29
29
  end
30
30
 
@@ -41,7 +41,7 @@ module Spec
41
41
  end
42
42
 
43
43
  def test_should_not_produce_summary_on_dry_run
44
- @formatter.dump_summary(4,3,2,1)
44
+ @formatter.dump_summary(3,2,1)
45
45
  assert_equal("", @io.string)
46
46
  end
47
47
  end
@@ -14,7 +14,7 @@ module Spec
14
14
  def test_should_push_time_to_reporter
15
15
  @formatter.should_receive(:start).with(5)
16
16
  @formatter.should_receive(:start_dump)
17
- @formatter.should_receive(:dump_summary) do |time, a, b, c|
17
+ @formatter.should_receive(:dump_summary) do |time, a, b|
18
18
  assert_match(/[0-9].[0-9|e|-]+/, time.to_s)
19
19
  end
20
20
  @reporter.start(5)
@@ -24,7 +24,7 @@ module Spec
24
24
 
25
25
  def test_should_push_stats_to_reporter_even_with_no_data
26
26
  @formatter.should_receive(:start_dump)
27
- @formatter.should_receive(:dump_summary).with(:anything, 0, 0, 0)
27
+ @formatter.should_receive(:dump_summary).with(:anything, 0, 0)
28
28
  @reporter.dump
29
29
  end
30
30
 
@@ -42,7 +42,7 @@ module Spec
42
42
  @formatter.should_receive(:spec_started)
43
43
  @formatter.should_receive(:spec_passed)
44
44
  @formatter.should_receive(:start_dump)
45
- @formatter.should_receive(:dump_summary).with(:anything, 0, 1, 0)
45
+ @formatter.should_receive(:dump_summary).with(:anything, 1, 0)
46
46
  @reporter.spec_started "spec"
47
47
  @reporter.spec_finished "spec"
48
48
  @reporter.dump
@@ -54,7 +54,7 @@ module Spec
54
54
  @formatter.should_receive(:spec_failed).with("spec", 1, failure)
55
55
  @formatter.should_receive(:start_dump)
56
56
  @formatter.should_receive(:dump_failure).with(1, :anything)
57
- @formatter.should_receive(:dump_summary).with(:anything, 1, 1, 1)
57
+ @formatter.should_receive(:dump_summary).with(:anything, 1, 1)
58
58
  @backtrace_tweaker.should.receive(:tweak_backtrace)
59
59
  @reporter.add_context "context"
60
60
  @reporter.spec_started "spec"
@@ -63,13 +63,20 @@ module Spec
63
63
  end
64
64
 
65
65
  def test_should_handle_multiple_contexts_same_name
66
- @formatter.should_receive(:add_context).with("context", true)
67
- @formatter.should_receive(:add_context).with("context", false).exactly(2).times
66
+ @formatter.should_receive(:add_context).exactly(3).times
67
+ @formatter.should.receive(:spec_started).exactly(3).times
68
+ @formatter.should.receive(:spec_passed).exactly(3).times
68
69
  @formatter.should_receive(:start_dump)
69
- @formatter.should_receive(:dump_summary).with(:anything, 3, 0, 0)
70
+ @formatter.should_receive(:dump_summary).with(:anything, 3, 0)
70
71
  @reporter.add_context "context"
72
+ @reporter.spec_started "spec 1"
73
+ @reporter.spec_finished "spec 1"
71
74
  @reporter.add_context "context"
75
+ @reporter.spec_started "spec 2"
76
+ @reporter.spec_finished "spec 2"
72
77
  @reporter.add_context "context"
78
+ @reporter.spec_started "spec 3"
79
+ @reporter.spec_finished "spec 3"
73
80
  @reporter.dump
74
81
  end
75
82
 
@@ -82,7 +89,7 @@ module Spec
82
89
  @formatter.should_receive(:spec_failed).with("spec", 2, failure)
83
90
  @formatter.should_receive(:dump_failure).exactly(2).times
84
91
  @formatter.should_receive(:start_dump)
85
- @formatter.should_receive(:dump_summary).with(:anything, 2, 4, 2)
92
+ @formatter.should_receive(:dump_summary).with(:anything, 4, 2)
86
93
  @backtrace_tweaker.should.receive(:tweak_backtrace)
87
94
  @reporter.add_context "context"
88
95
 
@@ -24,8 +24,8 @@ module Spec
24
24
  end
25
25
 
26
26
  def test_should_produce_standard_summary
27
- @formatter.dump_summary(4,3,2,1)
28
- assert_equal("\nFinished in 4 seconds\n\n3 contexts, 2 specifications, 1 failure\n", @io.string)
27
+ @formatter.dump_summary(3,2,1)
28
+ assert_equal("\nFinished in 3 seconds\n\n2 specifications, 1 failure\n", @io.string)
29
29
  end
30
30
 
31
31
  def test_should_push_nothing_on_start
@@ -47,7 +47,7 @@ module Spec
47
47
  end
48
48
 
49
49
  def test_should_not_produce_summary_on_dry_run
50
- @formatter.dump_summary(4,3,2,1)
50
+ @formatter.dump_summary(3,2,1)
51
51
  assert_equal("", @io.string)
52
52
  end
53
53
  end
@@ -1,5 +1,5 @@
1
- require 'test/unit'
2
- require 'spec/tool/ruby2ruby'
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'spec/test_to_spec/ruby2ruby'
3
3
 
4
4
  R2r2r = RubyToRuby.translate(RubyToRuby).sub("RubyToRuby","RubyToRubyToRuby")
5
5
  eval R2r2r
@@ -0,0 +1,207 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'test/unit'
3
+ require 'rubygems'
4
+ require 'parse_tree'
5
+ require 'spec/test_to_spec/sexp_transformer'
6
+
7
+ module Spec
8
+ module TestToSpec
9
+ # This Test::Unit class verifies that the core Test::Unit assertions
10
+ # can be translated to RSpec.
11
+ # The various test_assert_* methods must follow a rigorous form -
12
+ #
13
+ # The first statement must be a Test::Unit assertion, and the second
14
+ # statement its expected RSpec translation.
15
+ #
16
+ # For each of the test_assert_* method, a test_assert_*_translation
17
+ # method is dynamically added, which will test that the translation
18
+ # of the 1st statement is effectively equal to the second one.
19
+ class SexpTransformerAssertionTest < Test::Unit::TestCase
20
+ def setup
21
+ @t = SexpTransformer.new
22
+ end
23
+
24
+ def test_assert
25
+ assert(:foo == :foo)
26
+ (:foo == :foo).should_be true
27
+ end
28
+
29
+ def test_assert_with_message
30
+ assert(:foo == :foo, "msg")
31
+ (:foo == :foo).should_be true
32
+ end
33
+
34
+ def test_assert_nil
35
+ assert_nil([].index(5))
36
+ ([].index(5)).should_be nil
37
+ end
38
+
39
+ def test_assert_nil_with_message
40
+ assert_nil([].index(5), "msg")
41
+ ([].index(5)).should_be nil
42
+ end
43
+
44
+ def test_assert_not_nil
45
+ assert_not_nil([5].index(5))
46
+ ([5].index(5)).should_not_be nil
47
+ end
48
+
49
+ def test_assert_not_nil_with_message
50
+ assert_not_nil(3, "msg")
51
+ 3.should_not_be nil
52
+ end
53
+
54
+ def test_assert_equal
55
+ assert_equal(2, 1+1)
56
+ (1+1).should_equal 2
57
+ end
58
+
59
+ def test_assert_equal_with_message
60
+ assert_equal(2, 1+1, "1+1 should equal 2")
61
+ (1+1).should_equal 2
62
+ end
63
+
64
+ def test_assert_equal_with_each
65
+ [0,1,2].each_with_index do |b, c|
66
+ assert_equal c, b
67
+ end
68
+ [0,1,2].each_with_index do |b, c|
69
+ b.should_equal c
70
+ end
71
+ end
72
+
73
+ def test_assert_not_equal
74
+ assert_not_equal(2+3, 1)
75
+ 1.should_not_equal 2+3
76
+ end
77
+
78
+ def test_assert_not_equal_with_message
79
+ assert_not_equal(2+3, 1, "msg")
80
+ 1.should_not_equal 2+3
81
+ end
82
+
83
+ def test_assert_same
84
+ assert_same(2, 1+1)
85
+ (1+1).should_be 2
86
+ end
87
+
88
+ def test_assert_same_with_msg
89
+ assert_same(2, 1+1, "msg")
90
+ (1+1).should_be 2
91
+ end
92
+
93
+ def test_assert_not_same
94
+ assert_not_same(2+3, 1)
95
+ 1.should_not_be 2+3
96
+ end
97
+
98
+ def test_assert_not_same_with_message
99
+ assert_not_same(2+3, 1, "msg")
100
+ 1.should_not_be 2+3
101
+ end
102
+
103
+ def test_assert_instance_of
104
+ assert_instance_of Fixnum, 2
105
+ 2.should_be_instance_of Fixnum
106
+ end
107
+
108
+ def test_assert_kind_of
109
+ assert_kind_of Fixnum, 2
110
+ 2.should_be_kind_of Fixnum
111
+ end
112
+
113
+ def test_assert_match
114
+ assert_match /foo/, 'foo'
115
+ 'foo'.should_match /foo/
116
+ end
117
+
118
+ def test_assert_no_match
119
+ assert_no_match /foo/, 'bar'
120
+ 'bar'.should_not_match /foo/
121
+ end
122
+
123
+ def test_assert_respond_to
124
+ assert_respond_to 2, :to_f
125
+ 2.should_respond_to :to_f
126
+ end
127
+
128
+ def test_assert_respond_to_with_message
129
+ assert_respond_to 2, :to_f, "msg"
130
+ 2.should_respond_to :to_f
131
+ end
132
+
133
+ def test_assert_in_delta
134
+ assert_in_delta 123.5, 123.45, 0.1
135
+ 123.45.should_be_close 123.5, 0.1
136
+ end
137
+
138
+ def test_assert_in_delta_with_message
139
+ assert_in_delta 123.5, 123.45, 0.1, "123.45 should be close to 123.5"
140
+ 123.45.should_be_close 123.5, 0.1
141
+ end
142
+
143
+ def test_assert_raise
144
+ assert_raise(ZeroDivisionError){ 1/0 }
145
+ lambda {1/0}.should_raise(ZeroDivisionError)
146
+ end
147
+
148
+ def test_assert_raise_with_message
149
+ assert_raise(ZeroDivisionError, "msg"){ 1/0 }
150
+ lambda {1/0}.should_raise(ZeroDivisionError)
151
+ end
152
+
153
+ def test_assert_raises
154
+ assert_raises(ZeroDivisionError){ 1/0 }
155
+ lambda {1/0}.should_raise(ZeroDivisionError)
156
+ end
157
+
158
+ def test_assert_nothing_raised
159
+ assert_nothing_raised{ 0/1 }
160
+ lambda {0/1}.should_not_raise
161
+ end
162
+
163
+ def test_assert_throws
164
+ assert_throws(:foo){ throw :foo }
165
+ lambda {throw :foo}.should_throw(:foo)
166
+ end
167
+
168
+ def test_assert_nothing_thrown
169
+ assert_nothing_thrown{ 0/1 }
170
+ lambda {0/1}.should_not_throw
171
+ end
172
+
173
+ def test_assert_block
174
+ assert_block{:foo != :bar}
175
+ lambda{:foo != :bar}.should_be true
176
+ end
177
+
178
+ # Returns the body of one of my methods as a Sexp
179
+ def self.body(sym)
180
+ t = ParseTree.new.parse_tree_for_method(self, sym)
181
+ t[2][1][2..-1]
182
+ end
183
+
184
+ # Verifies that the 1st statement in test method +m+ is properly translated
185
+ # to the 2nd statement.
186
+ def should_translate(m)
187
+ body = self.class.body(m)
188
+ assert_equal 2, body.length
189
+ test_unit_sexp = body[0]
190
+ rspec_sexp = body[1]
191
+ translation = @t.process(test_unit_sexp)
192
+ verify_sexp_equal(rspec_sexp, translation)
193
+ end
194
+
195
+ # Dynamically define extra test methods
196
+ methods = self.instance_methods(false).reject do |m|
197
+ m == "test_translations" || !(m =~ /^test_assert/)
198
+ end
199
+ methods.each do |m|
200
+ define_method "#{m}_translation" do
201
+ should_translate(m)
202
+ end
203
+ end
204
+
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,232 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'test/unit'
3
+ require 'rubygems'
4
+ require 'parse_tree'
5
+ require 'spec/test_to_spec/sexp_transformer'
6
+ require 'spec/test_to_spec/ruby2ruby'
7
+
8
+ module Spec
9
+ module TestToSpec
10
+ class FirstTest < Test::Unit::TestCase
11
+ def test_foo
12
+ end
13
+ end
14
+ class FirstContext
15
+ def wrapper
16
+ context "First" do
17
+ specify "foo" do
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ class SecondTest < Test::Unit::TestCase
24
+ def test_foo
25
+ assert_same 3, 1+2
26
+ assert_match /sla/, 'aslak'
27
+ end
28
+ end
29
+ class SecondContext
30
+ def wrapper
31
+ context "Second" do
32
+ specify "foo" do
33
+ (1+2).should_be 3
34
+ 'aslak'.should_match(/sla/)
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ class ThirdTest < Test::Unit::TestCase
41
+ def test_bar
42
+ one = 1
43
+ two = 2
44
+ end
45
+ def test_foo
46
+ end
47
+ end
48
+ class ThirdContext
49
+ def wrapper
50
+ context "Third" do
51
+ specify "bar" do
52
+ one = 1
53
+ two = 2
54
+ end
55
+ specify "foo" do
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ class FourthTest < Test::Unit::TestCase
62
+ def setup
63
+ one = 1
64
+ end
65
+ def test_foo
66
+ two = 2
67
+ end
68
+ end
69
+ class FourthContext
70
+ def wrapper
71
+ context "Fourth" do
72
+ setup do
73
+ one = 1
74
+ end
75
+ specify "foo" do
76
+ two = 2
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ class FifthTest
83
+ def setup
84
+ one = 1
85
+ end
86
+ def foo
87
+ two = 2
88
+ end
89
+ end
90
+ class FifthContext
91
+ def wrapper
92
+ context "Fifth" do
93
+ setup do
94
+ one = 1
95
+ def foo
96
+ two = 2
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ class SixthTest
104
+ def setup
105
+ end
106
+ def foo
107
+ two = 2
108
+ end
109
+ end
110
+ class SixthContext
111
+ def wrapper
112
+ context "Sixth" do
113
+ setup do
114
+ def foo
115
+ two = 2
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ class SeventhTest
123
+ def foo
124
+ two = 2
125
+ end
126
+ end
127
+ class SeventhContext
128
+ def wrapper
129
+ context "Seventh" do
130
+ setup do
131
+ def foo
132
+ two = 2
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ class EighthTest < Test::Unit::TestCase
140
+ def foo
141
+ two = 2
142
+ end
143
+
144
+ def teardown
145
+ torn = true
146
+ end
147
+
148
+ def test_foo
149
+ bar = foo
150
+ assert_equal 2, bar
151
+ end
152
+ end
153
+ class EighthContext
154
+ def wrapper
155
+ context "Eighth" do
156
+ setup do
157
+ def foo
158
+ two = 2
159
+ end
160
+ end
161
+ teardown do
162
+ torn = true
163
+ end
164
+ specify "foo" do
165
+ bar = foo
166
+ bar.should_equal 2
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ class SexpTransformerTest < Test::Unit::TestCase
173
+ def test_first
174
+ should_translate_class_to_context('First')
175
+ end
176
+
177
+ def test_second
178
+ should_translate_class_to_context('Second')
179
+ end
180
+
181
+ def test_third
182
+ should_translate_class_to_context('Third')
183
+ end
184
+
185
+ def test_fourth
186
+ should_translate_class_to_context('Fourth')
187
+ end
188
+
189
+ def test_fifth
190
+ should_translate_class_to_context('Fifth')
191
+ end
192
+
193
+ def test_sixth
194
+ should_translate_class_to_context('Sixth')
195
+ end
196
+
197
+ def test_seventh
198
+ should_translate_class_to_context('Seventh')
199
+ end
200
+
201
+ def test_eighth
202
+ should_translate_class_to_context('Eighth')
203
+ end
204
+
205
+ def should_translate_class_to_context(name)
206
+ t = test_class_exp(eval("#{name}Test"))
207
+ c = context_exp(eval("#{name}Context"))
208
+
209
+ trans = @t.process(t)
210
+ verify_sexp_equal c, trans
211
+
212
+ # Verify that we can evaluate it after translated by R2R
213
+ eval(@r2r.process(trans[0]))
214
+ end
215
+
216
+ def test_class_exp(klass)
217
+ ParseTree.new.parse_tree(klass)[0]
218
+ end
219
+
220
+ def context_exp(klass)
221
+ #pp ParseTree.new.parse_tree_for_method(klass, :wrapper)
222
+ #exit
223
+ ParseTree.new.parse_tree_for_method(klass, :wrapper)[2][1][2..-1]
224
+ end
225
+
226
+ def setup
227
+ @t = SexpTransformer.new
228
+ @r2r = RubyToRuby.new
229
+ end
230
+ end
231
+ end
232
+ end