test-unit-context 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +21 -0
- data/lib/test/unit/context.rb +4 -1
- data/lib/test/unit/context/context.rb +5 -4
- data/lib/test/unit/context/helpers.rb +7 -3
- data/lib/test/unit/context/shared.rb +1 -1
- data/lib/test/unit/context/spec.rb +59 -0
- data/lib/test/unit/context/version.rb +1 -1
- data/test/test/unit/context/spec_spec.rb +46 -0
- data/test/test/unit/context/spec_test.rb +47 -0
- data/test/test/unit/context_test.rb +0 -1
- metadata +84 -77
data/README.md
CHANGED
@@ -94,6 +94,27 @@ end
|
|
94
94
|
|
95
95
|
```
|
96
96
|
|
97
|
+
### Spec Mode
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
require 'test/unit/context/spec'
|
101
|
+
|
102
|
+
describe ChuckNorris, '#fart' do
|
103
|
+
|
104
|
+
setup do
|
105
|
+
@subject = ChuckNorris.new
|
106
|
+
@subject.fart
|
107
|
+
end
|
108
|
+
|
109
|
+
it "creates a parallel universe" do
|
110
|
+
assert Object.const_defined?(:Universe)
|
111
|
+
assert_equal @subject, Universe.instance
|
112
|
+
assert_empty ObjectSpace.each_object(Universe).to_a
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
97
118
|
## Copyright
|
98
119
|
|
99
120
|
Copyright (c) 2012 [Karol Bucek](https://github.com/kares).
|
data/lib/test/unit/context.rb
CHANGED
@@ -23,4 +23,7 @@ end
|
|
23
23
|
require 'test/unit/context/context'
|
24
24
|
Test::Unit::TestCase.extend Test::Unit::Context::Context
|
25
25
|
require 'test/unit/context/shared'
|
26
|
-
Test::Unit::TestCase.extend Test::Unit::Context::Shared
|
26
|
+
Test::Unit::TestCase.extend Test::Unit::Context::Shared
|
27
|
+
|
28
|
+
# NOTE: this pollutes it's left to the user to load it :
|
29
|
+
# require 'test/unit/context/spec'
|
@@ -1,12 +1,13 @@
|
|
1
|
-
require 'time'
|
2
|
-
|
3
1
|
module Test::Unit::Context
|
4
2
|
module Context
|
5
3
|
|
4
|
+
PREFIX = 'Context'.freeze # :nodoc:
|
5
|
+
SUFFIX = nil # :nodoc:
|
6
|
+
|
6
7
|
# Add a context to a set of tests.
|
7
8
|
#
|
8
9
|
# context "A new account" do
|
9
|
-
# test "does not have users"
|
10
|
+
# test "does not have users" do
|
10
11
|
# assert Account.new.users.empty?
|
11
12
|
# end
|
12
13
|
# end
|
@@ -35,7 +36,7 @@ module Test::Unit::Context
|
|
35
36
|
def context(name = nil, &block)
|
36
37
|
name ||= Helpers.generate_uuid
|
37
38
|
# context "created with defaults" ... 'ContextCreatedWithDefaults'
|
38
|
-
class_name = Helpers.to_const_name(name.to_s,
|
39
|
+
class_name = Helpers.to_const_name(name.to_s, PREFIX, SUFFIX)
|
39
40
|
if const_defined?(class_name)
|
40
41
|
klass = const_get(class_name)
|
41
42
|
if ( klass.superclass == self rescue nil )
|
@@ -1,14 +1,18 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
1
3
|
module Test::Unit::Context
|
2
4
|
module Helpers
|
3
5
|
|
4
6
|
module_function
|
5
7
|
|
6
|
-
def to_const_name(str, prefix = nil)
|
7
|
-
name =
|
8
|
+
def to_const_name(str, prefix = nil, suffix = nil)
|
9
|
+
name = str.dup
|
10
|
+
name.lstrip! if prefix
|
11
|
+
name.rstrip! if suffix
|
8
12
|
name.gsub!(/[\s:',\.~;!#=\(\)&]+/, '_')
|
9
13
|
name.gsub!(/\/(.?)/) { $1.upcase }
|
10
14
|
name.gsub!(/(?:^|_)(.)/) { $1.upcase }
|
11
|
-
|
15
|
+
"#{prefix}#{name}#{suffix}"
|
12
16
|
end
|
13
17
|
|
14
18
|
def generate_uuid
|
@@ -3,7 +3,7 @@ module Test::Unit::Context
|
|
3
3
|
|
4
4
|
# Share behavior among different contexts.
|
5
5
|
# This creates a module (actually, a Module subclass) that is included
|
6
|
-
# using the +
|
6
|
+
# using the +like+ method (or one of its aliases) provided by context (or
|
7
7
|
# +include+ if you know the module's constant name).
|
8
8
|
#
|
9
9
|
# ==== Examples
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Test::Unit::Context
|
2
|
+
module Spec
|
3
|
+
|
4
|
+
PREFIX = nil # :nodoc:
|
5
|
+
SUFFIX = 'Test'.freeze # :nodoc:
|
6
|
+
|
7
|
+
# Spec style Test::Unit::TestCase (sub-classes) :
|
8
|
+
#
|
9
|
+
# describe User do
|
10
|
+
# it "creates a new instance" do
|
11
|
+
# assert_nothing_raised { User.new }
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# You need to `require 'test/unit/context/spec'` ( e.g. in test_helper.rb).
|
16
|
+
#
|
17
|
+
# The generated Test::Unit::TestCase sub-class follows the "Test" suffix
|
18
|
+
# naming convention e.g. `describe User` ends up as `UserTest < TestCase`.
|
19
|
+
#
|
20
|
+
def describe(*args, &block)
|
21
|
+
name = args.map { |arg| arg.is_a?(Class) ? arg.name : arg }.join
|
22
|
+
class_name = Helpers.to_const_name(name.freeze, PREFIX, SUFFIX)
|
23
|
+
self_klass = self.is_a?(Class) ? self : self.class # nested describes
|
24
|
+
if self_klass.const_defined?(class_name)
|
25
|
+
klass = self_klass.const_get(class_name)
|
26
|
+
if klass <= Test::Unit::TestCase
|
27
|
+
warn "duplicate test-case describe with args #{args.inspect} " <<
|
28
|
+
"found at #{caller.first} it is going to be merged with " <<
|
29
|
+
"the previous TestCase definition"
|
30
|
+
else
|
31
|
+
raise "could not create a test-case with args #{args.inspect} " <<
|
32
|
+
"as a constant #{class_name} is already defined and is not " <<
|
33
|
+
"another TestCase definition"
|
34
|
+
end
|
35
|
+
else
|
36
|
+
# NOTE: context inherits from nesting case but describe should not :
|
37
|
+
#is_test_case = ( self_klass <= Test::Unit::TestCase )
|
38
|
+
#klass = Class.new is_test_case ? self_klass : Test::Unit::TestCase
|
39
|
+
klass = Class.new(Test::Unit::TestCase)
|
40
|
+
klass.send(:define_method, :name) { name }
|
41
|
+
klass.extend Methods
|
42
|
+
self_klass.const_set(class_name, klass)
|
43
|
+
end
|
44
|
+
klass.class_eval(&block)
|
45
|
+
klass
|
46
|
+
end
|
47
|
+
|
48
|
+
module Methods
|
49
|
+
|
50
|
+
# It is an alias for Test::Unit's test method.
|
51
|
+
# @see Test::Unit::TestCase#test
|
52
|
+
def it(*args, &block); test(*args, &block); end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
Object.send :include, Test::Unit::Context::Spec
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# #see spec_test.rb
|
2
|
+
|
3
|
+
class User42; end
|
4
|
+
|
5
|
+
describe User42 do
|
6
|
+
|
7
|
+
it 'can create a new instance' do
|
8
|
+
assert_nothing_raised do
|
9
|
+
User42.new
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'nested' do
|
14
|
+
|
15
|
+
it 'sets SPEC_SAMPLE constant' do
|
16
|
+
User42.const_set(:SPEC_SAMPLE, true)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe User42, '#new' do
|
24
|
+
|
25
|
+
setup do
|
26
|
+
@user = User42.new
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'setup a new instance' do
|
30
|
+
assert_not_nil @user
|
31
|
+
assert_instance_of User42, @user
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
class Foo
|
37
|
+
def bar; :baz; end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe Foo, 'bar', :baz do
|
41
|
+
|
42
|
+
it 'works' do
|
43
|
+
assert_equal :'baz', Foo.new.bar
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.expand_path('../../../test_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module Test::Unit::Context
|
4
|
+
class SpecTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
require 'test/unit/context/spec'
|
7
|
+
require 'test/unit/context/spec_spec'
|
8
|
+
|
9
|
+
def test_sets_up_describe
|
10
|
+
assert self.class.respond_to?(:describe)
|
11
|
+
assert ::Object.respond_to?(:describe)
|
12
|
+
end
|
13
|
+
|
14
|
+
#def test_sets_up_it_alias
|
15
|
+
#assert self.class.respond_to? :it
|
16
|
+
#end
|
17
|
+
|
18
|
+
test "set-up spec_spec TestCase constants" do
|
19
|
+
# describe User42
|
20
|
+
assert Object.const_defined?(:User42Test)
|
21
|
+
user_42_test = Object.const_get(:User42Test)
|
22
|
+
assert_equal Test::Unit::TestCase, user_42_test.superclass
|
23
|
+
#assert_equal 'User42', user_42_test.name
|
24
|
+
|
25
|
+
# describe 'nested'
|
26
|
+
assert user_42_test.const_defined?(:NestedTest)
|
27
|
+
user_42_nested_test = user_42_test.const_get(:NestedTest)
|
28
|
+
#assert_equal user_42_test, user_42_nested_test.superclass
|
29
|
+
assert_equal Test::Unit::TestCase, user_42_nested_test.superclass
|
30
|
+
#assert_equal 'nested', user_42_nested_test.name
|
31
|
+
|
32
|
+
# describe User42, '#new'
|
33
|
+
assert Object.const_defined?(:User42NewTest)
|
34
|
+
user_42_new_test = Object.const_get(:User42NewTest)
|
35
|
+
assert_equal Test::Unit::TestCase, user_42_new_test.superclass
|
36
|
+
#assert_equal 'User42#new', user_42_new_test.name
|
37
|
+
|
38
|
+
# describe Foo, 'bar', :baz
|
39
|
+
assert Object.const_defined?(:FoobarbazTest)
|
40
|
+
end
|
41
|
+
|
42
|
+
test "run spec_spec 'it' tests" do
|
43
|
+
at_exit { assert ::User42::SPEC_SAMPLE }
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
metadata
CHANGED
@@ -1,98 +1,105 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: test-unit-context
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.0
|
3
|
+
version: !ruby/object:Gem::Version
|
5
4
|
prerelease:
|
5
|
+
version: 0.3.0
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
- kares
|
7
|
+
authors:
|
8
|
+
- kares
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
46
|
-
description: ! "Makes Test::Unit::TestCases 'contextable' and thus much\neasier to
|
47
|
-
read and write. If you've seen RSpec than it's the very same 'context \ndo ... end'
|
48
|
-
re-invendet for Test::Unit. Inspired by gem 'context' that does the\nsame for the
|
49
|
-
'old' Test::Unit 1.2.3 bundled with Ruby 1.8.x standard libraries."
|
50
|
-
email:
|
51
|
-
- self@kares.org
|
12
|
+
|
13
|
+
date: 2013-02-11 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: test-unit
|
17
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.4.0
|
23
|
+
requirement: *id001
|
24
|
+
prerelease: false
|
25
|
+
type: :runtime
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
requirement: *id002
|
35
|
+
prerelease: false
|
36
|
+
type: :development
|
37
|
+
description: |-
|
38
|
+
Makes Test::Unit::TestCases 'contextable' and thus much
|
39
|
+
easier to read and write. If you've seen RSpec than it's the very same 'context
|
40
|
+
do ... end' re-invendet for Test::Unit. Inspired by gem 'context' that does the
|
41
|
+
same for the 'old' Test::Unit 1.2.3 bundled with Ruby 1.8.x standard libraries.
|
42
|
+
email:
|
43
|
+
- self@kares.org
|
52
44
|
executables: []
|
45
|
+
|
53
46
|
extensions: []
|
54
|
-
|
55
|
-
|
56
|
-
-
|
57
|
-
|
58
|
-
|
59
|
-
- .
|
60
|
-
-
|
61
|
-
-
|
62
|
-
-
|
63
|
-
-
|
64
|
-
-
|
65
|
-
- lib/test/unit/context
|
66
|
-
- lib/test/unit/context/
|
67
|
-
- lib/test/unit/context/
|
68
|
-
- lib/test/unit/context/
|
69
|
-
- test
|
70
|
-
-
|
71
|
-
- test
|
72
|
-
- test/test/unit/
|
73
|
-
- test/
|
47
|
+
|
48
|
+
extra_rdoc_files:
|
49
|
+
- README.md
|
50
|
+
- LICENSE
|
51
|
+
files:
|
52
|
+
- .gitignore
|
53
|
+
- .travis.yml
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/test/unit/context.rb
|
59
|
+
- lib/test/unit/context/context.rb
|
60
|
+
- lib/test/unit/context/helpers.rb
|
61
|
+
- lib/test/unit/context/shared.rb
|
62
|
+
- lib/test/unit/context/spec.rb
|
63
|
+
- lib/test/unit/context/version.rb
|
64
|
+
- test-unit-context.gemspec
|
65
|
+
- test/test/unit/context/hooks_test.rb
|
66
|
+
- test/test/unit/context/shared_test.rb
|
67
|
+
- test/test/unit/context/spec_spec.rb
|
68
|
+
- test/test/unit/context/spec_test.rb
|
69
|
+
- test/test/unit/context_test.rb
|
70
|
+
- test/test_helper.rb
|
74
71
|
homepage: http://github.com/kares/test-unit-context
|
75
72
|
licenses: []
|
73
|
+
|
76
74
|
post_install_message:
|
77
75
|
rdoc_options: []
|
78
|
-
|
79
|
-
|
80
|
-
|
76
|
+
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
80
|
none: false
|
82
|
-
requirements:
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 2
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
89
|
none: false
|
88
|
-
requirements:
|
89
|
-
|
90
|
-
|
91
|
-
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 2
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
92
97
|
requirements: []
|
98
|
+
|
93
99
|
rubyforge_project:
|
94
100
|
rubygems_version: 1.8.24
|
95
101
|
signing_key:
|
96
102
|
specification_version: 3
|
97
103
|
summary: Context for Test::Unit (2.x)
|
98
104
|
test_files: []
|
105
|
+
|