Skip to content

Commit

Permalink
Merge pull request #58 from rspec/cleanup
Browse files Browse the repository at this point in the history
Cleanup from adding rubocop
  • Loading branch information
JonRowe authored Dec 10, 2023
2 parents ae7d3db + f6df198 commit ac34412
Show file tree
Hide file tree
Showing 23 changed files with 122 additions and 87 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
source "https://rubygems.org"

gemspec
Expand Down
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require "bundler"
begin
Bundler.setup
Expand All @@ -20,14 +21,14 @@ Cucumber::Rake::Task.new(:cucumber)

namespace :generate do
desc "generate a fresh app with rspec installed"
task :sample do |t|
task :sample do |_t|
unless File.directory?('./tmp/sample')
bindir = File.expand_path("bin")

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

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

desc "Push docs/cukes to relishapp using the relish-client-gem"
task :relish, :version do |t, args|
task :relish, :version do |_t, args|
raise "rake relish[VERSION]" unless args[:version]

sh "cp Changelog.md features/"
if `relish versions rspec/rspec-activemodel-mocks`.split.map(&:strip).include? args[:version]
puts "Version #{args[:version]} already exists"
Expand Down
7 changes: 4 additions & 3 deletions features/step_definitions/additional_cli_steps.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Then /^the example(s)? should( all)? pass$/ do |_, _|
step %q{the output should contain "0 failures"}
step %q{the exit status should be 0}
# frozen_string_literal: true
Then(/^the example(s)? should( all)? pass$/) do |_, _|
step 'the output should contain "0 failures"'
step 'the exit status should be 0'
end
3 changes: 0 additions & 3 deletions features/step_definitions/model_steps.rb

This file was deleted.

9 changes: 5 additions & 4 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'aruba/cucumber'

module ArubaExt
Expand All @@ -17,7 +18,7 @@ def run(cmd)
end

def aruba_path(file_or_dir)
File.expand_path("../../../#{file_or_dir.sub('sample','aruba')}", __FILE__)
File.expand_path("../../../#{file_or_dir.sub('sample', 'aruba')}", __FILE__)
end

def sample_path(file_or_dir)
Expand All @@ -37,12 +38,12 @@ def copy(file_or_dir)
end

Before do
steps %Q{
steps %(
Given a directory named "spec"
}
)

Dir['tmp/sample/*'].each do |file_or_dir|
if !(file_or_dir =~ /spec$/)
if file_or_dir !~ /spec$/
write_symlink(file_or_dir)
end
end
Expand Down
5 changes: 3 additions & 2 deletions features/support/rubinius.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
# Required until https://github.com/rubinius/rubinius/issues/2430 is resolved
ENV['RBXOPT'] = "#{ENV["RBXOPT"]} -Xcompiler.no_rbc"
ENV['RBXOPT'] = "#{ENV.fetch("RBXOPT", nil)} -Xcompiler.no_rbc"

Around "@unsupported-on-rbx" do |scenario, block|
Around "@unsupported-on-rbx" do |_scenario, block|
block.call unless defined?(Rubinius)
end
1 change: 1 addition & 0 deletions lib/rspec-activemodel-mocks.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# frozen_string_literal: true
require 'rspec/active_model/mocks'
3 changes: 2 additions & 1 deletion lib/rspec/active_model/mocks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require 'rspec/core'

RSpec::configure do |c|
RSpec.configure do |c|
c.backtrace_exclusion_patterns << /lib\/rspec\/active_model\/mocks/
end

Expand Down
74 changes: 39 additions & 35 deletions lib/rspec/active_model/mocks/mocks.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true
require 'active_support'
require 'active_support/core_ext/object/to_param'
require 'active_model'

module RSpec::ActiveModel::Mocks
class IllegalDataAccessException < StandardError; end
module Mocks

