Class: Minitest::Ok::AssertionObject

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/ok.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actual, context, location) ⇒ AssertionObject

Don’t create this object directly. Use ok {value} instead.



73
74
75
76
77
78
79
# File 'lib/minitest/ok.rb', line 73

def initialize(actual, context, location)
  @actual  = actual
  @context = context
  @not     = false
  @tested  = tested = [false]
  ObjectSpace.define_finalizer(self, self.class._finalizer_callback(tested, location))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, **kwargs, &block) ⇒ Object

When ok {actual}.xxx? called, tries assert_xxx(actual) at first. If it is not defined, tries assert actual.xxx?.

ok {'logo.jpg'}.end_with?('.jpg')   # Pass
ok {[1, 2, 3]}.all? {|x| x <= 3 }   # Pass


677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/minitest/ok.rb', line 677

def method_missing(symbol, *args, **kwargs, &block)
  unless symbol.to_s =~ /\?\z/
    return super
  end
  _mark_as_tested()
  unless @not
    ## Try assert_xxxx() at first.
    ## If not defined, try assert @actual.xxxx?.
    begin
      @context.__send__("assert_#{symbol.to_s[0..-2]}", @actual, *args, &block)
    rescue NoMethodError
      __assert_predicate(symbol, *args, **kwargs, &block)
    end
  else
    ## Try refute_xxxx() at first.
    ## If not defined, try refute @actual.xxxx?.
    begin
      @context.__send__("refute_#{symbol.to_s[0..-2]}", @actual, *args, &block)
    rescue NoMethodError
      __assert_predicate(symbol, *args, **kwargs, &block)
    end
  end
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

Class Method Details

._finalizer_callback(tested, location) ⇒ Object

:nodoc:



81
82
83
84
85
86
87
88
# File 'lib/minitest/ok.rb', line 81

def self._finalizer_callback(tested, location)  # :nodoc:
  return proc do
    unless tested[0]
      loc = location.to_s.split(/:in /).first
      $stderr.puts "** WARNING: ok() called but no assertion invoked (#{loc})"
    end
  end
end

Instance Method Details

#!=(expected) ⇒ Object

Same as refute_equal().

ok {1+1} != 1             # Pass
ok {1+1} != 2             # Fail


137
138
139
140
141
142
143
144
145
# File 'lib/minitest/ok.rb', line 137

def !=(expected)
  _mark_as_tested()
  @context.refute_equal expected, @actual  unless @not
  @context.assert_equal expected, @actual  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#!~(expected) ⇒ Object

Same as refute_match().

ok {"abc"} !~ /\d+/        # Pass
ok {"abc"} !~ /\w+/        # Fail


253
254
255
256
257
258
259
260
261
# File 'lib/minitest/ok.rb', line 253

def !~(expected)
  _mark_as_tested()
  @context.refute_match expected, @actual  unless @not
  @context.assert_match expected, @actual  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#<(expected) ⇒ Object

Tests actual < expected.

ok {1+1} < 3             # Pass
ok {1+1} < 2             # Fail
ok {1+1} < 1             # Fail


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

def <(expected)
  _mark_as_tested()
  @context.assert_operator @actual, :'<', expected  unless @not
  @context.refute_operator @actual, :'<', expected  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#<=(expected) ⇒ Object

Tests actual <= expected.

ok {1+1} <= 3             # Pass
ok {1+1} <= 2             # Pass
ok {1+1} <= 1             # Fail


205
206
207
208
209
210
211
212
213
# File 'lib/minitest/ok.rb', line 205

def <=(expected)
  _mark_as_tested()
  @context.assert_operator @actual, :'<=', expected  unless @not
  @context.refute_operator @actual, :'<=', expected  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#==(expected) ⇒ Object

Same as assert_equal().

ok {1+1} == 2             # Pass
ok {1+1} == 1             # Fail


116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/minitest/ok.rb', line 116

def ==(expected)
  _mark_as_tested()
  if nil == expected
    @context.assert_nil @actual  unless @not
    @context.refute_nil @actual  if     @not
  else
    @context.assert_equal expected, @actual  unless @not
    @context.refute_equal expected, @actual  if     @not
  end
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#===(expected) ⇒ Object

Tests actual === expected.

ok {String} === 'foo'     # Pass
ok {/\d+/}  === '123'     # Pass


221
222
223
224
225
226
227
228
229
# File 'lib/minitest/ok.rb', line 221

def ===(expected)
  _mark_as_tested()
  @context.assert_operator @actual, :'===', expected  unless @not
  @context.refute_operator @actual, :'===', expected  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#=~(expected) ⇒ Object

