Permalink
Browse files

test_unit_cleaner now also cleans ruby_koans

  • Loading branch information...
1 parent fcd5322 commit 2f2740a9d5e6583050ba6dacebef2c2c0fe603ad @bobbyno committed Sep 27, 2009
View
6 History.txt
@@ -1,3 +1,8 @@
+== 0.8.2 2009-09-26
+
+* 1 minor enhancements:
+ * test_unit_cleaner now also cleans ruby_koans.
+
== 0.8.1 2009-09-26
* 2 minor enhancements:
@@ -6,7 +11,6 @@
create a TextMate command with the contents of the script.
* Comments and whitespace are now mostly preserved in test cases; comments
appear after the test case in which they appear.
-
== 0.8 2009-09-22
View
38 cleaner_generators/test_unit_cleaner/lib/test_unit_cleaner.rb
@@ -1,6 +1,25 @@
module Shubox
class TestUnitCleaner
+ attr_reader :class_pattern
+ attr_reader :test_pattern
+ attr_reader :test_file_path
+ attr_reader :ending
+
+ attr_reader :koan_pattern
+ attr_reader :koan_file_path
+
+ def initialize
+ @class_pattern = /class [a-zA-Z]\S* < Test::Unit::TestCase/
+ @test_pattern = /^\s*def test_/
+ @test_file_path = "/test/**/test_*.rb"
+ @ending = "end"
+
+ # Support for github.com/edgecase/ruby_koans
+ @koan_pattern = /class [a-zA-Z]\S* < EdgeCase::Koan/
+ @koan_file_path = "/test/**/about_*.rb"
+ end
+
def run(args)
target = args.first
return usage if empty_or_help?(target)
@@ -19,16 +38,16 @@ def clean_class(lines)
lines.each do |line|
if (outside_class || comment?(line))
output << line
- elsif (/^\s*def test_/ =~ line)
+ elsif (start_of_test?(line))
output << line
- output << " end\n"
+ output << " #{@ending}\n"
end
- if (/class [a-zA-Z]\S* < Test::Unit::TestCase/ =~ line)
+ if (start_of_class?(line))
outside_class = false
end
end
- output << 'end'
+ output << @ending
output.join
end
@@ -46,8 +65,17 @@ def usage
end
private
+ def start_of_class?(line)
+ (@class_pattern =~ line || @koan_pattern =~ line)
+ end
+
+ def start_of_test?(line)
+ @test_pattern =~ line
+ end
+
def clean_directory(dir)
- files = Dir["#{dir}/test/**/test_*.rb"].each { |file| clean_file(file) }
+ Dir[dir + @test_file_path].each { |file| clean_file(file) }
+ Dir[dir + @koan_file_path].each { |file| clean_file(file) }
end
def clean_file(filename)
View
2 lib/shubox.rb
@@ -5,7 +5,7 @@
$:.include?(File.dirname(__FILE__) + '/../cleaner_generators') || $:.include?(File.expand_path(File.dirname(__FILE__) + '../cleaner_generators'))
module Shubox
- VERSION = '0.8.1'
+ VERSION = '0.8.2'
end
require 'test_unit_cleaner/lib/test_unit_cleaner'
View
28 test/cleaners/test_test_unit_cleaner.rb
@@ -80,4 +80,32 @@ def test_area
cleaned_contents = cleaner.clean_class(contents)
assert_equal(cleaned_contents, cleaned_file.strip)
end
+
+ # Including support for cleaning github.com/edgecase/ruby_koans
+ def test_clean_koans
+ koan = <<-EOF
+require File.dirname(__FILE__) + '/edgecase'
+
+class AboutAsserts < EdgeCase::Koan
+
+ # We shall contemplate truth by testing reality, via asserts.
+ def test_assert_truth
+ assert true
+ end
+end
+EOF
+
+ clean_koan = <<-EOF
+require File.dirname(__FILE__) + '/edgecase'
+
+class AboutAsserts < EdgeCase::Koan
+
+ # We shall contemplate truth by testing reality, via asserts.
+ def test_assert_truth
+ end
+end
+EOF
+ cleaner = Shubox::TestUnitCleaner.new
+ assert_equal(clean_koan.strip, cleaner.clean_class(koan))
+ end
end

0 comments on commit 2f2740a

Please sign in to comment.