Skip to content

Commit ac34412

Browse files
authored
Merge pull request #58 from rspec/cleanup
Cleanup from adding rubocop
2 parents ae7d3db + f6df198 commit ac34412

File tree

23 files changed

+122
-87
lines changed

23 files changed

+122
-87
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
source "https://rubygems.org"
23

34
gemspec

Rakefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
require "bundler"
23
begin
34
Bundler.setup
@@ -20,14 +21,14 @@ Cucumber::Rake::Task.new(:cucumber)
2021

2122
namespace :generate do
2223
desc "generate a fresh app with rspec installed"
23-
task :sample do |t|
24+
task :sample do |_t|
2425
unless File.directory?('./tmp/sample')
2526
bindir = File.expand_path("bin")
2627

2728
Dir.mkdir('tmp') unless File.directory?('tmp')
2829
sh "cp -r ./templates/sample ./tmp/sample"
2930

30-
if test ?d, bindir
31+
if test 'd', bindir
3132
Dir.chdir("./tmp/sample") do
3233
Dir.mkdir("bin") unless File.directory?("bin")
3334
sh "ln -sf #{bindir}/rake bin/rake"
@@ -63,8 +64,9 @@ namespace :clobber do
6364
end
6465

6566
desc "Push docs/cukes to relishapp using the relish-client-gem"
66-
task :relish, :version do |t, args|
67+
task :relish, :version do |_t, args|
6768
raise "rake relish[VERSION]" unless args[:version]
69+
6870
sh "cp Changelog.md features/"
6971
if `relish versions rspec/rspec-activemodel-mocks`.split.map(&:strip).include? args[:version]
7072
puts "Version #{args[:version]} already exists"
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Then /^the example(s)? should( all)? pass$/ do |_, _|
2-
step %q{the output should contain "0 failures"}
3-
step %q{the exit status should be 0}
1+
# frozen_string_literal: true
2+
Then(/^the example(s)? should( all)? pass$/) do |_, _|
3+
step 'the output should contain "0 failures"'
4+
step 'the exit status should be 0'
45
end

features/step_definitions/model_steps.rb

Lines changed: 0 additions & 3 deletions
This file was deleted.

features/support/env.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
require 'aruba/cucumber'
23

34
module ArubaExt
@@ -17,7 +18,7 @@ def run(cmd)
1718
end
1819

1920
def aruba_path(file_or_dir)
20-
File.expand_path("../../../#{file_or_dir.sub('sample','aruba')}", __FILE__)
21+
File.expand_path("../../../#{file_or_dir.sub('sample', 'aruba')}", __FILE__)
2122
end
2223

2324
def sample_path(file_or_dir)
@@ -37,12 +38,12 @@ def copy(file_or_dir)
3738
end
3839

3940
Before do
40-
steps %Q{
41+
steps %(
4142
Given a directory named "spec"
42-
}
43+
)
4344

4445
Dir['tmp/sample/*'].each do |file_or_dir|
45-
if !(file_or_dir =~ /spec$/)
46+
if file_or_dir !~ /spec$/
4647
write_symlink(file_or_dir)
4748
end
4849
end

features/support/rubinius.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# frozen_string_literal: true
12
# Required until https://github.com/rubinius/rubinius/issues/2430 is resolved
2-
ENV['RBXOPT'] = "#{ENV["RBXOPT"]} -Xcompiler.no_rbc"
3+
ENV['RBXOPT'] = "#{ENV.fetch("RBXOPT", nil)} -Xcompiler.no_rbc"
34

4-
Around "@unsupported-on-rbx" do |scenario, block|
5+
Around "@unsupported-on-rbx" do |_scenario, block|
56
block.call unless defined?(Rubinius)
67
end

lib/rspec-activemodel-mocks.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# frozen_string_literal: true
12
require 'rspec/active_model/mocks'

lib/rspec/active_model/mocks.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# frozen_string_literal: true
12
require 'rspec/core'
23

3-
RSpec::configure do |c|
4+
RSpec.configure do |c|
45
c.backtrace_exclusion_patterns << /lib\/rspec\/active_model\/mocks/
56
end
67

