Class: MicroTest::Template

Inherits:
Object
  • Object
show all
Includes:
Color
Defined in:
lib/micro_test/formatters/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Color

#default

Constructor Details

#initialize(context) ⇒ Template

Returns a new instance of Template.



12
13
14
# File 'lib/micro_test/formatters/template.rb', line 12

def initialize(context)
  @context = context
end

Class Method Details

.view(name) ⇒ Object



7
8
9
10
# File 'lib/micro_test/formatters/template.rb', line 7

def self.view(name)
  @views ||= {}
  @views[name] ||= File.read(File.expand_path("../views/#{name}.txt.erb", __FILE__))
end

Instance Method Details

#assert_lines(assert) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/micro_test/formatters/template.rb', line 34

def assert_lines(assert)
  index = assert[:line_num] - 1
  start = index - 2
  start = 0 if start <= 0
  finish = index + 2
  finish = assert[:lines].length - 1 if finish >= assert[:lines].length
  (start..finish).map do |i|
    {
      :line_num => (i + 1),
      :line => assert[:lines][i],
      :color => (i == index ? :red : :gray)
    }
  end
end

#duration_color(duration) ⇒ Object



29
30
31
32
# File 'lib/micro_test/formatters/template.rb', line 29

def duration_color(duration)
  return :yellow if @context.duration <= 0.01
  :red
end

#partial(name, *collection) ⇒ Object



22
23
24
25
26
27
# File 'lib/micro_test/formatters/template.rb', line 22

def partial(name, *collection)
  return render_to_string(name) if collection.empty?
  collection.map do |item|
    Template.new(item).render_to_string(name)
  end.join("\n")
end

#render_to_string(name) ⇒ Object



16
17
18
19
20
# File 'lib/micro_test/formatters/template.rb', line 16

def render_to_string(name)
  instance_eval do
    ERB.new(self.class.view(name), nil, "%<>-").result(binding)
  end
end