Class: Testy::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/testy.rb

Defined Under Namespace

Classes: BadResult, OrderedHash, Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Test

Returns a new instance of Test.



46
47
48
49
50
51
# File 'lib/testy.rb', line 46

def initialize(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  @name = args.first || options[:name] || options['name']
  @tests = OrderedHash.new
  @block = block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



44
45
46
# File 'lib/testy.rb', line 44

def block
  @block
end

#nameObject

Returns the value of attribute name.



42
43
44
# File 'lib/testy.rb', line 42

def name
  @name
end

#testsObject

Returns the value of attribute tests.



43
44
45
# File 'lib/testy.rb', line 43

def tests
  @tests
end

Instance Method Details

#run(port = STDOUT) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/testy.rb', line 57

def run port = STDOUT
  instance_eval(&@block) if @block
  report = OrderedHash.new
  failures = 0
  tests.each do |name, block|
    result = Result.new
    report[name] =
      begin
        block.call(result)
        raise BadResult, name unless result.ok?
        {'success' => result.actual}
      rescue Object => e
        failures += 1
        failure = OrderedHash.new
        unless e.is_a?(BadResult)
          error = OrderedHash.new
          error['class'] = e.class.name
          error['message'] = e.message.to_s
          error['backtrace'] = e.backtrace||[]
          failure['error'] = error
        end
        failure['expect'] = result.expect
        failure['actual'] = result.actual
        {'failure' => failure}
      end
  end
  port << {name => report}.to_yaml
  failures
end

#test(name, &block) ⇒ Object



53
54
55
# File 'lib/testy.rb', line 53

def test(name, &block)
  @tests[name.to_s] = block
end