Skip to content

Commit

Permalink
Added #new, #run, and some accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Stephens committed Sep 30, 2009
1 parent d90750e commit 2c9fbcb
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ end
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.pattern = 'test/test_*.rb'
test.verbose = true
end

begin
require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
test.libs << 'test'
test.pattern = 'test/**/test_*.rb'
test.pattern = 'test/test_*.rb'
test.verbose = true
end
rescue LoadError
Expand All @@ -52,5 +52,5 @@ Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "autowatch #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
rdoc.rdoc_files.include('lib/*.rb')
end
46 changes: 46 additions & 0 deletions lib/autowatch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Autowatch
attr_writer :ruby, :include, :lib_dir, :test_dir

def initialize(options = {})
options.each_pair do |key, value|
method = "#{key}="
if self.respond_to?(method)
self.send(method, value)
end
end
yield self if block_given?
end

def ruby
@ruby ||= "ruby"
end

def include
@include ||= ".:#{self.lib_dir}:#{self.test_dir}"
end

def lib_dir
@lib_dir ||= "lib"
end

def test_dir
@test_dir ||= "test"
end

def run(file)
if file =~ %r{^#{@lib_dir}#{File::SEPARATOR}?(.+)$}
# lib file
parts = $1.split(File::SEPARATOR)
parts[-1] = "test_#{parts[-1]}"
file = "#{@test_dir}/" + File.join(parts)
end
run_test_file(file)
end

private
def run_test_file(file)
cmd = "%s -I%s %s" % [ @ruby, @include, file ]
open("| #{cmd}", "r") do |f|
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures/lib/foo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Foo
end
10 changes: 10 additions & 0 deletions test/fixtures/results/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Loaded suite test/test_foo
Started
F.
Finished in 0.000871 seconds.

1) Failure:
test_flunk(TestFoo) [test/test_foo.rb:7]:
Flunked.

2 tests, 2 assertions, 1 failures, 0 errors
1 change: 1 addition & 0 deletions test/fixtures/test/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'test/unit'
11 changes: 11 additions & 0 deletions test/fixtures/test/test_foo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require File.dirname(__FILE__) + "/helper"

class TestFoo < Test::Unit::TestCase
def test_pass
assert true
end

def test_flunk
flunk
end
end
5 changes: 4 additions & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'
require 'mocha'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'autowatch'

class Test::Unit::TestCase
def fake_result(name)
StringIO.new(open(File.dirname(__FILE__) + "/fixtures/results/#{name}.txt").read)
end
end
63 changes: 61 additions & 2 deletions test/test_autowatch.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,66 @@
require 'helper'

class TestAutowatch < Test::Unit::TestCase
should "probably rename this file and start testing for real" do
flunk "hey buddy, you should probably rename this file and start testing for real"
def new_autowatch(options = {})
Autowatch.new({
:ruby => "/usr/local/bin/ruby",
:include => ".:lib:test",
:lib_dir => @lib_dir,
:test_dir => @test_dir
}.merge(options))
end

def setup
@lib_dir = File.dirname(__FILE__) + "/fixtures/lib"
@test_dir = File.dirname(__FILE__) + "/fixtures/test"
end

def test_new_with_hash
aw = Autowatch.new({
:ruby => "/usr/local/bin/ruby",
:include => ".:lib:test"
})
assert_equal "/usr/local/bin/ruby", aw.ruby
assert_equal ".:lib:test", aw.include
end

def test_new_with_block
aw = Autowatch.new do |config|
config.ruby = "/usr/local/bin/ruby"
config.include = ".:lib:test"
config.lib_dir = @lib_dir
config.test_dir = @test_dir
end
assert_equal "/usr/local/bin/ruby", aw.ruby
assert_equal ".:lib:test", aw.include
assert_equal @lib_dir, aw.lib_dir
assert_equal @test_dir, aw.test_dir
end

def test_auto_includes
aw = Autowatch.new do |config|
config.ruby = "/usr/local/bin/ruby"
config.lib_dir = @lib_dir
config.test_dir = @test_dir
end
assert_equal ".:#{@lib_dir}:#{@test_dir}", aw.include
end

def test_defaults
aw = Autowatch.new
assert_equal "ruby", aw.ruby
assert_equal ".:lib:test", aw.include
assert_equal "lib", aw.lib_dir
assert_equal "test", aw.test_dir
end

def test_run_with_lib_file
aw = new_autowatch
result = fake_result("foo")
aw.expects(:open).with(
"| /usr/local/bin/ruby -I.:lib:test #{@test_dir}/test_foo.rb", "r"
).yields(result)

aw.run("#{@lib_dir}/foo.rb")
end
end

0 comments on commit 2c9fbcb

Please sign in to comment.