dan-manges-unit_record 0.4.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/CHANGELOG +11 -2
  2. data/LICENSE +20 -0
  3. data/README.markdown +137 -0
  4. data/Rakefile +42 -33
  5. data/lib/active_record/connection_adapters/unit_record_adapter.rb +4 -0
  6. data/lib/unit_record/association_stubbing.rb +3 -1
  7. data/lib/unit_record/disconnected_active_record.rb +4 -5
  8. data/test/active_record/connection_adapters/unit_record_adapter_test.rb +13 -0
  9. data/test/sample_spec.rb +40 -0
  10. data/test/test_helper.rb +9 -8
  11. data/test/unit_record/association_stubbing_test.rb +15 -0
  12. data/test/unit_record/disconnected_active_record_test.rb +4 -0
  13. data/vendor/dust-0.1.6/lib/array_extension.rb +5 -0
  14. data/vendor/dust-0.1.6/lib/definition_error.rb +20 -0
  15. data/vendor/dust-0.1.6/lib/dust.rb +8 -0
  16. data/vendor/dust-0.1.6/lib/nil_extension.rb +5 -0
  17. data/vendor/dust-0.1.6/lib/object_extension.rb +62 -0
  18. data/vendor/dust-0.1.6/lib/string_extension.rb +5 -0
  19. data/vendor/dust-0.1.6/lib/symbol_extension.rb +5 -0
  20. data/vendor/dust-0.1.6/lib/test_case_extension.rb +76 -0
  21. data/vendor/dust-0.1.6/rakefile.rb +50 -0
  22. data/vendor/dust-0.1.6/test/all_tests.rb +1 -0
  23. data/vendor/dust-0.1.6/test/failing_with_helper_unit_test.rb +16 -0
  24. data/vendor/dust-0.1.6/test/failing_with_setup_unit_test.rb +16 -0
  25. data/vendor/dust-0.1.6/test/functional_test.rb +12 -0
  26. data/vendor/dust-0.1.6/test/passing_unit_test.rb +11 -0
  27. data/vendor/dust-0.1.6/test/passing_with_helper_unit_test.rb +10 -0
  28. data/vendor/dust-0.1.6/test/passing_with_helpers_unit_test.rb +13 -0
  29. data/vendor/dust-0.1.6/test/passing_with_setup_unit_test.rb +10 -0
  30. data/vendor/dust-0.1.6/test/test_helper.rb +1 -0
  31. metadata +28 -13
data/CHANGELOG CHANGED
@@ -1,7 +1,16 @@
1
1
  *HEAD
2
2
 
3
- * Added UnitRecord::AssociationStubbing module to stub associations. Is not included by default.
4
- * Give exception message if trying to use fixtures instead of failing silently.
3
+ * Add association stubbing with mocha
4
+
5
+ ActiveRecord::Base.disconnect! :stub_associations => true
6
+
7
+ * Add option to no-op instead of raise.
8
+
9
+ ActiveRecord::Base.disconnect! :strategy => :noop
10
+ or
11
+ ActiveRecord::Base.disconnect! :strategy => :raise
12
+
13
+ * Implemented as a connection adapter
5
14
 
6
15
  *0.4.1* (December 10th, 2007)
