Permalink
Browse files

extracted semi colon troll

  • Loading branch information...
1 parent 629eddf commit 4a1677b846241facf0770d0d08e1166a6655ae2e @nolman nolman committed Sep 17, 2011
Showing with 66 additions and 52 deletions.
  1. +1 −0 lib/troll.rb
  2. +24 −0 lib/troll/semi_colon_troll.rb
  3. +5 −20 lib/troll/troller.rb
  4. +36 −0 spec/semi_colon_troll_spec.rb
  5. +0 −32 spec/troller_spec.rb
View
@@ -1,2 +1,3 @@
+require 'troll/semi_colon_troll'
require 'troll/collector'
require 'troll/troller'
@@ -0,0 +1,24 @@
+module Troll
+ class SemiColonTroll
+ def troll_string(str)
+ result = ""
+ str.each_line { |line| result += troll_line(line) }
+ result
+ end
+
+ private
+ def troll_line(line)
+ case line
+ when /^\s+$/
+ line
+ when /^.*;\s*$/
+ line
+ when /^.*\n$/
+ "#{line.chomp};\n"
+ else
+ "#{line};"
+ end
+ end
+
+ end
+end
View
@@ -4,38 +4,23 @@ class Troller
def initialize(file_path=nil)
self.file = file_path
+ @trolls = [SemiColonTroll.new]
end
def troll
contents = File.read(self.file)
- self.result = troll_string(contents)
+ @trolls.each do |troll|
+ contents = troll.troll_string(contents)
+ end
+ self.result = contents
end
def troll!
troll
write_file
end
- def troll_string(str)
- result = ""
- str.each_line { |line| result += troll_line(line) }
- result
- end
-
private
- def troll_line(line)
- case line
- when /^\s+$/
- line
- when /^.*;\s*$/
- line
- when /^.*\n$/
- "#{line.chomp};\n"
- else
- "#{line};"
- end
- end
-
def write_file
File.open(output_file, 'w') do |f|
f.puts self.result
@@ -0,0 +1,36 @@
+require 'spec_helper'
+require 'troll'
+
+describe Troll::SemiColonTroll do
+ describe "#troll_string" do
+ it "appends a semicolon to a single-line string" do
+ Troll::SemiColonTroll.new.troll_string("foo").should eql("foo;")
+ end
+
+ it "does not append a semicolon to a single-line string ending in a semi-colon" do
+ Troll::SemiColonTroll.new.troll_string("foo;").should eql("foo;")
+ end
+
+ it "appends a semicolon to each line of a multi-line string" do
+ multi_line_string = <<-EOS
+ class Xzibit
+
+ def yo_dawg_i_heard_you_like(&procs)
+ Proc.new { procs.call }
+ end
+
+ end
+ EOS
+ semi_coloned_multi_line_string = <<-EOS
+ class Xzibit;
+
+ def yo_dawg_i_heard_you_like(&procs);
+ Proc.new { procs.call };
+ end;
+
+ end;
+ EOS
+ Troll::SemiColonTroll.new.troll_string(multi_line_string).should eql(semi_coloned_multi_line_string)
+ end
+ end
+end
View
@@ -28,36 +28,4 @@
original.should eql(trolled)
end
end
-
- describe "#troll_string" do
- it "appends a semicolon to a single-line string" do
- Troll::Troller.new.troll_string("foo").should eql("foo;")
- end
-
- it "does not append a semicolon to a single-line string ending in a semi-colon" do
- Troll::Troller.new.troll_string("foo;").should eql("foo;")
- end
-
- it "appends a semicolon to each line of a multi-line string" do
- multi_line_string = <<-EOS
- class Xzibit
-
- def yo_dawg_i_heard_you_like(&procs)
- Proc.new { procs.call }
- end
-
- end
- EOS
- semi_coloned_multi_line_string = <<-EOS
- class Xzibit;
-
- def yo_dawg_i_heard_you_like(&procs);
- Proc.new { procs.call };
- end;
-
- end;
- EOS
- Troll::Troller.new.troll_string(multi_line_string).should eql(semi_coloned_multi_line_string)
- end
- end
end

0 comments on commit 4a1677b

Please sign in to comment.