Class: Tunit::Test

Inherits:
Runnable show all
Includes:
Assertions, Hooks
Defined in:
lib/tunit/test.rb

Direct Known Subclasses

Spec

Constant Summary collapse

PREFIX =
/^test_/

Instance Attribute Summary

Attributes inherited from Runnable

#assertions, #failures, #name, #time

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks

#setup, #teardown

Methods included from Assertions

#assert, #assert_equal, #assert_includes, #assert_instance_of, #assert_respond_to, #refute, #refute_equal, #refute_includes, #refute_instance_of, #refute_respond_to, #skip

Methods inherited from Runnable

inherited, #initialize, run, runnables, runnables=

Constructor Details

This class inherits a constructor from Tunit::Runnable

Class Method Details

.order!Object



29
30
31
32
33
34
# File 'lib/tunit/test.rb', line 29

def self.order!
  class << self
    undef_method :test_order if method_defined?(:test_order)
    define_method(:test_order) { :alpha }
  end
end

.runnable_methodsObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tunit/test.rb', line 11

def self.runnable_methods
  methods = methods_matching PREFIX
  case self.test_order
  when :random
    max = methods.size
    methods.sort.sort_by { rand max }
  when :alpha
    methods.sort
  else
    raise "Unknown test_order: #{self.test_order.inspect}"
  end
end

.test_orderObject

Randomize tests by default



25
26
27
# File 'lib/tunit/test.rb', line 25

def self.test_order
  :random
end

Instance Method Details

#codeObject



72
73
74
# File 'lib/tunit/test.rb', line 72

def code
  failure && failure.result_code || "."
end

#failureObject



68
69
70
# File 'lib/tunit/test.rb', line 68

def failure
  self.failures.first
end

#locationObject



76
77
78
79
# File 'lib/tunit/test.rb', line 76

def location
  loc = " [#{self.failure.location}]" unless passed?
  "#{self.class}##{self.name}#{loc}"
end

#passed?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/tunit/test.rb', line 60

def passed?
  !failure
end

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tunit/test.rb', line 36

def run
  capture_exceptions do
    time_it do
      setup
      send name
    end

    if self.assertions.zero?
      begin
        fail ::Tunit::Empty, "Empty test, '#{self.to_s}'"
      rescue ::Tunit::Empty => e
        method_obj = self.method(name)
        e.location = method_obj.source_location.join(":")
        raise e
      end
    end
  end

  capture_exceptions do
    teardown
  end
  self
end

#skipped?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/tunit/test.rb', line 64

def skipped?
  failure && Skip === failure
end

#to_sObject



81
82
83
84
85
86
87
# File 'lib/tunit/test.rb', line 81

def to_s
  return location if passed? && !skipped?

  failures.map { |failure|
    "#{failure.result_label}:\n#{self.location}:\n#{failure.message}\n"
  }.join "\n"
end