lib/rspec/active_model/mocks/mocks.rb

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
# frozen_string_literal: true
12
require 'active_support'
23
require 'active_support/core_ext/object/to_param'
34
require 'active_model'
45

56
module RSpec::ActiveModel::Mocks
67
class IllegalDataAccessException < StandardError; end
78
module Mocks
8-
99
module ActiveModelInstanceMethods
1010
# Stubs `persisted?` to return false and `id` to return nil
1111
# @return self
@@ -91,12 +91,13 @@ def association(association_name)
9191
# * A String representing a Class that does not exist
9292
# * A String representing a Class that extends ActiveModel::Naming
9393
# * A Class that extends ActiveModel::Naming
94-
def mock_model(string_or_model_class, stubs = {})
94+
def mock_model(string_or_model_class, stubs={})
9595
if String === string_or_model_class
9696
if Object.const_defined?(string_or_model_class)
9797
model_class = Object.const_get(string_or_model_class)
9898
else
9999
model_class = Object.const_set(string_or_model_class, Class.new do
100+
# rubocop:disable Style/SingleLineMethods
100101
extend ::ActiveModel::Naming
101102
def self.primary_key; :id; end
102103

@@ -106,32 +107,33 @@ def self._reflect_on_association(_other); nil; end
106107
def self.composite_primary_key?; false; end
107108
def self.has_query_constraints?; false; end
108109
def self.param_delimiter; "-"; end
110+
# rubocop:enable Style/SingleLineMethods
109111
end)
110112
end
111113
else
112114
model_class = string_or_model_class
113115
end
114116

115117
unless model_class.kind_of? ::ActiveModel::Naming
116-
raise ArgumentError.new <<-EOM
118+
raise ArgumentError, <<-EOM
117119
The mock_model method can only accept as its first argument:
118120
* A String representing a Class that does not exist
119121
* A String representing a Class that extends ActiveModel::Naming
120122
* A Class that extends ActiveModel::Naming
121123
122124
It received #{model_class.inspect}
123-
EOM
125+
EOM
124126
end
125127

126-
stubs = {:id => next_id}.merge(stubs)
127-
stubs = {:persisted? => !!stubs[:id],
128-
:destroyed? => false,
129-
:marked_for_destruction? => false,
130-
:valid? => true,
131-
:blank? => false}.merge(stubs)
128+
stubs = { :id => next_id }.merge(stubs)
129+
stubs = { :persisted? => !!stubs[:id],
130+
:destroyed? => false,
131+
:marked_for_destruction? => false,
132+
:valid? => true,
133+
:blank? => false }.merge(stubs)
132134

133135
double("#{model_class.name}_#{stubs[:id]}", stubs).tap do |m|
134-
if model_class.method(:===).owner == Module && !stubs.has_key?(:===)
136+
if model_class.method(:===).owner == Module && !stubs.key?(:===)
135137
allow(model_class).to receive(:===).and_wrap_original do |original, other|
136138
m === other || original.call(other)
137139
end
@@ -143,50 +145,52 @@ def self.param_delimiter; "-"; end
143145
include ActiveModel::Conversion
144146
include ActiveModel::Validations
145147
end
146-
if defined?(ActiveRecord)
147-
if stubs.values_at(:save, :update_attributes, :update).include?(false)
148-
RSpec::Mocks.allow_message(m.errors, :empty?).and_return(false)
149-
RSpec::Mocks.allow_message(m.errors, :blank?).and_return(false)
150-
end
148+
if defined?(ActiveRecord) && stubs.values_at(:save, :update_attributes, :update).include?(false)
149+
RSpec::Mocks.allow_message(m.errors, :empty?).and_return(false)
150+
RSpec::Mocks.allow_message(m.errors, :blank?).and_return(false)
151151
end
152152

153153
msingleton.__send__(:define_method, :is_a?) do |other|
154154
model_class.ancestors.include?(other)
155-
end unless stubs.has_key?(:is_a?)
155+
end unless stubs.key?(:is_a?)
156156

157157
msingleton.__send__(:define_method, :kind_of?) do |other|
158158
model_class.ancestors.include?(other)
159-
end unless stubs.has_key?(:kind_of?)
159+
end unless stubs.key?(:kind_of?)
160160