7
16
 
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007-2008 Dan Manges
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/README.markdown ADDED
@@ -0,0 +1,137 @@
1
+ UnitRecord
2
+ ==========
3
+
4
+ Enables unit testing ActiveRecord classes without hitting the database.
5
+
6
+ Why?
7
+ ----
8
+
9
+ Rationale: [http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database](http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database)
10
+
11
+ One of the biggest benefits to disconnecting unit tests from the database is having a faster test suite. Here is the benchmark from one of my current projects:
12
+
13
+ Finished in 19.302702 seconds.
14
+ 4920 tests, 7878 assertions, 0 failures, 0 errors
15
+
16
+ 4 seconds per 1,000 tests is a good guideline.
17
+
18
+ Installation
19
+ ------------
20
+
21
+ gem install unit_record
22
+
23
+ Usage
24
+ -----
25
+
26
+ Restructuring the Rails Test Directory
27
+ --------------------------------------
28
+
29
+ The Rails test directory typically places testing for models under <tt>test/unit</tt> and tests for controllers under <tt>test/functional</tt>. However, we need to change the definition of unit and functional. Controllers can be unit tested (mocking out models and not rendering the view). Models can be functionally tested (hitting the database). Also, each type of test needs its own test\_helper. I recommend restructuring your test directory like this:
30
+
31
+ test
32
+ test_helper.rb
33
+ unit
34
+ unit_test_helper.rb
35
+ controllers
36
+ models
37
+ functional
38
+ functional_test_helper.rb
39
+ controllers
40
+ models
41
+
42
+ You should move existing functional tests into functional/controllers. You will also need to change the require line at the top of those tests to require the functional\_test\_helper.rb file instead of the test\_helper.rb file.
43
+
44
+ The <tt>functional_test_helper.rb</tt> file only needs to require <tt>test_helper.rb</tt>:
45
+
46
+ require File.dirname(__FILE__) + "/../test_helper"
47
+
48
+ For moving unit tests, you have a few options. I recommend moving them to unit/models and then disconnecting your unit tests from the database. Any tests that fail should then be modified to not hit the database or moved to functional/models.
49
+
50
+ Usage
51
+ -----
52
+
53
+ In the <tt>test/unit/unit\_test\_helper.rb</tt> file you created when restructuring your test directory, you should add these lines:
54
+
55
+ require File.dirname(__FILE__) + "/../test_helper"
56
+ require "unit_record"
57
+ ActiveRecord::Base.disconnect!
58
+
59
+ The <tt>disconnect!</tt> method will do everything necessary to run your unit tests without hitting the database.
60
+
61
+ Strategy
62
+ --------
63
+
64
+ There are two options for what should happen if you hit the database. You can either have UnitRecord raise an exception, or simply no-op. Raising an exception can help identify why a test is failing, but it also may be inconvenient to work around.
65
+
66
+ If you want to raise an exception:
67
+
68
+ ActiveRecord::Base.disconnect! :strategy => :raise
69
+
70
+ Person.find(:all)
71
+ #=> RuntimeError: ActiveRecord is disconnected; database access is unavailable in unit tests.
72
+
73
+ If you want to no-op:
74
+
75
+ ActiveRecord::Base.disconnect! :strategy => :noop
76
+
77
+ Person.find(:all)
78
+ #=> []
79
+
80
+ You can also change strategies within a block:
81
+
82
+ ActiveRecord::Base.connection.change_strategy(:raise) do
83
+ Person.find(:all)
84
+ #=> RuntimeError: ActiveRecord is disconnected; database access is unavailable in unit tests.
85
+ end
86
+
87
+ ActiveRecord::Base.connection.change_strategy(:noop) do
88
+ Person.find(:all)
89
+ #=> []
90
+ end
91
+
92
+ Association Stubbing
93
+ --------------------
94
+
95
+ One painful aspect of unit testing ActiveRecord classes is setting associations. Because Rails does type checking when setting an association, you'll receive an exception if you try to use a stub in place of the expected class.
96
+
97
+ Pet.new :owner => stub("person")
98
+ #=> ActiveRecord::AssociationTypeMismatch: Person(#16620740) expected, got Mocha::Mock(#11567340)
99
+
100
+ If you're using mocha, you can have UnitRecord stub associations. To enable association stubbing:
101
+
102
+ ActiveRecord::Base.disconnect! :stub_associations => true
103
+
104
+ The above example would no longer raise an exception. It would be the equivalent of:
105
+
106
+ pet = Pet.new
107
+ pet.stubs(:owner).returns(stub("person"))
108
+
109
+ Note that using this approach, the setter for the association will not work for that instance.
110
+
111
+ Development
112
+ -----------
113
+
114
+ Active development occurs on the [GitHub](http://github.com/dan-manges/unit-record). Changes are also pushed to the Rubyforge git repository.
115
+
116
+ For bugs/patches/etc please use the [Rubyforge tracker](http://rubyforge.org/tracker/?group_id=4239).
117
+
118
+ Continuous integration is provided by [RunCodeRun](http://runcoderun.com/dan-manges/unit-record).
119
+
120
+ Thanks
121
+ ------
122
+ Thanks to Jay Fields for the [original implementation](http://blog.jayfields.com/2007/03/rails-activerecord-unit-testing-part-ii.html).
123
+
124
+ Maintainer
125
+ ----------
126
+
127
+ [Dan Manges](http://www.dcmanges.com)
128
+
129
+ Contributors
130
+ ------------
131
+
132
+ * David Lowenfels
133
+ * Rob Sanheim
134
+
135
+ License
136
+ -------
137
+ Released under the MIT license
data/Rakefile CHANGED
@@ -1,62 +1,51 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
- require 'rake/rdoctask'
4
- require 'rake/gempackagetask'
5
- require 'rake/contrib/sshpublisher'
6
3
 
7
4
  desc "Default: run tests"
8
- task :default => :test
5
+ task :default => %w[test:multi_verbose spec]
9
6
 
10
7
  Rake::TestTask.new("test") do |t|
11
- t.libs << 'lib'
12
- t.pattern = 'test/**/*_test.rb'
8
+ t.pattern = "test/**/*_test.rb"
13
9
  t.verbose = true
14
10
  end
15
11
 
16
- desc "Generate documentation"
17
- Rake::RDocTask.new(:rdoc) do |rdoc|
18
- rdoc.rdoc_dir = "doc"
19
- rdoc.title = "UnitRecord"
20
- rdoc.options << '--line-numbers'
21
- rdoc.rdoc_files.include('README.rdoc', 'CHANGELOG')
22
- end
23
-
24
- desc "Upload RDoc to RubyForge"
25
- task :publish_rdoc => [:rdoc] do
26
- Rake::SshDirPublisher.new("dcmanges@rubyforge.org", "/var/www/gforge-projects/unit-test-ar", "doc").upload
12
+ begin
13
+ require "rcov/rcovtask"
14
+ desc "run tests with rcov"
15
+ Rcov::RcovTask.new do |t|
16
+ t.pattern = "test/**/*_test.rb"
17
+ t.rcov_opts << ["--no-html", "--exclude 'Library,#{Gem.path.join(',')}'"]
18
+ t.verbose = true
19
+ end
20
+ rescue LoadError
27
21
  end
28
22
 
29
- Gem.manage_gems
30
23
  require "date"
31
24
 
32
25
  gem_spec = Gem::Specification.new do |s|
33
26
  s.name = "unit_record"
34
27
  s.summary = "UnitRecord enables unit testing without hitting the database."
35
- s.version = "0.4.1"
28
+ s.version = "0.9.0"
36
29
  s.author = "Dan Manges"
37
30
  s.description = "UnitRecord enables unit testing without hitting the database."
38
31
  s.email = "daniel.manges@gmail.com"
39
32
  s.homepage = "http://unit-test-ar.rubyforge.org"
40
33
  s.rubyforge_project = "unit-test-ar"
41
-
42
- s.has_rdoc = true
43
- s.extra_rdoc_files = ['README.rdoc', 'CHANGELOG']
44
- s.rdoc_options << '--title' << "UnitRecord" << '--main' << 'README.rdoc' << '--line-numbers'
34
+
35
+ s.has_rdoc = false
45
36
 
46
37
  s.autorequire = "unit_record"
47
- s.files = FileList['{lib,test}/**/*.rb', 'CHANGELOG', 'README.rdoc', 'Rakefile'].to_a
38
+ s.files = FileList['{lib,test,vendor}/**/*.rb', 'CHANGELOG', 'LICENSE', 'README.markdown', 'Rakefile'].to_a
48
39
  end
49
40
 
50
- Rake::GemPackageTask.new(gem_spec) do |package|
51
- package.need_zip = false
52
- package.need_tar = false
41
+ task :gem => %w[test:multi] do
42
+ Gem::Builder.new(gem_spec).build
53
43
  end
54
- Rake::Task["gem"].prerequisites.unshift "test:multi"
55
44
 
56
45
  namespace :gemspec do
57
- desc "generates unit_record.gemspec"
46
+ desc "generates unit-record.gemspec"
58
47
  task :generate do
59
- File.open("unit_record.gemspec", "w") do |f|
48
+ File.open("unit-record.gemspec", "w") do |f|
60
49
  f.puts "# this file is generated by rake gemspec:generate for github"
61
50
  f.write gem_spec.to_ruby
62
51
  end
@@ -70,16 +59,36 @@ task :readme do
70
59
  sh "open #{file}"
71
60
  end
72
61
 
73
- RAILS_VERSIONS = %w[1.2.6 2.0.2 2.1.0 2.1.1]
62
+ RAILS_VERSIONS = %w[1.2.6 2.0.2 2.1.0 2.1.1 2.2.2]
74
63
 
75
64
  namespace :test do
76
65
  desc "test with multiple versions of rails"
77
66
  task :multi do
78
67
  RAILS_VERSIONS.each do |rails_version|
68
+ puts "Testing with Rails #{rails_version}"
79
69
  sh "RAILS_VERSION='#{rails_version}' rake test > /dev/null 2>&1"
80
70
  end
81
- end
71
+ end
72
+
73
+ task :multi_verbose do
74
+ (RAILS_VERSIONS - %w[2.2.2]).each do |rails_version|
75
+ task = defined?(Rcov) ? "rcov" : "test"
76
+ sh "RAILS_VERSION='#{rails_version}' rake #{task}"
77
+ end
78
+ end
79
+ end
80
+
81
+ begin
82
+ gem "rspec"
83
+ require "spec/rake/spectask"
84
+ Spec::Rake::SpecTask.new(:spec) do |t|
85
+ t.spec_files = %w[test/sample_spec.rb]
86
+ end
87
+ rescue LoadError
88
+ task :spec do
89
+ puts "== RSpec failed to load"
90
+ end
82
91
  end
83
92
 
84
93
  desc "pre-commit task"
85
- task :pc => %w[test:multi gemspec:generate]
94
+ task :pc => %w[test:multi spec gemspec:generate]
@@ -56,6 +56,10 @@ class ActiveRecord::ConnectionAdapters::UnitRecordAdapter < ::ActiveRecord::Conn
56
56
  def execute(sql, name = nil)
57
57
  raise_or_noop
58
58
  end
59
+
60
+ def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
61
+ raise_or_noop
62
+ end if Rails::VERSION::MAJOR == 1
59
63
 
60
64
  def select_rows(sql, name = nil)
61
65
  raise_or_noop []
@@ -15,7 +15,9 @@ module UnitRecord
15
15
  def extract_associations(attributes = {})
16
16
  attributes.inject({}) do |associations,(attr,value)|
17
17
  next associations unless self.class.reflections.keys.include? attr
18
- associations[attr] = attributes.delete attr
18
+ unless value.is_a?(self.class.reflections[attr].klass)
19
+ associations[attr] = attributes.delete attr
20
+ end
19
21
  associations
20
22
  end
21
23
  end
@@ -1,19 +1,18 @@
1
1
  module UnitRecord
2
2
  module DisconnectedActiveRecord
3
3
  def disconnected?
4
- false
4
+ connected? && connection.is_a?(ActiveRecord::ConnectionAdapters::UnitRecordAdapter)
5
5
  end
6
6
 
7
7
  def disconnect!(options = {})
8
8
  return if disconnected?
9
9
  establish_connection options.merge(:adapter => "unit_record")
10
- (class << self; self; end).class_eval do
11
- def disconnected?
12
- true
13
- end
10
+ if options[:stub_associations]
11
+ ActiveRecord::Base.send :include, UnitRecord::AssociationStubbing
14
12
  end
15
13
  Fixtures.disconnect!
16
14
  Test::Unit::TestCase.disconnect!
15
+ ActiveRecord::Migration.verbose = false
17
16
  ActiveRecord::Base.connection.change_strategy(:noop) do
18
17
  load(RAILS_ROOT + "/db/schema.rb")
19
18
  end
@@ -2,6 +2,12 @@ require File.dirname(__FILE__) + "/../../test_helper"
2
2
 
3
3
  functional_tests do
4
4
 
5
+ test "reconnect works" do
6
+ ActiveRecord::Base.connection.reconnect!
7
+ assert_kind_of ActiveRecord::ConnectionAdapters::UnitRecordAdapter,
8
+ ActiveRecord::Base.connection
9
+ end
10
+
5
11
  test "find(:all)" do
6
12
  ActiveRecord::Base.connection.change_strategy(:raise) do
7
13
  assert_raises(RuntimeError) { Person.find(:all) }
@@ -52,6 +58,12 @@ functional_tests do
52
58
  ActiveRecord::Base.connection.rename_column "people", "first_name", "name_first"
53
59
  end
54
60
  end
61
+
62
+ test "insert raises an exception" do
63
+ assert_raises_disconnected_exception do
64
+ ActiveRecord::Base.connection.rename_column "people", "first_name", "name_first"
65
+ end
66
+ end
55
67
 
56
68
  test "initialize can set strategy" do
57
69
  ActiveRecord::Base.establish_connection :adapter => "unit_record", :strategy => :noop
@@ -63,6 +75,7 @@ functional_tests do
63
75
  test "noop" do
64
76
  ActiveRecord::Base.connection.change_strategy(:noop) do
65
77
  assert_nil ActiveRecord::Base.connection.execute("SELECT 1")
78
+ assert_nil ActiveRecord::Base.connection.insert("INSERT INTO ...")
66
79
  assert_equal [], ActiveRecord::Base.connection.select_rows("SELECT * FROM people")
67
80
  assert_equal [], ActiveRecord::Base.connection.send(:select, "SELECT * FROM people")
68
81
  assert_nil ActiveRecord::Base.connection.rename_table("people", "persons")
@@ -0,0 +1,40 @@
1
+ require "rubygems"
2
+ gem "rspec", "1.1.11"
3
+ require "test/unit"
4
+ require "spec"
5
+
6
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
7
+
8
+ RAILS_ROOT = File.dirname(__FILE__)
9
+
10
+ if rails_version = ENV['RAILS_VERSION']
11
+ gem "rails", rails_version
12
+ end
13
+ require "rails/version"
14
+ puts "==== Testing with Rails #{Rails::VERSION::STRING} ===="
15
+ require 'active_record'
16
+ require 'active_record/fixtures'
17
+ require "action_controller"
18
+ require "action_controller/test_process"
19
+
20
+ require "unit_record"
21
+
22
+ # Needed because of this line in setup_with_fixtures and teardown_with_fixtures:
23
+ # return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?
24
+ ActiveRecord::Base.configurations = {"irrelevant" => {:adapter => "stub"}}
25
+
26
+ ActiveRecord::Base.disconnect! :strategy => :raise, :stub_associations => true
27
+
28
+ describe UnitRecord do
29
+ it "disconnects tests from the database" do
30
+ lambda do
31
+ ActiveRecord::Base.connection.select_value("SELECT 1")
32
+ end.should raise_error
33
+ end
34
+
35
+ it "can change strategy to noop" do
36
+ ActiveRecord::Base.connection.change_strategy(:noop) do
37
+ ActiveRecord::Base.connection.select_value("SELECT 1").should == nil
38
+ end
39
+ end
40
+ end
data/test/test_helper.rb CHANGED
@@ -20,14 +20,17 @@ end
20
20
  require "action_controller/test_process"
21
21
 
22
22
  begin
23
+ gem "mocha"
23
24
  require 'mocha'
24
- require 'dust'
25
- rescue LoadError
26
- raise "Need Mocha and Dust gems to Test"
25
+ rescue LoadError, Gem::LoadError
26
+ raise "need mocha to test"
27
27
  end
28
+ $LOAD_PATH << File.dirname(__FILE__) + "/../vendor/dust-0.1.6/lib"
29
+ require 'dust'
28
30
  Test::Unit::TestCase.disallow_setup!
29
31
 
30
- require "#{File.dirname(__FILE__)}/../init"
32
+ $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
33
+ require "unit_record"
31
34
 
32
35
  Test::Unit::TestCase.use_transactional_fixtures = true
33
36
 
@@ -35,8 +38,6 @@ Test::Unit::TestCase.use_transactional_fixtures = true
35
38
  # return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?
36
39
  ActiveRecord::Base.configurations = {"irrelevant" => {:adapter => "stub"}}
37
40
 
38
- ActiveRecord::Base.send :include, UnitRecord::AssociationStubbing
39
-
40
41
  class Preference < ActiveRecord::Base
41
42
  end
42
43
 
@@ -61,7 +62,7 @@ class DoesNotExist < ActiveRecord::Base
61
62
  set_table_name "table_does_not_exist"
62
63
  end
63
64
 
64
- ActiveRecord::Base.disconnect! :strategy => :raise
65
+ ActiveRecord::Base.disconnect! :strategy => :raise, :stub_associations => true
65
66
  # make sure calling disconnect multiple times does not cause problems
66
- ActiveRecord::Base.disconnect!
67
+ ActiveRecord::Base.disconnect! :strategy => :raise, :stub_associations => true
67
68
  end
@@ -14,6 +14,21 @@ functional_tests do
14
14
  assert_equal person, pet.person
15
15
  end
16
16
 
17
+ test "using correct classes does not stub" do
18
+ person = Person.new(:first_name => "Dan")
19
+ pet = Pet.new :person => person
20
+ pet.person = Person.new(:first_name => "Tom")
21
+ assert_equal "Tom", pet.person.first_name
22
+ end
23
+
24
+ test "using other than correct classes does stub" do
25
+ person = Object.new
26
+ def person.first_name; "Dan"; end
27
+ pet = Pet.new :person => person
28
+ pet.person = Person.new(:first_name => "Tom")
29
+ assert_equal "Dan", pet.person.first_name
30
+ end
31
+
17
32
  test "multiple includes doesn't hurt" do
18
33
  ActiveRecord::Base.send :include, UnitRecord::AssociationStubbing
19
34
  Person.new
@@ -45,4 +45,8 @@ functional_tests do
45
45
  assert_equal true, pref.show_help
46
46
  assert_equal true, pref.show_help?
47
47
  end
48
+
49
+ test "migrations are not verbose" do
50
+ assert_equal false, ActiveRecord::Migration.verbose
51
+ end
48
52
  end
@@ -0,0 +1,5 @@
1
+ class Array #:nodoc:
2
+ def arrayize
3
+ self
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module Dust #:nodoc:
2
+ # Dust::DefinitionError is raised when you attempt to define a disallowed method within a test file.
3
+ #
4
+ # Test::Unit::TestCase.disallow_setup!
5
+ #
6
+ # unit_tests do
7
+ # def setup
8
+ # ...
9
+ # end
10
+ #
11
+ # test "name" do
12
+ # ...
13
+ # end
14
+ # end
15
+ #
16
+ # The above code will generate the following error
17
+ # Dust::DefinitionError: setup is not allowed on class Units::[TestClassName]
18
+ class DefinitionError < StandardError
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+ require File.expand_path(File.dirname(__FILE__) + '/object_extension')
3
+ require File.expand_path(File.dirname(__FILE__) + '/array_extension')
4
+ require File.expand_path(File.dirname(__FILE__) + '/nil_extension')
5
+ require File.expand_path(File.dirname(__FILE__) + '/string_extension')
6
+ require File.expand_path(File.dirname(__FILE__) + '/symbol_extension')
7
+ require File.expand_path(File.dirname(__FILE__) + '/test_case_extension')
8
+ require File.expand_path(File.dirname(__FILE__) + '/definition_error')
@@ -0,0 +1,5 @@
1
+ class NilClass #:nodoc:
2
+ def arrayize
3
+ []
4
+ end
5
+ end
@@ -0,0 +1,62 @@
1
+ class Object
2
+ # call-seq: unit_tests(options={}, &block)
3
+ #
4
+ # Used to define a block of unit tests.
5
+ #
6
+ # unit_tests do
7
+ # test "verify something" do
8
+ # ...
9
+ # end
10
+ # end
11
+ #
12
+ # Configuration Options:
13
+ # * allow - Allows you to specify the methods that are allowed despite being disallowed.
14
+ # See Test::Unit::TestCase.disallow_helpers! or Test::Unit::TestCase.disallow_setup! for more info
15
+ def unit_tests(options={}, &block)
16
+ do_tests("Units", options, &block)
17
+ end
18
+
19
+ # call-seq: functional_tests(options={}, &block)
20
+ #
21
+ # Used to define a block of functional tests.
22
+ #
23
+ # functional_tests do
24
+ # test "verify something" do
25
+ # ...
26
+ # end
27
+ # end
28
+ #
29
+ # Configuration Options:
30
+ # * allow - Allows you to specify the methods that are allowed despite being disallowed.
31
+ # See Test::Unit::TestCase.disallow_helpers! or Test::Unit::TestCase.disallow_setup! for more info
32
+ def functional_tests(options={}, &block)
33
+ do_tests("Functionals", options, &block)
34
+ end
35
+
36
+ protected
37
+ def do_tests(type, options, &block) #:nodoc:
38
+ options[:allow] = options[:allow].arrayize
39
+ full_path_file_name = eval "__FILE__", block.binding
40
+ test_name = File.basename(full_path_file_name, ".rb")
41
+ test_class = eval "module #{type}; class #{test_name.to_class_name} < Test::Unit::TestCase; self; end; end"
42
+ test_class.class_eval &block
43
+ check_for_setup(test_class, options)
44
+ check_for_helpers(test_class, options)
45
+ end
46
+
47
+ def check_for_setup(test_class, options) #:nodoc:
48
+ if test_class.instance_methods(false).include?("setup") && Test::Unit::TestCase.disallow_setup? &&
49
+ !options[:allow].include?(:setup)
50
+ raise Dust::DefinitionError.new("setup is not allowed on class #{test_class.name}")
51
+ end
52
+ end
53
+
54
+ def check_for_helpers(test_class, options) #:nodoc:
55
+ test_class.instance_methods(false).each do |method_name|
56
+ if method_name !~ /^test_/ && Test::Unit::TestCase.disallow_helpers? && !options[:allow].include?(method_name.to_sym)
57
+ p method_name.to_sym
58
+ raise Dust::DefinitionError.new("helper methods are not allowed on class #{test_class.name}")
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,5 @@
1
+ class String #:nodoc:
2
+ def to_class_name
3
+ gsub(/(^|_)(.)/) { $2.upcase }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Symbol #:nodoc:
2
+ def arrayize
3
+ [self]
4
+ end
5
+ end
@@ -0,0 +1,76 @@
1
+ module Test #:nodoc:
2
+ module Unit #:nodoc:
3
+ class TestCase
4
+ # call-seq: disallow_setup!
5
+ #
6
+ # Used to disallow setup methods in test specifications.
7
+ #
8
+ # Test::Unit::TestCase.disallow_setup!
9
+ #
10
+ # A test specification can override this behavior by passing :setup in the :allow options.
11
+ #
12
+ # unit_tests :allow => :setup do
13
+ # def setup
14
+ # ...
15
+ # end
16
+ #
17
+ # test "verify something" do
18
+ # ...
19
+ # end
20
+ # end
21
+ def self.disallow_setup!
22
+ @disallow_setup = true
23
+ end
24
+
25
+ def self.disallow_setup? #:nodoc:
26
+ @disallow_setup
27
+ end
28
+
29
+ # call-seq: disallow_helpers!
30
+ #
31
+ # Used to disallow helper methods in test specifications.
32
+ #
33
+ # Test::Unit::TestCase.disallow_helper!
34
+ #
35
+ # A test specification can override this behavior by passing the helper name (as a symbol) in the :allow options.
36
+ #
37
+ # unit_tests :allow => [:create_something, :destroy_something] do
38
+ # test "verify something" do
39
+ # ...
40
+ # end
41
+ #
42
+ # def create_something
43
+ # ...
44
+ # end
45
+ #
46
+ # def destroy_something
47
+ # ...
48
+ # end
49
+ # end
50
+ def self.disallow_helpers!
51
+ @disallow_helpers = true
52
+ end
53
+
54
+ def self.disallow_helpers? #:nodoc:
55
+ @disallow_helpers
56
+ end
57
+
58
+ # call-seq: test(name, &block)
59
+ #
60
+ # Used to define a test and assign it a descriptive name.
61
+ #
62
+ # unit_tests do
63
+ # test "verify something" do
64
+ # ...
65
+ # end
66
+ # end
67
+ def self.test(name, &block)
68
+ test_name = "test_#{name.gsub(/[\s]/,'_')}".to_sym
69
+ raise "#{test_name} is already defined in #{self}" if self.instance_methods.include? test_name.to_s
70
+ define_method test_name do
71
+ instance_eval &block
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/contrib/sshpublisher'
5
+
6
+ task :default => :test
7
+
8
+ task :test do
9
+ require File.dirname(__FILE__) + '/test/all_tests.rb'
10
+ end
11
+
12
+ desc 'Generate RDoc'
13
+ Rake::RDocTask.new do |task|
14
+ task.main = 'README'
15
+ task.title = 'Dust'
16
+ task.rdoc_dir = 'doc'
17
+ task.options << "--line-numbers" << "--inline-source"
18
+ task.rdoc_files.include('README', 'lib/**/*.rb')
19
+ end
20
+
21
+ desc "Upload RDoc to RubyForge"
22
+ task :publish_rdoc => [:rdoc] do
23
+ Rake::SshDirPublisher.new("jaycfields@rubyforge.org", "/var/www/gforge-projects/dust", "doc").upload
24
+ end
25
+
26
+ Gem::manage_gems
27
+
28
+ specification = Gem::Specification.new do |s|
29
+ s.name = "dust"
30
+ s.summary = "Dust is an add on for Test::Unit that allows an alternative test definintion syntax."
31
+ s.version = "0.1.6"
32
+ s.author = 'Jay Fields'
33
+ s.description = "Dust is an add on for Test::Unit that allows an alternative test definintion syntax."
34
+ s.email = 'dust-developer@rubyforge.org'
35
+ s.homepage = 'http://dust.rubyforge.org'
36
+ s.rubyforge_project = 'dust'
37
+
38
+ s.has_rdoc = true
39
+ s.extra_rdoc_files = ['README']
40
+ s.rdoc_options << '--title' << 'Dust' << '--main' << 'README' << '--line-numbers'
41
+
42
+ s.autorequire = 'dust'
43
+ s.files = FileList['{lib,test}/**/*.rb', '[A-Z]*$', 'rakefile.rb'].to_a
44
+ s.test_file = "test/all_tests.rb"
45
+ end
46
+
47
+ Rake::GemPackageTask.new(specification) do |package|
48
+ package.need_zip = false
49
+ package.need_tar = false
50
+ end
@@ -0,0 +1 @@
1
+ Dir['**/*_test.rb'].each { |test_case| require test_case }
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ begin
4
+ unit_tests do
5
+ Test::Unit::TestCase.disallow_helpers!
6
+ def helper_method
7
+ end
8
+
9
+ test("true"){}
10
+ end
11
+ raise "shouldn't be here"
12
+ rescue Dust::DefinitionError => ex
13
+ raise unless ex.message == "helper methods are not allowed on class Units::FailingWithHelperUnitTest"
14
+ ensure
15
+ Test::Unit::TestCase.class_eval { @disallow_helpers = nil }
16
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ begin
4
+ unit_tests do
5
+ Test::Unit::TestCase.disallow_setup!
6
+ def setup
7
+ end
8
+
9
+ test("true"){}
10
+ end
11
+ raise "shouldn't be here"
12
+ rescue Dust::DefinitionError => ex
13
+ raise unless ex.message == "setup is not allowed on class Units::FailingWithSetupUnitTest"
14
+ ensure
15
+ Test::Unit::TestCase.class_eval { @disallow_setup = nil }
16
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ functional_tests do
4
+ test "assert true" do
5
+ assert_equal true, true
6
+ end
7
+
8
+ test "class name is Functionals::FunctionalTest" do
9
+ assert_equal "Functionals::FunctionalTest", self.class.name
10
+ end
11
+
12
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ unit_tests do
4
+ test "assert true" do
5
+ assert_equal true, true
6
+ end
7
+
8
+ test "class name is Units::PassingUnitTest" do
9
+ assert_equal "Units::PassingUnitTest", self.class.name
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ Test::Unit::TestCase.disallow_helpers!
4
+ unit_tests :allow => :helper do
5
+ def helper
6
+ end
7
+
8
+ test("true"){}
9
+ end
10
+ Test::Unit::TestCase.class_eval { @disallow_helpers = nil }
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ Test::Unit::TestCase.disallow_helpers!
4
+ unit_tests :allow => [:helper, :helper2] do
5
+ def helper
6
+ end
7
+
8
+ def helper2
9
+ end
10
+
11
+ test("true"){}
12
+ end
13
+ Test::Unit::TestCase.class_eval { @disallow_helpers = nil }
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ Test::Unit::TestCase.disallow_setup!
4
+ unit_tests :allow => :setup do
5
+ def setup
6
+ end
7
+
8
+ test("true"){}
9
+ end
10
+ Test::Unit::TestCase.class_eval { @disallow_setup = nil }
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/../lib/dust'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dan-manges-unit_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Manges
@@ -9,7 +9,7 @@ autorequire: unit_record
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-10 00:00:00 -07:00
12
+ date: 2008-12-15 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -19,9 +19,8 @@ executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
- extra_rdoc_files:
23
- - README.rdoc
24
- - CHANGELOG
22
+ extra_rdoc_files: []
23
+
25
24
  files:
26
25
  - lib/active_record/connection_adapters/unit_record_adapter.rb
27
26
  - lib/unit_record/association_stubbing.rb
@@ -32,6 +31,7 @@ files:
32
31
  - lib/unit_record.rb
33
32
  - test/active_record/connection_adapters/unit_record_adapter_test.rb
34
33
  - test/db/schema.rb
34
+ - test/sample_spec.rb
35
35
  - test/test_helper.rb
36
36
  - test/unit_record/association_stubbing_test.rb
37
37
  - test/unit_record/column_cacher_test.rb
@@ -41,18 +41,33 @@ files:
41
41
  - test/unit_record/disconnected_active_record_test.rb
42
42
  - test/unit_record/disconnected_fixtures_test.rb
43
43
  - test/unit_record/disconnected_test_case_test.rb
44
+ - vendor/dust-0.1.6/lib/array_extension.rb
45
+ - vendor/dust-0.1.6/lib/definition_error.rb
46
+ - vendor/dust-0.1.6/lib/dust.rb
47
+ - vendor/dust-0.1.6/lib/nil_extension.rb
48
+ - vendor/dust-0.1.6/lib/object_extension.rb
49
+ - vendor/dust-0.1.6/lib/string_extension.rb
50
+ - vendor/dust-0.1.6/lib/symbol_extension.rb
51
+ - vendor/dust-0.1.6/lib/test_case_extension.rb
52
+ - vendor/dust-0.1.6/rakefile.rb
53
+ - vendor/dust-0.1.6/test/all_tests.rb
54
+ - vendor/dust-0.1.6/test/failing_with_helper_unit_test.rb
55
+ - vendor/dust-0.1.6/test/failing_with_setup_unit_test.rb
56
+ - vendor/dust-0.1.6/test/functional_test.rb
57
+ - vendor/dust-0.1.6/test/passing_unit_test.rb
58
+ - vendor/dust-0.1.6/test/passing_with_helper_unit_test.rb
59
+ - vendor/dust-0.1.6/test/passing_with_helpers_unit_test.rb
60
+ - vendor/dust-0.1.6/test/passing_with_setup_unit_test.rb
61
+ - vendor/dust-0.1.6/test/test_helper.rb
44
62
  - CHANGELOG
45
- - README.rdoc
63
+ - LICENSE
64
+ - README.markdown
46
65
  - Rakefile
47
- has_rdoc: true
66
+ has_rdoc: false
48
67
  homepage: http://unit-test-ar.rubyforge.org
49
68
  post_install_message:
50
- rdoc_options:
51
- - --title
52
- - UnitRecord
53
- - --main
54
- - README.rdoc
55
- - --line-numbers
69
+ rdoc_options: []
70
+
56
71
  require_paths:
57
72
  - lib
58
73
  required_ruby_version: !ruby/object:Gem::Requirement