rr 1.0.5 → 1.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +6 -14
  2. data/CHANGES.md +24 -0
  3. data/LICENSE +2 -2
  4. data/README.md +124 -741
  5. data/VERSION +1 -1
  6. data/lib/rr.rb +2 -103
  7. data/lib/rr/adapters/minitest.rb +21 -13
  8. data/lib/rr/adapters/minitest_active_support.rb +34 -0
  9. data/lib/rr/adapters/none.rb +17 -0
  10. data/lib/rr/adapters/{rspec.rb → rspec/invocation_matcher.rb} +2 -27
  11. data/lib/rr/adapters/rspec_1.rb +42 -0
  12. data/lib/rr/adapters/rspec_2.rb +24 -0
  13. data/lib/rr/adapters/test_unit_1.rb +54 -0
  14. data/lib/rr/adapters/test_unit_2.rb +13 -0
  15. data/lib/rr/adapters/test_unit_2_active_support.rb +35 -0
  16. data/lib/rr/autohook.rb +43 -0
  17. data/lib/rr/core_ext/array.rb +12 -0
  18. data/lib/rr/core_ext/enumerable.rb +16 -0
  19. data/lib/rr/core_ext/hash.rb +20 -0
  20. data/lib/rr/core_ext/range.rb +8 -0
  21. data/lib/rr/core_ext/regexp.rb +8 -0
  22. data/lib/rr/double.rb +4 -4
  23. data/lib/rr/double_definitions/double_definition.rb +9 -3
  24. data/lib/rr/errors.rb +21 -0
  25. data/lib/rr/expectations/argument_equality_expectation.rb +10 -7
  26. data/lib/rr/expectations/times_called_expectation.rb +2 -8
  27. data/lib/rr/injections/double_injection.rb +1 -1
  28. data/lib/rr/method_dispatches/base_method_dispatch.rb +1 -1
  29. data/lib/rr/recorded_calls.rb +12 -12
  30. data/lib/rr/space.rb +5 -3
  31. data/lib/rr/times_called_matchers/never_matcher.rb +2 -2
  32. data/lib/rr/wildcard_matchers/anything.rb +2 -2
  33. data/lib/rr/wildcard_matchers/boolean.rb +3 -7
  34. data/lib/rr/wildcard_matchers/duck_type.rb +11 -15
  35. data/lib/rr/wildcard_matchers/hash_including.rb +14 -13
  36. data/lib/rr/wildcard_matchers/is_a.rb +6 -7
  37. data/lib/rr/wildcard_matchers/satisfy.rb +8 -8
  38. data/lib/rr/without_autohook.rb +112 -0
  39. data/rr.gemspec +28 -0
  40. data/spec/global_helper.rb +12 -0
  41. data/spec/suite.rb +93 -0
  42. data/spec/suites/common/adapter_tests.rb +37 -0
  43. data/spec/suites/common/rails_integration_test.rb +175 -0
  44. data/spec/suites/common/test_unit_tests.rb +25 -0
  45. data/spec/suites/minitest/integration/minitest_test.rb +13 -0
  46. data/spec/suites/minitest/test_helper.rb +3 -0
  47. data/spec/suites/rspec_1/integration/rspec_1_spec.rb +20 -0
  48. data/spec/suites/rspec_1/integration/test_unit_1_rails_spec.rb +19 -0
  49. data/spec/suites/rspec_1/integration/test_unit_2_rails_spec.rb +18 -0
  50. data/spec/suites/rspec_1/spec_helper.rb +24 -0
  51. data/spec/suites/rspec_2/functional/any_instance_of_spec.rb +47 -0
  52. data/spec/suites/rspec_2/functional/dont_allow_spec.rb +12 -0
  53. data/spec/suites/rspec_2/functional/dsl_spec.rb +13 -0
  54. data/spec/suites/rspec_2/functional/instance_of_spec.rb +14 -0
  55. data/spec/suites/rspec_2/functional/mock_spec.rb +241 -0
  56. data/spec/suites/rspec_2/functional/proxy_spec.rb +136 -0
  57. data/spec/suites/rspec_2/functional/spy_spec.rb +41 -0
  58. data/spec/suites/rspec_2/functional/strong_spec.rb +79 -0
  59. data/spec/suites/rspec_2/functional/stub_spec.rb +190 -0
  60. data/spec/suites/rspec_2/functional/wildcard_matchers_spec.rb +128 -0
  61. data/spec/suites/rspec_2/integration/minitest_rails_spec.rb +15 -0
  62. data/spec/suites/rspec_2/integration/rspec_2_spec.rb +20 -0
  63. data/spec/suites/rspec_2/integration/test_unit_rails_spec.rb +14 -0
  64. data/spec/suites/rspec_2/spec_helper.rb +27 -0
  65. data/spec/suites/rspec_2/support/matchers/wildcard_matcher_matchers.rb +32 -0
  66. data/spec/suites/rspec_2/support/shared_examples/space.rb +13 -0
  67. data/spec/suites/rspec_2/support/shared_examples/times_called_expectation.rb +9 -0
  68. data/spec/suites/rspec_2/unit/adapters/rr_methods/double_creators_spec.rb +135 -0
  69. data/spec/suites/rspec_2/unit/adapters/rr_methods/space_spec.rb +101 -0
  70. data/spec/suites/rspec_2/unit/adapters/rr_methods/wildcard_matchers_spec.rb +69 -0
  71. data/spec/suites/rspec_2/unit/adapters/rspec/invocation_matcher_spec.rb +297 -0
  72. data/spec/suites/rspec_2/unit/adapters/rspec_spec.rb +85 -0
  73. data/spec/suites/rspec_2/unit/core_ext/array_spec.rb +39 -0
  74. data/spec/suites/rspec_2/unit/core_ext/enumerable_spec.rb +81 -0
  75. data/spec/suites/rspec_2/unit/core_ext/hash_spec.rb +55 -0
  76. data/spec/suites/rspec_2/unit/core_ext/range_spec.rb +41 -0
  77. data/spec/suites/rspec_2/unit/core_ext/regexp_spec.rb +41 -0
  78. data/spec/suites/rspec_2/unit/double_definitions/child_double_definition_create_spec.rb +114 -0
  79. data/spec/suites/rspec_2/unit/double_definitions/double_definition_create_blank_slate_spec.rb +93 -0
  80. data/spec/suites/rspec_2/unit/double_definitions/double_definition_create_spec.rb +446 -0
  81. data/spec/suites/rspec_2/unit/errors/rr_error_spec.rb +67 -0
  82. data/spec/suites/rspec_2/unit/expectations/any_argument_expectation_spec.rb +48 -0
  83. data/spec/suites/rspec_2/unit/expectations/anything_argument_equality_expectation_spec.rb +14 -0
  84. data/spec/suites/rspec_2/unit/expectations/argument_equality_expectation_spec.rb +135 -0
  85. data/spec/suites/rspec_2/unit/expectations/boolean_argument_equality_expectation_spec.rb +30 -0
  86. data/spec/suites/rspec_2/unit/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
  87. data/spec/suites/rspec_2/unit/expectations/satisfy_argument_equality_expectation_spec.rb +61 -0
  88. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/any_times_matcher_spec.rb +22 -0
  89. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/at_least_matcher_spec.rb +37 -0
  90. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/at_most_matcher_spec.rb +43 -0
  91. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/integer_matcher_spec.rb +58 -0
  92. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/proc_matcher_spec.rb +35 -0
  93. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/range_matcher_spec.rb +39 -0
  94. data/spec/suites/rspec_2/unit/hash_with_object_id_key_spec.rb +88 -0
  95. data/spec/suites/rspec_2/unit/injections/double_injection/double_injection_spec.rb +545 -0
  96. data/spec/suites/rspec_2/unit/injections/double_injection/double_injection_verify_spec.rb +32 -0
  97. data/spec/suites/rspec_2/unit/proc_from_block_spec.rb +14 -0
  98. data/spec/suites/rspec_2/unit/rr_spec.rb +28 -0
  99. data/spec/suites/rspec_2/unit/space_spec.rb +595 -0
  100. data/spec/suites/rspec_2/unit/spy_verification_spec.rb +133 -0
  101. data/spec/suites/rspec_2/unit/times_called_matchers/any_times_matcher_spec.rb +46 -0
  102. data/spec/suites/rspec_2/unit/times_called_matchers/at_least_matcher_spec.rb +54 -0
  103. data/spec/suites/rspec_2/unit/times_called_matchers/at_most_matcher_spec.rb +69 -0
  104. data/spec/suites/rspec_2/unit/times_called_matchers/integer_matcher_spec.rb +69 -0
  105. data/spec/suites/rspec_2/unit/times_called_matchers/proc_matcher_spec.rb +54 -0
  106. data/spec/suites/rspec_2/unit/times_called_matchers/range_matcher_spec.rb +75 -0
  107. data/spec/suites/rspec_2/unit/times_called_matchers/times_called_matcher_spec.rb +117 -0
  108. data/spec/suites/rspec_2/unit/wildcard_matchers/anything_spec.rb +33 -0
  109. data/spec/suites/rspec_2/unit/wildcard_matchers/boolean_spec.rb +45 -0
  110. data/spec/suites/rspec_2/unit/wildcard_matchers/duck_type_spec.rb +64 -0
  111. data/spec/suites/rspec_2/unit/wildcard_matchers/hash_including_spec.rb +64 -0
  112. data/spec/suites/rspec_2/unit/wildcard_matchers/is_a_spec.rb +55 -0
  113. data/spec/suites/rspec_2/unit/wildcard_matchers/numeric_spec.rb +46 -0
  114. data/spec/suites/rspec_2/unit/wildcard_matchers/satisfy_spec.rb +57 -0
  115. data/spec/suites/test_unit_1/integration/test_unit_1_test.rb +6 -0
  116. data/spec/suites/test_unit_1/test_helper.rb +7 -0
  117. data/spec/suites/test_unit_2/integration/test_unit_2_test.rb +6 -0
  118. data/spec/suites/test_unit_2/test_helper.rb +3 -0
  119. metadata +183 -19
  120. data/Gemfile +0 -9
  121. data/Rakefile +0 -34
  122. data/lib/rr/adapters/rspec2.rb +0 -30
  123. data/lib/rr/adapters/test_unit.rb +0 -33
  124. data/lib/rr/errors/argument_equality_error.rb +0 -6
  125. data/lib/rr/wildcard_matchers/range.rb +0 -7
  126. data/lib/rr/wildcard_matchers/regexp.rb +0 -7
  127. data/spec/runner.rb +0 -41
