assay 0.4.1 → 0.5.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.
Files changed (142) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +7 -0
  3. data/HISTORY.md +128 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +287 -0
  6. data/demo/ae/01_overview.md +98 -0
  7. data/demo/ae/03_assert.md +398 -0
  8. data/demo/ae/04_subjunctive.md +100 -0
  9. data/demo/ae/05_expect.md +136 -0
  10. data/demo/ae/06_counts.md +33 -0
  11. data/demo/ae/07_matchers.md +34 -0
  12. data/demo/ae/08_check.md +61 -0
  13. data/demo/ae/09_assay.md +124 -0
  14. data/demo/ae/applique/setup.rb +11 -0
  15. data/{test → demo/applique}/helper.rb +8 -2
  16. data/demo/applique/setup.rb +1 -0
  17. data/demo/bang/01_bang.md +220 -0
  18. data/demo/core/00_introduction.md +2 -0
  19. data/demo/core/01_assay_classes/00_introduction.md +2 -0
  20. data/demo/core/01_assay_classes/01_nil_assay.md +35 -0
  21. data/demo/core/01_assay_classes/02_boolean_assay.md +35 -0
  22. data/demo/core/01_assay_classes/03_false_assay.md +19 -0
  23. data/demo/core/01_assay_classes/03_true_assay.md +20 -0
  24. data/demo/core/01_assay_classes/04_like_assay.md +19 -0
  25. data/demo/core/01_assay_classes/05_equal_assay.md +19 -0
  26. data/demo/core/01_assay_classes/06_unequal_assay.md +35 -0
  27. data/demo/core/01_assay_classes/07_equality_assay.md +19 -0
  28. data/demo/core/01_assay_classes/08_identity_assay.md +24 -0
  29. data/demo/core/01_assay_classes/09_case_assay.md +22 -0
  30. data/demo/core/01_assay_classes/10_match_assay.md +15 -0
  31. data/demo/core/01_assay_classes/11_nomatch_assay.md +16 -0
  32. data/demo/core/01_assay_classes/12_compare_assay.md +24 -0
  33. data/demo/core/01_assay_classes/13_less_assay.md +29 -0
  34. data/demo/core/01_assay_classes/14_more_assay.md +29 -0
  35. data/demo/core/01_assay_classes/15_less_equal_assay.md +30 -0
  36. data/demo/core/01_assay_classes/16_more_equal_assay.md +29 -0
  37. data/demo/core/01_assay_classes/17_close_assay.md +42 -0
  38. data/demo/core/01_assay_classes/17_within_assay.md +42 -0
  39. data/demo/core/01_assay_classes/18_kind_assay.md +17 -0
  40. data/demo/core/01_assay_classes/19_instance_assay.md +16 -0
  41. data/demo/core/01_assay_classes/20_include_assay.md +17 -0
  42. data/demo/core/01_assay_classes/21_empty_assay.md +15 -0
  43. data/demo/core/01_assay_classes/22_respond_assay.md +15 -0
  44. data/demo/core/01_assay_classes/23_execution_assay.md +40 -0
  45. data/demo/core/01_assay_classes/23_return_assay.md +37 -0
  46. data/demo/core/01_assay_classes/24_rescue_assay.md +40 -0
  47. data/demo/core/01_assay_classes/25_raise_assay.md +40 -0
  48. data/demo/core/01_assay_classes/26_throw_assay.md +47 -0
  49. data/demo/core/01_assay_classes/27_path_assay.md +20 -0
  50. data/demo/core/01_assay_classes/28_directory_assay.md +21 -0
  51. data/demo/core/01_assay_classes/29_file_assay.md +20 -0
  52. data/demo/core/01_assay_classes/30_output_assay.md +39 -0
  53. data/demo/core/01_assay_classes/31_stdout_assay.md +21 -0
  54. data/demo/core/01_assay_classes/32_stderr_assay.md +21 -0
  55. data/demo/core/02_lookup.md +25 -0
  56. data/demo/core/03_assertor.md +117 -0
  57. data/demo/core/04_assay.md +40 -0
  58. data/demo/fluidity/00_intro.md +7 -0
  59. data/demo/fluidity/01_should.md +52 -0
  60. data/demo/fluidity/02_must.md +58 -0
  61. data/demo/fluidity/03_assert.md +71 -0
  62. data/demo/fluidity/04_other.md +35 -0
  63. data/demo/fluidity/05_object_methods.md +102 -0
  64. data/demo/minitest/01_minitest_extensions.rdoc +217 -0
  65. data/demo/minitest/02_minitest_assertions.rdoc +250 -0
  66. data/demo/rspec/00_introduction.md +16 -0
  67. data/demo/rspec/01_should.md +24 -0
  68. data/demo/rspec/02_matchers.md +176 -0
  69. data/demo/rspec/03_have.md +51 -0
  70. data/demo/rspec/04_expect.md +49 -0
  71. data/demo/rspec/applique/setup.rb +2 -0
  72. data/demo/testunit/01_testunit_assertions.md +266 -0
  73. data/lib/assay/adapter/minitest.rb +32 -21
  74. data/lib/assay/adapter/testunit.rb +31 -38
  75. data/lib/assay/ae/assert.rb +77 -0
  76. data/lib/assay/ae/assertor.rb +453 -0
  77. data/lib/assay/ae/check.rb +154 -0
  78. data/lib/assay/ae/core_ext.rb +146 -0
  79. data/lib/assay/ae/expect.rb +72 -0
  80. data/lib/assay/ae/must.rb +64 -0
  81. data/lib/assay/ae/should.rb +58 -0
  82. data/lib/assay/ae/subjunctive.rb +66 -0
  83. data/lib/assay/ae.rb +29 -0
  84. data/lib/assay/assertable.rb +21 -5
  85. data/lib/assay/assertor.rb +4 -4
  86. data/lib/assay/bang.rb +147 -0
  87. data/lib/assay/close_assay.rb +2 -2
  88. data/lib/assay/core_ext/kernel.rb +4 -8
  89. data/lib/assay/fluidity/assert.rb +27 -0
  90. data/lib/assay/fluidity/grammar.rb +157 -0
  91. data/lib/assay/fluidity/is.rb +18 -0
  92. data/lib/assay/fluidity/must.rb +38 -0
  93. data/lib/assay/fluidity/should.rb +32 -0
  94. data/lib/assay/fluidity/will.rb +36 -0
  95. data/lib/assay/fluidity.rb +6 -0
  96. data/lib/assay/less_assay.rb +7 -1
  97. data/lib/assay/less_equal_assay.rb +7 -1
  98. data/lib/assay/minitest/assertions.rb +495 -0
  99. data/lib/assay/minitest/extensions.rb +496 -0
  100. data/lib/assay/minitest.rb +24 -0
  101. data/lib/assay/more_assay.rb +7 -1
  102. data/lib/assay/more_equal_assay.rb +7 -1
  103. data/lib/assay/rspec/expect.rb +151 -0
  104. data/lib/assay/rspec/have.rb +201 -0
  105. data/lib/assay/rspec/matchers.rb +272 -0
  106. data/lib/assay/rspec/operatics.rb +60 -0
  107. data/lib/assay/rspec/should.rb +49 -0
  108. data/lib/assay/rspec.rb +13 -0
  109. data/lib/assay/testunit/assertions.rb +484 -0
  110. data/lib/assay/testunit.rb +9 -0
  111. data/lib/assay/version.rb +3 -0
  112. data/lib/assay.rb +7 -21
  113. metadata +211 -80
  114. data/.ruby +0 -51
  115. data/COPYING.rdoc +0 -38
  116. data/HISTORY.rdoc +0 -65
  117. data/QED.rdoc +0 -1143
  118. data/README.rdoc +0 -173
  119. data/lib/assay.yml +0 -51
  120. data/test/case_compare_assay.rb +0 -59
  121. data/test/case_empty_assay.rb +0 -51
  122. data/test/case_equal_assay.rb +0 -53
  123. data/test/case_equality_assay.rb +0 -55
  124. data/test/case_false_assay.rb +0 -48
  125. data/test/case_identity_assay.rb +0 -51
  126. data/test/case_include_assay.rb +0 -51
  127. data/test/case_instance_assay.rb +0 -51
  128. data/test/case_kind_assay.rb +0 -51
  129. data/test/case_less_assay.rb +0 -53
  130. data/test/case_less_equal_assay.rb +0 -53
  131. data/test/case_like_assay.rb +0 -57
  132. data/test/case_match_assay.rb +0 -45
  133. data/test/case_more_assay.rb +0 -53
  134. data/test/case_more_equal_assay.rb +0 -53
  135. data/test/case_nil_assay.rb +0 -48
  136. data/test/case_nomatch_assay.rb +0 -47
  137. data/test/case_raise_assay.rb +0 -51
  138. data/test/case_respond_assay.rb +0 -51
  139. data/test/case_throw_assay.rb +0 -51
  140. data/test/case_true_assay.rb +0 -48
  141. data/test/case_unequal_assay.rb +0 -55
  142. data/test/case_within_assay.rb +0 -61
