Skip to content

Commit

Permalink
For "fun", I defined a rubocop config that best represents "Seattle S…
Browse files Browse the repository at this point in the history
…tyle". 14 issues later (https://github.com/bbatsov/rubocop/issues/created_by/zenspider) I wound up with the following:

Update all old single quoted strings to double quoted.
Get rid of some Really::Old::Coding::Styles().
Update for minor consistency issues.
Found and fixed some odd indentation issues
Marked more named args as optional by prefixing with _
Switched all %x array thingies to use square brackets
Swiched from Hash#has_key? to #key? because it is deprecated.
and probably more.

Was it worth my time? Probably not, but it was an interesting experiment.

[git-p4: depot-paths = "//src/minitest/dev/": change = 9952]
  • Loading branch information
zenspider committed Jan 27, 2015
1 parent 0ddacc6 commit e8d8c2a
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 195 deletions.
18 changes: 9 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# -*- ruby -*-

require 'rubygems'
require 'hoe'
require "rubygems"
require "hoe"

Hoe.plugin :seattlerb

Hoe.spec 'minitest' do
developer 'Ryan Davis', '[email protected]'
Hoe.spec "minitest" do
developer "Ryan Davis", "[email protected]"

license "MIT"

Expand All @@ -25,12 +25,12 @@ task :specs do

map = {
/(must_throw)s/ => '\1',
/(?!not)_same/ => '_be_same_as',
/_in_/ => '_be_within_',
/_operator/ => '_be',
/_includes/ => '_include',
/(?!not)_same/ => "_be_same_as",
/_in_/ => "_be_within_",
/_operator/ => "_be",
/_includes/ => "_include",
/(must|wont)_(.*_of|nil|silent|empty)/ => '\1_be_\2',
/must_raises/ => 'must_raise',
/must_raises/ => "must_raise",
}

expectations = Minitest::Expectations.public_instance_methods.map(&:to_s)
Expand Down
12 changes: 6 additions & 6 deletions lib/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Minitest
# Parallel test executor

mc.send :attr_accessor, :parallel_executor
self.parallel_executor = Parallel::Executor.new((ENV['N'] || 2).to_i)
self.parallel_executor = Parallel::Executor.new((ENV["N"] || 2).to_i)

##
# Filter object for backtraces.
Expand Down Expand Up @@ -175,13 +175,13 @@ def self.process_args args = [] # :nodoc:
options[:verbose] = true
end

opts.on "-n", "--name PATTERN","Filter run on /pattern/ or string." do |a|
opts.on "-n", "--name PATTERN", "Filter run on /regexp/ or string." do |a|
options[:filter] = a
end

unless extensions.empty?
opts.separator ""
opts.separator "Known extensions: #{extensions.join(', ')}"
opts.separator "Known extensions: #{extensions.join(", ")}"

extensions.each do |meth|
msg = "plugin_#{meth}_options"
Expand Down Expand Up @@ -277,8 +277,8 @@ def self.reset # :nodoc:
# reporter to record.

def self.run reporter, options = {}
filter = options[:filter] || '/./'
filter = Regexp.new $1 if filter =~ /\/(.*)\//
filter = options[:filter] || "/./"
filter = Regexp.new $1 if filter =~ %r%/(.*)/%

filtered_methods = self.runnable_methods.find_all { |m|
filter === m || filter === "#{self}##{m}"
Expand Down Expand Up @@ -692,7 +692,7 @@ def error # :nodoc:
end

def message # :nodoc:
bt = Minitest::filter_backtrace(self.backtrace).join "\n "
bt = Minitest.filter_backtrace(self.backtrace).join "\n "
"#{self.exception.class}: #{self.exception.message}\n #{bt}"
end

Expand Down
45 changes: 22 additions & 23 deletions lib/minitest/assertions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rbconfig"
require "tempfile"
require 'stringio'
require "stringio"

module Minitest
##
Expand All @@ -25,7 +25,7 @@ def UNDEFINED.inspect # :nodoc:
# figure out what diff to use.

def self.diff
@diff = if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ &&
@diff = if (RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ &&
system("diff.exe", __FILE__, __FILE__)) then
"diff.exe -u"
elsif Minitest::Test.maglev? then
Expand Down Expand Up @@ -67,7 +67,6 @@ def diff exp, act
expect == butwas) &&
Minitest::Assertions.diff


return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless
need_to_diff

Expand Down Expand Up @@ -116,7 +115,7 @@ def mu_pp obj
# uses mu_pp to do the first pass and then cleans it up.

def mu_pp_for_diff obj
mu_pp(obj).gsub(/\\n/, "\n").gsub(/:0x[a-fA-F0-9]{4,}/m, ':0xXXXXXX')
mu_pp(obj).gsub(/\\n/, "\n").gsub(/:0x[a-fA-F0-9]{4,}/m, ":0xXXXXXX")
end