@@ -11,15 +11,14 @@ module RR
11
11
  self == other || other.is_a?(klass)
12
12
  end
13
13
 
14
- def inspect
15
- "is_a(#{klass})"
14
+ def ==(other)
15
+ other.is_a?(self.class) && klass == other.klass
16
16
  end
17
+ alias :eql? :==
17
18
 
18
- def ==(other)
19
- return false unless other.is_a?(self.class)
20
- self.klass == other.klass
19
+ def inspect
20
+ "is_a(#{klass})"
21
21
  end
22
- alias_method :eql?, :==
23
22
  end
24
23
  end
25
- end
24
+ end
@@ -8,19 +8,19 @@ module RR
8
8
  end
9
9
 
10
10
  def wildcard_match?(other)
11
- return true if self == other
11
+ self == other ||
12
12
  !!expectation_proc.call(other)
13
13
  end
14
14
 
15
- def inspect
16
- "satisfy {block}"
15
+ def ==(other)
16
+ other.is_a?(self.class) &&
17
+ other.expectation_proc.equal?(self.expectation_proc)
17
18
  end
19
+ alias :eql? :==
18
20
 
19
- def ==(other)
20
- return false unless other.is_a?(self.class)
21
- self.expectation_proc == other.expectation_proc
21
+ def inspect
22
+ "satisfy { ... }"
22
23
  end