module ActiveModelInstanceMethods
# Stubs `persisted?` to return false and `id` to return nil
# @return self
Expand Down Expand Up @@ -91,12 +91,13 @@ def association(association_name)
# * A String representing a Class that does not exist
# * A String representing a Class that extends ActiveModel::Naming
# * A Class that extends ActiveModel::Naming
def mock_model(string_or_model_class, stubs = {})
def mock_model(string_or_model_class, stubs={})
if String === string_or_model_class
if Object.const_defined?(string_or_model_class)
model_class = Object.const_get(string_or_model_class)
else
model_class = Object.const_set(string_or_model_class, Class.new do
# rubocop:disable Style/SingleLineMethods
extend ::ActiveModel::Naming
def self.primary_key; :id; end

Expand All @@ -106,32 +107,33 @@ def self._reflect_on_association(_other); nil; end
def self.composite_primary_key?; false; end
def self.has_query_constraints?; false; end
def self.param_delimiter; "-"; end
# rubocop:enable Style/SingleLineMethods
end)
end
else
model_class = string_or_model_class
end

unless model_class.kind_of? ::ActiveModel::Naming
raise ArgumentError.new <<-EOM
raise ArgumentError, <<-EOM
The mock_model method can only accept as its first argument:
* A String representing a Class that does not exist
* A String representing a Class that extends ActiveModel::Naming
* A Class that extends ActiveModel::Naming
It received #{model_class.inspect}
EOM
EOM
end

stubs = {:id => next_id}.merge(stubs)
stubs = {:persisted? => !!stubs[:id],
:destroyed? => false,
:marked_for_destruction? => false,
:valid? => true,
:blank? => false}.merge(stubs)
stubs = { :id => next_id }.merge(stubs)
stubs = { :persisted? => !!stubs[:id],
:destroyed? => false,
:marked_for_destruction? => false,
:valid? => true,
:blank? => false }.merge(stubs)

double("#{model_class.name}_#{stubs[:id]}", stubs).tap do |m|
if model_class.method(:===).owner == Module && !stubs.has_key?(:===)
if model_class.method(:===).owner == Module && !stubs.key?(:===)
allow(model_class).to receive(:===).and_wrap_original do |original, other|
m === other || original.call(other)
end
Expand All @@ -143,50 +145,52 @@ def self.param_delimiter; "-"; end
include ActiveModel::Conversion
include ActiveModel::Validations
end
if defined?(ActiveRecord)
if stubs.values_at(:save, :update_attributes, :update).include?(false)
RSpec::Mocks.allow_message(m.errors, :empty?).and_return(false)
RSpec::Mocks.allow_message(m.errors, :blank?).and_return(false)
end
if defined?(ActiveRecord) && stubs.values_at(:save, :update_attributes, :update).include?(false)
RSpec::Mocks.allow_message(m.errors, :empty?).and_return(false)
RSpec::Mocks.allow_message(m.errors, :blank?).and_return(false)
end

msingleton.__send__(:define_method, :is_a?) do |other|
model_class.ancestors.include?(other)
end unless stubs.has_key?(:is_a?)
end unless stubs.key?(:is_a?)

msingleton.__send__(:define_method, :kind_of?) do |other|
model_class.ancestors.include?(other)
end unless stubs.has_key?(:kind_of?)
end unless stubs.key?(:kind_of?)

msingleton.__send__(:define_method, :instance_of?) do |other|
other == model_class
end unless stubs.has_key?(:instance_of?)
end unless stubs.key?(:instance_of?)

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

msingleton.__send__(:define_method, :has_attribute?) do |attr_name|
__model_class_has_column?(attr_name)
end unless stubs.has_key?(:has_attribute?)
end unless stubs.key?(:has_attribute?)

msingleton.__send__(:define_method, :respond_to?) do |method_name, *args|
include_private = args.first || false
include_private = args.first || false
__model_class_has_column?(method_name) ? true : super(method_name, include_private)
end unless stubs.has_key?(:respond_to?)
end unless stubs.key?(:respond_to?)

msingleton.__send__(:define_method, :method_missing) do |m, *a, &b|
respond_to?(m) ? null_object? ? self : nil : super(m, *a, &b)
msingleton.__send__(:define_method, :method_missing) do |missing_m, *a, &b|
if respond_to?(missing_m)
null_object? ? self : nil
else
super(missing_m, *a, &b)
end
end

msingleton.__send__(:define_method, :class) do
model_class
end unless stubs.has_key?(:class)
end unless stubs.key?(:class)

mock_param = to_param
msingleton.__send__(:define_method, :to_s) do
"#{model_class.name}_#{mock_param}"
end unless stubs.has_key?(:to_s)
end unless stubs.key?(:to_s)
yield m if block_given?
end
end
Expand All @@ -208,7 +212,7 @@ def persisted?
module ActiveRecordStubExtensions
# Stubs `id` (or other primary key method) to return nil
def as_new_record
self.__send__("#{self.class.primary_key}=", nil)
__send__("#{self.class.primary_key}=", nil)
super
end

