Class: Assert::Test

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Options

included

Constructor Details

#initialize(name, suite_context_info, code = nil, &block) ⇒ Test

Returns a new instance of Test.



25
26
27
28
29
30
31
# File 'lib/assert/test.rb', line 25

def initialize(name, suite_context_info, code = nil, &block)
  @context_info = suite_context_info
  @name = name_from_context(name)
  @code = (code || block)
  @results = ResultSet.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.



22
23
24
# File 'lib/assert/test.rb', line 22

def code
  @code
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.



22
23
24
# File 'lib/assert/test.rb', line 22

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.



22
23
24
# File 'lib/assert/test.rb', line 22

def name
  @name
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

#resultsObject

Returns the value of attribute results.



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

def results
  @results
end

Class Method Details

.halt_on_fail?Boolean

Returns:

  • (Boolean)


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

def self.halt_on_fail?
  ENV['halt_on_fail'] == 'true' || self.options.halt_on_fail
end

Instance Method Details

#<=>(other_test) ⇒ Object



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

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

#context_classObject



33
34
35
# File 'lib/assert/test.rb', line 33

def context_class
  self.context_info.klass
end

#inspectObject



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

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

#result_count(type = nil) ⇒ Object



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

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



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

def run(&result_callback)
  # setup the a new test run
  @results = ResultSet.new(result_callback)
  run_scope = self.context_class.new(self)

  # 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 Exception => err
    @results << Result::Error.new(self, err)
  ensure
    begin
      run_test_teardown(run_scope)
    rescue Exception => teardown_err
      @results << Result::Error.new(self, teardown_err)
    end
  end
  # return the results of the test run
  @results
end