Skip to content
Browse files

script/clean now operates on both directories and individual files

  • Loading branch information...
1 parent 3579581 commit 28c2468899cc811476ad5428dc0825f1a434b8f0 @bobbyno committed Sep 22, 2009
Showing with 38 additions and 24 deletions.
  1. +21 −11 cleaner_generators/test_unit_cleaner/lib/test_unit_cleaner.rb
  2. +1 −1 shubox.gemspec
  3. +16 −12 test/test_test_unit_cleaner.rb
View
32 cleaner_generators/test_unit_cleaner/lib/test_unit_cleaner.rb
@@ -4,35 +4,45 @@ class TestUnitCleaner
def run(args)
if (%w(-h --help).include?(args.first) || args.empty?)
puts usage
- exit(0)
+ return 0
end
- dir = ARGV.first
-
- files = Dir["#{dir}/test/**/test_*.rb"].each do |file|
- unless file.match(/test_helper.rb/)
- puts "cleaning: #{file}"
- clean(file)
- end
+ target = args.first
+
+ if (/.rb$/ =~ target)
+ clean(target)
+ else
+ clean_directory(target)
end
end
+ private
+ def clean_directory(dir)
+ files = Dir["#{dir}/test/**/test_*.rb"].each { |file| clean(file) }
+ end
+
def clean(filename)
+ return if /test_helper.rb/ =~ filename
+ puts "cleaning: #{filename}"
+
lines = File.readlines(filename)
output = []
outside_class = true
+
lines.each do |line|
- if outside_class
+ if (outside_class)
output << line
- elsif line.match(/^\s*def test_/)
+ elsif (/^\s*def test_/ =~ line)
output << line
output << " end\n"
end
- if line.match(/class [a-zA-Z]\S* < Test::Unit::TestCase/)
+
+ if (/class [a-zA-Z]\S* < Test::Unit::TestCase/ =~ line)
outside_class = false
end
end
output << 'end'
+
open(filename, 'w') do |f|
f.puts output.join
end
View
2 shubox.gemspec
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Bobby Norton"]
- s.date = %q{2009-09-21}
+ s.date = %q{2009-09-22}
s.default_executable = %q{shubox}
s.description = %q{Test-driven learning is a way to master a programming language by writing unit tests around its API's.
shubox lowers the barrier to entry for test-driven learning by providing a basic infrastructure in which
View
28 test/test_test_unit_cleaner.rb
@@ -16,18 +16,22 @@ def teardown
end
def test_display_usage_with_no_args
- usage = Shubox::TestUnitCleaner.new.usage
- assert_not_nil(usage)
+ no_args = Shubox::TestUnitCleaner.new.run([])
+ assert_equal(0, no_args)
end
-
- def test_cleaner_without_options
- run_generator('test_unit_cleaner', [APP_ROOT], sources)
+
+ def test_display_usage_with_help
+ help = Shubox::TestUnitCleaner.new.run(["--help"])
+ assert_equal(0, help)
end
-
- def test_cleaner_on_simple_file
- file_to_clean = File.expand_path(File.join(File.dirname(__FILE__), 'tmp', 'myproject', 'test', 'test_io.rb'))
- Shubox::TestUnitCleaner.new.clean file_to_clean
- assert_equal <<-EOF, File.read(file_to_clean)
+
+ def test_run_cleaner_on_directory
+ dir_to_clean = File.expand_path(File.join(File.dirname(__FILE__), 'tmp', 'myproject'))
+ puts "\n" + dir_to_clean
+ clean_file = File.join(dir_to_clean, 'test', 'test_io.rb')
+ puts clean_file
+ Shubox::TestUnitCleaner.new.run([dir_to_clean, "ignored extra parameter"])
+ assert_equal <<-EOF, File.read(clean_file)
require File.dirname(__FILE__) + '/test_helper'
# Pro tip: Keeping an inline bookmark to the rdocs for the class under study
@@ -41,9 +45,9 @@ def test_readlines
EOF
end
- def test_cleaner_on_more_complex_file
+ def test_run_cleaner_on_single_file
file_to_clean = File.expand_path(File.join(File.dirname(__FILE__), 'tmp', 'myproject', 'test', 'samples', 'test_right_triangle.rb'))
- Shubox::TestUnitCleaner.new.clean file_to_clean
+ Shubox::TestUnitCleaner.new.run([file_to_clean])
assert_equal <<-EOF, File.read(file_to_clean)
require File.dirname(__FILE__) + '/../test_helper'
require 'samples/right_triangle'

0 comments on commit 28c2468

Please sign in to comment.
Something went wrong with that request. Please try again.