Roman2K-test-unit-ext 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,44 @@
1
+ Copyright (c) 2008-2009 Roman Le Négrate
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
21
+ ########################
22
+ # For assert_queries:
23
+ ########################
24
+
25
+ Copyright (c) 2004-2008 David Heinemeier Hansson
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining
28
+ a copy of this software and associated documentation files (the
29
+ "Software"), to deal in the Software without restriction, including
30
+ without limitation the rights to use, copy, modify, merge, publish,
31
+ distribute, sublicense, and/or sell copies of the Software, and to
32
+ permit persons to whom the Software is furnished to do so, subject to
33
+ the following conditions:
34
+
35
+ The above copyright notice and this permission notice shall be
36
+ included in all copies or substantial portions of the Software.
37
+
38
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
39
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
41
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
42
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
43
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
44
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # TestUnitExt
2
+
3
+ Helper methods for `Test::Unit::TestCase`. See the documentation of each method for a description and usage examples.
4
+
5
+ ## Installation
6
+
7
+ gem install Roman2K-test-unit-ext -s http://gems.github.com/
8
+
9
+ ## Usage
10
+
11
+ In `test_helper.rb` or any particular test file:
12
+
13
+ require 'test_unit_ext'
14
+
15
+ Test::Unit::TestCase.class_eval do
16
+ include TestUnitExt
17
+ end
18
+
19
+ If you are using [Mocha](http://mocha.rubyforge.org/), be sure to `require 'mocha'` first.
20
+
21
+ ## Credits
22
+
23
+ * Written by [Roman Le Négrate](http://roman.flucti.com) ([contact](mailto:roman.lenegrate@gmail.com)).
24
+ * Portions (`assert_queries` and related code) by [David Heinemeier Hansson](http://loudthinking.com).
25
+ * Released under the MIT license: see the `LICENSES` file.
@@ -0,0 +1,11 @@
1
+ require "echoe"
2
+
3
+ Echoe.new('test-unit-ext', '0.2.0') do |p|
4
+ p.description = "Helper methods for Test::Unit::TestCase"
5
+ p.url = "https://github.com/Roman2K/test-unit-ext"
6
+ p.author = "Roman Le Négrate"
7
+ p.email = "roman.lenegrate@gmail.com"
8
+ p.ignore_pattern = "*.gemspec"
9
+ p.development_dependencies = []
10
+ p.rdoc_options = %w(--main README.mdown --inline-source --line-numbers --charset UTF-8)
11
+ end
@@ -0,0 +1,22 @@
1
+ module TestUnitExt
2
+ autoload :ExceptionAssertions, 'test_unit_ext/exception_assertions'
3
+ autoload :OppositeExpectation, 'test_unit_ext/opposite_expectation'
4
+ autoload :EasyRecordInsertion, 'test_unit_ext/easy_record_insertion'
5
+ autoload :QueryCountAssertions, 'test_unit_ext/query_count_assertions'
6
+
7
+ include ExceptionAssertions
8
+ include OppositeExpectation
9
+ if defined? ActiveRecord
10
+ include EasyRecordInsertion
11
+ include QueryCountAssertions
12
+ end
13
+ end
14
+
15
+ Object.class_eval do
16
+ alias ieval :instance_eval
17
+ end
18
+
19
+ if defined? Mocha::Configuration
20
+ Mocha::Configuration.prevent :stubbing_non_existent_method
21
+ Mocha::Configuration.warn_when :stubbing_method_unnecessarily
22
+ end
@@ -0,0 +1,34 @@
1
+ # Details in the {introduction article}[http://roman.flucti.com/painless-record-creation-with-activerecord].
2
+ module TestUnitExt::EasyRecordInsertion
3
+ # Usage examples:
4
+ #
5
+ # insert! Host, :ip_address => '192.168.0.1'
6
+ # insert! Host, {:hostname => 'foo'}, :trigger_validation => true
7
+ # insert! Host, {}, :inhibit_callbacks => false
8
+ #
9
+ def insert!(model, attributes={}, options={})
10
+ attributes = attributes.stringify_keys
11
+ trigger_validation = options.fetch(:trigger_validation, false)
12
+ inhibit_callbacks = options.fetch(:inhibit_callbacks, !trigger_validation)
13
+ begin
14
+ record = model.new { |record| record.send(:attributes=, attributes, false) }
15
+ if inhibit_callbacks
16
+ def record.callback(*args)
17
+ end
18
+ end
19
+ if trigger_validation
20
+ record.valid?
21
+ end
22
+ record.save(false)
23
+ rescue ActiveRecord::StatementInvalid
24
+ if $!.message =~ /Column '(.+?)' cannot be null/
25
+ unless attributes.key?($1)
26
+ attributes[$1] = record.column_for_attribute($1).number? ? 0 : "-"
27
+ retry
28
+ end
29
+ end
30
+ raise
31
+ end
32
+ return record
33
+ end
34
+ end
@@ -0,0 +1,46 @@
1
+ module TestUnitExt::ExceptionAssertions
2
+ # Same as the stock equivalent, with the addition of the message being matched
3
+ # against the exception raised within the block.
4
+ #
5
+ # Messages can be matched in a strict manner:
6
+ #
7
+ # assert_raise(RuntimeError, "the message") do
8
+ # raise "the message"
9
+ # end
10
+ #
11
+ # Or loosely:
12
+ #
13
+ # assert_raise(ArgumentError, /wrong number of arguments/) do
14
+ # "hello".gsub(/e/)
15
+ # end
16
+ #
17
+ def assert_raise(exception_class, message=nil)
18
+ begin
19
+ yield
20
+ rescue => error
21
+ end
22
+ exc_expected_msg = message ? ": #{message.inspect}" : ''
23
+ assertion_message = "expected to raise <#{exception_class}#{exc_expected_msg}> but raised #{error.inspect}"
24
+ assertion_message << "\n#{(error.backtrace || []).join("\n").gsub(/^/, "\t")}" if error
25
+ assert_block(assertion_message) do
26
+ exception_class === error && (message || //) === (error.message || '')
27
+ end
28
+ return error
29
+ end
30
+
31
+ # Catch any exception of a given type that would be raised within the block.
32
+ #
33
+ # For example:
34
+ #
35
+ # err = catch_error { puts "success" } # => nil
36
+ # err = catch_error { raise "error" } # => #<RuntimeError: error>
37
+ #
38
+ def catch_error(type=Exception)
39
+ begin
40
+ yield
41
+ rescue => error
42
+ end
43
+ assert_kind_of(type, error)
44
+ return error
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ module TestUnitExt::OppositeExpectation
2
+ # Usage example:
3
+ #
4
+ # host = Host.new
5
+ # expects_chain(host, 'portage.install').returns :success
6
+ #
7
+ # host.portage.install # => :success
8
+ #
9
+ def expects_chain(obj, chain)
10
+ stub_chain_for_object_with(:expects, :mock, obj, chain)
11
+ end
12
+
13
+ # Usage example:
14
+ #
15
+ # host = Host.new
16
+ # stubs_chain(host, 'portage.installed?').returns false
17
+ #
18
+ # host.portage.installed? # => false
19
+ #
20
+ def stubs_chain(obj, chain)
21
+ stub_chain_for_object_with(:stubs, :stub, obj, chain)
22
+ end
23
+
24
+ # Usage example:
25
+ #
26
+ # host = Host.new
27
+ # expects_chain_never(host, 'portage.uninstall')
28
+ #
29
+ # host.portage.uninstall # does nothing
30
+ #
31
+ def expects_chain_never(obj, chain)
32
+ chain = parse_chain! chain
33
+ obj.expects(chain.first.to_sym).never
34
+ end
35
+
36
+ private
37
+
38
+ def stub_chain_for_object_with(method, receiver_builder, obj, chain)
39
+ chain = parse_chain! chain
40
+ expectation, receiver = nil, obj
41
+ loop do
42
+ expectation = receiver.send(method, chain.shift.to_sym)
43
+ break if chain.empty?
44
+ expectation.returns(receiver = send(receiver_builder))
45
+ end
46
+ return expectation
47
+ end
48
+
49
+ def parse_chain!(chain)
50
+ chain = chain.to_s.split('.')
51
+ raise ArgumentError, "empty call chain" if chain.empty?
52
+ return chain
53
+ end
54
+ end
@@ -0,0 +1,24 @@
1
+ module TestUnitExt::QueryCountAssertions
2
+ # Extracted from activerecord/test/cases/helper.rb
3
+ ActiveRecord::Base.connection.class.class_eval do
4
+ unless method_defined? :execute_with_query_record
5
+ unless const_defined? :IGNORED_SQL
6
+ IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SHOW FIELDS/]
7
+ end
8
+ def execute_with_query_record(sql, name = nil, &block)
9
+ $queries_executed ||= []
10
+ $queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r }
11
+ execute_without_query_record(sql, name, &block)
12
+ end
13
+ alias_method_chain :execute, :query_record
14
+ end
15
+ end
16
+
17
+ # Extracted from activerecord/lib/active_record/test_case.rb
18
+ def assert_queries(num=1)
19
+ $queries_executed = []
20
+ yield
21
+ ensure
22
+ assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}"
23
+ end
24
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{test-unit-ext}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Roman Le N\303\251grate"]
9
+ s.date = %q{2009-03-14}
10
+ s.description = %q{Helper methods for Test::Unit::TestCase}
11
+ s.email = %q{roman.lenegrate@gmail.com}
12
+ s.extra_rdoc_files = ["lib/test_unit_ext/easy_record_insertion.rb", "lib/test_unit_ext/exception_assertions.rb", "lib/test_unit_ext/opposite_expectation.rb", "lib/test_unit_ext/query_count_assertions.rb", "lib/test_unit_ext.rb", "LICENSES", "README.mdown"]
13
+ s.files = ["lib/test_unit_ext/easy_record_insertion.rb", "lib/test_unit_ext/exception_assertions.rb", "lib/test_unit_ext/opposite_expectation.rb", "lib/test_unit_ext/query_count_assertions.rb", "lib/test_unit_ext.rb", "LICENSES", "Manifest", "Rakefile", "README.mdown", "test-unit-ext.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{https://github.com/Roman2K/test-unit-ext}
16
+ s.rdoc_options = ["--main", "README.mdown", "--inline-source", "--line-numbers", "--charset", "UTF-8"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{test-unit-ext}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Helper methods for Test::Unit::TestCase}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Roman2K-test-unit-ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - "Roman Le N\xC3\xA9grate"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Helper methods for Test::Unit::TestCase
17
+ email: roman.lenegrate@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/test_unit_ext/easy_record_insertion.rb
24
+ - lib/test_unit_ext/exception_assertions.rb
25
+ - lib/test_unit_ext/opposite_expectation.rb
26
+ - lib/test_unit_ext/query_count_assertions.rb
27
+ - lib/test_unit_ext.rb
28
+ - LICENSES
29
+ - README.mdown
30
+ files:
31
+ - lib/test_unit_ext/easy_record_insertion.rb
32
+ - lib/test_unit_ext/exception_assertions.rb
33
+ - lib/test_unit_ext/opposite_expectation.rb
34
+ - lib/test_unit_ext/query_count_assertions.rb
35
+ - lib/test_unit_ext.rb
36
+ - LICENSES
37
+ - Manifest
38
+ - Rakefile
39
+ - README.mdown
40
+ - test-unit-ext.gemspec
41
+ has_rdoc: true
42
+ homepage: https://github.com/Roman2K/test-unit-ext
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --main
46
+ - README.mdown
47
+ - --inline-source
48
+ - --line-numbers
49
+ - --charset
50
+ - UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "1.2"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: test-unit-ext
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: Helper methods for Test::Unit::TestCase
72
+ test_files: []
73
+