Same as assert_match().

ok {"abc"} =~ /\w+/        # Pass
ok {"abc"} =~ /\d+/        # Fail


237
238
239
240
241
242
243
244
245
# File 'lib/minitest/ok.rb', line 237

def =~(expected)
  _mark_as_tested()
  @context.assert_match expected, @actual  unless @not
  @context.refute_match expected, @actual  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#>(expected) ⇒ Object

Tests actual > expected.

ok {1+1} > 1             # Pass
ok {1+1} > 2             # Fail
ok {1+1} > 3             # Fail


154
155
156
157
158
159
160
161
162
# File 'lib/minitest/ok.rb', line 154

def >(expected)
  _mark_as_tested()
  @context.assert_operator @actual, :'>', expected  unless @not
  @context.refute_operator @actual, :'>', expected  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#>=(expected) ⇒ Object

Tests actual >= expected.

ok {1+1} >= 1             # Pass
ok {1+1} >= 2             # Pass
ok {1+1} >= 3             # Fail


171
172
173
174
175
176
177
178
179
# File 'lib/minitest/ok.rb', line 171

def >=(expected)
  _mark_as_tested()
  @context.assert_operator @actual, :'>=', expected  unless @not
  @context.refute_operator @actual, :'>=', expected  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#_mark_as_testedObject

:nodoc:



90
91
92
93
# File 'lib/minitest/ok.rb', line 90

def _mark_as_tested   # :nodoc:
  @tested[0] = true
  self
end

#_msg(&block) ⇒ Object



95
96
97
# File 'lib/minitest/ok.rb', line 95

def _msg(&block)
  Msg.new(block)
end

#attr(name, expected) ⇒ Object

Tests attribute value.

User = Struct.new(:user, :age)   # define User class quickly
user = User.new('Haruhi', 16)
ok {user}.attr(:name, 'Haruhi').attr(:age, 16)   # Pass


761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'lib/minitest/ok.rb', line 761

def attr(name, expected)
  _mark_as_tested()
  object = @actual
  actual = object.__send__(name)
  pr = proc {|op|
    "Expected <object>.#{name} #{op} <exected>, but failed.\n" +
    " (object: #{object.inspect})"
  }
  @context.assert_equal expected, actual, Msg.new { pr.call('==') }  unless @not
  @context.refute_equal expected, actual, Msg.new { pr.call('!=') }  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#attrs(expecteds = {}) ⇒ Object

Tests attribute values.

User = Struct.new(:user, :age)   # define User class quickly
user = User.new('Haruhi', 16)
ok {user}.attrs(name: 'Haruhi', age: 16)   # Pass


784
785
786
787
788
789
790
791
792
793
# File 'lib/minitest/ok.rb', line 784

def attrs(expecteds={})
  _mark_as_tested()
  expecteds.each do |name, expected|
    attr(name, expected)
  end
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#dir_exist?Boolean

Tests whether directory exists or not.

ok {Dir.pwd}.dir_exist?           # Pass
ok {'/some/where'}.dir_exist?     # Fail
ok {__FILE__}.dir_exist?          # Fail


862
863
864
865
866
867
868
869
870
871
872
873
874
# File 'lib/minitest/ok.rb', line 862

def dir_exist?
  _mark_as_tested()
  fpath = @actual
  unless @not
    @context.assert File.exist?(fpath),     "Directory '#{fpath}' doesn't exist."
    @context.assert File.directory?(fpath), "'#{fpath}' is not a directory."
  else
    @context.refute File.exist?(fpath),     "Directory '#{fpath}' exists unexpectedly."
  end
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#empty?Boolean

Same as assert_empty().

ok {""}.empty?                    # Pass
ok {[]}.empty?                    # Pass
ok {"X"}.empty?                   # Fail
ok {"X"}.NOT.empty?               # Pass


406
407
408
409
410
411
412
413
414
# File 'lib/minitest/ok.rb', line 406

def empty?
  _mark_as_tested()
  @context.assert_empty @actual  unless @not
  @context.refute_empty @actual  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#falsy?Boolean Also known as: falthy?

Tests whether actual is false or nil.

ok {nil}.falsy?                  # Pass
ok {false}.falsy?                # Pass
ok {true}.falsy?                 # Fail
ok {0}.falsy?                    # Fail
ok {""}.falsy?                   # Fail
ok {[]}.falsy?                   # Fail


740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/minitest/ok.rb', line 740

