Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from harob/master

Allowing 'focus' to apply to entire contexts, as well as single tests.
  • Loading branch information...
commit 99cdd31ab848ce28fd013be0e34fbf850ecc5112 2 parents ee0fe88 + 49819ce
Phil Crosby philc authored
Showing with 17 additions and 7 deletions.
  1. +3 −2 README.markdown
  2. +14 −5 lib/scope.rb
5 README.markdown
View
@@ -9,7 +9,7 @@ Features
* 150 lines of code, so it's very easy to understand and enhance.
* Tests are run in the order you define them in, instead of alphabetically or randomly, which follows the principle of least surprise.
* setup\_once and teardown\_once, for writing integration tests (see below for details).
-* The ability to call focus() before a test method to have only that method run. Very useful when writing and troubleshooting tests.
+* The ability to call focus() before a test method or context to have only that method/context run. Very useful when writing and troubleshooting tests.
* Built on top of minitest, which is Ruby 1.9's official replacement for test/unit. Minitest is a gem and works in Ruby 1.8 as well.
Example usage
@@ -75,7 +75,7 @@ You'll notice that variables created in setup\_once blocks need to be class vari
focus
-----
-You can use the `focus` method to indicate that only a single test should be run. When hacking on or troubleshooting tests, this is usually more convenient than running your test with command line parameters (-n). It's also super useful if you're using [watchr](https://github.com/mynyml/watchr) to run your tests.
+You can use the `focus` method to indicate that only a single test/context should be run. When hacking on or troubleshooting tests, this is usually more convenient than running your test with command line parameters (-n). It's also super useful if you're using [watchr](https://github.com/mynyml/watchr) to run your tests.
should "decrease file count when files are removed" do
assert_equal 2, @directory.file_count
@@ -99,3 +99,4 @@ Credits
Phil Crosby (twitter @philcrosby)
Harry Robertson (github harob)
+
19 lib/scope.rb
View
@@ -32,19 +32,26 @@ def test_methods()
end
def self.context(name, &block)
+ context_focused = false
+ if @focus_enabled && @focus_next_test_or_context
+ @focus_next_test_or_context = false
+ @inside_focused_context = true
+ context_focused = true
+ end
parent = @contexts.last
new_context = Context.new(name, parent)
parent.tests_and_subcontexts << new_context
@contexts << new_context
block.call
@contexts.pop
+ @inside_focused_context = false if context_focused
end
def self.should(name, &block)
# When focus_enabled is true, we'll only be running the next should() block that gets defined.
if @focus_enabled
- return unless @focus_next_test
- @focus_next_test = false
+ return unless @focus_next_test_or_context || @inside_focused_context
+ @focus_next_test_or_context &&= false
end
context_name = @contexts[1..-1].map { |context| context.name }.join(" ")
@@ -63,14 +70,16 @@ def self.teardown(&block) @contexts.last.teardown = block end
def self.setup_once(&block) @contexts.last.setup_once = block end
def self.teardown_once(&block) @contexts.last.teardown_once = block end
- # "Focuses" the next test that's defined after this method is called, ensuring that only that test is run.
+ # "Focuses" the next test or context that's defined after this method is called, ensuring that only that
+ # test/context is run.
def self.focus
- # Since we're focusing only the next test, remove any tests which were already defined.
+ # Since we're focusing only the next test/context, remove any tests which were already defined.
context_for_test.values.uniq.each do |context|
context.tests_and_subcontexts.reject! { |test| test.is_a?(String) }
end
@focus_enabled = true
- @focus_next_test = true
+ @focus_next_test_or_context = true
+ @inside_focused_context = false
end
# run() is called by the MiniTest framework. This TestCase class is instantiated once per test method
Please sign in to comment.
Something went wrong with that request. Please try again.