Class: Testy::Test

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

Defined Under Namespace

Classes: BadResult, Context, OrderedHash, Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Test

Returns a new instance of Test.



51
52
53
54
55
56
57
# File 'lib/testy.rb', line 51

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

Instance Attribute Details

#blockObject

Returns the value of attribute block.



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

def block
  @block
end

#contextsObject

Returns the value of attribute contexts.



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

def contexts
  @contexts
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#testsObject

Returns the value of attribute tests.



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

def tests
  @tests
end

Instance Method Details

#context(name = nil, &block) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/testy.rb', line 75

def context(name = nil, &block)
  if block
    @contexts[name] = Context.new(name, &block)
    block.call
  else
    @contexts.values.last
  end
end

#listObject



92
93
94
95
# File 'lib/testy.rb', line 92

def list
  instance_eval(&@block) if @block
  tests.map{|kv| kv.first.to_s}
end

#run(*args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/testy.rb', line 97

def run(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  port = options[:port]||options['port']||STDOUT
  selectors = options[:selectors]||options['selectors']||[]

  context do
    instance_eval(&@block) if @block

    report = OrderedHash.new
    failures = 0

    tests.each do |name, context_and_block|
      next unless selectors.any?{|selector| selector===name} unless selectors.empty?

      result = Result.new

      dup.instance_eval do
        context, block = context_and_block

        contexts = []
        tests.each do |n, cb|
          c, b = cb
          break if context==c
          contexts << c
        end
        contexts << context

        report[name] =
          begin
            value =
              begin
                contexts.each{|context| instance_eval(&context.setup)}
                (class << self;self;end).module_eval{ define_method(:call, &block) }
                call(result)
              ensure
                contexts.reverse.each{|context| instance_eval(&context.teardown)}
              end
            raise BadResult, name unless result.ok?
            value = result.actual.with_string_keys unless result.empty?
            begin; value.to_yaml; rescue; value=true; end
            {'success' => value}
          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.with_string_keys
            failure['actual'] = result.actual.with_string_keys
            {'failure' => failure}
          end
      end
    end
    port << {name => report}.to_yaml
    failures
  end
end

#setup(&block) ⇒ Object



84
85
86
# File 'lib/testy.rb', line 84

def setup(&block)
  context.setup = block
end

#sizeObject



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

def size
  @tests.size
end

#teardown(&block) ⇒ Object



88
89
90
# File 'lib/testy.rb', line 88

def teardown(&block)
  context.teardown = block
end

#test(name, &block) ⇒ Object



59
60
61
62
# File 'lib/testy.rb', line 59

def test(name, &block)
  name = [contexts.values.map{|context| context.name}, name].flatten.compact.join(' - ')
  @tests[name] = [context, block]
end