Module: Atto::Test

Extended by:
Test
Included in:
Test
Defined in:
lib/atto/test.rb

Overview

Tiny module for test driven development. Use with include Atto::Test.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.describe_testsObject

Describes the tests



71
72
73
# File 'lib/atto/test.rb', line 71

def self.describe_tests
  @@messages.join(".\n") + ".\n"
end

.resultsObject

Results of the tests



64
65
66
67
68
# File 'lib/atto/test.rb', line 64

def self.results 
  aid = @@dots.join + "\n" + @@failures.join("\n") + "\n"
  col = @@failures.empty? ? :green : :red
  aid << Atto::Ansi.color_string(col, "=" * 78)
end

Instance Method Details

#assert(msg = nil, file = nil, line = nil, delay = 10, stack = caller, &block) ⇒ Object

Test assertion.



52
53
54
55
56
57
58
59
60
61
# File 'lib/atto/test.rb', line 52

def assert(msg=nil, file=nil, line=nil, delay=10, stack=caller, &block)
  res, raised = run_block(delay, &block)
  @@dots     << make_dot(res, raised)
  failure     = make_failure(res, raised, msg, stack)
  @@failures << failure if failure
  if msg         
    @@messages << ( failure ?  msg + " (failed) " : msg)   
  end
  res
end

#format_exception(raised, msg = "") ⇒ Object

Formats an exception backtrace to a string.



21
22
23
# File 'lib/atto/test.rb', line 21

def format_exception(raised, msg = "")
  return msg + raised.to_s + "\n" + raised.backtrace.join("\n")
end

#format_failure(message, stack = caller) ⇒ Object

Formats a message that reporst on any test failiures



15
16
17
18
# File 'lib/atto/test.rb', line 15

def format_failure(message, stack = caller)
  # file, line = 
  return "(%s:%0.3d) %s" % [*parse_stack(stack), message]
end

#make_dot(res, raised) ⇒ Object

Makes a dot for the given raised and result state



36
37
38
# File 'lib/atto/test.rb', line 36

def make_dot(res, raised)
  return res ? '.' : ( raised ? 'E' : 'F')
end

#make_failure(res, raised, msg, stack) ⇒ Object

Creates a failiure for the given raised and result state Returns nil if no failure



27
28
29
30
31
32
33
# File 'lib/atto/test.rb', line 27

def make_failure(res, raised, msg, stack)
  return nil if res
  if raised
    return format_failure(msg || format_exception(raised, "Exception!\n"), stack)
  end       
  return format_failure(msg || "Assertion failed!", stack)
end

#parse_stack(stack = caller) ⇒ Object

Returns the file and line of the calling context



10
11
12
# File 'lib/atto/test.rb', line 10

def parse_stack(stack=caller)
  return *(stack.first.match(/(.*):(\d+)/)[1..2])
end

#run_block(delay = 10, &block) ⇒ Object

Calls a block safely with a timeout



41
42
43
44
45
46
47
48
49
# File 'lib/atto/test.rb', line 41

def run_block(delay = 10, &block)
   res, raised = nil, nil
   begin
     Timeout.timeout(delay) do res = block.call ; end
   rescue 
     raised = $!
   end
   return res, raised 
end