minitest-predicates 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ # require 'minitest/predicates' will automatically include
2
+ # predicate support into MiniTest::Unit::TestCase classes and
3
+ # include predicate support on Object.
4
+ #
5
+ # To manually include this functionality, require 'minitest/predicates/core'
6
+ # and include the modules manually.
7
+
8
+ require 'minitest/unit'
9
+ require 'minitest/predicates/core'
10
+
11
+ MiniTestPredicates.enable_assert_syntax!
12
+ MiniTestPredicates.enable_spec_syntax!
@@ -0,0 +1,82 @@
1
+ require 'minitest-predicates/version'
2
+
3
+ module MiniTestPredicates
4
+
5
+ # Includes MiniTestPredicates::Unit into MiniTest::Unit::TestCase so
6
+ # must_be_foo @object and wont_be_foo @object
7
+ def self.enable_assert_syntax!
8
+ MiniTest::Unit::TestCase.send :include, MiniTestPredicates::Unit
9
+ end
10
+
11
+ # Includes MiniTestPredicates::Spec into Object so
12
+ # @object.must_be_foo and @object.wont_be_foo will work.
13
+ def self.enable_spec_syntax!
14
+ Object.send :include, MiniTestPredicates::Spec
15
+ end
16
+
17
+ # @private
18
+ def self.make_assertion! options
19
+ args = options[:args]
20
+
21
+ # Given the name of the method called and the prefaces for
22
+ # positive and negative assertions, get the name of the
23
+ # predicate method and whether this is an assert or a refute.
24
+ positive = nil
25
+ predicate_method = nil
26
+ case options[:name].to_s
27
+ when *options[:positive]
28
+ positive = true
29
+ predicate_method = $1
30
+ when *options[:negative]
31
+ positive = false
32
+ predicate_method = $1
33
+ else
34
+ return :super
35
+ end
36
+
37
+ # Get the value of the predicate method.
38
+ # We try calling the method instead of checking is the
39
+ # object respond_to? the method to support objects like
40
+ # OpenStruct where method_missing is implemented but not respond_to?
41
+ value = nil
42
+ begin
43
+ value = options[:object].send predicate_method + '?', *args
44
+ rescue NoMethodError
45
+ return :super
46
+ end
47
+
48
+ # Check the value, based on whether we're checking for negative or positive, and make the real assertion!
49
+ if (positive and value == false) or (not positive and value == true)
50
+ if args.empty?
51
+ message = "Expected #{options[:object].inspect} to#{positive ? '' : ' not'} be #{predicate_method.sub(/\?$/, '')}"
52
+ else
53
+ formatted_args = args.map(&:inspect).join(', ')
54
+ message = "Expected #{options[:object].inspect} to#{positive ? '' : ' not'} #{predicate_method.sub(/\?$/, '')} #{formatted_args}"
55
+ end
56
+ MiniTest::Spec.current.assert false, message
57
+ end
58
+ end
59
+
60
+ module Unit
61
+ def method_missing name, *args
62
+ result = MiniTestPredicates.make_assertion! :name => name,
63
+ :object => args.pop,
64
+ :args => args,
65
+ :positive => [/^assert_(.+)$/],
66
+ :negative => [/^refute_(.+)$/]
67
+ result == :super ? super : result
68
+ end
69
+ end
70
+
71
+ module Spec
72
+ def method_missing name, *args
73
+ result = MiniTestPredicates.make_assertion! :name => name,
74
+ :object => self,
75
+ :args => args,
76
+ :positive => [/^must_be_(.+)$/, /^must_(.+)$/],
77
+ :negative => [/^wont_be_(.+)$/, /^wont_(.+)$/]
78
+ result == :super ? super : result
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,8 @@
1
+ module MiniTestPredicates
2
+ begin
3
+ old, $VERBOSE = $VERBOSE, nil
4
+ VERSION = '0.1.0'
5
+ ensure
6
+ $VERBOSE = old
7
+ end
8
+ end
@@ -0,0 +1,2 @@
1
+ # Shortcut to let you require 'minitest/predicates'
2
+ require 'minitest-predicates'
@@ -0,0 +1,2 @@
1
+ # Shortcut to let you require 'minitest/predicates/core'
2
+ require 'minitest-predicates/core'
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-predicates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - remi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-06 00:00:00.000000000 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: Adds support for .predicate? methods to MiniTest (via method_missing)
16
+ email: remi@remitaylor.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/minitest/predicates/core.rb
22
+ - lib/minitest/predicates.rb
23
+ - lib/minitest-predicates/core.rb
24
+ - lib/minitest-predicates/version.rb
25
+ - lib/minitest-predicates.rb
26
+ has_rdoc: true
27
+ homepage: http://github.com/remi/minitest-assertions
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.6.2
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Adds support for .predicate? methods
51
+ test_files: []