161161
msingleton.__send__(:define_method, :instance_of?) do |other|
162162
other == model_class
163-
end unless stubs.has_key?(:instance_of?)
163+
end unless stubs.key?(:instance_of?)
164164

165165
msingleton.__send__(:define_method, :__model_class_has_column?) do |method_name|
166166
model_class.respond_to?(:column_names) && model_class.column_names.include?(method_name.to_s)
167167
end
168168

169169
msingleton.__send__(:define_method, :has_attribute?) do |attr_name|
170170
__model_class_has_column?(attr_name)
171-
end unless stubs.has_key?(:has_attribute?)
171+
end unless stubs.key?(:has_attribute?)
172172

173173
msingleton.__send__(:define_method, :respond_to?) do |method_name, *args|
174-
include_private = args.first || false
174+
include_private = args.first || false
175175
__model_class_has_column?(method_name) ? true : super(method_name, include_private)
176-
end unless stubs.has_key?(:respond_to?)
176+
end unless stubs.key?(:respond_to?)
177177

178-
msingleton.__send__(:define_method, :method_missing) do |m, *a, &b|
179-
respond_to?(m) ? null_object? ? self : nil : super(m, *a, &b)
178+
msingleton.__send__(:define_method, :method_missing) do |missing_m, *a, &b|
179+
if respond_to?(missing_m)
180+
null_object? ? self : nil
181+
else
182+
super(missing_m, *a, &b)
183+
end
180184
end
181185

182186
msingleton.__send__(:define_method, :class) do
183187
model_class
184-
end unless stubs.has_key?(:class)
188+
end unless stubs.key?(:class)
185189

186190
mock_param = to_param
187191
msingleton.__send__(:define_method, :to_s) do
188192
"#{model_class.name}_#{mock_param}"
189-
end unless stubs.has_key?(:to_s)
193+
end unless stubs.key?(:to_s)
190194
yield m if block_given?
191195
end
192196
end
@@ -208,7 +212,7 @@ def persisted?
208212
module ActiveRecordStubExtensions
209213
# Stubs `id` (or other primary key method) to return nil
210214
def as_new_record
211-
self.__send__("#{self.class.primary_key}=", nil)
215+
__send__("#{self.class.primary_key}=", nil)
212216
super
213217
end
214218

@@ -220,7 +224,8 @@ def new_record?
220224
# Raises an IllegalDataAccessException (stubbed models are not allowed to access the database)
221225
# @raises IllegalDataAccessException
222226
def connection
223-
raise RSpec::ActiveModel::Mocks::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
227+
raise RSpec::ActiveModel::Mocks::IllegalDataAccessException,
228+
"stubbed models are not allowed to access the database"
224229
end
225230
end
226231

@@ -257,13 +262,13 @@ def stub_model(model_class, stubs={})
257262
if defined?(ActiveRecord) && model_class < ActiveRecord::Base && model_class.primary_key
258263
m.extend ActiveRecordStubExtensions
259264
primary_key = model_class.primary_key.to_sym
260-
stubs = {primary_key => next_id}.merge(stubs)
261-
stubs = {:persisted? => !!stubs[primary_key]}.merge(stubs)
265+
stubs = { primary_key => next_id }.merge(stubs)
266+
stubs = { :persisted? => !!stubs[primary_key] }.merge(stubs)
262267
else
263-
stubs = {:id => next_id}.merge(stubs)
264-
stubs = {:persisted? => !!stubs[:id]}.merge(stubs)
268+
stubs = { :id => next_id }.merge(stubs)
269+
stubs = { :persisted? => !!stubs[:id] }.merge(stubs)
265270
end
266-
stubs = {:blank? => false}.merge(stubs)
271+
stubs = { :blank? => false }.merge(stubs)
267272

268273
stubs.each do |message, return_value|
269274
if m.respond_to?("#{message}=")
@@ -281,14 +286,13 @@ def stub_model(model_class, stubs={})
281286
end
282287
end
283288

284-
private
289+
private
285290

286291
@@model_id = 1000
287292

288293
def next_id
289294
@@model_id += 1
290295
end
291-
292296
end
293297
end
294298

lib/rspec/active_model/mocks/version.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
module RSpec
23
module ActiveModel
34
module Mocks

0 commit comments

Comments
 (0)