Skip to content

Commit

Permalink
Experimenting with moving output to erb templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed Nov 14, 2014
1 parent bca498a commit a5f4c40
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 41 deletions.
2 changes: 0 additions & 2 deletions lib/micro_test/formatters/default_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def before_class(test_class)
end

def after_test(test)
print_duration test.duration

if test.passed?
print_test_pass test
else
Expand Down
69 changes: 32 additions & 37 deletions lib/micro_test/formatters/default_printer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
require_relative "template"
require_relative "./helpers/default_helper"

module MicroTest
module DefaultPrinter

protected

def print_duration(duration)
color = duration < 0.01 ? :yellow : :red
print send(color, " #{duration.to_s.ljust(6, "0")}")
end

def print_line
puts "".ljust(80, "-")
end
Expand All @@ -17,43 +15,40 @@ def print_with_line(value)
end

def print_test_pass(test)
print green(" #{test.desc}")
Template.new(test, DefaultHelper).render(:_test_pass)
end

def print_test_fail(test)
puts red(" #{test.desc}")
test.failed_asserts.each do |assert|
print_assert_fail assert
end
Template.new(test, DefaultHelper).render(:_test_fail)
end

def print_assert_fail(assert)
puts
print "".ljust(9)
puts "#{assert[:file_path]}:#{red(assert[:line_num])}"
puts "".ljust(9) + "".rjust(71, "-")
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
print_assert_fail_lines assert, start, finish, index
end

def print_assert_fail_lines(assert, start, finish, index)
(start..finish).each do |i|
print "".ljust(9)
if i == index
print red((i + 1).to_s.rjust(3, "0"))
print red("|")
print red(assert[:lines][i])
else
print (i + 1).to_s.rjust(3, "0")
print "|"
print assert[:lines][i]
end
end
end
#def print_assert_fail(assert)
# puts
# print "".ljust(9)
# puts "#{assert[:file_path]}:#{red(assert[:line_num])}"
# puts "".ljust(9) + "".rjust(71, "-")
# 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
# print_assert_fail_lines assert, start, finish, index
#end

#def print_assert_fail_lines(assert, start, finish, index)
# (start..finish).each do |i|
# print "".ljust(9)
# if i == index
# print red((i + 1).to_s.rjust(3, "0"))
# print red("|")
# print red(assert[:lines][i])
# else
# print (i + 1).to_s.rjust(3, "0")
# print "|"
# print assert[:lines][i]
# end
# end
#end

end
end
10 changes: 10 additions & 0 deletions lib/micro_test/formatters/helpers/default_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module MicroTest
module DefaultHelper

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

end
end
29 changes: 29 additions & 0 deletions lib/micro_test/formatters/template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module MicroTest
class Template
include Color

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

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

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

def partial(name, options={})
return render(name) if options[:collection].nil?
options[:collection].each do |item|
Template.new(item).render(name)
end
end

end
end
2 changes: 2 additions & 0 deletions lib/micro_test/formatters/views/default/_assert_fail.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= @context[:file_path] %>:<%= red @context[:line_num] %>
-------------------------------------------------------------
3 changes: 3 additions & 0 deletions lib/micro_test/formatters/views/default/_test_fail.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= send duration_color(@context.duration), @context.duration %>
<%= red @context.desc %>
<%= partial :_assert_fail, :collection => @context.failed_asserts %>
2 changes: 2 additions & 0 deletions lib/micro_test/formatters/views/default/_test_pass.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= send duration_color(@context.duration), @context.duration %>
<%= green @context.desc %>
4 changes: 2 additions & 2 deletions lib/micro_test/test_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def reset
@duration = nil
end

# Rounded duration with 4 decimal places.
# Rounded duration.
def duration
return nil if @duration.nil?
(@duration.to_f * 10**4).round / 10**4
(@duration * 10**4).ceil.to_f / 10**4
end

private
Expand Down
1 change: 1 addition & 0 deletions test/color_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Crayon
CRAYON = Crayon.new

test "red" do
assert false
assert MicroTest::Color.red("foo") == "\e[31mfoo\e[0m"
assert ColorTest::CRAYON.red("foo") == "\e[31mfoo\e[0m"
end
Expand Down

0 comments on commit a5f4c40

Please sign in to comment.