class Minitest::Ok::AssertionObject
Public Class Methods
Don't create this object directly. Use ok {value}
instead.
# File lib/minitest/ok.rb, line 72 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
Public Instance Methods
Same as refute_equal()
.
ok {1+1} != 1 # Pass ok {1+1} != 2 # Fail
# File lib/minitest/ok.rb, line 136 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
Same as refute_match()
.
ok {"abc"} !~ /\d+/ # Pass ok {"abc"} !~ /\w+/ # Fail
# File lib/minitest/ok.rb, line 252 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
Tests actual < expected
.
ok {1+1} < 3 # Pass ok {1+1} < 2 # Fail ok {1+1} < 1 # Fail
# File lib/minitest/ok.rb, line 187 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
Tests actual <= expected
.
ok {1+1} <= 3 # Pass ok {1+1} <= 2 # Pass ok {1+1} <= 1 # Fail
# File lib/minitest/ok.rb, line 204 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
Same as assert_equal()
.
ok {1+1} == 2 # Pass ok {1+1} == 1 # Fail
# File lib/minitest/ok.rb, line 115 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
Tests actual === expected
.
ok {String} === 'foo' # Pass ok {/\d+/} === '123' # Pass
# File lib/minitest/ok.rb, line 220 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
Same as assert_match()
.
ok {"abc"} =~ /\w+/ # Pass ok {"abc"} =~ /\d+/ # Fail
# File lib/minitest/ok.rb, line 236 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
Tests actual > expected
.
ok {1+1} > 1 # Pass ok {1+1} > 2 # Fail ok {1+1} > 3 # Fail
# File lib/minitest/ok.rb, line 153 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
Tests actual >= expected
.
ok {1+1} >= 1 # Pass ok {1+1} >= 2 # Pass ok {1+1} >= 3 # Fail
# File lib/minitest/ok.rb, line 170 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
Make logical condition reversed.
ok {[1,2,3]}.empty? # Fail ok {[1,2,3]}.NOT.empty? # Pass
# File lib/minitest/ok.rb, line 104 def NOT() @not = ! @not self end
# File lib/minitest/ok.rb, line 94 def _msg(&block) Msg.new(block) end
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
# File lib/minitest/ok.rb, line 659 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
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
# File lib/minitest/ok.rb, line 682 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
Tests whether directory exists or not.
ok {Dir.pwd}.dir_exist? # Pass ok {'/some/where'}.dir_exist? # Fail ok {__FILE__}.dir_exist? # Fail
# File lib/minitest/ok.rb, line 760 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
Same as assert_empty()
.
ok {""}.empty? # Pass ok {[]}.empty? # Pass ok {"X"}.empty? # Fail ok {"X"}.NOT.empty? # Pass
# File lib/minitest/ok.rb, line 330 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
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
# File lib/minitest/ok.rb, line 638 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
Tests whether file exists or not.
ok {__FILE__}.file_exist? # Pass ok {'/some/where'}.file_exist? # Fail ok {Dir.pwd}.file_exist? # Fail
# File lib/minitest/ok.rb, line 739 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
Tests whether object is frozen or not.
ok {"foo".freeze}.frozen? # Pass ok {"foo"}.NOT.frozen? # Pass
# File lib/minitest/ok.rb, line 518 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
Same as assert_includes()
.
ok {1}.in?(1..9) # Pass ok {0}.in?(1..9) # Fail ok {0}.NOT.in?(1..9) # Pass
# File lib/minitest/ok.rb, line 466 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
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
# File lib/minitest/ok.rb, line 346 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
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
# File lib/minitest/ok.rb, line 362 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
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
# File lib/minitest/ok.rb, line 449 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
Same as assert_instance_of()
.
ok {123}.instance_of?(Fixnum) # Pass ok {123}.instance_of?(Integer) # Fail
# File lib/minitest/ok.rb, line 294 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
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
# File lib/minitest/ok.rb, line 552 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
Same as assert_kind_of()
.
ok {123}.is_a?(Fixnum) # Pass ok {123}.is_a?(Integer) # Pass ok {123}.is_a?(Float) # Fail
Tests key and value.
user = {:name=>'Haruhi', :age=>16} ok {user}.item(:name, 'Haruhi').item(:age, 16) # Pass
# File lib/minitest/ok.rb, line 699 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
Tests keys and values.
user = {:name=>'Haruhi', :age=>16} ok {user}.items(name: 'Haruhi', age: 16) # Pass
# File lib/minitest/ok.rb, line 721 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
Same as assert_kind_of()
.
ok {123}.kind_of?(Fixnum) # Pass ok {123}.kind_af?(Integer) # Pass ok {123}.kind_of?(Float) # Fail
# File lib/minitest/ok.rb, line 269 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
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
# File lib/minitest/ok.rb, line 573 def method_missing(symbol, *args, &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 result = @actual.__send__(symbol, *args, &block) @context.assert result, Msg.new { "Expected #{@actual.inspect}.#{symbol}(#{args.inspect[1..-2]}) but failed." } 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 result = @actual.__send__(symbol, *args, &block) @context.refute result, Msg.new { "Expected #{@actual.inspect}.#{symbol}(#{args.inspect[1..-2]}) to fail but succeeded." } end end self rescue Minitest::Assertion => ex ex.backtrace.delete_if {|bt| bt.start_with?(__FILE__) } raise end
Tests whether file or directory not exist.
ok {'/some/where'}.not_exist? # Pass ok {__FILE__}.not_exist? # Fail ok {Dir.pwd}.not_exist? # Fail
# File lib/minitest/ok.rb, line 781 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
Same as assert_output()
.
ok {proc { puts 'X' }}.output?("X\n") # Pass ok {proc { x = 123 }}.NOT.output? # NOT AVAILABLE
# File lib/minitest/ok.rb, line 482 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
Same as assert_raises()
.
ex = ok { proc { 1/0 } }.raise?(ZeroDivisionError) # Pass p ex.class #=> ZeroDivisionError ex = ok { proc { 1/0 } }.raise?(ZeroDivisionError, "divided by zero") ex = ok { proc { 1/0 } }.raise?(ZeroDivisionError, /^divided by zero$/) ok { proc { 1 / 1 } }.NOT.raise?(Exception) # Pass
# File lib/minitest/ok.rb, line 382 def raise?(exception_class, message=nil) _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 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
Same as assert_respond_to()
.
ok {"X"}.respond_to?(:each) # Pass ok {123}.respond_to?(:each) # Fail
# File lib/minitest/ok.rb, line 432 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 as assert_same()
.
arr = [1] ok {arr}.same?(arr) # Pass ok {arr}.same?([1]) # Fail ok {arr}.NOT.same?([1]) # Pass
# File lib/minitest/ok.rb, line 312 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
Same as assert_silent()
.
ok {proc { x = 1234 }}.silent? # Pass ok {proc { puts 'X' }}.NOT.silent? # NOT AVAILABLE
# File lib/minitest/ok.rb, line 499 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
Tests whether object is tainted or not.
ok {"foo".tainted}.tainted? # Pass ok {"foo"}.NOT.tainted? # Pass
# File lib/minitest/ok.rb, line 534 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
Same as assert_throws()
.
ok {proc {throw :exit}}.throw?(:exit) # Pass ok {proc {nil}}.NOT.throw?(:exit) # NOT AVAILABLE
# File lib/minitest/ok.rb, line 415 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
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
# File lib/minitest/ok.rb, line 615 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