def falsy?
  _mark_as_tested()
  unless @not
    @context.refute @actual, Msg.new { "Expected (!! #{@actual.inspect}) == false, but not." }
  else
    @context.assert @actual, Msg.new { "Expected (!! #{@actual.inspect}) == true, but not." }
  end
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#file_exist?Boolean

Tests whether file exists or not.

ok {__FILE__}.file_exist?         # Pass
ok {'/some/where'}.file_exist?    # Fail
ok {Dir.pwd}.file_exist?          # Fail


841
842
843
844
845
846
847
848
849
850
851
852
853
# File 'lib/minitest/ok.rb', line 841

def file_exist?
  _mark_as_tested()
  fpath = @actual
  unless @not
    @context.assert File.exist?(fpath), "File '#{fpath}' doesn't exist."
    @context.assert File.file?(fpath),  "'#{fpath}' is not a file."
  else
    @context.refute File.exist?(fpath), "File '#{fpath}' exists unexpectedly."
  end
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#frozen?Boolean

Tests whether object is frozen or not.

ok {"foo".freeze}.frozen?     # Pass
ok {"foo"}.NOT.frozen?        # Pass


622
623
624
625
626
627
628
629
630
# File 'lib/minitest/ok.rb', line 622

def frozen?
  _mark_as_tested()
  @context.assert_predicate @actual, :frozen?  unless @not
  @context.refute_predicate @actual, :frozen?  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#in?(expected) ⇒ Boolean

Same as assert_includes().

ok {1}.in?(1..9)         # Pass
ok {0}.in?(1..9)         # Fail
ok {0}.NOT.in?(1..9)     # Pass


570
571
572
573
574
575
576
577
578
# File 'lib/minitest/ok.rb', line 570

def in?(expected)
  _mark_as_tested()
  @context.assert_includes expected, @actual  unless @not
  @context.refute_includes expected, @actual  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#in_delta?(expected, delta) ⇒ Boolean

Same as assert_in_delta().

ok {3.14159}.in_delta?(3.14, 0.01)       # Pass
ok {3.14159}.in_delta?(3.14, 0.001)      # Fail


422
423
424
425
426
427
428
429
430
# File 'lib/minitest/ok.rb', line 422

def in_delta?(expected, delta)
  _mark_as_tested()
  @context.assert_in_delta(expected, @actual, delta)  unless @not
  @context.refute_in_delta(expected, @actual, delta)  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#in_epsilon?(expected, epsilon) ⇒ Boolean

Same as assert_in_epsilon().

ok {3.14159}.in_epsilon?(3.14, 0.001)    # Pass
ok {3.14159}.in_epsilon?(3.14, 0.0001)   # Fail


438
439
440
441
442
443
444
445
446
# File 'lib/minitest/ok.rb', line 438

def in_epsilon?(expected, epsilon)
  _mark_as_tested()
  @context.assert_in_epsilon(expected, @actual, epsilon)  unless @not
  @context.refute_in_epsilon(expected, @actual, epsilon)  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#include?(expected) ⇒ Boolean

Same as assert_includes().

ok {[1, 2, 3]}.include?(2)         # Pass
ok {[1, 2, 3]}.include?(0)         # Fail
ok {[1, 2, 3]}.NOT.include?(0)     # Pass


553
554
555
556
557
558
559
560
561
# File 'lib/minitest/ok.rb', line 553

def include?(expected)
  _mark_as_tested()
  @context.assert_includes @actual, expected  unless @not
  @context.refute_includes @actual, expected  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#instance_of?(expected) ⇒ Boolean

Same as assert_instance_of().

ok {123}.instance_of?(Fixnum)     # Pass
ok {123}.instance_of?(Integer)    # Fail


370
371
372
373
374
375
376
377
378
# File 'lib/minitest/ok.rb', line 370

def instance_of?(expected)
  _mark_as_tested()
  @context.assert_instance_of expected, @actual  unless @not
  @context.refute_instance_of expected, @actual  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#instance_variable_defined?(varname) ⇒ Boolean

Tests whether object has instance variable or not.

class User
  def initialize(name); @name = name; end
end
ok {User.new('Haruhi')}.instance_variable_defined?('@name')  # Pass


656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/minitest/ok.rb', line 656

def instance_variable_defined?(varname)
  _mark_as_tested()
  result = @actual.instance_variable_defined?(varname)
  unless @not
    @context.assert result, "Expected #{@actual.inspect} to have instance variable #{varname}, but not."
  else
    @context.refute result, "Expected #{@actual.inspect} not to have instance variable #{varname}, but has it."
  end
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#item(key, expected) ⇒ Object

Tests key and value.

user = {:name=>'Haruhi', :age=>16}
ok {user}.item(:name, 'Haruhi').item(:age, 16)   # Pass


