Class: Assert::Test

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(build_data = nil) ⇒ Test

Returns a new instance of Test.


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

def initialize(build_data = nil)
  @build_data      = build_data || {}
  @result_callback = nil
end

Class Method Details

.for_block(name, context_info, config, &block) ⇒ Object


17
18
19
20
21
22
23
# File 'lib/assert/test.rb', line 17

def self.for_block(name, context_info, config, &block)
  new(name_file_line_context_data(context_info, name).merge({
    context_info: context_info,
    config: config,
    code: block,
  }))
end

.name_file_line_context_data(ci, name) ⇒ Object

Test is some code/method to run in the scope of a Context that may produce results.


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

def self.name_file_line_context_data(ci, name)
  { name: ci.test_name(name),
    file_line: ci.called_from,
  }
end

Instance Method Details

#<=>(other) ⇒ Object


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

def <=>(other)
  name <=> other.name
end

#codeObject


66
67
68
# File 'lib/assert/test.rb', line 66

def code
  @code ||= @build_data[:code]
end

#configObject


62
63
64
# File 'lib/assert/test.rb', line 62

def config
  @config ||= @build_data[:config]
end

#context_classObject


58
59
60
# File 'lib/assert/test.rb', line 58

def context_class
  context_info.klass
end

#context_infoObject


54
55
56
# File 'lib/assert/test.rb', line 54

def context_info
  @context_info ||= @build_data[:context_info]
end

#file_lineObject


30
31
32
# File 'lib/assert/test.rb', line 30

def file_line
  @file_line ||= FileLine.parse((@build_data[:file_line] || "").to_s)
end

#file_nameObject


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

def file_name
  file_line.file
end

#inspectObject


85
86
87
88
89
90
# File 'lib/assert/test.rb', line 85

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

#line_numObject


38
39
40
# File 'lib/assert/test.rb', line 38

def line_num
  file_line.line.to_i
end

#nameObject


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

def name
  @name ||= (@build_data[:name] || "")
end

#outputObject


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

def output
  @output ||= (@build_data[:output] || +"")
end

#run(&result_callback) ⇒ Object


70
71
72
73
74
75
76
77
78
79
# File 'lib/assert/test.rb', line 70

def run(&result_callback)
  @result_callback = result_callback || proc{ |result| } # noop by default
  scope = context_class.new(self, config, @result_callback)
  start_time = Time.now
  capture_output do
    context_class.run_arounds(scope){ run_test(scope) }
  end
  @result_callback = nil
  @run_time = Time.now - start_time
end

#run_timeObject


50
51
52
# File 'lib/assert/test.rb', line 50

def run_time
  @run_time ||= (@build_data[:run_time] || 0)
end