Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added test for nested lifecycles.

Signed-off-by: Jeremy McAnally <jeremymcanally@gmail.com>
  • Loading branch information...
commit 82908a46b66332a9b28388682caf243a3fbbe1e4 1 parent e09b8ef
@mhennemeyer mhennemeyer authored committed
View
65 countloc.rb
@@ -0,0 +1,65 @@
+
+def extract_path(argv)
+ if argv[1].nil?
+ if argv[0] =~ /-a/
+ return "/**/*.rb"
+ elsif argv[0]
+ if argv[0] =~ /\.rb$/
+ return argv[0]
+ end
+ return argv[0] + "/**/*.rb"
+ else
+ return "/**/*.rb"
+ end
+ elsif argv[1] =~ /\.rb$/
+ return argv[1]
+ else
+ return argv[1] + "/**/*.rb"
+ end
+end
+
+def all?
+ ARGV.join =~ /-a/
+end
+
+def comment?(line)
+ line =~ /^\s*#/
+end
+
+def blank?(line)
+ line =~ /^\s*$/
+end
+
+def puke(header, locs, comments, blanks)
+ puts header + ":"
+ puts "#{locs} loc"
+ puts "#{comments} lines of comments"
+ puts "#{blanks} blank lines"
+end
+
+dir = File.dirname(__FILE__)
+full_path = File.join(dir,extract_path(ARGV))
+gloc = gcmts = gblanks = 0
+Dir[File.expand_path("#{full_path}")].uniq.each do |file|
+ if file =~ /.*\.rb$/
+
+ loc = cmts = blanks = 0
+
+ File.open(file, "r") do |f|
+ while f.gets
+ if comment?($_)
+ cmts += 1
+ elsif blank?($_)
+ blanks += 1
+ else
+ loc += 1
+ end
+ end
+ end
+ gcmts += cmts
+ gloc += loc
+ gblanks += blanks
+ puke(file, loc, cmts, blanks) if all?
+ end
+end
+puke("Total", gloc, gcmts, gblanks)
View
1  lib/context/context.rb
@@ -49,7 +49,6 @@ def context(name, &block)
# puts "Creating context #{cls.context_name}"
cls.class_eval(&block)
(self.context_list ||= []) << cls
-
const_set("Test#{name.to_class_name}#{cls.object_id.abs}", cls)
cls
end
View
2  lib/context/core_ext/string.rb
@@ -2,7 +2,7 @@ class String
# Replaces spaces and tabs with _ so we can use the string as a method name
# Also replace dangerous punctuation
def to_method_name
- self.downcase.gsub(/[\s:',;!#]+/,'_')
+ self.downcase.gsub(/[\s:',;!#=]+/,'_')
end
# Borrowed from +camelize+ in ActiveSupport
View
114 test/test_context.rb
@@ -1,57 +1,57 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestContext < Test::Unit::TestCase
- def test_can_write_tests_without_context
- assert true
- end
-
- def test_context_aliases
- [:context, :contexts, :describe, :describes, :group, :specify, :specifies].each do |method_alias|
- assert self.class.respond_to?(method_alias)
- end
- end
-
- context "A new context" do
- context "when not nested" do
- before do
- @context = Class.new(Test::Unit::TestCase).context("When testing") do
- def test_this_thing
- true
- end
- end
- end
-
- it "should set the context name" do
- assert_equal "When testing", @context.context_name
- end
-
- it "should be a Test::Unit::TestCase" do
- assert @context.ancestors.include?(Test::Unit::TestCase)
- end
- end
-
- context "when nested" do
- before do
- @context = self.class.context("and we're testing") do
- def self.nested
- @nested
- end
-
- @nested = context "should be nested" do
- def test_this_thing
- true
- end
- end
- end
- end
-
- it "should set a nested context's name" do
- assert_equal "A new context when nested and we're testing should be nested", @context.nested.context_name
- end
-
- it "should also be a Test::Unit::TestCase" do
- assert @context.nested.ancestors.include?(Test::Unit::TestCase)
- end
- end
- end
-end
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class TestContext < Test::Unit::TestCase
+ def test_can_write_tests_without_context
+ assert true
+ end
+
+ def test_context_aliases
+ [:context, :contexts, :describe, :describes, :group, :specify, :specifies].each do |method_alias|
+ assert self.class.respond_to?(method_alias)
+ end
+ end
+
+ context "A new context" do
+ context "when not nested" do
+ before do
+ @context = Class.new(Test::Unit::TestCase).context("When testing") do
+ def test_this_thing
+ true
+ end
+ end
+ end
+
+ it "should set the context name" do
+ assert_equal "When testing", @context.context_name
+ end
+
+ it "should be a Test::Unit::TestCase" do
+ assert @context.ancestors.include?(Test::Unit::TestCase)
+ end
+ end
+
+ context "when nested" do
+ before do
+ @context = self.class.context("and we're testing") do
+ def self.nested
+ @nested
+ end
+
+ @nested = context "should be nested" do
+ def test_this_thing
+ true
+ end
+ end
+ end
+ end
+
+ it "should set a nested context's name" do
+ assert_equal "A new context when nested and we're testing should be nested", @context.nested.context_name
+ end
+
+ it "should also be a Test::Unit::TestCase" do
+ assert @context.nested.ancestors.include?(Test::Unit::TestCase)
+ end
+ end
+ end
+end
View
44 test/test_nested_lifecycle.rb
@@ -0,0 +1,44 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class TestNestedLifecycle < Test::Unit::TestCase
+ before :all do
+ @var = 0
+ end
+
+ before do
+ @var += 1
+ end
+ context "A new context" do
+ before do
+ @var += 1
+ end
+
+ before :all do
+ @var = 0
+ end
+
+ context "A nested context" do
+ before do
+ @var += 1
+ end
+
+ before :all do
+ @var += 1
+ end
+
+ context "A second, nested context" do
+ before do
+ @var += 1
+ end
+
+ before :all do
+ @var += 1
+ end
+
+ it "should set var" do
+ assert_equal 6, @var
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.