shoulda-matchers 2.7.0 → 2.8.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +13 -3
  3. data/Appraisals +18 -0
  4. data/CONTRIBUTING.md +13 -29
  5. data/Gemfile +1 -4
  6. data/Gemfile.lock +2 -10
  7. data/NEWS.md +100 -14
  8. data/README.md +62 -58
  9. data/Rakefile +9 -16
  10. data/gemfiles/3.0.gemfile +11 -12
  11. data/gemfiles/3.0.gemfile.lock +15 -10
  12. data/gemfiles/3.1.gemfile +11 -12
  13. data/gemfiles/3.1.gemfile.lock +14 -10
  14. data/gemfiles/3.1_1.9.2.gemfile +12 -11
  15. data/gemfiles/3.1_1.9.2.gemfile.lock +14 -3
  16. data/gemfiles/3.2.gemfile +11 -12
  17. data/gemfiles/3.2.gemfile.lock +15 -10
  18. data/gemfiles/3.2_1.9.2.gemfile +12 -11
  19. data/gemfiles/3.2_1.9.2.gemfile.lock +14 -2
  20. data/gemfiles/4.0.0.gemfile +10 -12
  21. data/gemfiles/4.0.0.gemfile.lock +13 -10
  22. data/gemfiles/4.0.1.gemfile +10 -12
  23. data/gemfiles/4.0.1.gemfile.lock +13 -10
  24. data/gemfiles/4.1.gemfile +13 -15
  25. data/gemfiles/4.1.gemfile.lock +45 -50
  26. data/gemfiles/4.2.gemfile +36 -0
  27. data/gemfiles/4.2.gemfile.lock +245 -0
  28. data/lib/shoulda/matchers.rb +3 -1
  29. data/lib/shoulda/matchers/action_controller.rb +1 -1
  30. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +1 -1
  31. data/lib/shoulda/matchers/action_controller/route_params.rb +9 -4
  32. data/lib/shoulda/matchers/action_controller/{set_the_flash_matcher.rb → set_flash_matcher.rb} +34 -26
  33. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +125 -69
  34. data/lib/shoulda/matchers/active_model.rb +1 -2
  35. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +18 -5
  36. data/lib/shoulda/matchers/active_model/exception_message_finder.rb +2 -2
  37. data/lib/shoulda/matchers/active_model/helpers.rb +4 -4
  38. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +10 -3
  39. data/lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb +1 -1
  40. data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +3 -1
  41. data/lib/shoulda/matchers/active_model/{ensure_length_of_matcher.rb → validate_length_of_matcher.rb} +30 -20
  42. data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +21 -0
  43. data/lib/shoulda/matchers/active_model/validation_message_finder.rb +2 -2
  44. data/lib/shoulda/matchers/active_record.rb +2 -0
  45. data/lib/shoulda/matchers/active_record/association_matcher.rb +96 -2
  46. data/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb +1 -1
  47. data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +3 -3
  48. data/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb +22 -2
  49. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +30 -4
  50. data/lib/shoulda/matchers/active_record/serialize_matcher.rb +19 -3
  51. data/lib/shoulda/matchers/active_record/uniqueness.rb +14 -0
  52. data/lib/shoulda/matchers/active_record/uniqueness/model.rb +45 -0
  53. data/lib/shoulda/matchers/active_record/uniqueness/namespace.rb +36 -0
  54. data/lib/shoulda/matchers/active_record/uniqueness/test_model_creator.rb +50 -0
  55. data/lib/shoulda/matchers/active_record/uniqueness/test_models.rb +24 -0
  56. data/lib/shoulda/matchers/{active_model → active_record}/validate_uniqueness_of_matcher.rb +76 -16
  57. data/lib/shoulda/matchers/independent/delegate_method_matcher.rb +117 -51
  58. data/lib/shoulda/matchers/independent/delegate_method_matcher/target_not_defined_error.rb +1 -1
  59. data/lib/shoulda/matchers/matcher_context.rb +35 -0
  60. data/lib/shoulda/matchers/rails_shim.rb +23 -0
  61. data/lib/shoulda/matchers/util.rb +28 -0
  62. data/lib/shoulda/matchers/version.rb +1 -1
  63. data/script/SUPPORTED_VERSIONS +1 -1
  64. data/spec/acceptance/active_model_integration_spec.rb +20 -0
  65. data/spec/acceptance/independent_matchers_spec.rb +64 -0
  66. data/spec/acceptance/rails_integration_spec.rb +142 -0
  67. data/spec/acceptance_spec_helper.rb +23 -0
  68. data/spec/support/acceptance/helpers.rb +29 -0
  69. data/spec/support/acceptance/helpers/active_model_helpers.rb +11 -0
  70. data/spec/support/acceptance/helpers/array_helpers.rb +13 -0
  71. data/spec/support/acceptance/helpers/base_helpers.rb +14 -0
  72. data/spec/support/acceptance/helpers/command_helpers.rb +51 -0
  73. data/spec/support/acceptance/helpers/file_helpers.rb +19 -0
  74. data/spec/support/acceptance/helpers/gem_helpers.rb +31 -0
  75. data/spec/support/acceptance/helpers/minitest_helpers.rb +19 -0
  76. data/spec/support/acceptance/helpers/pluralization_helpers.rb +13 -0
  77. data/spec/support/acceptance/helpers/rails_version_helpers.rb +11 -0
  78. data/spec/support/acceptance/helpers/rspec_helpers.rb +26 -0
  79. data/spec/support/acceptance/helpers/ruby_version_helpers.rb +9 -0
  80. data/spec/support/acceptance/helpers/step_helpers.rb +117 -0
  81. data/spec/support/acceptance/matchers/have_output.rb +31 -0
  82. data/spec/support/acceptance/matchers/indicate_number_of_tests_was_run_matcher.rb +55 -0
  83. data/spec/support/acceptance/matchers/indicate_that_tests_were_run_matcher.rb +103 -0
  84. data/spec/support/tests/bundle.rb +94 -0
  85. data/spec/support/tests/command_runner.rb +214 -0
  86. data/spec/support/tests/filesystem.rb +77 -0
  87. data/spec/support/tests/version.rb +45 -0
  88. data/spec/support/unit/capture.rb +34 -0
  89. data/spec/support/unit/helpers/active_model_helpers.rb +25 -0
  90. data/spec/support/unit/helpers/active_model_versions.rb +20 -0
  91. data/spec/support/unit/helpers/active_resource_builder.rb +27 -0
  92. data/spec/support/unit/helpers/allow_value_matcher_helpers.rb +15 -0
  93. data/spec/support/unit/helpers/class_builder.rb +72 -0
  94. data/spec/support/unit/helpers/confirmation_matcher_helpers.rb +17 -0
  95. data/spec/support/unit/helpers/controller_builder.rb +91 -0
  96. data/spec/support/unit/helpers/i18n_faker.rb +15 -0
  97. data/spec/support/unit/helpers/mailer_builder.rb +12 -0
  98. data/spec/support/unit/helpers/model_builder.rb +102 -0
  99. data/spec/support/unit/helpers/rails_versions.rb +28 -0
  100. data/spec/support/unit/i18n.rb +7 -0
  101. data/spec/support/unit/matchers/deprecate.rb +60 -0
  102. data/spec/support/unit/matchers/fail_with_message_including_matcher.rb +50 -0
  103. data/spec/support/unit/matchers/fail_with_message_matcher.rb +50 -0
  104. data/spec/support/unit/matchers/print_warning_including.rb +59 -0
  105. data/spec/support/unit/rails_application.rb +110 -0
  106. data/spec/support/unit/record_builder_with_i18n_validation_message.rb +69 -0
  107. data/spec/support/unit/record_validating_confirmation_builder.rb +56 -0
  108. data/spec/support/unit/record_with_different_error_attribute_builder.rb +92 -0
  109. data/spec/support/{shared_examples → unit/shared_examples}/numerical_submatcher.rb +0 -2
  110. data/spec/support/{shared_examples → unit/shared_examples}/numerical_type_submatcher.rb +0 -2
  111. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/callback_matcher_spec.rb +2 -2
  112. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/filter_param_matcher_spec.rb +2 -2
  113. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/redirect_to_matcher_spec.rb +3 -3
  114. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/render_template_matcher_spec.rb +2 -4
  115. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/render_with_layout_matcher_spec.rb +9 -6
  116. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/rescue_from_matcher_spec.rb +2 -2
  117. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/respond_with_matcher_spec.rb +2 -2
  118. data/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb +126 -0
  119. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/route_params_spec.rb +2 -2
  120. data/spec/unit/shoulda/matchers/action_controller/set_flash_matcher_spec.rb +167 -0
  121. data/spec/unit/shoulda/matchers/action_controller/set_session_matcher_spec.rb +294 -0
  122. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/strong_parameters_matcher_spec.rb +19 -11
  123. data/spec/{shoulda → unit/shoulda}/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +2 -2
  124. data/spec/{shoulda → unit/shoulda}/matchers/active_model/allow_value_matcher_spec.rb +49 -21
  125. data/spec/{shoulda → unit/shoulda}/matchers/active_model/disallow_value_matcher_spec.rb +8 -4
  126. data/spec/{shoulda → unit/shoulda}/matchers/active_model/exception_message_finder_spec.rb +4 -4
  127. data/spec/{shoulda → unit/shoulda}/matchers/active_model/have_secure_password_matcher_spec.rb +2 -2
  128. data/spec/{shoulda → unit/shoulda}/matchers/active_model/helpers_spec.rb +7 -3
  129. data/spec/{shoulda → unit/shoulda}/matchers/active_model/numericality_matchers/comparison_matcher_spec.rb +1 -1
  130. data/spec/{shoulda → unit/shoulda}/matchers/active_model/numericality_matchers/even_number_matcher_spec.rb +1 -1
  131. data/spec/{shoulda → unit/shoulda}/matchers/active_model/numericality_matchers/odd_number_matcher_spec.rb +1 -1
  132. data/spec/{shoulda → unit/shoulda}/matchers/active_model/numericality_matchers/only_integer_matcher_spec.rb +1 -1
  133. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_absence_of_matcher_spec.rb +3 -3
  134. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_acceptance_of_matcher_spec.rb +2 -2
  135. data/spec/unit/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +63 -0
  136. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_exclusion_of_matcher_spec.rb +5 -4
  137. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_inclusion_of_matcher_spec.rb +7 -14
  138. data/spec/{shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb → unit/shoulda/matchers/active_model/validate_length_of_matcher_spec.rb} +43 -23
  139. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_numericality_of_matcher_spec.rb +3 -4
  140. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_presence_of_matcher_spec.rb +3 -3
  141. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +127 -2
  142. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validation_message_finder_spec.rb +8 -6
  143. data/spec/{shoulda → unit/shoulda}/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +2 -2
  144. data/spec/{shoulda → unit/shoulda}/matchers/active_record/association_matcher_spec.rb +217 -26
  145. data/spec/{shoulda → unit/shoulda}/matchers/active_record/association_matchers/model_reflection_spec.rb +2 -2
  146. data/spec/{shoulda → unit/shoulda}/matchers/active_record/define_enum_for_matcher_spec.rb +2 -2
  147. data/spec/{shoulda → unit/shoulda}/matchers/active_record/have_db_column_matcher_spec.rb +2 -2
  148. data/spec/{shoulda → unit/shoulda}/matchers/active_record/have_db_index_matcher_spec.rb +8 -5
  149. data/spec/{shoulda → unit/shoulda}/matchers/active_record/have_readonly_attributes_matcher_spec.rb +2 -2
  150. data/spec/{shoulda → unit/shoulda}/matchers/active_record/serialize_matcher_spec.rb +3 -3
  151. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/double_collection_spec.rb +29 -7
  152. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/double_implementation_registry_spec.rb +1 -1
  153. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/double_spec.rb +20 -10
  154. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/object_double_spec.rb +1 -1
  155. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/proxy_implementation_spec.rb +13 -6
  156. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/stub_implementation_spec.rb +2 -2
  157. data/spec/unit/shoulda/matchers/doublespeak/world_spec.rb +77 -0
  158. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak_spec.rb +11 -3
  159. data/spec/{shoulda → unit/shoulda}/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb +1 -1
  160. data/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb +517 -0
  161. data/spec/unit_spec_helper.rb +66 -0
  162. data/spec/warnings_spy/partitioner.rb +10 -3
  163. data/spec/warnings_spy/reader.rb +9 -20
  164. data/spec/warnings_spy/reporter.rb +2 -1
  165. metadata +212 -149
  166. data/features/activemodel_integration.feature +0 -15
  167. data/features/rails_integration.feature +0 -160
  168. data/features/step_definitions/activemodel_steps.rb +0 -21
  169. data/features/step_definitions/rails_steps.rb +0 -227
  170. data/features/support/env.rb +0 -6
  171. data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +0 -70
  172. data/spec/shoulda/matchers/action_controller/set_session_matcher_spec.rb +0 -113
  173. data/spec/shoulda/matchers/action_controller/set_the_flash_matcher_spec.rb +0 -153
  174. data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +0 -47
  175. data/spec/shoulda/matchers/doublespeak/world_spec.rb +0 -70
  176. data/spec/shoulda/matchers/independent/delegate_method_matcher_spec.rb +0 -309
  177. data/spec/spec_helper.rb +0 -42
  178. data/spec/support/active_model_versions.rb +0 -13
  179. data/spec/support/active_resource_builder.rb +0 -29
  180. data/spec/support/activemodel_helpers.rb +0 -23
  181. data/spec/support/capture_helpers.rb +0 -19
  182. data/spec/support/class_builder.rb +0 -46
  183. data/spec/support/controller_builder.rb +0 -102
  184. data/spec/support/fail_with_message_including_matcher.rb +0 -44
  185. data/spec/support/fail_with_message_matcher.rb +0 -44
  186. data/spec/support/i18n_faker.rb +0 -10
  187. data/spec/support/mailer_builder.rb +0 -10
  188. data/spec/support/model_builder.rb +0 -81
  189. data/spec/support/rails_versions.rb +0 -26
  190. data/spec/support/test_application.rb +0 -120