23
- alias_method :eql?, :==
24
24
  end
25
25
  end
26
- end
26
+ end
@@ -0,0 +1,112 @@
1
+ require 'forwardable'
2
+
3
+ require 'rr/core_ext/enumerable'
4
+ require 'rr/core_ext/hash'
5
+ require 'rr/core_ext/array'
6
+ require 'rr/core_ext/range'
7
+ require 'rr/core_ext/regexp'
8
+
9
+ require 'rr/class_instance_method_defined'
10
+ require 'rr/blank_slate'
11
+
12
+ require 'rr/errors'
13
+ require 'rr/errors/rr_error'
14
+ require 'rr/errors/subject_does_not_implement_method_error'
15
+ require 'rr/errors/subject_has_different_arity_error'
16
+ require 'rr/errors/double_definition_error'
17
+ require 'rr/errors/double_not_found_error'
18
+ require 'rr/errors/double_order_error'
19
+ require 'rr/errors/times_called_error'
20
+ require 'rr/errors/spy_verification_errors/spy_verification_error'
21
+ require 'rr/errors/spy_verification_errors/double_injection_not_found_error'
22
+ require 'rr/errors/spy_verification_errors/invocation_count_error'
23
+
24
+ require 'rr/space'
25
+
26
+ require 'rr/double_definitions/strategies/strategy'
27
+ require 'rr/double_definitions/strategies/strategy_methods'
28
+ require 'rr/double_definitions/strategies/verification/verification_strategy'
29
+ require 'rr/double_definitions/strategies/verification/mock'
30
+ require 'rr/double_definitions/strategies/verification/stub'
31
+ require 'rr/double_definitions/strategies/verification/dont_allow'
32
+ require 'rr/double_definitions/strategies/implementation/implementation_strategy'
33
+ require 'rr/double_definitions/strategies/implementation/reimplementation'
34
+ require 'rr/double_definitions/strategies/implementation/strongly_typed_reimplementation'
35
+ require 'rr/double_definitions/strategies/implementation/proxy'
36
+ require 'rr/double_definitions/strategies/double_injection/double_injection_strategy'
37
+ require 'rr/double_definitions/strategies/double_injection/instance'
38
+ require 'rr/double_definitions/strategies/double_injection/any_instance_of'
39
+ require 'rr/adapters/rr_methods'
40
+ require 'rr/double_definitions/double_injections/instance'
41
+ require 'rr/double_definitions/double_injections/any_instance_of'
42
+ require 'rr/double_definitions/double_definition'
43
+
44
+ require 'rr/injections/injection'
45
+ require 'rr/injections/double_injection'
46
+ require 'rr/injections/method_missing_injection'
47
+ require 'rr/injections/singleton_method_added_injection'
48
+ require 'rr/method_dispatches/base_method_dispatch'
49
+ require 'rr/method_dispatches/method_dispatch'
50
+ require 'rr/method_dispatches/method_missing_dispatch'
51
+ require 'rr/hash_with_object_id_key'
52
+ require 'rr/recorded_calls'
53
+ require 'rr/proc_from_block'
54
+
55
+ require 'rr/double_definitions/double_definition_create_blank_slate'
56
+ require 'rr/double_definitions/double_definition_create'
57
+ require 'rr/double_definitions/child_double_definition_create'
58
+
59
+ require 'rr/double'
60
+ require 'rr/double_matches'
61
+
62
+ require 'rr/expectations/argument_equality_expectation'
63
+ require 'rr/expectations/any_argument_expectation'
64
+ require 'rr/expectations/times_called_expectation'
65
+
66
+ require 'rr/wildcard_matchers/anything'
67
+ require 'rr/wildcard_matchers/is_a'
68
+ require 'rr/wildcard_matchers/numeric'
69
+ require 'rr/wildcard_matchers/boolean'
70
+ require 'rr/wildcard_matchers/duck_type'
71
+ require 'rr/wildcard_matchers/satisfy'
72
+ require 'rr/wildcard_matchers/hash_including'
73
+
74
+ require 'rr/times_called_matchers/terminal'
75
+ require 'rr/times_called_matchers/non_terminal'
76
+ require 'rr/times_called_matchers/times_called_matcher'
77
+ require 'rr/times_called_matchers/never_matcher'
78
+ require 'rr/times_called_matchers/any_times_matcher'
79
+ require 'rr/times_called_matchers/integer_matcher'
80
+ require 'rr/times_called_matchers/range_matcher'
81
+ require 'rr/times_called_matchers/proc_matcher'
82
+ require 'rr/times_called_matchers/at_least_matcher'
83
+ require 'rr/times_called_matchers/at_most_matcher'
84
+
85
+ require 'rr/spy_verification_proxy'
86
+ require 'rr/spy_verification'
87
+
88
+ require 'rr/adapters/rspec/invocation_matcher'
89
+ require 'rr/adapters/rspec_1'
90
+ require 'rr/adapters/rspec_2'
91
+ require 'rr/adapters/test_unit_1'
92
+ require 'rr/adapters/test_unit_2'
93
+ require 'rr/adapters/test_unit_2_active_support'
94
+ require 'rr/adapters/minitest'
95
+ require 'rr/adapters/minitest_active_support'
96
+ require 'rr/adapters/none'
97
+
98
+ require 'rr/version'
99
+
100
+ module RR
101
+ class << self
102
+ include Adapters::RRMethods
103
+
104
+ (RR::Space.instance_methods - Object.instance_methods).each do |method_name|
105
+ class_eval((<<-METHOD), __FILE__, __LINE__ + 1)
106
+ def #{method_name}(*args, &block)
107
+ RR::Space.instance.__send__(:#{method_name}, *args, &block)
108
+ end
109
+ METHOD
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rake'
4
+ require File.expand_path('../lib/rr/version', __FILE__)
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'rr'
8
+ gem.version = RR.version
9
+ gem.authors = ['Brian Takita', 'Elliot Winkler']
10
+ gem.email = ['elliot.winkler@gmail.com']
11
+ gem.description = "RR is a double framework that features a rich selection of double techniques and a terse syntax."
12
+ gem.summary = "RR is a double framework that features a rich selection of double techniques and a terse syntax."
13
+ gem.homepage = "http://rr.github.com/rr"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = FileList[
17
+ 'CHANGES.md',
18
+ 'LICENSE',
19
+ 'README.md',
20
+ 'VERSION',
21
+ 'lib/**/*.rb',
22
+ 'rr.gemspec'
23
+ ].to_a
24
+
25
+ gem.test_files = FileList['spec/**/*']
26
+
27
+ gem.require_paths = ['lib']
28
+ end
@@ -0,0 +1,12 @@
1
+
2
+ require 'pp'
3
+
4
+ require 'rubygems'
5
+
6
+ if ENV['COVERAGE']
7
+ require 'simplecov'
8
+ SimpleCov.start
9
+ end
10
+
11
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
12
+ require 'rr'
@@ -0,0 +1,93 @@
1
+ require 'session'
2
+
3
+ class SpecSuite
4
+ def self.def_runner(runner_name, runner_desc, &block)
5
+ runner_method = "run_#{runner_name}"
6
+ define_method(runner_method, &block)
7
+ runners << [runner_name, runner_desc]
8
+ end
9
+
10
+ def self.runners
11
+ @runners ||= []
12
+ end
13
+
14
+ def self.ruby_18?
15
+ RUBY_VERSION =~ /^1\.8/
16
+ end
17
+
18
+ def define_tasks(ctx)
19
+ suite = self
20
+
21
+ SpecSuite.runners.each do |runner_name, runner_desc|
22
+ runner_method = "run_#{runner_name}"
23
+
24
+ ctx.__send__ :desc, "Run #{runner_desc} tests"
25
+ ctx.__send__ :task, :"spec:#{runner_name}" do
26
+ session = suite.__send__(runner_method)
27
+ if session.exit_status != 0
28
+ raise "#{runner_desc} suite failed"
29
+ end
30
+ end
31
+ end
32
+
33
+ ctx.__send__ :desc, "Run all tests"
34
+ ctx.__send__ :task, :spec do
35
+ sessions = []
36
+ SpecSuite.runners.each do |runner_name, runner_desc|
37
+ puts "=== Running #{runner_desc} tests ================================================"
38
+ runner_method = "run_#{runner_name}"
39
+ sessions << suite.__send__(runner_method)
40
+ puts
41
+ end
42
+ if sessions.any? {|session| session.exit_status != 0 }
43
+ raise "Spec suite failed"
44
+ end
45
+ end
46
+ end
47
+
48
+ if ruby_18?
49
+ def_runner :test_unit_1, 'Test::Unit 1' do
50
+ run_command(build_command('ruby', 'test_unit_1', 'test'))
51
+ end
52
+ end
53
+
54
+ def_runner :test_unit_2, 'Test::Unit 2' do
55
+ run_command(build_command('ruby', 'test_unit_2', 'test'))
56
+ end
57
+
58
+ unless ruby_18?
59
+ def_runner :minitest, 'MiniTest' do
60
+ run_command(build_command('ruby', 'minitest', 'test'))
61
+ end
62
+ end
63
+
64
+ if ruby_18?
65
+ def_runner :rspec_1, 'RSpec 1' do
66
+ run_command(build_command('spec', 'rspec_1', 'spec'))
67
+ end
68
+ else
69
+ def_runner :rspec_2, 'RSpec 2' do
70
+ ENV['SPEC_OPTS'] = '--format progress'
71
+ run_command(build_command('rspec', 'rspec_2', 'spec'))
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def run_command(*parts)
78
+ session = Session::Bash.new
79
+ command = parts.join(" ")
80
+ session.execute command, :out => STDOUT, :err => STDERR
81
+ session
82
+ end
83
+
84
+ def build_command(program_name, adapter_name, suffix)
85
+ ENV['ADAPTER'] = adapter_name
86
+ file_list = build_file_list(adapter_name, suffix)
87
+ ['bundle', 'exec', program_name, *file_list]
88
+ end
89
+
90
+ def build_file_list(adapter_name, suffix)
91
+ Dir[ File.expand_path("../suites/#{adapter_name}/{.,*,**}/*_#{suffix}.rb", __FILE__) ]
92
+ end
93
+ end
@@ -0,0 +1,37 @@
1
+ module AdapterTests
2
+ def test_using_a_mock
3
+ subject = Object.new
4
+ mock(subject).foobar(1, 2) { :baz }
5
+ assert_equal :baz, subject.foobar(1, 2)
6
+ end
7
+
8
+ def test_using_a_stub
9
+ subject = Object.new
10
+ stub(subject).foobar { :baz }
11
+ assert_equal :baz, subject.foobar("any", "thing")
12
+ end
13
+
14
+ def test_using_a_mock_proxy
15
+ subject = Object.new
16
+ def subject.foobar; :baz; end
17
+
18
+ mock.proxy(subject).foobar
19
+ assert_equal :baz, subject.foobar
20
+ end
21
+
22
+ def test_using_a_stub_proxy
23
+ subject = Object.new
24
+ def subject.foobar; :baz; end
25
+
26
+ stub.proxy(subject).foobar
27
+ assert_equal :baz, subject.foobar
28
+ end
29
+
30
+ def test_times_called_verification
31
+ subject = Object.new
32
+ mock(subject).foobar(1, 2) { :baz }
33
+ assert_raise RR::Errors.error_class(:TimesCalledError) do
34
+ RR.verify
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,175 @@
1
+ require 'rubygems'
2
+ require 'session'
3
+ require 'tempfile'
4
+ $is_java = (RUBY_PLATFORM == 'java')
5
+ if $is_java
6
+ require 'arjdbc'
7
+ require 'arjdbc/sqlite3'
8
+ else
9
+ require 'sqlite3'
10
+ end
11
+
12
+ module IntegrationWithRails
13
+ def debug?
14
+ false
15
+ end
16
+
17
+ def run_fixture_tests(content)
18
+ output = nil
19
+ f = Tempfile.new('rr_test_fixture')
20
+ f.write(content)
21
+ f.close
22
+ bash = Session::Bash.new
23
+ cmd = "ruby #{f.path} 2>&1"
24
+ puts cmd if debug?
25
+ stdout, stderr = bash.execute(cmd)
26
+ success = !!(bash.exit_status == 0 || stdout =~ /Finished/)
27
+ if debug? or !success
28
+ puts stdout
29
+ puts stderr
30
+ end
31
+ success.should be_true
32
+ stdout
33
+ ensure
34
+ f.unlink
35
+ end
36
+
37
+ def test_helper_path
38
+ File.expand_path('../../../global_helper', __FILE__)
39
+ end
40
+
41
+ def sqlite_adapter
42
+ $is_java ? 'jdbcsqlite3' : 'sqlite3'
43
+ end
44
+
45
+ def sqlite_db_file_path
46
+ '/tmp/rr-test-db.sqlite3'
47
+ end
48
+
49
+ def rails_test_helper
50
+ ruby_18? ? 'test_help' : 'rails/test_help'
51
+ end
52
+
53
+ def ruby_18?
54
+ RUBY_VERSION =~ /^1\.8/
55
+ end
56
+
57
+ def bootstrap_active_record
58
+ <<-EOT
59
+ #{bootstrap}
60
+
61
+ require 'active_record'
62
+
63
+ # This is necessary to turn on transactional tests, for some reason
64
+ config = ActiveRecord::Base.configurations[:foo] = {
65
+ :adapter => '#{sqlite_adapter}',
66
+ :database => '#{sqlite_db_file_path}'
67
+ }
68
+ ActiveRecord::Base.establish_connection(config)
69
+
70
+ require '#{rails_test_helper}'
71
+ EOT
72
+ end
73
+
74
+ def self.included(base)
75
+ base.class_eval do
76
+ specify "when RR raises an error it raises a failure not an exception" do
77
+ output = run_fixture_tests <<-EOT
78
+ #{bootstrap}
79
+ require "#{test_helper_path}"
80
+
81
+ class FooTest < ActiveSupport::TestCase
82
+ def test_one
83
+ object = Object.new
84
+ mock(object).foo
85
+ end
86
+ end
87
+ EOT
88
+ output.should match /Failure/
89
+ output.should match /1 failures/
90
+ end
91
+
92
+ specify "the database is properly rolled back after an RR error" do
93
+ require 'active_record'
94
+ FileUtils.rm_f(sqlite_db_file_path)
95
+ ActiveRecord::Base.establish_connection(
96
+ :adapter => sqlite_adapter,
97
+ :database => sqlite_db_file_path
98
+ )
99
+ unless debug?
100
+ old_stdout = $stdout
101
+ $stdout = File.open('/dev/null', 'w')
102
+ end
103
+ ActiveRecord::Migration.create_table :people do |t|
104
+ t.string :name
105
+ end
106
+ unless debug?
107
+ $stdout = old_stdout
108
+ end
109
+
110
+ count = ActiveRecord::Base.connection.select_value('SELECT COUNT(*) from people')
111
+ count.to_i.should be == 0
112
+
113
+ run_fixture_tests <<-EOT
114
+ #{bootstrap_active_record}
115
+
116
+ ActiveRecord::Base.logger = Logger.new(File.open('/tmp/tests.log', 'a+'))
117
+
118
+ class Person < ActiveRecord::Base; end
119
+
120
+ require "#{test_helper_path}"
121
+
122
+ class FooTest < ActiveRecord::TestCase
123
+ def test_one
124
+ Person.create!(:name => 'Joe Blow')
125
+ object = Object.new
126
+ mock(object).foo
127
+ end
128
+ end
129
+ EOT
130
+
131
+ count = ActiveRecord::Base.connection.select_value('SELECT COUNT(*) from people')
132
+ count.to_i.should be == 0
133
+ end
134
+
135
+ specify "throwing an error in teardown doesn't mess things up" do
136
+ require 'active_record'
137
+ FileUtils.rm_f(sqlite_db_file_path)
138
+ ActiveRecord::Base.establish_connection(
139
+ :adapter => sqlite_adapter,
140
+ :database => sqlite_db_file_path
141
+ )
142
+ unless debug?
143
+ old_stdout = $stdout
144
+ $stdout = File.open('/dev/null', 'w')
145
+ end
146
+ ActiveRecord::Migration.create_table :people do |t|
147
+ t.string :name
148
+ end
149
+ unless debug?
150
+ $stdout = old_stdout
151
+ end
152
+
153
+ output = run_fixture_tests <<-EOT
154
+ #{bootstrap_active_record}
155
+
156
+ ActiveRecord::Base.logger = Logger.new(File.open('/tmp/tests.log', 'a+'))
157
+
158
+ class Person < ActiveRecord::Base; end
159
+
160
+ require "#{test_helper_path}"
161
+
162
+ class FooTest < ActiveRecord::TestCase
163
+ teardown do
164
+ raise 'hell'
165
+ end
166
+
167
+ def test_one
168
+ # whatever
169
+ end
170
+ end
171
+ EOT
172
+ end
173
+ end
174
+ end
175
+ end