Skip to content

Commit

Permalink
Working on emitting output with templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed Nov 15, 2014
1 parent dce101d commit b0eb154
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
15 changes: 15 additions & 0 deletions lib/micro_test/formatters/helpers/default_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,20 @@ def duration_color(duration)
:red
end

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|
{
:number => (i + 1),
:color => (i == index ? :red : :default),
:code => assert[:lines][i]
}
end
end

end
end
4 changes: 4 additions & 0 deletions lib/micro_test/formatters/lib/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module Color
:gray => 90
}

def default(text)
text
end

colors.each do |name, code|
define_method name do |text|
"\e[#{code}m#{text}\e[0m"
Expand Down
11 changes: 6 additions & 5 deletions lib/micro_test/formatters/lib/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ def self.view(name)
@views[name] ||= File.read(File.expand_path("../../views/default/#{name}.txt.erb", __FILE__))
end

def initialize(object, *helpers)
@context = object
def initialize(context, *helpers)
@context = context
@helpers = helpers
helpers.each { |helper| extend helper }
end

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

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

end
Expand Down
8 changes: 5 additions & 3 deletions lib/micro_test/formatters/views/default/_assert_fail.txt.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%= @context[:file_path] %>:<%= red @context[:line_num] %>

-------------------------------------------------------------
<%= @context[:file_path].strip %>:<%= red @context[:line_num] %>
-------------------------------------------------------------
<% assert_lines(@context).each do |line| -%>
<%= send line[:color], line[:number].to_s.rjust(3, "0") -%>| <%= send line[:color], line[:code] -%>
<% end -%>
5 changes: 2 additions & 3 deletions lib/micro_test/formatters/views/default/_test_fail.txt.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<%= send duration_color(@context.duration), @context.duration %>
<%= red @context.desc %>
<%= partial :_assert_fail, *@context.failed_asserts %>
<%= send duration_color(@context.duration), @context.duration %> <%= red @context.desc %>
<%= partial :_assert_fail, *@context.failed_asserts -%>
3 changes: 1 addition & 2 deletions lib/micro_test/formatters/views/default/_test_pass.txt.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<%= send duration_color(@context.duration), @context.duration %>
<%= green @context.desc %>
<%= send duration_color(@context.duration), @context.duration %> <%= green @context.desc -%>

0 comments on commit b0eb154

Please sign in to comment.