##
Expand Down Expand Up @@ -435,25 +434,25 @@ def capture_io
def capture_subprocess_io
_synchronize do
begin
require 'tempfile'
require "tempfile"

captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err")
captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err")

orig_stdout, orig_stderr = $stdout.dup, $stderr.dup
$stdout.reopen captured_stdout
$stderr.reopen captured_stderr
orig_stdout, orig_stderr = $stdout.dup, $stderr.dup
$stdout.reopen captured_stdout
$stderr.reopen captured_stderr

yield
yield

$stdout.rewind
$stderr.rewind
$stdout.rewind
$stderr.rewind

return captured_stdout.read, captured_stderr.read
ensure
captured_stdout.unlink
captured_stderr.unlink
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr
return captured_stdout.read, captured_stderr.read
ensure
captured_stdout.unlink
captured_stderr.unlink
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr
end
end
end
Expand All @@ -467,7 +466,7 @@ def exception_details e, msg
"Class: <#{e.class}>",
"Message: <#{e.message.inspect}>",
"---Backtrace---",
"#{Minitest::filter_backtrace(e.backtrace).join("\n")}",
"#{Minitest.filter_backtrace(e.backtrace).join("\n")}",
"---------------",
].join "\n"
end
Expand All @@ -494,7 +493,7 @@ def message msg = nil, ending = nil, &default
##
# used for counting assertions

def pass msg = nil
def pass _msg = nil
assert true
end

Expand All @@ -503,7 +502,7 @@ def pass msg = nil

def refute test, msg = nil
msg ||= "Failed refutation, no message given"
not assert(! test, msg)
not assert !test, msg
end

##
Expand Down Expand Up @@ -581,7 +580,7 @@ def refute_kind_of cls, obj, msg = nil
# Fails if +matcher+ <tt>=~</tt> +obj+.

def refute_match matcher, obj, msg = nil
msg = message(msg) {"Expected #{mu_pp matcher} to not match #{mu_pp obj}"}
msg = message(msg) { "Expected #{mu_pp matcher} to not match #{mu_pp obj}" }
assert_respond_to matcher, :"=~"
matcher = Regexp.new Regexp.escape matcher if String === matcher
refute matcher =~ obj, msg
Expand All @@ -603,7 +602,7 @@ def refute_nil obj, msg = nil

def refute_operator o1, op, o2 = UNDEFINED, msg = nil
return refute_predicate o1, op, msg if UNDEFINED == o2
msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}"}
msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}" }
refute o1.__send__(op, o2), msg
end

Expand Down
29 changes: 14 additions & 15 deletions lib/minitest/benchmark.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'minitest/unit'
require 'minitest/spec'
require "minitest/unit"
require "minitest/spec"

module Minitest
##
Expand Down Expand Up @@ -221,8 +221,8 @@ def assert_performance_power threshold = 0.99, &work
# See: http://en.wikipedia.org/wiki/Coefficient_of_determination

def fit_error xys
y_bar = sigma(xys) { |x, y| y } / xys.size.to_f
ss_tot = sigma(xys) { |x, y| (y - y_bar) ** 2 }
y_bar = sigma(xys) { |_, y| y } / xys.size.to_f
ss_tot = sigma(xys) { |_, y| (y - y_bar) ** 2 }
ss_err = sigma(xys) { |x, y| (yield(x) - y) ** 2 }

1 - (ss_err / ss_tot)
Expand All @@ -238,9 +238,9 @@ def fit_error xys
def fit_exponential xs, ys
n = xs.size
xys = xs.zip(ys)
sxlny = sigma(xys) { |x,y| x * Math.log(y) }
slny = sigma(xys) { |x,y| Math.log(y) }
sx2 = sigma(xys) { |x,y| x * x }
sxlny = sigma(xys) { |x, y| x * Math.log(y) }
slny = sigma(xys) { |_, y| Math.log(y) }
sx2 = sigma(xys) { |x, _| x * x }
sx = sigma xs

c = n * sx2 - sx ** 2
Expand All @@ -260,10 +260,10 @@ def fit_exponential xs, ys
def fit_logarithmic xs, ys
n = xs.size
xys = xs.zip(ys)
slnx2 = sigma(xys) { |x,y| Math.log(x) ** 2 }
slnx = sigma(xys) { |x,y| Math.log(x) }
sylnx = sigma(xys) { |x,y| y * Math.log(x) }
sy = sigma(xys) { |x,y| y }
slnx2 = sigma(xys) { |x, _| Math.log(x) ** 2 }
slnx = sigma(xys) { |x, _| Math.log(x) }
sylnx = sigma(xys) { |x, y| y * Math.log(x) }
sy = sigma(xys) { |_, y| y }