@@ -0,0 +1,214 @@
1
+ require 'timeout'
2
+ require 'shellwords'
3
+
4
+ module Tests
5
+ class CommandRunner
6
+ TimeoutError = Class.new(StandardError)
7
+
8
+ def self.run(*args)
9
+ new(*args).tap do |runner|
10
+ yield runner
11
+ runner.call
12
+ end
13
+ end
14
+
15
+ def self.run!(*args)
16
+ run(*args) do |runner|
17
+ runner.run_successfully = true
18
+ yield runner if block_given?
19
+ end
20
+ end
21
+
22
+ attr_reader :status, :options, :env, :directory
23
+ attr_accessor :command_prefix, :run_quickly, :run_successfully, :retries,
24
+ :timeout
25
+
26
+ def initialize(*args)
27
+ @reader, @writer = IO.pipe
28
+ options = (args.last.is_a?(Hash) ? args.pop : {})
29
+ @args = args
30
+ @options = options.merge(
31
+ err: [:child, :out],
32
+ out: writer
33
+ )
34
+ @env = extract_env_from(@options)
35
+
36
+ @wrapper = ->(block) { block.call }
37
+ @command_prefix = ''
38
+ @directory = Dir.pwd
39
+ @run_quickly = false
40
+ @run_successfully = false
41
+ @retries = 1
42
+ @num_times_run = 0
43
+ @timeout = 20
44
+ end
45
+
46
+ def around_command(&block)
47
+ @wrapper = block
48
+ end
49
+
50
+ def directory=(directory)
51
+ @directory = directory || Dir.pwd
52
+ end
53
+
54
+ def formatted_command
55
+ [formatted_env, Shellwords.join(command)].
56
+ select { |value| !value.empty? }.
57
+ join(' ')
58
+ end
59
+
60
+ def call
61
+ possibly_retrying do
62
+ possibly_running_quickly do
63
+ debug { "\n\e[32mRunning command:\e[0m #{formatted_command}" }
64
+ wrapper.call(-> { run })
65
+ debug { "\n" + divider('START') + output + divider('END') }
66
+
67
+ if run_successfully && !success?
68
+ fail!
69
+ end
70
+ end
71
+ end
72
+
73
+ self
74
+ end
75
+
76
+ def stop
77
+ unless writer.closed?
78
+ writer.close
79
+ end
80
+ end
81
+
82
+ def output
83
+ @_output ||= begin
84
+ stop
85
+ without_colors(reader.read)
86
+ end
87
+ end
88
+
89
+ def elided_output
90
+ lines = output.split(/\n/)
91
+ new_lines = lines[0..4]
92
+
93
+ if lines.size > 10
94
+ new_lines << "(...#{lines.size - 10} more lines...)"
95
+ end
96
+
97
+ new_lines << lines[-5..-1]
98
+ new_lines.join("\n")
99
+ end
100
+
101
+ def success?
102
+ status.success?
103
+ end
104
+
105
+ def exit_status
106
+ status.exitstatus
107
+ end
108
+
109
+ def fail!
110
+ raise <<-MESSAGE
111
+ Command #{command.inspect} exited with status #{exit_status}.
112
+ Output:
113
+ #{divider('START') + output + divider('END')}
114
+ MESSAGE
115
+ end
116
+
117
+ def has_output?(expected_output)
118
+ output.include?(expected_output)
119
+ end
120
+
121
+ protected
122
+
123
+ attr_reader :args, :reader, :writer, :wrapper
124
+
125
+ private
126
+
127
+ def extract_env_from(options)
128
+ options.delete(:env) { {} }.inject({}) do |hash, (key, value)|
129
+ hash[key.to_s] = value
130
+ hash
131
+ end
132
+ end
133
+
134
+ def command
135
+ ([command_prefix] + args).flatten.flat_map do |word|
136
+ Shellwords.split(word)
137
+ end
138
+ end
139
+
140
+ def formatted_env
141
+ env.map { |key, value| "#{key}=#{value.inspect}" }.join(' ')
142
+ end
143
+
144
+ def run
145
+ Dir.chdir(directory) do
146
+ pid = spawn(env, *command, options)
147
+ Process.waitpid(pid)
148
+ end
149
+
150
+ @status = $?
151
+ end
152
+
153
+ def possibly_running_quickly(&block)
154
+ if run_quickly
155
+ begin
156
+ Timeout.timeout(timeout, &block)
157
+ rescue Timeout::Error
158
+ stop
159
+
160
+ message =
161
+ "Command timed out after #{timeout} seconds: #{formatted_command}\n" +
162
+ "Output:\n" +
163
+ elided_output
164
+
165
+ raise TimeoutError, message
166
+ end
167
+ else
168
+ yield
169
+ end
170
+ end
171
+
172
+ def possibly_retrying
173
+ begin
174
+ @num_times_run += 1
175
+ yield
176
+ rescue => error
177
+ debug { "#{error.class}: #{error.message}" }
178
+
179
+ if @num_times_run < @retries
180
+ sleep @num_times_run
181
+ retry
182
+ else
183
+ raise error
184
+ end
185
+ end
186
+ end
187
+
188
+ def divider(title = '')
189
+ total_length = 72
190
+ start_length = 3
191
+
192
+ string = ''
193
+ string << ('-' * start_length)
194
+ string << title
195
+ string << '-' * (total_length - start_length - title.length)
196
+ string << "\n"
197
+ string
198
+ end
199
+
200
+ def without_colors(string)
201
+ string.gsub(/\e\[\d+(?:;\d+)?m(.+?)\e\[0m/, '\1')
202
+ end
203
+
204
+ def debugging_enabled?
205
+ ENV['DEBUG_COMMANDS'] == '1'
206
+ end
207
+
208
+ def debug(&block)
209
+ if debugging_enabled?
210
+ puts block.call
211
+ end
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,77 @@
1
+ require 'fileutils'
2
+
3
+ module Tests
4
+ class Filesystem
5
+ PROJECT_NAME = 'test-project'
6
+ ROOT_DIRECTORY = Pathname.new('../../../..').expand_path(__FILE__)
7
+ TEMP_DIRECTORY = ROOT_DIRECTORY.join('tmp/acceptance')
8
+ PROJECT_DIRECTORY = TEMP_DIRECTORY.join(PROJECT_NAME)
9
+
10
+ def root_directory
11
+ ROOT_DIRECTORY
12
+ end
13
+
14
+ def temp_directory
15
+ TEMP_DIRECTORY
16
+ end
17
+
18
+ def project_directory
19
+ PROJECT_DIRECTORY
20
+ end
21
+
22
+ def wrap(path)
23
+ if path.is_a?(Pathname)
24
+ path
25
+ else
26
+ find_in_project(path)
27
+ end
28
+ end
29
+
30
+ def within_project(&block)
31
+ Dir.chdir(project_directory, &block)
32
+ end
33
+
34
+ def clean
35
+ if temp_directory.exist?
36
+ temp_directory.rmtree
37
+ end
38
+ end
39
+
40
+ def create
41
+ project_directory.mkpath
42
+ end
43
+
44
+ def find_in_project(path)
45
+ project_directory.join(path)
46
+ end
47
+
48
+ def open(path, *args, &block)
49
+ find_in_project(path).open(*args, &block)
50
+ end
51
+
52
+ def read(path)
53
+ find_in_project(path).read
54
+ end
55
+
56
+ def write(path, content)
57
+ pathname = wrap(path)
58
+ create_parents_of(pathname)
59
+ pathname.open('w') { |f| f.write(content) }
60
+ end
61
+
62
+ def create_parents_of(path)
63
+ wrap(path).dirname.mkpath
64
+ end
65
+
66
+ def append_to_file(path, content, options = {})
67
+ create_parents_of(path)
68
+ open(path, 'a') { |f| f.puts(content + "\n") }
69
+ end
70
+
71
+ def remove_from_file(path, pattern)
72
+ content = read(path)
73
+ content.sub!(/#{pattern}\n/, '')
74
+ write(path, content)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,45 @@
1
+ module Tests
2
+ class Version
3
+ def initialize(version)
4
+ @version = Gem::Version.new(version.to_s + '')
5
+ end
6
+
7
+ def <(other_version)
8
+ compare?(:<, other_version)
9
+ end
10
+
11
+ def <=(other_version)
12
+ compare?(:<=, other_version)
13
+ end
14
+
15
+ def ==(other_version)
16
+ compare?(:==, other_version)
17
+ end
18
+
19
+ def >=(other_version)
20
+ compare?(:>=, other_version)
21
+ end
22
+
23
+ def >(other_version)
24
+ compare?(:>, other_version)
25
+ end
26
+
27
+ def =~(other_version)
28
+ Gem::Requirement.new(other_version).satisfied_by?(version)
29
+ end
30
+
31
+ def to_s
32
+ version.to_s
33
+ end
34
+
35
+ protected
36
+
37
+ attr_reader :version
38
+
39
+ private
40
+
41
+ def compare?(op, other_version)
42
+ Gem::Requirement.new("#{op} #{other_version}").satisfied_by?(version)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,34 @@
1
+ module Kernel
2
+ # #capture, #silence_stream, and #silence_stderr are deprecated after Rails
3
+ # 4.2 and will be removed in 5.0, so just override them completely here
4
+
5
+ def capture(stream)
6
+ stream = stream.to_s
7
+ captured_stream = Tempfile.new(stream)
8
+ stream_io = eval("$#{stream}")
9
+ origin_stream = stream_io.dup
10
+ stream_io.reopen(captured_stream)
11
+
12
+ yield
13
+
14
+ stream_io.rewind
15
+ return captured_stream.read
16
+ ensure
17
+ captured_stream.unlink
18
+ stream_io.reopen(origin_stream)
19
+ end
20
+
21
+ def silence_stream(stream)
22
+ old_stream = stream.dup
23
+ stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
24
+ stream.sync = true
25
+ yield
26
+ ensure
27
+ stream.reopen(old_stream)
28
+ old_stream.close
29
+ end
30
+
31
+ def silence_stderr
32
+ silence_stream(STDERR) { yield }
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ module UnitTests
2
+ module ActiveModelHelpers
3
+ def self.configure_example_group(example_group)
4
+ example_group.include(self)
5
+ end
6
+
7
+ def custom_validation(options = {}, &block)
8
+ attribute_name = options.fetch(:attribute_name, :attr)
9
+ attribute_type = options.fetch(:attribute_type, :integer)
10
+
11
+ define_model(:example, attribute_name => attribute_type) do
12
+ validate :custom_validation
13
+
14
+ define_method(:custom_validation, &block)
15
+ end.new
16
+ end
17
+ alias record_with_custom_validation custom_validation
18
+
19
+ def validating_format(options)
20
+ define_model :example, attr: :string do
21
+ validates_format_of :attr, options
22
+ end.new
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ module UnitTests
2
+ module ActiveModelVersions
3
+ def self.configure_example_group(example_group)
4
+ example_group.include(self)
5
+ example_group.extend(self)
6
+ end
7
+
8
+ def active_model_3_1?
9
+ (::ActiveModel::VERSION::MAJOR == 3 && ::ActiveModel::VERSION::MINOR >= 1) || active_model_4_0?
10
+ end
11
+
12
+ def active_model_3_2?
13
+ (::ActiveModel::VERSION::MAJOR == 3 && ::ActiveModel::VERSION::MINOR >= 2) || active_model_4_0?
14
+ end
15
+
16
+ def active_model_4_0?
17
+ ::ActiveModel::VERSION::MAJOR == 4
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ require 'active_resource'
2
+
3
+ module UnitTests
4
+ module ActiveResourceBuilder
5
+ def self.configure_example_group(example_group)
6
+ example_group.include ActiveResourceBuilder
7
+
8
+ example_group.after do
9
+ ActiveSupport::Dependencies.clear
10
+ end
11
+ end
12
+
13
+ def define_active_resource_class(class_name, attributes = {}, &block)
14
+ define_class(class_name, ActiveResource::Base) do
15
+ schema do
16
+ attributes.each do |attr, type|
17
+ attribute attr, type
18
+ end
19
+ end
20
+
21
+ if block_given?
22
+ class_eval(&block)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end