Permalink
Browse files

Basic formatter and line counter

  • Loading branch information...
1 parent bb9654c commit ebd9017e1ed8d0e9c559d45940981705427ae88e @jamesalmond committed May 14, 2012
Showing with 46 additions and 0 deletions.
  1. +34 −0 spec/golf_formatter.rb
  2. +9 −0 spec/line_counter.rb
  3. +3 −0 spec/spec_helper.rb
View
@@ -0,0 +1,34 @@
+require "rspec/core/formatters/base_text_formatter"
+require File.dirname(__FILE__) + '/line_counter.rb'
+class GolfFormatter < RSpec::Core::Formatters::BaseTextFormatter
+
+ def initialize(output)
+ super
+ @passed_examples = []
+ @passed_holes = []
+ @failed_holes = []
+ end
+
+ def example_passed(example)
+ super
+ @passed_examples << example
+ end
+
+ def example_group_finished(example_group)
+ super
+ if example_group.metadata[:hole]
+ if example_group.examples.all?{|example| @passed_examples.include? example}
+ @passed_holes << example_group
+ else
+ @failed_holes << example_group
+ end
+ end
+ end
+
+ def close
+ super
+ puts "Hole in one: #{@passed_holes.map{|h| h.metadata[:hole]}.join(', ')}" unless @passed_holes.empty?
+ puts "In the rough: #{@failed_holes.map{|h| h.metadata[:hole]}.join(', ')}"
+ puts "Character Count: #{LineCounter.count(File.dirname(__FILE__) + "/../lib/golf.rb")}"
+ end
+end
View
@@ -0,0 +1,9 @@
+class LineCounter
+ def self.count(filename)
+ open(filename) do |file|
+ solution = file.read
+ solution.gsub!(/\s/,"")
+ solution.length
+ end
+ end
+end
View
@@ -0,0 +1,3 @@
+require 'rspec'
+require File.dirname(__FILE__) + "/../lib/golf.rb"
+

0 comments on commit ebd9017

Please sign in to comment.