Expand All @@ -220,7 +224,8 @@ def new_record?
# Raises an IllegalDataAccessException (stubbed models are not allowed to access the database)
# @raises IllegalDataAccessException
def connection
raise RSpec::ActiveModel::Mocks::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
raise RSpec::ActiveModel::Mocks::IllegalDataAccessException,
"stubbed models are not allowed to access the database"
end
end

Expand Down Expand Up @@ -257,13 +262,13 @@ def stub_model(model_class, stubs={})
if defined?(ActiveRecord) && model_class < ActiveRecord::Base && model_class.primary_key
m.extend ActiveRecordStubExtensions
primary_key = model_class.primary_key.to_sym
stubs = {primary_key => next_id}.merge(stubs)
stubs = {:persisted? => !!stubs[primary_key]}.merge(stubs)
stubs = { primary_key => next_id }.merge(stubs)
stubs = { :persisted? => !!stubs[primary_key] }.merge(stubs)
else
stubs = {:id => next_id}.merge(stubs)
stubs = {:persisted? => !!stubs[:id]}.merge(stubs)
stubs = { :id => next_id }.merge(stubs)
stubs = { :persisted? => !!stubs[:id] }.merge(stubs)
end
stubs = {:blank? => false}.merge(stubs)
stubs = { :blank? => false }.merge(stubs)

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

private
private

@@model_id = 1000

def next_id
@@model_id += 1
end

end
end

Expand Down
1 change: 1 addition & 0 deletions lib/rspec/active_model/mocks/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module RSpec
module ActiveModel
module Mocks
Expand Down
21 changes: 11 additions & 10 deletions rspec-activemodel-mocks.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding: utf-8 -*-
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
# frozen_string_literal: true
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
require 'rspec/active_model/mocks/version'

Gem::Specification.new do |s|
Expand All @@ -14,10 +14,11 @@ Gem::Specification.new do |s|
s.description = "RSpec test doubles for ActiveModel and ActiveRecord"

s.metadata = {
'bug_tracker_uri' => 'https://github.com/rspec/rspec-activemodel-mocks/issues',
'bug_tracker_uri' => 'https://github.com/rspec/rspec-activemodel-mocks/issues',
'documentation_uri' => 'https://rspec.info/documentation/',
'mailing_list_uri' => 'https://groups.google.com/forum/#!forum/rspec',
'source_code_uri' => 'https://github.com/rspec/rspec-activemodel-mocks',
'mailing_list_uri' => 'https://groups.google.com/forum/#!forum/rspec',
'source_code_uri' => 'https://github.com/rspec/rspec-activemodel-mocks',
'rubygems_mfa_required' => 'true'
}

s.files = `git ls-files -- lib/*`.split("\n")
Expand All @@ -32,9 +33,9 @@ Gem::Specification.new do |s|
s.cert_chain = [File.expand_path('~/.gem/rspec-gem-public_cert.pem')]
end

s.add_runtime_dependency(%q<activesupport>, [">= 3.0"])
s.add_runtime_dependency(%q<activemodel>, [">= 3.0"])
s.add_runtime_dependency(%q<rspec-mocks>, [">= 2.99", "< 4.0"])
s.add_runtime_dependency('activemodel', [">= 3.0"])
s.add_runtime_dependency('activesupport', [">= 3.0"])
s.add_runtime_dependency('rspec-mocks', [">= 2.99", "< 4.0"])

if RUBY_VERSION.to_f < 1.9 || RUBY_VERSION == '1.9.2'
s.add_development_dependency "rake", "~> 10.0.0"
Expand All @@ -46,8 +47,8 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake", "~> 13.0.0"
end

s.add_development_dependency 'cucumber', '>= 1.3'
s.add_development_dependency('activerecord', [">= 3.0"])
s.add_development_dependency 'aruba', '~> 0.4.11'
s.add_development_dependency 'cucumber', '>= 1.3'
s.add_development_dependency 'ZenTest', '~> 4.11.2'
s.add_development_dependency(%q<activerecord>, [">= 3.0"])
end
Loading

0 comments on commit ac34412

Please sign in to comment.