Permalink
Browse files

Merge pull request #10 from cldwalker/options

added -h, -l, --version
  • Loading branch information...
2 parents 2c0de2e + cfb3227 commit 1b33c65b44a7ca5b569c68b8cd4c8dab6ec187da @qrush committed Jul 31, 2012
Showing with 64 additions and 4 deletions.
  1. +31 −4 lib/m.rb
  2. +33 −0 test/options_test.rb
View
@@ -38,12 +38,12 @@
#
# $ cat -n test/example_test.rb
# 1 require 'test/unit'
-# 2
+# 2
# 3 class ExampleTest < Test::Unit::TestCase
# 4 def test_apple
# 5 assert_equal 1, 1
# 6 end
-# 7
+# 7
# 8 def test_banana
# 9 assert_equal 1, 1
# 10 end
@@ -73,7 +73,7 @@
#Want to run the whole test? Just leave off the line number.
#
# $ m test/example_test.rb
-# Run options:
+# Run options:
#
# # Running tests:
#
@@ -128,9 +128,11 @@ def parse
# Just shell out to `rake test`.
exec "rake test"
else
+ parse_options! @argv
+
# Parse out ARGV, it should be coming in in a format like `test/test_file.rb:9`
@file, line = @argv.first.split(':')
- @line = line.to_i
+ @line ||= line.to_i
# If this file is a directory, not a file, run the tests inside of this directory
if Dir.exist?(@file)
@@ -147,6 +149,31 @@ def parse
end
end
+ def parse_options!(argv)
+ require 'optparse'
+
+ OptionParser.new do |opts|
+ opts.banner = 'Options:'
+ opts.version = M::VERSION
+
+ opts.on '-h', '--help', 'Display this help.' do
+ puts "Usage: m [OPTIONS] [FILES]\n\n", opts
+ exit
+ end
+
+ opts.on '--version', 'Display the version.' do
+ puts "m #{M::VERSION}"
+ exit
+ end
+
+ opts.on '-l', '--line LINE', Integer, 'Line number for file.' do |line|
+ @line = line
+ end
+
+ opts.parse! argv
+ end
+ end
+
def execute
# Locate tests to run that may be inside of this line. There could be more than one!
tests_to_run = tests.within(@line)
View
@@ -0,0 +1,33 @@
+require 'test_helper'
+
+class OptionsTest < MTest
+ def test_short_help_option
+ output = m('-h')
+ assert_output /^Usage: m \[OPTIONS\] \[FILES\]/, output
+ end
+
+ def test_long_help_option
+ output = m('--help')
+ assert_output /^Usage: m \[OPTIONS\] \[FILES\]/, output
+ end
+
+ def test_verbose_option
+ output = m('--version')
+ assert_output /^m #{M::VERSION}/, output
+ end
+
+ def test_short_line_option
+ output = m('-l19 examples/minitest_example_test.rb')
+ assert_output /1 tests, 1 assertions/, output
+ end
+
+ def test_long_line_option
+ output = m('--line 19 examples/minitest_example_test.rb')
+ assert_output /1 tests, 1 assertions/, output
+ end
+
+ def test_line_option_has_precedence_over_colon_format
+ output = m('--line 19 examples/minitest_example_test.rb:2')
+ assert_output /1 tests, 1 assertions/, output
+ end
+end

0 comments on commit 1b33c65

Please sign in to comment.