Class: Assert::Test

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, suite_ci, config, opts = nil, &block) ⇒ Test

Returns a new instance of Test.



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

def initialize(name, suite_ci, config, opts = nil, &block)
  @context_info = suite_ci
  @name, @config = name_from_context(name), config

  o = opts || {}
  @code = o[:code] || block || Proc.new{}

  @results = Result::Set.new
  @output  = ""
end

Instance Attribute Details

#codeObject (readonly)

a Test is some code/method to run in the scope of a Context. After a a test runs, it should have some assertions which are its results.



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

def code
  @code
end

#configObject (readonly)

a Test is some code/method to run in the scope of a Context. After a a test runs, it should have some assertions which are its results.



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

def config
  @config
end

#context_infoObject (readonly)

a Test is some code/method to run in the scope of a Context. After a a test runs, it should have some assertions which are its results.



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

def context_info
  @context_info
end

#nameObject (readonly)

a Test is some code/method to run in the scope of a Context. After a a test runs, it should have some assertions which are its results.



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

def name
  @name
end

#outputObject

Returns the value of attribute output.



11
12
13
# File 'lib/assert/test.rb', line 11

def output
  @output
end

#resultsObject

Returns the value of attribute results.



11
12
13
# File 'lib/assert/test.rb', line 11

def results
  @results
end

Instance Method Details

#<=>(other_test) ⇒ Object



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

def <=>(other_test)
  self.name <=> other_test.name
end

#context_classObject



24
25
26
# File 'lib/assert/test.rb', line 24

def context_class
  self.context_info.klass
end

#inspectObject



80
81
82
83
84
85
# File 'lib/assert/test.rb', line 80

def inspect
  attributes_string = ([ :name, :context_info, :results ].collect do |attr|
    "@#{attr}=#{self.send(attr).inspect}"
  end).join(" ")
  "#<#{self.class}:#{'0x0%x' % (object_id << 1)} #{attributes_string}>"
end

#result_count(type = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/assert/test.rb', line 68

def result_count(type=nil)
  if Assert::Result.types.include?(type)
    self.send("#{type}_results").size
  else
    @results.size
  end
end

#run(&result_callback) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/assert/test.rb', line 28

def run(&result_callback)
  # setup the a new test run
  @results = Result::Set.new(result_callback)
  run_scope = self.context_class.new(self, self.config)

  # run the test, capturing its output
  begin
    run_test_setup(run_scope)
    run_test_code(run_scope)
  rescue Result::TestFailure => err
    @results << Result::Fail.new(self, err)
  rescue Result::TestSkipped => err
    @results << Result::Skip.new(self, err)
  rescue SignalException => err
    raise(err)
  rescue Exception => err
    @results << Result::Error.new(self, err)
  ensure
    begin
      run_test_teardown(run_scope)
    rescue Result::TestFailure => err
      @results << Result::Fail.new(self, err)
    rescue Result::TestSkipped => err
      @results << Result::Skip.new(self, err)
    rescue SignalException => err
      raise(err)
    rescue Exception => teardown_err
      @results << Result::Error.new(self, teardown_err)
    end
  end
  # return the results of the test run
  @results
end