Class: Test::Spec::Should

Inherits:
Object show all
Includes:
Unit::Assertions
Defined in:
lib/test/spec/should-output.rb,
lib/test/spec.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, message = nil) ⇒ Should

Returns a new instance of Should.



45
46
47
48
# File 'lib/test/spec.rb', line 45

def initialize(object, message=nil)
  @object = object
  @message = message
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/test/spec.rb', line 183

def method_missing(name, *args, &block)
  # This will make raise call Kernel.raise, and self.raise call _raise.
  return _raise(*args, &block)  if name == :raise
  
  if @object.respond_to?("#{name}?")
    assert @object.__send__("#{name}?", *args),
      "#{name}? expected to be true. #{@message}"
  else
    if @object.respond_to?(name)
      assert @object.__send__(name, *args),
      "#{name} expected to be true. #{@message}"
    else
      super
    end
  end
end

Class Method Details

.deprecated_alias(to, from) ⇒ Object

:nodoc:



38
39
40
41
42
43
# File 'lib/test/spec.rb', line 38

def self.deprecated_alias(to, from)    # :nodoc:
  define_method(to) { |*args|
    warn "Test::Spec::Should##{to} is deprecated and will be removed in future versions."
    __send__ from, *args
  }
end

Instance Method Details

#<(value) ⇒ Object



162
163
164
# File 'lib/test/spec.rb', line 162

def <(value)
  assert_operator @object, :<, value, @message
end

#<=(value) ⇒ Object



166
167
168
# File 'lib/test/spec.rb', line 166

def <=(value)
  assert_operator @object, :<=, value, @message
end

#===(value) ⇒ Object



170
171
172
# File 'lib/test/spec.rb', line 170

def ===(value)
  assert_operator @object, :===, value, @message
end

#>(value) ⇒ Object



154
155
156
# File 'lib/test/spec.rb', line 154

def >(value)
  assert_operator @object, :>, value, @message
end

#>=(value) ⇒ Object



158
159
160
# File 'lib/test/spec.rb', line 158

def >=(value)
  assert_operator @object, :>=, value, @message
end

#_raise(*args, &block) ⇒ Object



131
132
133
134
135
136
# File 'lib/test/spec.rb', line 131

def _raise(*args, &block)
  args = [RuntimeError]  if args.empty?
  block ||= @object
  args << @message  if @message
  assert_raise(*args, &block)
end

#aObject



60
61
62
# File 'lib/test/spec.rb', line 60

def a
  self
end

#add_assertionObject



51
52
53
# File 'lib/test/spec.rb', line 51

def add_assertion
  $TEST_SPEC_TESTCASE && $TEST_SPEC_TESTCASE.__send__(:add_assertion)
end

#anObject



56
57
58
# File 'lib/test/spec.rb', line 56

def an
  self
end

#be(*value) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/test/spec.rb', line 97

def be(*value)
  case value.size
  when 0
    self
  when 1
    if CustomShould === value.first 
      pass value.first
    else
      assert_same value.first, @object, @message
    end
  else
    raise ArgumentError, "should.be needs zero or one argument"
  end
end

#close(value, delta) ⇒ Object



92
93
94
# File 'lib/test/spec.rb', line 92

def close(value, delta)
  assert_in_delta value, @object, delta, @message
end

#equal(value) ⇒ Object Also known as: ==



87
88
89
# File 'lib/test/spec.rb', line 87

def equal(value)
  assert_equal value, @object, @message
end

#include(value) ⇒ Object



148
149
150
151
152
# File 'lib/test/spec.rb', line 148

def include(value)
  msg = build_message(@message, "<?> expected to include ?, but it didn't.",
                      @object, value)
  assert_block(msg) { @object.include?(value) }
end

#instance_of(klass) ⇒ Object



117
118
119
# File 'lib/test/spec.rb', line 117

def instance_of(klass)
  assert_instance_of klass, @object, @message
end

#kind_of(klass) ⇒ Object



122
123
124
# File 'lib/test/spec.rb', line 122

def kind_of(klass)
  assert_kind_of klass, @object, @message
end

#match(value) ⇒ Object Also known as: =~



112
113
114
# File 'lib/test/spec.rb', line 112

def match(value)
  assert_match value, @object, @message
end

#messaging(message) ⇒ Object Also known as: blaming



75
76
77
78
# File 'lib/test/spec.rb', line 75

def messaging(message)
  @message = message.to_s
  self
end

#nilObject



142
143
144
# File 'lib/test/spec.rb', line 142

def nil
  assert_nil @object, @message
end

#not(*args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/test/spec.rb', line 64

def not(*args)
  case args.size
  when 0
    ShouldNot.new(@object, @message)
  when 1
    ShouldNot.new(@object, @message).pass(args.first)
  else
    raise ArgumentError, "#not takes zero or one argument(s)."
  end
end

#output(expected, to = STDOUT) ⇒ Object

Captures output from the IO given as the second argument (STDIN by default) and matches it against a String or Regexp given as the first argument.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/test/spec/should-output.rb', line 10

def output(expected, to = STDOUT)
  # Store the old stream
  old_to = to.dup

  # Obtain a filehandle to replace (works with Readline)
  to.reopen File.open(File.join(Dir.tmpdir, "should_output_#{$$}"), "w+")
  
  # Execute
  @object.call

  # Restore
  out = to.dup
  to.reopen old_to

  # Grab the data
  out.rewind
  output = out.read

  # Match up
  case expected
    when Regexp
      output.should.match expected
    else
      output.should.equal expected
  end                               # case expected

# Clean up
ensure
  out.close

  # STDIO redirection will break else
  begin
    to.seek 0, IO::SEEK_END
  rescue Errno::ESPIPE
  rescue Errno::EPIPE
  end

  FileUtils.rm_f out.path
end

#pass(custom) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/test/spec.rb', line 174

def pass(custom)
  _wrap_assertion {
    assert_nothing_raised(Test::Unit::AssertionFailedError,
                          @message || custom.failure_message) {
      assert custom.matches?(@object), @message || custom.failure_message
    }
  }
end

#respond_to(method) ⇒ Object



127
128
129
# File 'lib/test/spec.rb', line 127

def respond_to(method)
  assert_respond_to @object, method, @message
end

#satisfy(&block) ⇒ Object



81
82
83
84
85
# File 'lib/test/spec.rb', line 81

def satisfy(&block)
  assert_block(@message || "satisfy block failed.") {
    yield @object
  }
end

#throw(sym) ⇒ Object



138
139
140
# File 'lib/test/spec.rb', line 138

def throw(sym)
  assert_throws(sym, @message, &@object)
end