Skip to content

Commit

Permalink
first step on refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Quaranto and Josh Clayton authored and qrush committed Dec 26, 2011
1 parent 635b563 commit 3f71e41
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
37 changes: 25 additions & 12 deletions lib/m.rb
@@ -1,6 +1,9 @@
require "ostruct"

require "ruby_parser"
require "sourcify"

require "m/test"
require "m/version"

module M
Expand All @@ -14,17 +17,27 @@ def run
if @line.zero?
run_tests
else
tests_to_run = tests.select do |method_name, (start_line, end_line)|
@line >= start_line && @line <= end_line
# collection of tests:
# what tests are within this range of lines?
# what is the size of the longest test case?
# tests sorted by start number
#
# test class:
# #start_line
# #end_line
# #name

tests_to_run = tests.select do |test|
(test.start_line..test.end_line).include? @line
end

if tests_to_run.size > 0
run_tests(tests_to_run)
else
message = "No tests found on line #{@line}. Valid tests to run:\n\n"
column_size = tests.keys.map { |method_name| method_name.to_s.size }.max
tests.sort_by { |test| test.last.first }.each do |method_name, (start_line, end_line)|
message << "#{sprintf("%0#{column_size}s", method_name)}: m #{@file}:#{start_line}\n"
column_size = tests.map { |test| test.name.to_s.size }.max
tests.sort_by(&:start_line).each do |test|
message << "#{sprintf("%0#{column_size}s", test.name)}: m #{@file}:#{test.start_line}\n"
end
abort message
end
Expand All @@ -37,30 +50,30 @@ def tests
@tests ||= begin
$:.unshift "./test"
load @file
suites = Test::Unit::TestCase.test_suites.inject({}) do |suites, suite_class|
suites = ::Test::Unit::TestCase.test_suites.inject({}) do |suites, suite_class|
suites[suite_class] = suite_class.test_methods unless suite_class.test_methods.empty?
suites
end
tests = suites.map do |suite_class, test_methods|
suites.map do |suite_class, test_methods|
suite = suite_class.new(//)
test_methods.map do |test_method|
method = suite.method(test_method)
start_line = method.source_location.last
end_line = method.to_source.split("\n").size + start_line - 1
[test_method, [start_line, end_line]]

M::Test.new(test_method, start_line, end_line)
end
end
Hash[*tests]
end.flatten
end
end

def run_tests(tests_to_run = [])
method_names = tests_to_run.map(&:first)
method_names = tests_to_run.map(&:name)

if method_names.empty?
exec "ruby -Itest #{@file}"
else
exit Test::Unit::AutoRunner.run(false, nil, ["-n", "/(#{method_names.join('|')})/"])
exit ::Test::Unit::AutoRunner.run(false, nil, ["-n", "/(#{method_names.join('|')})/"])
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/m/test.rb
@@ -0,0 +1,4 @@
module M
class Test < Struct.new(:name, :start_line, :end_line)
end
end
4 changes: 4 additions & 0 deletions lib/m/test_collection.rb
@@ -0,0 +1,4 @@
module M
class TestCollection
end
end

0 comments on commit 3f71e41

Please sign in to comment.