Module: AssertAgile

Includes:
AssertLatestAndGreatest, Coulor, RubyNodeReflector
Included in:
Test::Unit::TestCase
Defined in:
lib/assert_agile.rb

Overview

FIXME evaluate parts

ERGO  if the block is a block, decorate with do-end
ERGO  decorate assert_latest's block at fault time

Instance Method Summary collapse

Methods included from AssertLatestAndGreatest

#assert_latest, #deny_latest, #get_latest

Methods included from RubyNodeReflector

#reflect, #reflect_source, #reflect_string

Instance Method Details

#assert(*args, &block) ⇒ Object

This assertion calls a block, and faults if this returns

+false+ or +nil+. The fault diagnostic will reflect the
intermediate value of every variable and expression in the
block.

The first argument can be a diagnostic string:

  assert("foo failed"){ foo() }

The fault diagnostic will print that line.

The next time you think to write any of these assertions...

- +assert+
- +assert_equal+
- +assert_instance_of+
- +assert_kind_of+
- +assert_operator+
- +assert_match+
- +assert_not_nil+

use <code>assert{ 2.0 }</code> instead.

If no block is provided, the assertion calls +assert_classic+,
which simulates RubyUnit's standard <code>assert()</code>.

Note: This only works for Ruby 1.8.6 so far...


43
44
45
46
47
48
49
# File 'lib/assert_agile.rb', line 43

def assert(*args, &block)
  if block
    assert_ args.first, '', &block
  else
    assert_classic *args
  end
end

#assert_(diagnostic = nil, twizzler = '_', &block) ⇒ Object

The new assert() calls this to interpret

blocks of assertive statements.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/assert_agile.rb', line 54

def assert_(diagnostic = nil, twizzler = '_', &block)
    # puts reflect(&block) # activate this line and test to see all your successes!
    result = nil
    
    begin
	result = block.call
    rescue => e
      diagnostic = [diagnostic, e.inspect, *e.backtrace].compact.join("\n\t")
     _flunk_2_0("\nassert#{ twizzler }{ ", diagnostic, block, result)
    end
    
    add_assertion and return if result
   _flunk_2_0("assert#{ twizzler }{ ", diagnostic, block, result)
end

#assert_classic(boolean, message = nil) ⇒ Object

This is a copy of the classic assert, so your pre�xisting assert calls will not change their behavior



155
156
157
158
159
160
# File 'lib/assert_agile.rb', line 155

def assert_classic(boolean, message=nil)
  _wrap_assertion do
    assert_block("assert<classic> should not be called with a block.") { !block_given? }
    assert_block(build_message(message, "<?> is not true.", boolean)) { boolean }
  end
end

#assert_flunked(gripe, diagnostic = nil, &block) ⇒ Object

:nodoc:



138
139
140
141
142
143
# File 'lib/assert_agile.rb', line 138

def assert_flunked(gripe, diagnostic = nil, &block) #:nodoc:
  assert_raise_message Test::Unit::AssertionFailedError,
                       gripe,
                       diagnostic,
                      &block
end

#assert_raise_message(types, matcher, diagnostic = nil, &block) ⇒ Object

Assert that a block raises a given Exception type matching a given message

  • types - single exception class or array of classes

  • matcher - Regular Expression to match the inner_text of XML nodes

  • diagnostic - optional string to add to failure message

  • block - Ruby statements that should raise an exception

Examples: %transclude AssertXPathSuite#test_assert_raise_message_detects_assertion_failure

%transclude AssertXPathSuite#test_assert_raise_message_raises_message

See: assert_raise - Don’t Just Say “No”



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/assert_agile.rb', line 109

def assert_raise_message(types, matcher, diagnostic = nil, &block)
  args = [types].flatten + [diagnostic]
  exception = assert_raise(*args, &block)
  
  assert_match matcher,
               exception.message,
               [ diagnostic, 
                 "incorrect #{ exception.class.name 
                   } message raised from block:", 
                 "\t"+reflect_source(&block).split("\n").join("\n\t")
                 ].compact.join("\n")
  
  return exception
end

#assert_yin_yang(*args, &block) ⇒ Object

wrap this common idiom:

  foo = assemble()
  deny{ foo.bar() }
  foo.activate()
  assert{ foo.bar() }

that becomes:
  foo = assemble()

  assert_yin_yang proc{ foo.bar() } do
    foo.activate()
  end


175
176
177
178
179
180
181
182
183
184
185
# File 'lib/assert_agile.rb', line 175

def assert_yin_yang(*args, &block)
    # prock, diagnostic = nil, &block)
  procks, diagnostic = args.partition{|p| p.respond_to? :call }
  block ||= procks.shift
  source = reflect_source(&block)
  fuss = [diagnostic, "fault before calling:", source].compact.join("\n")
  procks.each do |prock|  deny(fuss, &prock);  end
  block.call
  fuss = [diagnostic, "fault after calling:", source].compact.join("\n")
  procks.each do |prock|  assert(fuss, &prock);  end
end

#deny(diagnostic = nil, &block) ⇒ Object

This assertion replaces:

- +assert_nil+
- +assert_no_match+
- +assert_not_equal+

It faults, and prints its block's contents and values,
if its block returns non-+false+ and non-+nil+.


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/assert_agile.rb', line 78

def deny(diagnostic = nil, &block)  
    #  "None shall pass!" --the Black Knight
    # puts reflect(&block) # activate this line and test to see all your denials!
  result = nil
  
  begin
    result = block.call
  rescue => e
    diagnostic = [diagnostic, e.inspect, *e.backtrace].compact.join("\n\t")
   _flunk_2_0("\ndeny{ ", diagnostic, block, result)
  end
  
  return unless result
 _flunk_2_0('deny{ ', diagnostic, block, result)
end

#deny_flunked(gripe, diagnostic = nil, &block) ⇒ Object

:nodoc:



145
146
147
148
149
150
# File 'lib/assert_agile.rb', line 145

def deny_flunked(gripe, diagnostic = nil, &block) #:nodoc:
  deny_raise_message Test::Unit::AssertionFailedError,
                     gripe,
                     diagnostic,
                    &block
end

#deny_raise_message(types, matcher, diagnostic = nil, &block) ⇒ Object

:nodoc:



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/assert_agile.rb', line 124

def deny_raise_message(types, matcher, diagnostic = nil, &block) #:nodoc:
  exception = assert_raise_message(types, //, diagnostic, &block)
  
  assert_no_match matcher,
               exception.message,
               [ diagnostic, 
                 "exception #{ exception.class.name 
                   } with this message should not raise from block:", 
                 "\t"+reflect_source(&block).split("\n").join("\n\t")
                 ].compact.join("\n")
  
  return exception.message
end

#deny_yin_yang(prock, diagnostic = nil, &block) ⇒ Object

the prock assertion must pass on both sides of the called block



189
190
191
192
193
194
195
196
# File 'lib/assert_agile.rb', line 189

def deny_yin_yang(prock, diagnostic = nil, &block)
  source = reflect_source(&block)
  fuss = [diagnostic, "fault before calling:", source].compact.join("\n")
  assert fuss, &prock
  block.call
  fuss = [diagnostic, "fault after calling:", source].compact.join("\n")
  assert fuss, &prock
end