Module: RBS::UnitTest::WithAliases

Includes:
Convertibles
Defined in:
sig/unit_test/with_aliases.rbs,
lib/rbs/unit_test/with_aliases.rb

Overview

WithAliases module provides with_*** methods that helps writing tests which passes convertible objects

include WithAliases

with_int do |i|
  # Yields Integer and RBS::UnitTest::Convertibles::ToInt
end

with_string.and_nil do |s|
  # Yields String, RBS::UnitTest::Convertibles::ToStr, and nil
end

Defined Under Namespace

Classes: WithEnum

Instance Method Summary collapse

Instance Method Details

#with(args) ⇒ void #with(args) ⇒ void

Yields args objects

Overloads:

  • #with(args) ⇒ void

    This method returns an undefined value.

    Parameters:

    • args (Object)
  • #with(args) ⇒ void

    This method returns an undefined value.

    Parameters:

    • args (Object)

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (void)


68
69
70
71
# File 'sig/unit_test/with_aliases.rbs', line 68

def with(*args, &block)
  return WithEnum.new to_enum(__method__ || raise, *args) unless block
  args.each(&block)
end

#with_arrayvoid #with_arrayvoid

Yields ::array objects

Overloads:

  • #with_arrayvoid

    This method returns an undefined value.

  • #with_arrayvoid

    This method returns an undefined value.



88
89
90
91
92
93
# File 'sig/unit_test/with_aliases.rbs', line 88

def with_array(*elements)
  return WithEnum.new to_enum(__method__ || raise, *elements) unless block_given?

  yield _ = elements
  yield ToArray.new(*elements)
end

#with_boolvoid #with_boolWithEnum[bool]

Yields ::bool objects

Overloads:

  • #with_boolvoid

    This method returns an undefined value.

  • #with_boolWithEnum[bool]

    Returns:

Yields:

Yield Parameters:

  • arg0 (Boolean)

Yield Returns:

  • (void)


118
119
120
121
122
# File 'sig/unit_test/with_aliases.rbs', line 118

def with_bool(&block)
  return WithEnum.new to_enum(__method__ || raise) unless block
  yield true
  yield false
end

#with_boolishvoid #with_boolishWithEnum[boolish] Also known as: with_untyped

Yields ::boolish objects

Overloads:

  • #with_boolishvoid

    This method returns an undefined value.

  • #with_boolishWithEnum[boolish]

    Returns:

Yields:

Yield Parameters:

  • arg0 (boolish)

Yield Returns:

  • (void)


123
124
125
126
127
# File 'sig/unit_test/with_aliases.rbs', line 123

def with_boolish(&block)
  return WithEnum.new to_enum(__method__ || raise) unless block
  with_bool(&block)
  [nil, 1, Object.new, BlankSlate.new, "hello, world!"].each(&block)
end

#with_encoding(encoding) ⇒ void #with_encoding(encoding) ⇒ WithEnum[encoding]

Yields ::encoding objects

Overloads:

  • #with_encoding(encoding) ⇒ void

    This method returns an undefined value.

    Parameters:

    • encoding (Object)
  • #with_encoding(encoding) ⇒ WithEnum[encoding]

    Parameters:

    • encoding (Object)

    Returns:

Yields:

Yield Parameters:

  • arg0 (encoding)

Yield Returns:

  • (void)


108
109
110
111
112
113
# File 'sig/unit_test/with_aliases.rbs', line 108

def with_encoding(encoding = Encoding::UTF_8, &block)
  return WithEnum.new to_enum(__method__ || raise, encoding) unless block

  block.call encoding
  with_string(encoding.to_s, &block)
end

#with_float(value) ⇒ void #with_float(value) ⇒ WithEnum[float]

Yields ::float objects

Overloads:

  • #with_float(value) ⇒ void

    This method returns an undefined value.

    Parameters:

    • value (Float)
  • #with_float(value) ⇒ WithEnum[float]

    Parameters:

    • value (Float)

    Returns:

Yields:

Yield Parameters:

  • arg0 (float)

Yield Returns:

  • (void)


78
79
80
81
82
# File 'sig/unit_test/with_aliases.rbs', line 78

def with_float(value = 0.1)
  return WithEnum.new to_enum(__method__ || raise, value) unless block_given?
  yield value
  yield ToF.new(value)
end

#with_hashvoid #with_hashvoid

Yields ::hash objects

Overloads:

  • #with_hashvoid

    This method returns an undefined value.

  • #with_hashvoid

    This method returns an undefined value.



93
94
95
96
97
98
# File 'sig/unit_test/with_aliases.rbs', line 93

def with_hash(hash = {})
  return WithEnum.new to_enum(__method__ || raise, hash) unless block_given?

  yield _ = hash
  yield ToHash.new(hash)
end

#with_int(value) ⇒ void #with_int(value) ⇒ WithEnum[int]

Yields ::int objects