@@ -0,0 +1,146 @@
1
+ # Query methods the AE notation relies on. Most are also in Ruby Facets.
2
+ # Kernel#identical?, #true? and #false? come with Assay itself.
3
+
4
+ module Kernel
5
+ # Word form of `#==`. Can take a block instead of a value.
6
+ def eq?(value=NA) #:yield:
7
+ self == (block_given? ? yield : value)
8
+ end
9
+
10
+ # Word form of `#===`. Can take a block instead of a value.
11
+ def case?(value=NA) #:yield:
12
+ self === (block_given? ? yield : value)
13
+ end
14
+
15
+ # Word form of `#=~`. Can take a block instead of a value. An object
16
+ # without `#=~` matches nothing, as Object#=~ did before Ruby 3.2.
17
+ #
18
+ # String, Symbol and Regexp have their own `#match?`.
19
+ def match?(value=NA)
20
+ value = yield if block_given?
21
+ respond_to?(:=~) ? (self =~ value) : nil
22
+ end
23
+
24
+ # Broad equality: identical, `eql?`, `==` or `===`.
25
+ def equate?(x)
26
+ equal?(x) || eql?(x) || self == x || self === x
27
+ end
28
+
29
+ # Can a message be sent to the receiver successfully? Only a missing
30
+ # +method+ counts as failure; a NoMethodError raised from inside the
31
+ # method is a bug, and is raised as usual.
32
+ def send?(method, *args, **kw, &block)
33
+ __send__(method, *args, **kw, &block)
34
+ true
35
+ rescue NoMethodError => error
36
+ raise unless error.name == method.to_sym && error.receiver.equal?(self)
37
+ false
38
+ end
39
+ end
40
+
41
+ class Object
42
+ # Allows `equal?` to take a block.
43
+ def equal?(value=NA) #:yield:
44
+ block_given? ? super(yield) : super
45
+ end
46
+
47
+ # Allows `eql?` to take a block.
48
+ def eql?(value=NA) #:yield:
49
+ block_given? ? super(yield) : super
50
+ end
51
+ end
52
+
53
+ class Numeric
54
+ # Is the number within +delta+ of another?
55
+ #
56
+ # 0.05.in_delta?(50000.0 / 10**6, 0.00001)
57
+ #
58
+ def in_delta?(orig, delta=0.001)
59
+ delta >= (orig - self).abs
60
+ end
61
+
62
+ # Is the number within +epsilon+ of another, relative to the smaller
63
+ # of the two magnitudes?
64
+ def in_epsilon?(orig, epsilon=0.001)
65
+ in_delta?(orig, [orig.abs, self.abs].min * epsilon)
66
+ end
67
+ end
68
+
69
+ class Module
70
+ # Is a given class or module an ancestor of this class or module?
71
+ #
72
+ # class X ; end
73
+ # class Y < X ; end
74
+ #
75
+ # Y.is?(X) #=> true
76
+ #
77
+ def is?(base)
78
+ Module===base && ancestors.slice(1..-1).include?(base)
79
+ end
80
+ end
81
+
82
+ class Proc
83
+ # Does calling the procedure raise the exception, or a kind of it?
84
+ def raises?(exception=Exception, *args)
85
+ call(*args)
86
+ false
87
+ rescue exception => error
88
+ exception === error
89
+ end
90
+
91
+ # Does calling the procedure throw +sym+? Throwing another symbol
92
+ # counts as no; any error is raised as usual.
93
+ def throws?(sym, *args)
94
+ catch(sym) do
95
+ begin
96
+ call(*args)
97
+ rescue UncaughtThrowError
98
+ end
99
+ return false
100
+ end
101
+ return true
102
+ end
103
+
104
+ # Does calling the procedure change what the block returns?
105
+ def change?
106
+ pre_result = yield
107
+ call
108
+ post_result = yield
109
+ pre_result != post_result
110
+ end
111
+ end
112
+
113
+ class Symbol
114
+ # Does the block throw this symbol? Throwing another symbol counts
115
+ # as no; any error is raised as usual.
116
+ def thrown?(*args)
117
+ catch(self) do
118
+ begin
119
+ yield(*args)
120
+ rescue UncaughtThrowError
121
+ end
122
+ return false
123
+ end
124
+ return true
125
+ end
126
+ end
127
+
128
+ class Exception
129
+ # Does the block raise this exception, or a kind of it?
130
+ def self.raised? #:yield:
131
+ yield
132
+ false
133
+ rescue self
134
+ true
135
+ end
136
+
137
+ # Was the assertion that raised this exception a negated one?
138
+ def negative?
139
+ @negative || false
140
+ end
141
+
142
+ # Set whether the exception came from a negated assertion.
143
+ def set_negative(boolean)
144
+ @negative = !!boolean
145
+ end
146
+ end
@@ -0,0 +1,72 @@
1
+ require_relative 'assertor'
2
+
3
+ module Assay::AE
4
+ # = Expect
5
+ #
6
+ # "When love and skill work together, expect a masterpiece."
7
+ # --John Ruskin (1819 - 1900)
8
+ #
9
+ module Expect
10
+
11
+ # The #expect method is a convenient tool for defining
12
+ # certain sets of expectations in your specifications.
13
+ #
14
+ # Expect is used to expect a result from a block of code.
15
+ # If the argument to expect is a subclass of Exception
16
+ # or instance thereof, then the block is monitored for
17
+ # the raising of such an exception.
18
+ #
19
+ # expect StandardError do
20
+ # raise ArgumentError
21
+ # end
22
+ #
23
+ # All other expectations are compared using case equality (#===).
24
+ # This allows one to verify matching Regexp.
25
+ #
26
+ # expect /x/ do
27
+ # "x"
28
+ # end
29
+ #
30
+ # As well as checking that an object is an instance of a given Class.
31
+ #
32
+ # expect String do
33
+ # "x"
34
+ # end
35
+ #
36
+ # Like #assert it can be used to designate an expectation
37
+ # via a *functor*.
38
+ #
39
+ # 4.expect == 3
40
+ #
41
+ def expect(*args, &block)
42
+ Assertor.new(self, :backtrace=>caller).expect(*args, &block)
43
+ end
44
+
45
+ # Designate a negated expectation. Read this as "expect not".
46
+ #
47
+ # See #expect.
48
+ #
49
+ def expect!(*args, &block)
50
+ Assertor.new(self, :backtrace=>caller).not.expect(*args, &block)
51
+ end
52
+
53
+ # Alias for #expect! method.
54
+ alias_method :forbid, :expect!
55
+
56
+ # Like #expect but uses the reciever as the object
57
+ # of expectation.
58
+ #
59
+ # @example
60
+ # /x/.expected do
61
+ # "oooxooo"
62
+ # end
63
+ #
64
+ def expected(*args, &block)
65
+ expect(self, *args, &block)
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ # Copyright (c) 2008 Thomas Sawyer
@@ -0,0 +1,64 @@
1
+ require_relative 'subjunctive'
2
+
3
+ module Assay::AE
4
+ # Must
5
+ #
6
+ # "It is not enough to succeed. Others must fail."
7
+ # --Gore Vidal (1925 - )
8
+ #
9
+ module Must
10
+ # The #must method is functionaly the same as #should.
11
+ #
12
+ # @example
13
+ # 4.must == 3 #=> Assertion Error
14
+ #
15
+ # @example
16
+ # 4.must do |x|
17
+ # x == 4
18
+ # end
19
+ #
20
+ # @return [Assertor] Assertion functor.
21
+ def must(*args, &block)
22
+ Assertor.new(self, :backtrace=>caller).be(*args, &block)
23
+ end
24
+
25
+ # Same as 'object.must == other'.
26
+ #
27
+ # @return [Assertor] Assertion functor.
28
+ def must=(cmp)
29
+ Assertor.new(self, :backtrace=>caller) == cmp
30
+ end
31
+
32
+ # Designate a negated expectation via a *functor*.
33
+ # Read this as "must not".
34
+ #
35
+ # @example
36
+ # 4.must! == 4 #=> Assertion Error
37
+ #
38
+ # @return [Assertor] Assertion functor.
39
+ def must!(*args, &block)
40
+ Assertor.new(self, :backtrace=>caller).not.be(*args, &block)
41
+ end
42
+
43
+ # TODO: Are these negation methods needed now, since Ruby 1.9 allows for
44
+ # redefining `!` as a method?
45
+
46
+ # Perhaps not literally the counter-term to *must* (rather *will*),
47
+ # but close enough for our purposes, and conveys the appropriate
48
+ # semantics.
49
+ alias_method :wont, :must!
50
+
51
+ # Alias for #must! method.
52
+ alias_method :must_not, :must!
53
+
54
+ # Alias for #must! method.
55
+ alias_method :mustnt, :must!
56
+ end
57
+
58
+ end
59
+
60
+ class ::Object #:nodoc:
61
+ include Assay::AE::Must
62
+ end
63
+
64
+ # Copyright (c) 2008 Thomas Sawyer
@@ -0,0 +1,58 @@
1
+ require_relative 'subjunctive'
2
+
3
+ module Assay::AE
4
+
5
+ # Should
6
+ #
7
+ # "Always and never are two words you should always
8
+ # remember never to use."
9
+ # --Wendell Johnson
10
+ #
11
+ module Should
12
+ # Make an assertion in subjunctive tense.
13
+ #
14
+ # 4.should == 3 #=> Assertion Error
15
+ #
16
+ # 4.should do |x|
17
+ # x == 4
18
+ # end
19
+ #
20
+ # @return [Assertor] Assertion functor.
21
+ def should(*args, &block)
22
+ Assertor.new(self, :backtrace=>caller).be(*args, &block)
23
+ end
24
+
25
+ # Same as 'object.should == other'.
26
+ #
27
+ # @return [Assertor] Assertion functor.
28
+ def should=(cmp)
29
+ Assertor.new(self, :backtrace=>caller).assert == cmp
30
+ end
31
+
32
+ # Designate a negated expectation via a *functor*.
33
+ # Read this as "should not".
34
+ #
35
+ # 4.should! = 4 #=> Assertion Error
36
+ #
37
+ # @return [Assertor] Assertion functor.
38
+ def should!(*args, &block)
39
+ Assertor.new(self, :backtrace=>caller).not.be(*args, &block)
40
+ end
41
+
42
+ # NOTE: It would be nice if their were a single term that
43
+ # meant the opposite of should, rather than a two word compound.
44
+
45
+ # Alias for #should! method.
46
+ alias_method :should_not, :should!
47
+
48
+ # Alias for #should! method.
49
+ alias_method :shouldnt, :should!
50
+ end
51
+
52
+ end
53
+
54
+ class ::Object #:nodoc:
55
+ include Assay::AE::Should
56
+ end
57
+
58
+ # Copyright (c) 2008 Thomas Sawyer, Rubyworks
@@ -0,0 +1,66 @@
1
+ require_relative 'assertor'
2
+
3
+ module Assay::AE
4
+
5
+ # Subjunctive
6
+ #
7
+ # Mixin for Assertor that provides additional English-eque verbage
8
+ # such as 'be' and 'an'. This makes it easier to create assertor
9
+ # methods of subjunctive terms like 'should'.
10
+ #
11
+ module Subjunctive
12
+
13
+ # Like #assert, except if an argument is provided and no block,
14
+ # uses #equate? to compare the argument to the receiver. This
15
+ # allows for statements of the form:
16
+ #
17
+ # 5.should.be Numeric
18
+ #
19
+ def be(*args, &block)
20
+ return self if args.empty? && !block
21
+ block = args.shift if !block && ::Proc === args.first
22
+ if block
23
+ pass = block.arity > 0 ? block.call(@delegate) : block.call #@delegate.instance_eval(&block)
24
+ msg = args.shift || @message || block.inspect
25
+ else
26
+ match = args.shift
27
+ pass = match.equate?(@delegate)
28
+ msg = args.shift || @message || ::Kernel.proc { "#{'! ' if @negated}#{__inspect__(match)} equate? #{__inspect__(@delegate)}" }
29
+ end
30
+ __assert__(pass, msg)
31
+ end
32
+
33
+ # Alias of #be.
34
+ #
35
+ # 5.assert.is Numeric
36
+ #
37
+ alias_method :is , :be
38
+ alias_method :does, :be
39
+
40
+ # The indefinite article is like #be, except that it compares a lone argument
41
+ # with #case?, rather than #equate?
42
+ #
43
+ def a(*args, &block)
44
+ return self if args.empty? && !block
45
+ block = args.shift if !block && ::Proc === args.first
46
+ if block
47
+ pass = block.arity > 0 ? block.call(@delegate) : block.call #@delegate.instance_eval(&block)
48
+ msg = args.shift || @message || block.inspect
49
+ else
50
+ match = args.shift
51
+ pass = (match === @delegate) # case equality
52
+ msg = args.shift || @message || ::Kernel.proc { "#{'! ' if @negated}#{__inspect__(match)} === #{__inspect__(@delegate)}" }
53
+ end
54
+ __assert__(pass, msg, ::CaseAssay)
55
+ end
56
+
57
+ alias_method :an, :a
58
+ end
59
+
60
+ end
61
+
62
+ class Assay::AE::Assertor
63
+ include ::Assay::AE::Subjunctive
64
+ end
65
+
66
+ # Copyright (c) 2008 Thomas Sawyer
data/lib/assay/ae.rb ADDED
@@ -0,0 +1,29 @@
1
+ require_relative '../assay'
2
+
3
+ require_relative 'ae/assertor'
4
+ require_relative 'ae/assert'
5
+ require_relative 'ae/expect'
6
+
7
+ # The AE notation, from the Assertive Expressive (AE) gem: any method of
8
+ # the object under test becomes an assertion when called through `assert`.
9
+ #
10
+ # x.assert == y
11
+ # a.assert.include?(e)
12
+ # x.refute.nil?
13
+ #
14
+ # expect ArgumentError do
15
+ # ...
16
+ # end
17
+ #
18
+ # Where Assay has a class for the method, a failure is of that class;
19
+ # otherwise it is a plain Assertion. The subjunctive verbs, and the
20
+ # `ok`/`no` checks, are loaded separately:
21
+ #
22
+ # require 'assay/ae/should'
23
+ # require 'assay/ae/must'
24
+ # require 'assay/ae/check'
25
+ #
26
+ class ::Object
27
+ include ::Assay::AE::Assert
28
+ include ::Assay::AE::Expect
29
+ end
@@ -75,14 +75,14 @@ module Assay
75
75
  # Test the assertion, raising the exception if failing.
