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
-
#with(*args) {|arg0| ... } ⇒ Object
Yields
argsobjects. -
#with_array(*elements) ⇒ Object
Yields
::arrayobjects. -
#with_bool {|arg0| ... } ⇒ Object
Yields
::boolobjects. -
#with_boolish {|arg0| ... } ⇒ Object
(also: #with_untyped)
Yields
::boolishobjects. -
#with_encoding(encoding = Encoding::UTF_8) {|arg0| ... } ⇒ Object
Yields
::encodingobjects. -
#with_float(value = 0.1) {|arg0| ... } ⇒ Object
Yields
::floatobjects. -
#with_hash(hash = {}) ⇒ Object
Yields
::hashobjects. -
#with_int(value = 3) {|arg0| ... } ⇒ Object
Yields
::intobjects. -
#with_interned(value = :&) {|arg0| ... } ⇒ Object
Yields
::internedobjects. -
#with_io(io = $stdout) {|arg0| ... } ⇒ Object
Yields
::ioobjects. -
#with_path(path = "/tmp/foo.txt") {|arg0| ... } ⇒ Object
Yields
::pathobjects. -
#with_range(start, stop, exclude_end = false) {|arg0| ... } ⇒ void
Yields
::rangeobjects. -
#with_string(value = '') {|arg0| ... } ⇒ Object
Yields
::stringobjects.
Instance Method Details
#with(args) ⇒ void #with(args) ⇒ void
Yields args objects
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_array ⇒ void #with_array ⇒ void
Yields ::array objects
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_bool ⇒ void #with_bool ⇒ WithEnum[bool]
Yields ::bool objects
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_boolish ⇒ void #with_boolish ⇒ WithEnum[boolish] Also known as: with_untyped
Yields ::boolish objects
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
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
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_hash ⇒ void #with_hash ⇒ void
Yields ::hash objects
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
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
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
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
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
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 |