801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/minitest/ok.rb', line 801

def item(key, expected)
  _mark_as_tested()
  object = @actual
  actual = object[key]
  pr = proc {|op|
    "Expected <object>[#{key.inspect}] #{op} <exected>, but failed.\n" +
    " (object: #{object.inspect})"
  }
  @context.assert_equal expected, actual, Msg.new { pr.call('==') }  unless @not
  @context.refute_equal expected, actual, Msg.new { pr.call('!=') }  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#items(expecteds = {}) ⇒ Object

Tests keys and values.

user = {:name=>'Haruhi', :age=>16}
ok {user}.items(name: 'Haruhi', age: 16)   # Pass


823
824
825
826
827
828
829
830
831
832
# File 'lib/minitest/ok.rb', line 823

def items(expecteds={})
  _mark_as_tested()
  expecteds.each do |key, expected|
    item(key, expected)
  end
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#kind_of?(expected) ⇒ Boolean Also known as: is_a?

Same as assert_kind_of().

ok {123}.kind_of?(Fixnum)     # Pass
ok {123}.kind_af?(Integer)    # Pass
ok {123}.kind_of?(Float)      # Fail


345
346
347
348
349
350
351
352
353
# File 'lib/minitest/ok.rb', line 345

def kind_of?(expected)
  _mark_as_tested()
  @context.assert_kind_of expected, @actual  unless @not
  @context.refute_kind_of expected, @actual  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#nil?Boolean

Same as assert_il().

ok {nil}.nil?            # Pass
ok {" "}.nil?            # Fail
ok {" "}.NOT.nil?        # Pass


328
329
330
331
332
333
334
335
336
# File 'lib/minitest/ok.rb', line 328

def nil?()
  _mark_as_tested()
  @context.assert_nil @actual unless @not
  @context.refute_nil @actual if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#NOTObject

Make logical condition reversed.

ok {[1,2,3]}.empty?       # Fail
ok {[1,2,3]}.NOT.empty?   # Pass


105
106
107
108
# File 'lib/minitest/ok.rb', line 105

def NOT()
  @not = ! @not
  self
end

#not_exist?Boolean

Tests whether file or directory not exist.

ok {'/some/where'}.not_exist?     # Pass
ok {__FILE__}.not_exist?          # Fail
ok {Dir.pwd}.not_exist?           # Fail


883
884
885
886
887
888
889
890
891
892
# File 'lib/minitest/ok.rb', line 883

def not_exist?
  _mark_as_tested()
  fpath = @actual
  @context.assert ! File.exist?(fpath), "'#{fpath}' exists unexpectedly." unless @not
  @context.refute ! File.exist?(fpath), "'#{fpath}' doesn't exist."       if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#output?(stdout = nil, stderr = nil) ⇒ Boolean

Same as assert_output().

ok {proc { puts 'X' }}.output?("X\n")  # Pass
ok {proc { x = 123  }}.NOT.output?     # NOT AVAILABLE


586
587
588
589
590
591
592
593
594
595
# File 'lib/minitest/ok.rb', line 586

def output?(stdout=nil, stderr=nil)
  _mark_as_tested()
  ! @not  or
    raise "use ok().silent? instead of ok().NOT.output?."
  @context.assert_output(stdout, stderr, &@actual)
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#raise?(exception_class, message = nil, &b) ⇒ Boolean

Same as assert_raises(). Optional block will be yielded only when exception raised with exception raised.

ex = ok { proc { 1/0 } }.raise?(ZeroDivisionError)  # Pass
p ex.class        #=> ZeroDivisionError
ex = ok { proc { 1/0 } }.raise?(ZeroDivisionError, "divided by 0")
ex = ok { proc { 1/0 } }.raise?(ZeroDivisionError, /^divided by 0$/)
ok { proc { 1/0 } }.raise?(ZeroDivisionError) do |exc|
  ok {exc.message} =~ /^divided by 0$/
end

ok { proc { 1 / 1 } }.NOT.raise?(Exception)         # Pass


462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/minitest/ok.rb', line 462

def raise?(exception_class, message=nil, &b)
  _mark_as_tested()
  ex = nil
  unless @not
    ex = @context.assert_raises(exception_class) { @actual.call }
    unless message.nil?
      if message.is_a?(Regexp)
        @context.assert_match message, ex.message
      else
        @context.assert_equal message, ex.message
      end
    end
    yield ex if block_given?()
  else
    begin
      @actual.call
    rescue exception_class => ex
      @context.assert false, "Exception #{ex.class} raised unexpectedly."
    else
      @context.assert true
    end
  end
  return ex   # not self!
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#raise_nothing?(&b) ⇒ Boolean

