Skip to content

Commit 82908a4

Browse files
mhennemeyerjm
authored andcommitted
Added test for nested lifecycles.
Signed-off-by: Jeremy McAnally <jeremymcanally@gmail.com>
1 parent e09b8ef commit 82908a4

5 files changed

Lines changed: 167 additions & 59 deletions

File tree

countloc.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
def extract_path(argv)
3+
if argv[1].nil?
4+
if argv[0] =~ /-a/
5+
return "/**/*.rb"
6+
elsif argv[0]
7+
if argv[0] =~ /\.rb$/
8+
return argv[0]
9+
end
10+
return argv[0] + "/**/*.rb"
11+
else
12+
return "/**/*.rb"
13+
end
14+
elsif argv[1] =~ /\.rb$/
15+
return argv[1]
16+
else
17+
return argv[1] + "/**/*.rb"
18+
end
19+
end
20+
21+
def all?
22+
ARGV.join =~ /-a/
23+
end
24+
25+
def comment?(line)
26+
line =~ /^\s*#/
27+
end
28+
29+
def blank?(line)
30+
line =~ /^\s*$/
31+
end
32+
33+
def puke(header, locs, comments, blanks)
34+
puts header + ":"
35+
puts "#{locs} loc"
36+
puts "#{comments} lines of comments"
37+
puts "#{blanks} blank lines"
38+
end
39+
40+
dir = File.dirname(__FILE__)
41+
full_path = File.join(dir,extract_path(ARGV))
42+
gloc = gcmts = gblanks = 0
43+
Dir[File.expand_path("#{full_path}")].uniq.each do |file|
44+
if file =~ /.*\.rb$/
45+
46+
loc = cmts = blanks = 0
47+
48+
File.open(file, "r") do |f|
49+
while f.gets
50+
if comment?($_)
51+
cmts += 1
52+
elsif blank?($_)
53+
blanks += 1
54+
else
55+
loc += 1
56+
end
57+
end
58+
end
59+
gcmts += cmts
60+
gloc += loc
61+
gblanks += blanks
62+
puke(file, loc, cmts, blanks) if all?
63+
end
64+
end
65+
puke("Total", gloc, gcmts, gblanks)

lib/context/context.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def context(name, &block)
4949
# puts "Creating context #{cls.context_name}"
5050
cls.class_eval(&block)
5151
(self.context_list ||= []) << cls
52-
5352
const_set("Test#{name.to_class_name}#{cls.object_id.abs}", cls)
5453
cls
5554
end

lib/context/core_ext/string.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class String
22
# Replaces spaces and tabs with _ so we can use the string as a method name
33
# Also replace dangerous punctuation
44
def to_method_name
5-
self.downcase.gsub(/[\s:',;!#]+/,'_')
5+
self.downcase.gsub(/[\s:',;!#=]+/,'_')
66
end
77

88
# Borrowed from +camelize+ in ActiveSupport

test/test_context.rb

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
1-
require File.dirname(__FILE__) + '/test_helper.rb'
2-
3-
class TestContext < Test::Unit::TestCase
4-
def test_can_write_tests_without_context
5-
assert true
6-
end
7-
8-
def test_context_aliases
9-
[:context, :contexts, :describe, :describes, :group, :specify, :specifies].each do |method_alias|
10-
assert self.class.respond_to?(method_alias)
11-
end
12-
end
13-
14-
context "A new context" do
15-
context "when not nested" do
16-
before do
17-
@context = Class.new(Test::Unit::TestCase).context("When testing") do
18-
def test_this_thing
19-
true
20-
end
21-
end
22-
end
23-
24-
it "should set the context name" do
25-
assert_equal "When testing", @context.context_name
26-
end
27-
28-
it "should be a Test::Unit::TestCase" do
29-
assert @context.ancestors.include?(Test::Unit::TestCase)
30-
end
31-
end
32-
33-
context "when nested" do
34-
before do
35-
@context = self.class.context("and we're testing") do
36-
def self.nested
37-
@nested
38-
end
39-
40-
@nested = context "should be nested" do
41-
def test_this_thing
42-
true
43-
end
44-
end
45-
end
46-
end
47-
48-
it "should set a nested context's name" do
49-
assert_equal "A new context when nested and we're testing should be nested", @context.nested.context_name
50-
end
51-
52-
it "should also be a Test::Unit::TestCase" do
53-
assert @context.nested.ancestors.include?(Test::Unit::TestCase)
54-
end
55-
end
56-
end
57-
end
1+
require File.dirname(__FILE__) + '/test_helper.rb'
2+
3+
class TestContext < Test::Unit::TestCase
4+
def test_can_write_tests_without_context
5+
assert true
6+
end
7+
8+
def test_context_aliases
9+
[:context, :contexts, :describe, :describes, :group, :specify, :specifies].each do |method_alias|
10+
assert self.class.respond_to?(method_alias)
11+
end
12+
end
13+
14+
context "A new context" do
15+
context "when not nested" do
16+
before do
17+
@context = Class.new(Test::Unit::TestCase).context("When testing") do
18+
def test_this_thing
19+
true
20+
end
21+
end
22+
end
23+
24+
it "should set the context name" do
25+
assert_equal "When testing", @context.context_name
26+
end
27+
28+
it "should be a Test::Unit::TestCase" do
29+
assert @context.ancestors.include?(Test::Unit::TestCase)
30+
end
31+
end
32+
33+
context "when nested" do
34+
before do
35+
@context = self.class.context("and we're testing") do
36+
def self.nested
37+
@nested
38+
end
39+
40+
@nested = context "should be nested" do
41+
def test_this_thing
42+
true
43+
end
44+
end
45+
end
46+
end
47+
48+
it "should set a nested context's name" do
49+
assert_equal "A new context when nested and we're testing should be nested", @context.nested.context_name
50+
end
51+
52+
it "should also be a Test::Unit::TestCase" do
53+
assert @context.nested.ancestors.include?(Test::Unit::TestCase)
54+
end
55+
end
56+
end
57+
end

test/test_nested_lifecycle.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require File.dirname(__FILE__) + '/test_helper.rb'
2+
3+
class TestNestedLifecycle < Test::Unit::TestCase
4+
before :all do
5+
@var = 0
6+
end
7+
8+
before do
9+
@var += 1
10+
end
11+
context "A new context" do
12+
before do
13+
@var += 1
14+
end
15+
16+
before :all do
17+
@var = 0
18+
end
19+
20+
context "A nested context" do
21+
before do
22+
@var += 1
23+
end
24+
25+
before :all do
26+
@var += 1
27+
end
28+
29+
context "A second, nested context" do
30+
before do
31+
@var += 1
32+
end
33+
34+
before :all do
35+
@var += 1
36+
end
37+
38+
it "should set var" do
39+
assert_equal 6, @var
40+
end
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)