Overloads:

  • #with_int(value) ⇒ void

    This method returns an undefined value.

    Parameters:

    • value (Integer)
  • #with_int(value) ⇒ WithEnum[int]

    Parameters:

    • value (Integer)

    Returns:

Yields:

Yield Parameters:

  • arg0 (int)

Yield Returns:

  • (void)


73
74
75
76
77
# File 'sig/unit_test/with_aliases.rbs', line 73

def with_int(value = 3, &block)
  return WithEnum.new to_enum(__method__ || raise, value) unless block
  yield value
  yield ToInt.new(value)
end

#with_interned(value) ⇒ void #with_interned(value) ⇒ WithEnum[interned]

Yields ::interned objects

Overloads:

  • #with_interned(value) ⇒ void

    This method returns an undefined value.

    Parameters:

    • value (Symbol)
  • #with_interned(value) ⇒ WithEnum[interned]

    Parameters:

    • value (Symbol)

    Returns:

Yields:

Yield Parameters:

  • arg0 (interned)

Yield Returns:

  • (void)


113
114
115
116
117
118
# File 'sig/unit_test/with_aliases.rbs', line 113

def with_interned(value = :&, &block)
  return WithEnum.new to_enum(__method__ || raise, value) unless block

  with_string(value.to_s, &block)
  block.call value.to_sym
end

#with_io(io) ⇒ void #with_io(io) ⇒ WithEnum[io]

Yields ::io objects

Overloads:

  • #with_io(io) ⇒ void

    This method returns an undefined value.

    Parameters:

    • io (Object)
  • #with_io(io) ⇒ WithEnum[io]

    Parameters:

    • io (Object)

    Returns:

Yields:

Yield Parameters:

  • arg0 (io)

Yield Returns:

  • (void)


98
99
100
101
102
# File 'sig/unit_test/with_aliases.rbs', line 98

def with_io(io = $stdout)
  return WithEnum.new to_enum(__method__ || raise, io) unless block_given?
  yield io
  yield ToIO.new(io)
end

#with_path(path) ⇒ void #with_path(path) ⇒ WithEnum[path]

Yields ::path objects

Overloads:

  • #with_path(path) ⇒ void

    This method returns an undefined value.

    Parameters:

    • path (String)
  • #with_path(path) ⇒ WithEnum[path]

    Parameters:

    • path (String)

    Returns:

Yields:

Yield Parameters:

  • arg0 (path)

Yield Returns:

  • (void)


103
104
105
106
107
108
# File 'sig/unit_test/with_aliases.rbs', line 103

def with_path(path = "/tmp/foo.txt", &block)
  return WithEnum.new to_enum(__method__ || raise, path) unless block

  with_string(path, &block)
  block.call ToPath.new(path)
end

#with_range(start, stop, exclude_end = false) {|arg0| ... } ⇒ void

This method returns an undefined value.

Yields ::range objects

Parameters:

  • start (WithEnum[untyped])
  • stop (WithEnum[untyped])
  • exclude_end (Boolean) (defaults to: false)

Yields:

Yield Parameters:

  • arg0 (range[untyped])

Yield Returns:

  • (void)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'sig/unit_test/with_aliases.rbs', line 133

def with_range(start, stop, exclude_end = false)
  # If you need fixed starting and stopping points, you can just do `with_range with(1), with(2)`.
  raise ArgumentError, '`start` must be from a `with` method' unless start.is_a? WithEnum
  raise ArgumentError, '`stop` must be from a `with` method' unless stop.is_a? WithEnum

  start.each do |lower|
    stop.each do |upper|
      yield CustomRange.new(lower, upper, exclude_end)

      # `Range` requires `begin <=> end` to return non-nil, but doesn't actually
      # end up using the return value of it. This is to add that in when needed.
      unless defined? lower.<=>
        def lower.<=>(rhs) = :not_nil # steep:ignore MethodDefinitionInUndeclaredModule
      end

      # If `lower <=> rhs` is defined but nil, then that means we're going to be constructing
      # an illegal range (eg `3..ToInt.new(4)`). So, we need to skip yielding an invalid range
      # in that case.
      next if defined?(lower.<=>) && nil == (lower <=> upper)

      yield Range.new(lower, upper, exclude_end)
    end
  end
end

#with_string(value) ⇒ void #with_string(value) ⇒ WithEnum[string]

Yields ::string objects

Overloads:

  • #with_string(value) ⇒ void

    This method returns an undefined value.

    Parameters:

    • value (String)
  • #with_string(value) ⇒ WithEnum[string]

    Parameters:

    • value (String)

    Returns:

Yields:

Yield Parameters:

  • arg0 (string)

Yield Returns:

  • (void)


83
84
85
86
87
# File 'sig/unit_test/with_aliases.rbs', line 83

def with_string(value = '')
  return WithEnum.new to_enum(__method__ || raise, value) unless block_given?
  yield value
  yield ToStr.new(value)
end