76
76
  #
77
77
  def assert!(*arguments, &block)
78
- options = (Hash === arguments.last ? arguments.pop : {})
78
+ options = extract_options(arguments)
79
79
 
80
80
  backtrace = options[:backtrace] || caller
81
81
  message = options[:message] || assert_message(*arguments, &block)
82
82
 
83
83
  pass = pass?(*arguments, &block)
84
84
 
85
- assert pass, self, message, backtrace
85
+ ::Kernel.assert pass, self, message, backtrace
86
86
 
87
87
  #if pass?(*arguments, &block)
88
88
  # increment(:pass)
@@ -98,14 +98,14 @@ module Assay
98
98
  # Test the inverse assertion, raising the exception if not failing.
99
99
  #
100
100
  def refute!(*arguments, &block)
101
- options = (Hash === arguments.last ? arguments.pop : {})
101
+ options = extract_options(arguments)
102
102
 
103
103
  backtrace = options[:backtrace] || caller
104
104
  message = options[:message] || refute_message(*arguments, &block)
105
105
 
106
106
  fail = fail?(*arguments, &block)
107
107
 
108
- assert fail, self, message, backtrace
108
+ ::Kernel.assert fail, self, message, backtrace
109
109
 
110
110
  #if fail?(*arguments, &block)