Asserts that a Proc object raises nothing. Not available with .NOT(). Optional block will be yielded when unexpected exception raised.

pr = proc { 1.0 / 0.0 }
ok {pr}.raise_nothing?


498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/minitest/ok.rb', line 498

def raise_nothing?(&b)
  _mark_as_tested()
  ! @not  or
    raise "`NOT.raise_nothing?` is not supported because double negation is difficult to understand."
  @context.assert true          # count up number of assertions
  begin
    @actual.call
  rescue Exception => ex
    yield ex if block_given?()
    #@context.assert false, "Exception #{ex.class} raised unexpectedly."
    ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
    raise
  end
end

#respond_to?(expected) ⇒ Boolean

Same as assert_respond_to().

ok {"X"}.respond_to?(:each)     # Pass
ok {123}.respond_to?(:each)     # Fail


536
537
538
539
540
541
542
543
544
# File 'lib/minitest/ok.rb', line 536

def respond_to?(expected)
  _mark_as_tested()
  @context.assert_respond_to @actual, expected  unless @not
  @context.refute_respond_to @actual, expected  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#same?(expected) ⇒ Boolean

Same as assert_same().

arr = [1]
ok {arr}.same?(arr)               # Pass
ok {arr}.same?([1])               # Fail
ok {arr}.NOT.same?([1])           # Pass


388
389
390
391
392
393
394
395
396
# File 'lib/minitest/ok.rb', line 388

def same?(expected)
  _mark_as_tested()
  @context.assert_same expected, @actual  unless @not
  @context.refute_same expected, @actual  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#silent?Boolean

Same as assert_silent().

ok {proc { x = 1234 }}.silent?       # Pass
ok {proc { puts 'X' }}.NOT.silent?   # NOT AVAILABLE


603
604
605
606
607
608
609
610
611
612
# File 'lib/minitest/ok.rb', line 603

def silent?
  _mark_as_tested()
  ! @not  or
    raise "use ok().output? instead of ok().NOT.silent?."
  @context.assert_silent(&@actual)
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#tainted?Boolean

Tests whether object is tainted or not.

ok {"foo".tainted}.tainted?   # Pass
ok {"foo"}.NOT.tainted?       # Pass


638
639
640
641
642
643
644
645
646
# File 'lib/minitest/ok.rb', line 638

def tainted?
  _mark_as_tested()
  @context.assert_predicate @actual, :tainted?  unless @not
  @context.refute_predicate @actual, :tainted?  if     @not
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#throw?(sym) ⇒ Boolean

Same as assert_throws().

ok {proc {throw :exit}}.throw?(:exit)   # Pass
ok {proc {nil}}.NOT.throw?(:exit)       # NOT AVAILABLE


519
520
521
522
523
524
525
526
527
528
# File 'lib/minitest/ok.rb', line 519

def throw?(sym)
  _mark_as_tested()
  ! @not  or
    raise "NOT.throw? is unsupported because refute_throws() is not defined in Minitest."
  @context.assert_throws(sym) { @actual.call }
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#truthy?Boolean

Tests whether actual is regarded as true-like value.

ok {true}.truthy?                 # Pass
ok {0}.truthy?                    # Pass
ok {""}.truthy?                   # Pass
ok {[]}.truthy?                   # Pass
ok {nil}.truthy?                  # Fail
ok {false}.truthy?                # Fail


717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/minitest/ok.rb', line 717

def truthy?
  _mark_as_tested()
  unless @not
    @context.assert @actual, Msg.new { "Expected (!! #{@actual.inspect}) == true, but not." }
  else
    @context.refute @actual, Msg.new { "Expected (!! #{@actual.inspect}) == false, but not." }
  end
  self
rescue Minitest::Assertion => ex
  ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }
  raise
end

#xObject

Same as assert_predicate().



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/minitest/ok.rb', line 279

self.instance_methods().grep(/\?/).each do |x|
  next if x == :equal?
  next if x == :nil? || x == :frozen? || x == :respond_to?
  next if x == :is_a? || x == :kind_of? || x == :instance_of?
  next if x == :instance_variable_defined? || x == :tainted?
  eval "    def \#{x}(*args, **kwargs, &block)\n      _mark_as_tested()\n      __assert_predicate(:\#{x}, *args, **kwargs, &block)\n    rescue Minitest::Assertion => ex\n      #ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) }\n      raise\n    end\n  END\nend\n", binding(), __FILE__, __LINE__ + 1