c = n * slnx2 - slnx ** 2
b = ( n * sylnx - sy * slnx ) / c
Expand All @@ -272,7 +272,6 @@ def fit_logarithmic xs, ys
return a, b, fit_error(xys) { |x| a + b * Math.log(x) }
end


##
# Fits the functional form: a + bx.
#
Expand All @@ -286,7 +285,7 @@ def fit_linear xs, ys
sx = sigma xs
sy = sigma ys
sx2 = sigma(xs) { |x| x ** 2 }
sxy = sigma(xys) { |x,y| x * y }
sxy = sigma(xys) { |x, y| x * y }

c = n * sx2 - sx**2
a = (sy * sx2 - sx * sxy) / c
Expand All @@ -310,7 +309,7 @@ def fit_power xs, ys
slny = sigma(ys) { | y| Math.log(y) }
slnx2 = sigma(xs) { |x | Math.log(x) ** 2 }

b = (n * slnxlny - slnx * slny) / (n * slnx2 - slnx ** 2);
b = (n * slnxlny - slnx * slny) / (n * slnx2 - slnx ** 2)
a = (slny - b * slnx) / n

return Math.exp(a), b, fit_error(xys) { |x| (Math.exp(a) * (x ** b)) }
Expand Down Expand Up @@ -357,7 +356,7 @@ class BenchSpec < Benchmark
# See ::bench_performance_linear for an example of how to use this.

def self.bench name, &block
define_method "bench_#{name.gsub(/\W+/, '_')}", &block
define_method "bench_#{name.gsub(/\W+/, "_")}", &block
end

##
Expand Down
32 changes: 15 additions & 17 deletions lib/minitest/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ module Minitest # :nodoc:
class Mock
alias :__respond_to? :respond_to?

overridden_methods = %w(
overridden_methods = %w[
===
inspect
object_id
public_send
respond_to_missing?
send
to_s
)
]

instance_methods.each do |m|
undef_method m unless overridden_methods.include?(m.to_s) || m =~ /^__/
end

overridden_methods.map(&:to_sym).each do |method_id|
define_method method_id do |*args, &b|
if @expected_calls.has_key? method_id then
if @expected_calls.key? method_id then
method_missing(method_id, *args, &b)
else
super(*args, &b)
Expand Down Expand Up @@ -64,7 +64,7 @@ def initialize # :nodoc:
# @mock.uses_one_string("bar") # => true
# @mock.verify # => raises MockExpectationError

def expect(name, retval, args=[], &blk)
def expect(name, retval, args = [], &blk)
name = name.to_sym

if block_given?
Expand Down Expand Up @@ -98,19 +98,19 @@ def verify
msg2 = "#{msg1}, got [#{__call name, @actual_calls[name]}]"

raise MockExpectationError, msg2 if
@actual_calls.has_key?(name) and
@actual_calls.key?(name) and
not @actual_calls[name].include?(expected)

raise MockExpectationError, msg1 unless
@actual_calls.has_key?(name) and
@actual_calls.key?(name) and
@actual_calls[name].include?(expected)
end
end
true
end

def method_missing(sym, *args, &block) # :nodoc:
unless @expected_calls.has_key?(sym) then
unless @expected_calls.key?(sym) then
raise NoMethodError, "unmocked method %p, expected one of %p" %
[sym, @expected_calls.keys.sort_by(&:to_s)]
end
Expand Down Expand Up @@ -152,15 +152,15 @@ def method_missing(sym, *args, &block) # :nodoc:

@actual_calls[sym] << {
:retval => retval,
:args => expected_args.zip(args).map { |mod, a| mod === a ? mod : a }
:args => expected_args.zip(args).map { |mod, a| mod === a ? mod : a },
}

retval
end

def respond_to?(sym, include_private = false) # :nodoc:
return true if @expected_calls.has_key? sym.to_sym
return __respond_to?(sym, include_private)
return true if @expected_calls.key? sym.to_sym
__respond_to?(sym, include_private)
end
end
end
Expand All @@ -185,7 +185,7 @@ class Object # :nodoc:
# end
#

def stub name, val_or_callable, *block_args, &block
def stub name, val_or_callable, *block_args
new_name = "__minitest_stub__#{name}"

metaclass = class << self; self; end
Expand All @@ -199,12 +199,11 @@ def stub name, val_or_callable, *block_args, &block
metaclass.send :alias_method, new_name, name

metaclass.send :define_method, name do |*args, &blk|

ret = if val_or_callable.respond_to? :call then
val_or_callable.call(*args)
else
val_or_callable
end
val_or_callable.call(*args)
else
val_or_callable
end

blk.call(*block_args) if blk

Expand All @@ -217,5 +216,4 @@ def stub name, val_or_callable, *block_args, &block
metaclass.send :alias_method, name, new_name
metaclass.send :undef_method, new_name
end

end
Loading

0 comments on commit e8d8c2a

Please sign in to comment.