111
111
  # increment(:pass)
@@ -138,6 +138,22 @@ module Assay
138
138
 
139
139
  private
140
140
 
141
+ #
142
+ # Take the options off the end of an argument list, if there are any.
143
+ # A trailing Hash counts as options only if every key is `:message` or
144
+ # `:backtrace`, so that a Hash under test isn't mistaken for them.
145
+ # To be certain, pass options explicitly, even if empty of use:
146
+ # `EqualAssay.assert!(a, b, :backtrace=>caller)`.
147
+ #
148
+ def extract_options(arguments)
149
+ last = arguments.last
150
+ if Hash === last && !last.empty? && last.keys.all?{ |k| k == :message || k == :backtrace }
151
+ arguments.pop
152
+ else
153
+ {}
154
+ end
155
+ end
156
+
141
157
  ##
142
158
  ## Increment global `$ASSERTION_COUNTS` variable.
143
159
  ##
@@ -161,7 +177,7 @@ module Assay
161
177
  t.times{ vars << vars.last.succ }
162
178
 
163
179
  msg = ''
164
- msg << "a#{op} " + vars.join(',') + "\n"
180
+ msg << "a#{op}" + vars.join(', ') + "\n"
165
181
  msg << args_inspect.join("\n")
166
182
  msg
167
183
  else
