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_context_info, code = nil, &block) ⇒ Test

Returns a new instance of Test.



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

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

#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



73
74
75
# File 'lib/assert/test.rb', line 73

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

#context_classObject



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

def context_class
  self.context_info.klass
end

#inspectObject



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

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



65
66
67
68
69
70
71
# File 'lib/assert/test.rb', line 65

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



25
26
27
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
# File 'lib/assert/test.rb', line 25

def run(&result_callback)
  # setup the a new test run
  @results = Result::Set.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 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