lib/simplecov-markdown.rb in simplecov-markdown-0.1.0 vs lib/simplecov-markdown.rb in simplecov-markdown-0.1.1
- old
+ new
@@ -1,18 +1,79 @@
require "simplecov-markdown/version"
class SimpleCov::Formatter::MarkdownFormatter
def format(result)
- @report = File.open(SimpleCov::Formatter::MarkdownFormatter.output_path, 'w')
- @report.puts "Code Coverage Report"
- @report.puts "======================\n\n"
- @report.puts "|File|Coverage|"
- @report.puts "|:--|--:|"
- result.files.each do |file|
- @report.puts "|#{file.filename}|#{file.covered_percent}%|"
- end
- @report.close
+ report_writer = ReportWriter.new(SimpleCov::Formatter::MarkdownFormatter.output_path)
+ report_writer.write_header "Code Coverage Report"
+ report_writer.write_result(result)
+ report_writer.destroy!
end
def self.output_path
File.join(SimpleCov.coverage_path, 'report.md')
+ end
+
+ class ReportWriter
+ attr_reader :report
+
+ def initialize(report_path)
+ @report = File.open(report_path, 'w')
+ @table_writer = TableWriter.new(report)
+ end
+
+ def write_header(title)
+ @report.puts title
+ @report.puts "=" * 20 + "\n\n"
+ end
+
+ def write_result(result)
+ @table_writer.write_header
+ result.files.each do |file|
+ @table_writer.write_record(file.filename, file.covered_percent.to_s + "%")
+ end
+ @table_writer.destroy!
+ end
+
+ def destroy!
+ @report.close
+ @report = nil
+ end
+ end
+
+ class TableWriter
+ attr_reader :report, :columns_headers, :columns_header_aligns
+
+ def initialize(report)
+ @report = report
+ @columns_headers = ["File", "Coverage"]
+ @columns_header_aligns = [:left, :right]
+ end
+
+ def write_header
+ write_values @columns_headers
+ headers = @columns_header_aligns.map do |align|
+ case align
+ when :left
+ ":--"
+ when :right
+ "--:"
+ else
+ "--"
+ end
+ end
+ write_values headers
+ end
+
+ def write_record(*args)
+ write_values args
+ end
+
+ def destroy!
+ @report = nil
+ end
+
+ private
+
+ def write_values(*args)
+ @report.puts "|" + args.join("|") + "|"
+ end
end
end