Class: MiniTest::Spec

Inherits:
Unit::TestCase show all
Defined in:
lib/minitest/spec.rb,
lib/minitest/benchmark.rb

Constant Summary collapse

@@describe_stack =
[]

Constants inherited from Unit::TestCase

Unit::TestCase::PASSTHROUGH_EXCEPTIONS, Unit::TestCase::SUPPORTS_INFO_SIGNAL

Instance Attribute Summary

Attributes inherited from Unit::TestCase

#__name__

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unit::TestCase

#assert_performance, #assert_performance_constant, #assert_performance_exponential, #assert_performance_linear, #assert_performance_power, bench_exp, bench_linear, benchmark_methods, benchmark_suites, #fit_error, #fit_exponential, #fit_linear, #fit_power, inherited, #io, #io?, #passed?, reset, #run, #setup, #sigma, #teardown, test_methods, test_order, test_suites, #validation_for_fit

Methods included from Assertions

#_assertions, #_assertions=, #assert, #assert_block, #assert_empty, #assert_equal, #assert_in_delta, #assert_in_epsilon, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_operator, #assert_output, #assert_raises, #assert_respond_to, #assert_same, #assert_send, #assert_silent, #assert_throws, #capture_io, #exception_details, #flunk, #message, #mu_pp, #pass, #refute, #refute_empty, #refute_equal, #refute_in_delta, #refute_in_epsilon, #refute_includes, #refute_instance_of, #refute_kind_of, #refute_match, #refute_nil, #refute_operator, #refute_respond_to, #refute_same, #skip

Constructor Details

#initialize(name) ⇒ Spec

:nodoc:



105
106
107
108
# File 'lib/minitest/spec.rb', line 105

def initialize name # :nodoc:
  super
  @@current_spec = self
end

Class Method Details

.after(type = :each, &block) ⇒ Object

Define an ‘after’ action. Inherits the way normal methods should.

NOTE: type is ignored and is only there to make porting easier.

Equivalent to MiniTest::Unit::TestCase#teardown.



144
145
146
147
# File 'lib/minitest/spec.rb', line 144

def self.after type = :each, &block
  raise "unsupported after type: #{type}" unless type == :each
  define_inheritable_method :teardown, &block
end

.before(type = :each, &block) ⇒ Object

Define a ‘before’ action. Inherits the way normal methods should.

NOTE: type is ignored and is only there to make porting easier.

Equivalent to MiniTest::Unit::TestCase#setup.



132
133
134
135
# File 'lib/minitest/spec.rb', line 132

def self.before type = :each, &block
  raise "unsupported before type: #{type}" unless type == :each
  define_inheritable_method :setup, &block
end

.bench(name, &block) ⇒ Object

This is used to define a new benchmark method. You usually don’t use this directly and is intended for those needing to write new performance curve fits (eg: you need a specific polynomial fit).

See ::bench_performance_linear for an example of how to use this.



305
306
307
# File 'lib/minitest/benchmark.rb', line 305

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

.bench_performance_constant(name, threshold = 0.99, &work) ⇒ Object

Create a benchmark that verifies that the performance is constant.

describe "my class" do
  bench_performance_constant "zoom_algorithm!" do
    @obj.zoom_algorithm!
  end
end


340
341
342
343
344
# File 'lib/minitest/benchmark.rb', line 340

def self.bench_performance_constant name, threshold = 0.99, &work
  bench name do
    assert_performance_constant threshold, &work
  end
end

.bench_performance_exponential(name, threshold = 0.99, &work) ⇒ Object

Create a benchmark that verifies that the performance is exponential.

describe "my class" do
  bench_performance_exponential "algorithm" do
    @obj.algorithm
  end
end


355
356
357
358
359
# File 'lib/minitest/benchmark.rb', line 355

def self.bench_performance_exponential name, threshold = 0.99, &work
  bench name do
    assert_performance_exponential threshold, &work
  end
end

.bench_performance_linear(name, threshold = 0.9, &work) ⇒ Object

Create a benchmark that verifies that the performance is linear.

describe "my class" do
  bench_performance_linear "fast_algorithm", 0.9999 do
    @obj.fast_algorithm
  end
end


325
326
327
328
329
# File 'lib/minitest/benchmark.rb', line 325

def self.bench_performance_linear name, threshold = 0.9, &work
  bench name do
    assert_performance_linear threshold, &work
  end
end

.bench_range(&block) ⇒ Object



309
310
311
312
313
314
# File 'lib/minitest/benchmark.rb', line 309

def self.bench_range &block
  return super unless block

  meta = (class << self; self; end)
  meta.send :define_method, "bench_range", &block
end

.currentObject

:nodoc:



101
102
103
# File 'lib/minitest/spec.rb', line 101

def self.current # :nodoc:
  @@current_spec
end

.define_inheritable_method(name, &block) ⇒ Object

:nodoc:



116
117
118
119
120
121
122
123
# File 'lib/minitest/spec.rb', line 116

def self.define_inheritable_method name, &block # :nodoc:
  super_method = self.superclass.instance_method name

  define_method name do
    super_method.bind(self).call if super_method # regular super() warns
    instance_eval(&block)
  end
end

.describe_stackObject

:nodoc:



97
98
99
# File 'lib/minitest/spec.rb', line 97

def self.describe_stack # :nodoc:
  @@describe_stack
end

.it(desc, &block) ⇒ Object

Define an expectation with name desc. Name gets morphed to a proper test method name. For some freakish reason, people who write specs don’t like class inheritence, so this goes way out of its way to make sure that expectations aren’t inherited.

Hint: If you do want inheritence, use minitest/unit. You can mix and match between assertions and expectations as much as you want.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/minitest/spec.rb', line 158

def self.it desc, &block
  block ||= proc { skip "(no tests defined)" }

  @specs ||= 0
  @specs += 1

  name = "test_%04d_%s" % [ @specs, desc.gsub(/\W+/, '_').downcase ]

  define_method name, &block

  classes(MiniTest::Spec).each do |mod|
    mod.send :undef_method, name if mod.respond_to? name
  end
end

.nuke_test_methods!Object

:nodoc:



110
111
112
113
114
# File 'lib/minitest/spec.rb', line 110

def self.nuke_test_methods! # :nodoc:
  self.public_instance_methods.grep(/^test_/).each do |name|
    self.send :undef_method, name
  end
end