Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

#160: add option --name or -n for running specific tests by giving a …

…test name

By using --name or -n we can specify the test method we want to run. We can
also give a pattern for test names that we want to run. To give a pattern,
just enclose the string between slashes (/). When the given test name or
pattern does not match any test, valid test names will be printed and the
process will abort.
  • Loading branch information...
commit 13b4670e9770b4d1332dbe8deeb2e8653199a4bc 1 parent 0528aed
@arturopie arturopie authored
View
58 rubygem/lib/zeus/m.rb
@@ -177,6 +177,14 @@ def parse_options!(argv)
@line = line
end
+ opts.on '-n', '--name NAME', String, 'Name or pattern for test methods to run.' do |name|
+ if name[0] == "/" && name[-1] == "/"
+ @test_name = Regexp.new(name[1..-2])
+ else
+ @test_name = name
+ end
+ end
+
opts.parse! argv
end
end
@@ -184,11 +192,6 @@ def parse_options!(argv)
def execute
generate_tests_to_run
- if !@line.nil? && @tests_to_run.empty?
- # We didn't find any tests, but the user specified line number
- abort_with_no_test_found_msg
- end
-
test_arguments = build_test_arguments
# directly run the tests from here and exit with the status of the tests passing or failing
@@ -210,10 +213,35 @@ def generate_tests_to_run
end
end
- def abort_with_no_test_found_msg
- # Otherwise we found no tests on this line, so you need to pick one.
- message = "No tests found on line #{@line}. Valid tests to run:\n\n"
+ def build_test_arguments
+ if @line
+ abort_with_no_test_found_by_line_number if @tests_to_run.empty?
+ # assemble the regexp to run these tests,
+ test_names = @tests_to_run.map(&:name).join('|')
+
+ # set up the args needed for the runner
+ ["-n", "/(#{test_names})/"]
+ elsif user_specified_name?
+ abort_with_no_test_found_by_name unless tests.contains?(@test_name)
+
+ test_names = test_name_to_s
+ ["-n", test_names]
+ else
+ []
+ end
+ end
+
+ def abort_with_no_test_found_by_line_number
+ abort_with_valid_tests_msg "No tests found on line #{@line}. "
+ end
+
+ def abort_with_no_test_found_by_name
+ abort_with_valid_tests_msg "No test name matches '#{test_name_to_s}'. "
+ end
+
+ def abort_with_valid_tests_msg message=""
+ message << "Valid tests to run:\n\n"
# For every test ordered by line number,
# spit out the test name and line number where it starts,
tests.by_line_number do |test|
@@ -224,16 +252,12 @@ def abort_with_no_test_found_msg
abort message
end
- def build_test_arguments
- if @line
- # assemble the regexp to run these tests,
- test_names = @tests_to_run.map(&:name).join('|')
+ def test_name_to_s
+ @test_name.is_a?(Regexp)? "/#{@test_name.source}/" : @test_name
+ end
- # set up the args needed for the runner
- test_arguments = ["-n", "/(#{test_names})/"]
- else
- test_arguments = []
- end
+ def user_specified_name?
+ !@test_name.nil?
end
def framework
View
7 rubygem/lib/zeus/m/test_collection.rb
@@ -40,6 +40,13 @@ def by_line_number(&block)
# the block into the sorted collection
sort_by(&:start_line).each(&block)
end
+
+ def contains? test_name
+ @collection.each do |test|
+ return true if test_name.match(test.name)
+ end
+ false
+ end
end
end
end
View
54 rubygem/spec/m_spec.rb
@@ -8,6 +8,16 @@
stub_mini_test_methods
end
+ context "no option is given" do
+ it "runs the test without giving any option" do
+ argv = ["path/to/file.rb"]
+
+ fake_runner.should_receive(:run).with([])
+
+ lambda { Runner.new(argv).run }.should exit_with_code(0)
+ end
+ end
+
context "given a line number" do
it "aborts if no test is found" do
argv = ["path/to/file.rb:100"]
@@ -26,4 +36,48 @@
lambda { Runner.new(argv).run }.should exit_with_code(0)
end
end
+
+ context "specifying test name" do
+ it "runs the specified tests when using a pattern in --name option" do
+ argv = ["path/to/file.rb", "--name", "/#{fake_test_method}/"]
+
+ fake_runner.should_receive(:run).with(["-n", "/#{fake_test_method}/"])
+
+ lambda { Runner.new(argv).run }.should exit_with_code(0)
+ end
+
+ it "runs the specified tests when using a pattern in -n option" do
+ argv = ["path/to/file.rb", "-n", "/method/"]
+
+ fake_runner.should_receive(:run).with(["-n", "/method/"])
+
+ lambda { Runner.new(argv).run }.should exit_with_code(0)
+ end
+
+ it "aborts if no test matches the given pattern" do
+ argv = ["path/to/file.rb", "-n", "/garbage/"]
+
+ STDERR.should_receive(:write).with(%r{No test name matches \'/garbage/\'})
+ fake_runner.should_not_receive :run
+
+ lambda { Runner.new(argv).run }.should_not exit_with_code(0)
+ end
+
+ it "runs the specified tests when using a name (no pattern)" do
+ argv = ["path/to/file.rb", "-n", "#{fake_test_method}"]
+
+ fake_runner.should_receive(:run).with(["-n", fake_test_method])
+
+ lambda { Runner.new(argv).run }.should exit_with_code(0)
+ end
+
+ it "aborts if no test matches the given test name" do
+ argv = ["path/to/file.rb", "-n", "method"]
+
+ STDERR.should_receive(:write).with(%r{No test name matches \'method\'})
+ fake_runner.should_not_receive :run
+
+ lambda { Runner.new(argv).run }.should_not exit_with_code(0)
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.