Class Test::Spec::Should
In: lib/test/spec/should-output.rb
lib/test/spec.rb
Parent: Object

Methods

<   <=   ==   ===   =~   >   >=   _raise   a   add_assertion   an   be   blaming   close   equal   include   instance_of   kind_of   match   messaging   method_missing   new   nil   not   output   pass   respond_to   satisfy   throw  

Included Modules

Test::Unit::Assertions

Public Class methods

[Source]

    # File lib/test/spec.rb, line 45
45:     def initialize(object, message=nil)
46:       @object = object
47:       @message = message
48:     end

Public Instance methods

[Source]

     # File lib/test/spec.rb, line 161
161:     def <(value)
162:       assert_operator @object, :<, value, @message
163:     end

[Source]

     # File lib/test/spec.rb, line 165
165:     def <=(value)
166:       assert_operator @object, :<=, value, @message
167:     end
==(value)

Alias for equal

[Source]

     # File lib/test/spec.rb, line 169
169:     def ===(value)
170:       assert_operator @object, :===, value, @message
171:     end
=~(value)

Alias for match

[Source]

     # File lib/test/spec.rb, line 153
153:     def >(value)
154:       assert_operator @object, :>, value, @message
155:     end

[Source]

     # File lib/test/spec.rb, line 157
157:     def >=(value)
158:       assert_operator @object, :>=, value, @message
159:     end

[Source]

     # File lib/test/spec.rb, line 131
131:     def _raise(*args, &block)
132:       args = [RuntimeError]  if args.empty?
133:       block ||= @object
134:       assert_raise(*(args + [@message]), &block)
135:     end

[Source]

    # File lib/test/spec.rb, line 60
60:     def a
61:       self
62:     end

[Source]

    # File lib/test/spec.rb, line 51
51:     def add_assertion
52:       $TEST_SPEC_TESTCASE && $TEST_SPEC_TESTCASE.__send__(:add_assertion)
53:     end

[Source]

    # File lib/test/spec.rb, line 56
56:     def an
57:       self
58:     end

[Source]

     # File lib/test/spec.rb, line 97
 97:     def be(*value)
 98:       case value.size
 99:       when 0
100:         self
101:       when 1
102:         if CustomShould === value.first 
103:           pass value.first
104:         else
105:           assert_same value.first, @object, @message
106:         end
107:       else
108:         raise ArgumentError, "should.be needs zero or one argument"
109:       end
110:     end
blaming(message)

Alias for messaging

[Source]

    # File lib/test/spec.rb, line 92
92:     def close(value, delta)
93:       assert_in_delta value, @object, delta, @message
94:     end

[Source]

    # File lib/test/spec.rb, line 87
87:     def equal(value)
88:       assert_equal value, @object, @message
89:     end

[Source]

     # File lib/test/spec.rb, line 147
147:     def include(value)
148:       msg = build_message(@message, "<?> expected to include ?, but it didn't.",
149:                           @object, value)
150:       assert_block(msg) { @object.include?(value) }
151:     end

[Source]

     # File lib/test/spec.rb, line 117
117:     def instance_of(klass)
118:       assert_instance_of klass, @object, @message
119:     end

[Source]

     # File lib/test/spec.rb, line 122
122:     def kind_of(klass)
123:       assert_kind_of klass, @object, @message
124:     end

[Source]

     # File lib/test/spec.rb, line 112
112:     def match(value)
113:       assert_match value, @object, @message
114:     end

[Source]

    # File lib/test/spec.rb, line 75
75:     def messaging(message)
76:       @message = message.to_s
77:       self
78:     end

[Source]

     # File lib/test/spec.rb, line 182
182:     def method_missing(name, *args, &block)
183:       # This will make raise call Kernel.raise, and self.raise call _raise.
184:       return _raise(*args, &block)  if name == :raise
185:       
186:       if @object.respond_to?("#{name}?")
187:         assert @object.__send__("#{name}?", *args),
188:           "#{name}? expected to be true. #{@message}"
189:       else
190:         if @object.respond_to?(name)
191:           assert @object.__send__(name, *args),
192:           "#{name} expected to be true. #{@message}"
193:         else
194:           super
195:         end
196:       end
197:     end

[Source]

     # File lib/test/spec.rb, line 141
141:     def nil
142:       assert_nil @object, @message
143:     end

[Source]

    # File lib/test/spec.rb, line 64
64:     def not(*args)
65:       case args.size
66:       when 0
67:         ShouldNot.new(@object, @message)
68:       when 1
69:         ShouldNot.new(@object, @message).pass(args.first)
70:       else
71:         raise ArgumentError, "#not takes zero or one argument(s)."
72:       end
73:     end

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.

[Source]

    # File lib/test/spec/should-output.rb, line 10
10:   def output(expected, to = STDOUT)
11:     # Store the old stream
12:     old_to = to.dup
13: 
14:     # Obtain a filehandle to replace (works with Readline)
15:     to.reopen File.open(File.join(Dir.tmpdir, "should_output_#{$$}"), "w+")
16:     
17:     # Execute
18:     @object.call
19: 
20:     # Restore
21:     out = to.dup
22:     to.reopen old_to
23: 
24:     # Grab the data
25:     out.rewind
26:     output = out.read
27: 
28:     # Match up
29:     case expected
30:       when Regexp
31:         output.should.match expected
32:       else
33:         output.should.equal expected
34:     end                               # case expected
35: 
36:   # Clean up
37:   ensure
38:     out.close
39: 
40:     # STDIO redirection will break else
41:     begin
42:       to.seek 0, IO::SEEK_END
43:     rescue Errno::ESPIPE
44:     rescue Errno::EPIPE
45:     end
46: 
47:     FileUtils.rm_f out.path
48:   end

[Source]

     # File lib/test/spec.rb, line 173
173:     def pass(custom)
174:       _wrap_assertion {
175:         assert_nothing_raised(Test::Unit::AssertionFailedError,
176:                               @message || custom.failure_message) {
177:           assert custom.matches?(@object), @message || custom.failure_message
178:         }
179:       }
180:     end

[Source]

     # File lib/test/spec.rb, line 127
127:     def respond_to(method)
128:       assert_respond_to @object, method, @message
129:     end

[Source]

    # File lib/test/spec.rb, line 81
81:     def satisfy(&block)
82:       assert_block(@message || "satisfy block failed.") {
83:         yield @object
84:       }
85:     end

[Source]

     # File lib/test/spec.rb, line 137
137:     def throw(*args)
138:       assert_throws(*(args + [@message]), &@object)
139:     end

[Validate]