From 1553bd049c83bbe1414249ce4e52b810bf1202e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Thu, 28 Jun 2018 18:02:56 +0200 Subject: [PATCH 1/3] Add Spec::Formatter#@io to avoid directly printing to stdout --- src/spec/formatter.cr | 35 ++++++++++++++++++----------------- src/spec/junit_formatter.cr | 6 +----- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/spec/formatter.cr b/src/spec/formatter.cr index 9ad6310e3a9a..e9a45b844756 100644 --- a/src/spec/formatter.cr +++ b/src/spec/formatter.cr @@ -1,6 +1,9 @@ module Spec # :nodoc: abstract class Formatter + def initialize(@io : IO = STDOUT) + end + def push(context) end @@ -20,11 +23,11 @@ module Spec # :nodoc: class DotFormatter < Formatter def report(result) - print Spec.color(LETTERS[result.kind], result.kind) + @io << Spec.color(LETTERS[result.kind], result.kind) end def finish - puts + @io.puts end end @@ -35,20 +38,18 @@ module Spec @printed = false end - def print + def print(io) return if @printed @printed = true - VerboseFormatter.print_indent(@indent) - puts @description + VerboseFormatter.print_indent(io, @indent) + io.puts @description end end - def initialize - @indent = 0 - @last_description = "" - @items = [] of Item - end + @indent = 0 + @last_description = "" + @items = [] of Item def push(context) @items << Item.new(@indent, context.description) @@ -61,24 +62,24 @@ module Spec end def print_indent - self.class.print_indent(@indent) + self.class.print_indent(@io, @indent) end - def self.print_indent(indent) - indent.times { print " " } + def self.print_indent(io, indent) + indent.times { io << " " } end def before_example(description) - @items.each &.print + @items.each &.print(@io) print_indent - print description + @io << description @last_description = description end def report(result) - print '\r' + @io << '\r' print_indent - puts Spec.color(@last_description, result.kind) + @io.puts Spec.color(@last_description, result.kind) end end diff --git a/src/spec/junit_formatter.cr b/src/spec/junit_formatter.cr index c944490c1328..967363d0a52f 100644 --- a/src/spec/junit_formatter.cr +++ b/src/spec/junit_formatter.cr @@ -3,13 +3,9 @@ require "html" module Spec # :nodoc: class JUnitFormatter < Formatter - @output : IO @results = [] of Spec::Result @summary = {} of Symbol => Int32 - def initialize(@output) - end - def report(result) current = @summary[result.kind]? || 0 @summary[result.kind] = current + 1 @@ -17,7 +13,7 @@ module Spec end def finish - io = @output + io = @io io.puts %() io << %(