Class: Minitest::ShouldJustWork::Should

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/should_just_work.rb,
sig/minitest/should_just_work.rbs

Overview

The object returned by Object#should. It remembers what .should was called on and forwards the comparison to the test that is currently running.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left) ⇒ Should

Returns a new instance of Should.

Parameters:



58
59
60
61
62
63
64
65
66
# File 'lib/minitest/should_just_work.rb', line 58

def initialize(left)
  @left = left
  @neg = false

  pending = test.msg
  return unless pending

  test.msg = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)



142
143
144
145
146
# File 'lib/minitest/should_just_work.rb', line 142

def method_missing(name, ...)
  ::Kernel.raise(NoMethodError,
                 "undefined matcher '#{name}'. minitest-should_just_work supports the operators " \
                 "==, !=, =~, <, >, <= and >=, plus should.raise and should.throw")
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.

Returns:



115
116
117
# File 'lib/minitest/should_just_work.rb', line 115

def left
  @left
end

#msgString? (readonly)

Returns the value of attribute msg.

Returns:

  • (String, nil)


115
116
117
# File 'lib/minitest/should_just_work.rb', line 115

def msg
  @msg
end

Class Method Details

.currentObject

:nodoc:

Returns:



54
55
56
# File 'lib/minitest/should_just_work.rb', line 54

def self.current # :nodoc:
  ::Thread.current.thread_variable_get(:minitest_should_just_work_test)
end

.init(test) ⇒ void

This method returns an undefined value.

Remembers the test that is currently running. Minitest can run tests in parallel threads, so the test is kept in a thread variable rather than in a class variable shared by every thread.

Parameters:



50
51
52
# File 'lib/minitest/should_just_work.rb', line 50

def self.init(test) # :nodoc:
  ::Thread.current.thread_variable_set(:minitest_should_just_work_test, test)
end

Instance Method Details

#!=(other) ⇒ Object

Parameters:

Returns:



74
75
76
# File 'lib/minitest/should_just_work.rb', line 74

def !=(other)
  other.nil? ? refute_or_assert(:nil, left) : refute_or_assert(:equal, other, left)
end

#<(other) ⇒ Object

Parameters:

Returns:



80
# File 'lib/minitest/should_just_work.rb', line 80

def <(other) = assert_or_refute(:operator, left, :<, other)

#<=(other) ⇒ Object

Parameters:

Returns:



82
# File 'lib/minitest/should_just_work.rb', line 82

def <=(other) = assert_or_refute(:operator, left, :<=, other)

#==(other) ⇒ Object

should == nil means assert_nil rather than assert_equal, which Minitest 6 refuses outright and Minitest 5 deprecates.

Parameters:

Returns:



70
71
72
# File 'lib/minitest/should_just_work.rb', line 70

def ==(other)
  other.nil? ? assert_or_refute(:nil, left) : assert_or_refute(:equal, other, left)
end

#=~(other) ⇒ Object

Parameters:

Returns:



78
# File 'lib/minitest/should_just_work.rb', line 78

def =~(other) = assert_or_refute(:match, other, left)

#>(other) ⇒ Object

Parameters:

Returns:



79
# File 'lib/minitest/should_just_work.rb', line 79

def >(other) = assert_or_refute(:operator, left, :>, other)

#>=(other) ⇒ Object

Parameters:

Returns:



81
# File 'lib/minitest/should_just_work.rb', line 81

def >=(other) = assert_or_refute(:operator, left, :>=, other)

#assert_or_refute(what, *args) ⇒ Object

Parameters:

Returns:



125
126
127
128
# File 'lib/minitest/should_just_work.rb', line 125

def assert_or_refute(what, *args)
  args << msg if msg
  test.send(positive? ? :"assert_#{what}" : :"refute_#{what}", *args)
end

#capture(exception) { ... } ⇒ Exception?

Parameters:

  • exception (Class)

Yields:

Yield Returns:

Returns:

  • (Exception, nil)


135
136
137
138
139
140
# File 'lib/minitest/should_just_work.rb', line 135

def capture(exception)
  yield
  nil
rescue exception => e
  e
end

#negative?Boolean

Returns:

  • (Boolean)


123
# File 'lib/minitest/should_just_work.rb', line 123

def negative? = @neg

#notObject

Negates the comparison that follows. should_not is the shorthand.



85
86
87
88
# File 'lib/minitest/should_just_work.rb', line 85

def not
  @neg = true
  self
end

#positive?Boolean

Returns:

  • (Boolean)


122
# File 'lib/minitest/should_just_work.rb', line 122

def positive? = !@neg

#raise(exception = ::StandardError) { ... } ⇒ Object

should.raise(SomeError) { ... } and should_not.raise(SomeError) { ... }

Parameters:

  • exception (Class) (defaults to: ::StandardError)

Yields:

Yield Returns:

Returns:



105
106
107
108
109
110
111
# File 'lib/minitest/should_just_work.rb', line 105

def raise(exception = ::StandardError, &)
  return test.send(:assert_raises, *[exception, msg].compact, &) if positive?

  raised = capture(exception, &)
  test.send(:refute, raised,
            [msg, "Expected the block not to raise #{exception}, got #{raised.inspect}"].compact.join("\n"))
end

#refute_or_assert(what, *args) ⇒ Object

Parameters:

Returns:



130
131
132
133
# File 'lib/minitest/should_just_work.rb', line 130

def refute_or_assert(what, *args)
  args << msg if msg
  test.send(negative? ? :"assert_#{what}" : :"refute_#{what}", *args)
end

#testObject

Returns:



117
118
119
120
# File 'lib/minitest/should_just_work.rb', line 117

def test
  Should.current ||
    ::Kernel.raise(Error, "#should can only be used inside a running Minitest test")
end

#throw(sym = nil) { ... } ⇒ Object

should.throw(:sym) { ... } and should_not.throw(:sym) { ... }

Parameters:

  • sym (Symbol) (defaults to: nil)

Yields:

Yield Returns:

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/minitest/should_just_work.rb', line 91

def throw(sym = nil, &blk)
  return test.send(:assert_throws, sym, msg, &blk) if positive?

  ::Kernel.raise(Error, "should_not.throw needs a symbol to watch for") if sym.nil?

  thrown = true
  ::Kernel.catch(sym) do
    blk.call
    thrown = false
  end
  test.send(:refute, thrown, [msg, "Expected the block not to throw #{sym.inspect}"].compact.join("\n"))
end