@@ -63,9 +63,9 @@ module Assay
63
63
  arguments, block = complete_criteria(subject, &block)
64
64
 
65
65
  if @not
66
- @assay.refute!(*arguments, &block)
66
+ @assay.refute!(*arguments, {:backtrace=>caller}, &block)
67
67
  else
68
- @assay.assert!(*arguments, &block)
68
+ @assay.assert!(*arguments, {:backtrace=>caller}, &block)
69
69
  end
70
70
  end
71
71
 
@@ -81,9 +81,9 @@ module Assay
81
81
  arguments, block = complete_criteria(subject, &block)
82
82
 
83
83
  if @not
84
- @assay.assert!(*arguments, &block)
84
+ @assay.assert!(*arguments, {:backtrace=>caller}, &block)
85
85
  else
86
- @assay.refute!(*arguments, &block)
86
+ @assay.refute!(*arguments, {:backtrace=>caller}, &block)
87
87
  end
88
88
  end
89
89
 
data/lib/assay/bang.rb ADDED
@@ -0,0 +1,147 @@
1
+ require_relative '../assay'
2
+
3
+ module Assay
4
+
5
+ # Bang turns query methods into assertions: call one with `!` in place
6
+ # of `?` and it has to hold.
7
+ #
8
+ # "string".instance_of!(String)
9
+ # "string".equal!("string")
10
+ # "string".not_equal!("another")
11
+ #
12
+ # Where Assay has a class for the assertion, a failure is an instance of
13
+ # that class, here `InstanceAssay` and `EqualAssay`. Any other query
14
+ # method an object has works too, and fails with a plain `Assertion`:
15
+ #
16
+ # [].empty! # EmptyAssay
17
+ # 10.even! # Assertion, by way of Integer#even?
18
+ #
19
+ # Nothing is defined ahead of time. A bang method that Ruby doesn't know
20
+ # is looked up when it is called, so assay classes added later are
21
+ # found, and real bang methods such as `String#upcase!` are never
22
+ # touched.
23
+ #
24
+ module Bang
25
+
26
+ # Bang names that differ from the assay's own name.
27
+ NAMES = {
28
+ :satisfy => :executes,
29
+ :thrown => :throw
30
+ }
31
+
32
+ #
33
+ # Find the assay class for a bang name (less the `!` and any `not_`),
34
+ # by name or by the query method it stands for. Returns nil if Assay
35
+ # has none.
36
+ #
37
+ def self.lookup(name)
38
+ name = NAMES[name.to_sym] || name.to_sym
39
+ ::Assertion.by_name(name) || ::Assertion.by_operator(:"#{name}?")
40
+ end
41
+
42
+ #
43
+ # Split a method name into the bare name and whether it is negated.
44
+ # The name is nil unless it is a bang method. Always returns a pair:
45
+ # destructuring a bare nil would ask nil for `to_ary`, and so come
46
+ # back through `respond_to_missing?` without end.
47
+ #
48
+ def self.parse(method_name)
49
+ name = method_name.to_s
50
+ return [nil, false] unless name.end_with?('!') && name.size > 1
51
+ name = name.chomp('!')
52
+ if name.start_with?('not_') && name.size > 4
53
+ [name[4..-1], true]
54
+ else
55
+ [name, false]
56
+ end
57
+ end
58
+
59
+ # Included into Object.
60
+ #
61
+ module Methods
62
+
63
+ #
64
+ # Handle `foo!` and `not_foo!` by way of an assay class or the
65
+ # object's own `foo?`.
66
+ #
67
+ def method_missing(method_name, *args, &block)
68
+ name, negate = ::Assay::Bang.parse(method_name)
69
+
70
+ return super unless name
71
+
72
+ if assay = ::Assay::Bang.lookup(name)
73
+ if negate
74
+ assay.refute!(self, *args, {:backtrace=>caller}, &block)
75
+ else
76
+ assay.assert!(self, *args, {:backtrace=>caller}, &block)
77
+ end
78
+ elsif respond_to?("#{name}?")
79
+ truth = __send__("#{name}?", *args, &block)
80
+ call = "#{inspect}.#{name}?"
81
+ call += "(#{args.map{ |a| a.inspect }.join(', ')})" unless args.empty?
82
+ if negate
83
+ ::Kernel.refute(truth, ::Assertion, "! #{call}", caller)
84
+ else
85
+ ::Kernel.assert(truth, ::Assertion, call, caller)
86
+ end
87
+ else
88
+ super
89
+ end
90
+ end
91
+
92
+ #
93
+ def respond_to_missing?(method_name, include_private=false)
94
+ name, _ = ::Assay::Bang.parse(method_name)
95
+ if name && (::Assay::Bang.lookup(name) || respond_to?("#{name}?"))
96
+ true
97
+ else
98
+ super
99
+ end
100
+ end
101
+
102
+ end
103
+
104
+ # Included into Proc, for assertions about what calling it does.
105
+ #
106
+ module ProcMethods
107
+
108
+ # Calling the procedure has to raise exactly this exception.
109
+ def raises!(*exceptions)
110
+ ::RaiseAssay.assert!(*exceptions, {:backtrace=>caller}, &self)
111
+ end
112
+
113
+ def not_raises!(*exceptions)
114
+ ::RaiseAssay.refute!(*exceptions, {:backtrace=>caller}, &self)
115
+ end
116
+
117
+ # Calling the procedure has to raise this exception or a kind of it.
118
+ def rescues!(*exceptions)
119
+ ::RescueAssay.assert!(*exceptions, {:backtrace=>caller}, &self)
120
+ end
121
+
122
+ def not_rescues!(*exceptions)
123
+ ::RescueAssay.refute!(*exceptions, {:backtrace=>caller}, &self)
124
+ end
125
+
126
+ # Calling the procedure has to throw this symbol.
127
+ def throws!(symbol)
128
+ ::ThrowAssay.assert!(symbol, {:backtrace=>caller}, &self)
129
+ end
130
+
131
+ def not_throws!(symbol)
132
+ ::ThrowAssay.refute!(symbol, {:backtrace=>caller}, &self)
133
+ end
134
+
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+
141
+ class Object
142
+ include Assay::Bang::Methods
143
+ end
144
+
145
+ class Proc
146
+ include Assay::Bang::ProcMethods
147
+ end
@@ -18,7 +18,7 @@ class CloseAssay < CompareAssay
18
18
 
19
19
  #(a - b).abs / [a.abs, b.abs].max <= e
20
20
 
21
- d = b * e # [a.abs,b.abs].max * e
21
+ d = (b * e).abs
22
22
 
23
23
  (b - d) <= a && (b + d) >= a
24
24
  end
@@ -32,7 +32,7 @@ class CloseAssay < CompareAssay
32
32
  e = delta.inspect
33
33
 
34
34
  if [a, b, e].any?{ |e| e.to_s.size > SIZE_LIMIT }
35
- "b(1 - e) <= a <= b(1 + e)\na) #{a}\nb) #{b}\nd) #{d}"
35
+ "b(1 - e) <= a <= b(1 + e)\na) #{a}\nb) #{b}\ne) #{e}"
36
36
  else
37
37
  "#{b}(1 - #{e}) <= #{a} <= #{b}(1 + #{e})"
38
38
  end