jeremymcanally-context 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,10 @@
1
+ == 0.0.3 2008-10-05
2
+
3
+ * 1 minor enhancement:
4
+ * Fiddled with the way contexts are defined; makes the object model make more sense and the code cleaner
5
+
6
+ == 0.0.1 2008-10-02
7
+
8
+ * 1 major enhancement:
9
+ * Initial release
10
+ * Contexts, lifecycle, and test methods
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Jeremy McAnally
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,25 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ PostInstall.txt
5
+ README.rdoc
6
+ Rakefile
7
+ config/hoe.rb
8
+ config/requirements.rb
9
+ lib/context.rb
10
+ lib/context/version.rb
11
+ lib/context/lifecycle.rb
12
+ lib/context/suite.rb
13
+ lib/context/context.rb
14
+ lib/context/test.rb
15
+ lib/context/version.rb
16
+ lib/context/shared_behavior.rb
17
+ lib/context/core_ext/string.rb
18
+ setup.rb
19
+ tasks/deployment.rake
20
+ tasks/environment.rake
21
+ test/test_context.rb
22
+ test/test_core_ext.rb
23
+ test/test_lifecycle.rb
24
+ test/test_test.rb
25
+ test/test_helper.rb
data/PostInstall.txt ADDED
File without changes
data/README.rdoc ADDED
@@ -0,0 +1,158 @@
1
+ = context – The testing library you've been looking for
2
+
3
+ http://github.com/jeremymcanally/context
4
+
5
+ == DESCRIPTION:
6
+
7
+ If you've ever wanted contexts in your Test::Unit tests, then context is for you. Your tests will be easier to read and write without all the magic and extra code smell!
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Add contexts to Test::Unit tests
12
+ * Small DSL for specifying tests that are pretty
13
+ * Ability to chain context lifecycle methods (coming soon)
14
+
15
+ == SYNOPSIS:
16
+
17
+ * Add contexts using familiar syntax:
18
+
19
+ class UserTest < Test::Unit::TestCase
20
+ context "A new User" do
21
+ # Before/after lifecycle blocks
22
+ before do
23
+ @user = User.first
24
+ end
25
+
26
+ # Specify tests using DSL
27
+ test "should have the right full_name" do
28
+ assert_equal "Dude Man", @user.full_name
29
+ end
30
+
31
+ test "should be able to set parts of the name" do
32
+ @user.first_name = "Mad"
33
+ @user.last_name = "Max"
34
+ @user.save
35
+ assert_equal "Mad Max", @user.full_name
36
+ end
37
+
38
+ after do
39
+ @user.first_name = "Dude"
40
+ @user.last_name = "Man"
41
+ @user.save!
42
+ end
43
+ end
44
+ end
45
+
46
+ * It also has aliases that match other library's syntaxes (all of which can be mixed and matched):
47
+
48
+ class UserTest < Test::Unit::TestCase
49
+ context "A new Account" do
50
+ test "should be new" do
51
+ Account.new.new_record?
52
+ end
53
+ end
54
+
55
+ # RSpec-esque
56
+ describe "A new User" do
57
+ it "should do things" do
58
+ User.first.do_things!
59
+ end
60
+ end
61
+
62
+ # Shoulda-esque
63
+ context "Another User" do
64
+ should "do things that are fun" do
65
+ User.first.do_things!(:fun)
66
+ end
67
+ end
68
+ end
69
+
70
+ * Contexts can also be nested:
71
+
72
+ class UserTest < Test::Unit::TestCase
73
+ context "A new User" do
74
+ context "with clown shoes" do
75
+ test "should squeak" do
76
+ assert_true User.find_by_shoes("clown").squeak?
77
+ end
78
+ end
79
+
80
+ context "without clown shoes" do
81
+ test "should not squeak" do
82
+ assert_false User.find_by_shoes("dressy").squeak?
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ * You can also share behavior among contexts:
89
+
90
+ class UserTest < Test::Unit::TestCase
91
+ shared "shared things" do
92
+ test "things are shared" do
93
+ # test logic here...
94
+ end
95
+ end
96
+
97
+ context "the first thing" do
98
+ uses "shared things"
99
+
100
+ test "other things..." do
101
+ # More testing...
102
+ end
103
+ end
104
+ end
105
+
106
+ * Shared behaviors can also use RSpec syntax
107
+
108
+ class UserTest < Test::Unit::TestCase
109
+ share_examples_for "shared things" do
110
+ it "things are shared" do
111
+ # test logic here...
112
+ end
113
+ end
114
+
115
+ describe "the first thing" do
116
+ it_should_behave_like "shared things"
117
+
118
+ it "other things..." do
119
+ # More testing...
120
+ end
121
+ end
122
+ end
123
+
124
+ == REQUIREMENTS:
125
+
126
+ * Test::Unit (you have it; trust me)
127
+
128
+ == INSTALL:
129
+
130
+ $ gem sources -a http://gems.github.com
131
+ $ sudo gem install jeremymcanally-context
132
+
133
+ == LICENSE:
134
+
135
+ Copyright (c) 2008 Jeremy McAnally
136
+
137
+ Permission is hereby granted, free of charge, to any person obtaining
138
+ a copy of this software and associated documentation files (the
139
+ 'Software'), to deal in the Software without restriction, including
140
+ without limitation the rights to use, copy, modify, merge, publish,
141
+ distribute, sublicense, and/or sell copies of the Software, and to
142
+ permit persons to whom the Software is furnished to do so, subject to
143
+ the following conditions:
144
+
145
+ The above copyright notice and this permission notice shall be
146
+ included in all copies or substantial portions of the Software.
147
+
148
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
149
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
150
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
151
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
152
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
153
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
154
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
155
+
156
+ == ACKNOWLEDGEMENTS:
157
+
158
+ Original implementation by myself, but heavily tweaked and borrowed from Rails Core and Pratik Naik.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'context/version'
2
+
3
+ AUTHOR = 'Jeremy McAnally' # can also be an array of Authors
4
+ EMAIL = "jeremy@entp.com"
5
+ DESCRIPTION = "Contexts and DSL sugar for your tests"
6
+ GEM_NAME = 'context' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'context' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+ EXTRA_DEPENDENCIES = [
11
+ # ['activesupport', '>= 1.3.1']
12
+ ] # An array of rubygem dependencies [name, version]
13
+
14
+ @config_file = "~/.rubyforge/user-config.yml"
15
+ @config = nil
16
+ RUBYFORGE_USERNAME = "unknown"
17
+ def rubyforge_username
18
+ unless @config
19
+ begin
20
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
21
+ rescue
22
+ puts <<-EOS
23
+ ERROR: No rubyforge config file found: #{@config_file}
24
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
25
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
26
+ EOS
27
+ exit
28
+ end
29
+ end
30
+ RUBYFORGE_USERNAME.replace @config["username"]
31
+ end
32
+
33
+
34
+ REV = nil
35
+ # UNCOMMENT IF REQUIRED:
36
+ # REV = YAML.load(`svn info`)['Revision']
37
+ VERS = Context::VERSION::STRING + (REV ? ".#{REV}" : "")
38
+ RDOC_OPTS = ['--quiet', '--title', 'context documentation',
39
+ "--opname", "index.html",
40
+ "--line-numbers",
41
+ "--main", "README.rdoc",
42
+ "--inline-source"]
43
+
44
+ class Hoe
45
+ def extra_deps
46
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
47
+ @extra_deps
48
+ end
49
+ end
50
+
51
+ # Generate all the Rake tasks
52
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
53
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
54
+ p.developer(AUTHOR, EMAIL)
55
+ p.description = DESCRIPTION
56
+ p.summary = DESCRIPTION
57
+ p.url = HOMEPATH
58
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
59
+ p.test_globs = ["test/**/test_*.rb"]
60
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
61
+
62
+ # == Optional
63
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
64
+ #p.extra_deps = EXTRA_DEPENDENCIES
65
+
66
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
67
+ end
68
+
69
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
70
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
71
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
72
+ $hoe.rsync_args = '-av --delete --ignore-errors'
73
+ $hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
data/context.gemspec ADDED
@@ -0,0 +1,43 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "context"
3
+ s.version = "0.0.3"
4
+ s.date = "2008-10-03"
5
+ s.summary = "Contexts and DSL sugar for your tests"
6
+ s.email = "jeremy@entp.com"
7
+ s.homepage = "http://github.com/jeremymcanally/context"
8
+ s.description = "If you've ever wanted contexts in your Test::Unit tests, then context is for you. Your tests will be easier to read and write without all the magic and extra code smell!"
9
+ s.has_rdoc = true
10
+ s.authors = ["Jeremy McAnally"]
11
+ s.files = [
12
+ "README.rdoc",
13
+ "Rakefile",
14
+ "context.gemspec",
15
+ "History.txt",
16
+ "License.txt",
17
+ "Manifest.txt",
18
+ "PostInstall.txt",
19
+ "config/hoe.rb",
20
+ "config/requirements.rb",
21
+ "lib/context.rb",
22
+ "lib/context/version.rb",
23
+ "lib/context/lifecycle.rb",
24
+ "lib/context/suite.rb",
25
+ "lib/context/context.rb",
26
+ "lib/context/shared_behavior.rb",
27
+ "lib/context/test.rb",
28
+ "lib/context/version.rb",
29
+ "lib/context/core_ext/string.rb",
30
+ "setup.rb"
31
+ ]
32
+
33
+ s.test_files = [
34
+ "test/test_context.rb",
35
+ "test/test_core_ext.rb",
36
+ "test/test_lifecycle.rb",
37
+ "test/test_test.rb",
38
+ "test/test_helper.rb"
39
+ ]
40
+
41
+ s.rdoc_options = ["--main", "README.rdoc"]
42
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
43
+ end
@@ -0,0 +1,52 @@
1
+ class Test::Unit::TestCase
2
+ class << self
3
+ def context_name #:nodoc:
4
+ @context_name ||= ""
5
+ if superclass.respond_to?(:context_name)
6
+ return "#{superclass.context_name} #{@context_name}".gsub(/^\s+/, "")
7
+ end
8
+ end
9
+
10
+ def context_name=(val) #:nodoc:
11
+ @context_name = val
12
+ end
13
+
14
+ # Add a context to a set of tests.
15
+ #
16
+ # context "A new account" do
17
+ # it "should not have users"
18
+ # assert Account.new.users.empty?
19
+ # end
20
+ # end
21
+ #
22
+ # The context name is prepended to the test name, so failures look like this:
23
+ #
24
+ # 1) Failure:
25
+ # test_a_new_account_should_not_have_users() [./test/test_accounts.rb:4]:
26
+ # <false> is not true.
27
+ #
28
+ # Contexts can also be nested like so:
29
+ #
30
+ # context "A new account" do
31
+ # context "created by the web application" do
32
+ # it "should have web as its vendor" do
33
+ # assert_equal "web", users(:web_user).vendor
34
+ # end
35
+ # end
36
+ # end
37
+ #
38
+ # Since contexts create a singleton instance of a class, each one must have its own before/after blocks. This
39
+ # will be tweaked in future releases to allow you to chain these blocks from its parent contexts.
40
+ #
41
+ def context(name, &block)
42
+ cls = Class.new(self)
43
+ cls.context_name = name
44
+ puts "Creating context #{cls.context_name}"
45
+ cls.class_eval(&block)
46
+
47
+ cls
48
+ end
49
+
50
+ %w(contexts describe describes group specify specifies).each {|m| alias_method m, :context}
51
+ end
52
+ end
@@ -0,0 +1,11 @@
1
+ class String
2
+ # Replaces spaces and tabs with _ so we can use the string as a method name
3
+ def to_method_name
4
+ self.downcase.gsub(/\s+/,'_')
5
+ end
6
+
7
+ # Borrowed from +camelize+ in ActiveSupport
8
+ def to_module_name
9
+ self.to_method_name.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ class Test::Unit::TestCase
2
+ # TODO: Chained lifecycle methods
3
+ class << self
4
+ # Add logic to run before the tests (i.e., a +setup+ method)
5
+ #
6
+ # before do
7
+ # @user = User.first
8
+ # end
9
+ #
10
+ def before(&block)
11
+ define_method(:setup, &block)
12
+ end
13
+
14
+ # Add logic to run after the tests (i.e., a +teardown+ method)
15
+ #
16
+ # after do
17
+ # User.delete_all
18
+ # end
19
+ #
20
+ def after(&block)
21
+ define_method(:teardown, &block)
22
+ end
23
+ end
24
+ end