1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
describe "Method#parameters" do
class MethodSpecs::Methods
def one_key(a: 1); end
def one_keyrest(**a); end
def one_keyreq(a:); end
def one_nokey(**nil); end
def one_splat_one_req(*a,b); end
def one_splat_two_req(*a,b,c); end
def one_splat_one_req_with_block(*a,b,&blk); end
def one_opt_with_stabby(a=-> b { true }); end
def one_unnamed_splat(*); end
def one_unnamed_keyrest(**); end
def one_splat_one_block(*args, &block)
local_is_not_parameter = {}
end
def underscore_parameters(_, _, _ = 1, *_, _:, _: 2, **_, &_); end
define_method(:one_optional_defined_method) {|x = 1|}
end
it "returns an empty Array when the method expects no arguments" do
MethodSpecs::Methods.instance_method(:zero).parameters.should == []
end
it "returns [[:req,:name]] for a method expecting one required argument called 'name'" do
MethodSpecs::Methods.instance_method(:one_req).parameters.should == [[:req,:a]]
end
it "returns [[:req,:a],[:req,:b]] for a method expecting two required arguments called 'a' and 'b''" do
m = MethodSpecs::Methods.instance_method(:two_req)
m.parameters.should == [[:req,:a], [:req,:b]]
end
it "returns [[:block,:blk]] for a method expecting one block argument called 'a'" do
m = MethodSpecs::Methods.instance_method(:zero_with_block)
m.parameters.should == [[:block,:blk]]
end
it "returns [[:req,:a],[:block,:b] for a method expecting a required argument ('a') and a block argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_req_with_block)
m.parameters.should == [[:req,:a], [:block,:blk]]
end
it "returns [[:req,:a],[:req,:b],[:block,:c] for a method expecting two required arguments ('a','b') and a block argument ('c')" do
m = MethodSpecs::Methods.instance_method(:two_req_with_block)
m.parameters.should == [[:req,:a], [:req,:b], [:block,:blk]]
end
it "returns [[:opt,:a]] for a method expecting one optional argument ('a')" do
m = MethodSpecs::Methods.instance_method(:one_opt)
m.parameters.should == [[:opt,:a]]
end
it "returns [[:req,:a],[:opt,:b]] for a method expecting one required argument ('a') and one optional argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt)
m.parameters.should == [[:req,:a],[:opt,:b]]
end
it "returns [[:req,:a],[:opt,:b]] for a method expecting one required argument ('a') and one optional argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt)
m.parameters.should == [[:req,:a],[:opt,:b]]
end
it "returns [[:req,:a],[:opt,:b],[:opt,:c]] for a method expecting one required argument ('a') and two optional arguments ('b','c')" do
m = MethodSpecs::Methods.instance_method(:one_req_two_opt)
m.parameters.should == [[:req,:a],[:opt,:b],[:opt,:c]]
end
it "returns [[:req,:a],[:req,:b],[:opt,:c]] for a method expecting two required arguments ('a','b') and one optional arguments ('c')" do
m = MethodSpecs::Methods.instance_method(:two_req_one_opt)
m.parameters.should == [[:req,:a],[:req,:b],[:opt,:c]]
end
it "returns [[:opt,:a],[:block,:b]] for a method expecting one required argument ('a') and one block argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_opt_with_block)
m.parameters.should == [[:opt,:a],[:block,:blk]]
end
it "returns [[:req,:a],[:opt,:b],[:block,:c]] for a method expecting one required argument ('a'), one optional argument ('b'), and a block ('c')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt_with_block)
m.parameters.should == [[:req,:a],[:opt,:b],[:block,:blk]]
end
it "returns [[:req,:a],[:opt,:b],[:opt,:c],[:block,:d]] for a method expecting one required argument ('a'), two optional arguments ('b','c'), and a block ('d')" do
m = MethodSpecs::Methods.instance_method(:one_req_two_opt_with_block)
m.parameters.should == [[:req,:a],[:opt,:b],[:opt,:c],[:block,:blk]]
end
it "returns [[:rest,:a]] for a method expecting a single splat argument ('a')" do
m = MethodSpecs::Methods.instance_method(:zero_with_splat)
m.parameters.should == [[:rest,:a]]
end
it "returns [[:req,:a],[:rest,:b]] for a method expecting a splat argument ('a') and a required argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_req_with_splat)
m.parameters.should == [[:req,:a],[:rest,:b]]
end
it "returns [[:req,:a],[:req,:b],[:rest,:c]] for a method expecting two required arguments ('a','b') and a splat argument ('c')" do
m = MethodSpecs::Methods.instance_method(:two_req_with_splat)
m.parameters.should == [[:req,:a],[:req,:b],[:rest,:c]]
end
it "returns [[:req,:a],[:opt,:b],[:rest,:c]] for a method expecting a required argument ('a','b'), an optional argument ('b'), and a splat argument ('c')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt_with_splat)
m.parameters.should == [[:req,:a],[:opt,:b],[:rest,:c]]
end
it "returns [[:req,:a],[:req,:b],[:opt,:b],[:rest,:d]] for a method expecting two required arguments ('a','b'), an optional argument ('c'), and a splat argument ('d')" do
m = MethodSpecs::Methods.instance_method(:two_req_one_opt_with_splat)
m.parameters.should == [[:req,:a],[:req,:b],[:opt,:c],[:rest,:d]]
end
it "returns [[:req,:a],[:opt,:b],[:opt,:c],[:rest,:d]] for a method expecting a required argument ('a'), two optional arguments ('b','c'), and a splat argument ('d')" do
m = MethodSpecs::Methods.instance_method(:one_req_two_opt_with_splat)
m.parameters.should == [[:req,:a],[:opt,:b],[:opt,:c],[:rest,:d]]
end
it "returns [[:rest,:a],[:block,:b]] for a method expecting a splat argument ('a') and a block argument ('b')" do
m = MethodSpecs::Methods.instance_method(:zero_with_splat_and_block)
m.parameters.should == [[:rest,:a],[:block,:blk]]
end
it "returns [[:req,:a],[:rest,:b],[:block,:c]] for a method expecting a required argument ('a'), a splat argument ('b'), and a block ('c')" do
m = MethodSpecs::Methods.instance_method(:one_req_with_splat_and_block)
m.parameters.should == [[:req,:a],[:rest,:b],[:block,:blk]]
end
it "returns [[:req,:a],[:req,:b],[:rest,:c],[:block,:d]] for a method expecting two required arguments ('a','b'), a splat argument ('c'), and a block ('d')" do
m = MethodSpecs::Methods.instance_method(:two_req_with_splat_and_block)
m.parameters.should == [[:req,:a],[:req,:b],[:rest,:c],[:block,:blk]]
end
it "returns [[:req,:a],[:opt,:b],[:rest,:c],[:block,:d]] for a method expecting a required argument ('a'), a splat argument ('c'), and a block ('d')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt_with_splat_and_block)
m.parameters.should == [[:req,:a],[:opt,:b],[:rest,:c],[:block,:blk]]
end
it "returns [[:req,:a],[:req,:b],[:opt,:c],[:block,:d]] for a method expecting two required arguments ('a','b'), an optional argument ('c'), a splat argument ('d'), and a block ('e')" do
m = MethodSpecs::Methods.instance_method(:two_req_one_opt_with_splat_and_block)
m.parameters.should == [[:req,:a],[:req,:b],[:opt,:c],[:rest,:d],[:block,:blk]]
end
it "returns [[:rest,:a],[:req,:b]] for a method expecting a splat argument ('a') and a required argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_splat_one_req)
m.parameters.should == [[:rest,:a],[:req,:b]]
end
it "returns [[:rest,:a],[:req,:b],[:req,:c]] for a method expecting a splat argument ('a') and two required arguments ('b','c')" do
m = MethodSpecs::Methods.instance_method(:one_splat_two_req)
m.parameters.should == [[:rest,:a],[:req,:b],[:req,:c]]
end
it "returns [[:rest,:a],[:req,:b],[:block,:c]] for a method expecting a splat argument ('a'), a required argument ('b'), and a block ('c')" do
m = MethodSpecs::Methods.instance_method(:one_splat_one_req_with_block)
m.parameters.should == [[:rest,:a],[:req,:b],[:block,:blk]]
end
it "returns [[:key,:a]] for a method with a single optional keyword argument" do
m = MethodSpecs::Methods.instance_method(:one_key)
m.parameters.should == [[:key,:a]]
end
it "returns [[:keyrest,:a]] for a method with a keyword rest argument" do
m = MethodSpecs::Methods.instance_method(:one_keyrest)
m.parameters.should == [[:keyrest,:a]]
end
it "returns [[:keyreq,:a]] for a method with a single required keyword argument" do
m = MethodSpecs::Methods.instance_method(:one_keyreq)
m.parameters.should == [[:keyreq,:a]]
end
it "returns [[:nokey]] for a method with a single **nil parameter" do
m = MethodSpecs::Methods.instance_method(:one_nokey)
m.parameters.should == [[:nokey]]
end
it "works with ->(){} as the value of an optional argument" do
m = MethodSpecs::Methods.instance_method(:one_opt_with_stabby)
m.parameters.should == [[:opt,:a]]
end
# define_method variants
it "returns [] for a define_method method with explicit no-args || specification" do
m = MethodSpecs::Methods.instance_method(:zero_defined_method)
m.parameters.should == []
end
it "returns [[:rest, :x]] for a define_method method with rest arg 'x' only" do
m = MethodSpecs::Methods.instance_method(:zero_with_splat_defined_method)
m.parameters.should == [[:rest, :x]]
end
it "returns [[:req, :x]] for a define_method method expecting one required argument 'x'" do
m = MethodSpecs::Methods.instance_method(:one_req_defined_method)
m.parameters.should == [[:req, :x]]
end
it "returns [[:req, :x], [:req, :y]] for a define_method method expecting two required arguments 'x' and 'y'" do
m = MethodSpecs::Methods.instance_method(:two_req_defined_method)
m.parameters.should == [[:req, :x], [:req, :y]]
end
it "returns [] for a define_method method with no args specification" do
m = MethodSpecs::Methods.instance_method(:no_args_defined_method)
m.parameters.should == []
end
it "returns [[:req]] for a define_method method with a grouping as its only argument" do
m = MethodSpecs::Methods.instance_method(:two_grouped_defined_method)
m.parameters.should == [[:req]]
end
it "returns [[:opt, :x]] for a define_method method with an optional argument 'x'" do
m = MethodSpecs::Methods.instance_method(:one_optional_defined_method)
m.parameters.should == [[:opt, :x]]
end
it "returns [[:rest]] for a Method generated by respond_to_missing?" do
m = MethodSpecs::Methods.new
m.method(:handled_via_method_missing).parameters.should == [[:rest]]
end
ruby_version_is '3.2' do
it "adds rest arg with name * for \"star\" argument" do
m = MethodSpecs::Methods.new
m.method(:one_unnamed_splat).parameters.should == [[:rest, :*]]
end
it "adds keyrest arg with ** as a name for \"double star\" argument" do
m = MethodSpecs::Methods.new
m.method(:one_unnamed_keyrest).parameters.should == [[:keyrest, :**]]
end
end
ruby_version_is ''...'3.2' do
it "adds nameless rest arg for \"star\" argument" do
m = MethodSpecs::Methods.new
m.method(:one_unnamed_splat).parameters.should == [[:rest]]
end
it "adds nameless keyrest arg for \"double star\" argument" do
m = MethodSpecs::Methods.new
m.method(:one_unnamed_keyrest).parameters.should == [[:keyrest]]
end
end
ruby_version_is '3.1' do
it "adds block arg with name & for anonymous block argument" do
object = Object.new
eval(<<~RUBY).should == [[:block, :&]]
def object.foo(&)
end
object.method(:foo).parameters
RUBY
end
end
it "returns the args and block for a splat and block argument" do
m = MethodSpecs::Methods.new
m.method(:one_splat_one_block).parameters.should == [[:rest, :args], [:block, :block]]
end
it "returns [] for a Method generated by attr_reader" do
m = MethodSpecs::Methods.new
m.method(:reader).parameters.should == []
end
it "return [[:req]] for a Method generated by attr_writer" do
m = MethodSpecs::Methods.new
m.method(:writer=).parameters.should == [[:req]]
end
it "returns all parameters defined with the name _ as _" do
m = MethodSpecs::Methods.instance_method(:underscore_parameters)
m.parameters.should == [
[:req, :_],
[:req, :_],
[:opt, :_],
[:rest, :_],
[:keyreq, :_],
[:key, :_],
[:keyrest, :_],
[:block, :_]
]
end
it "returns [[:rest]] for core methods with variable-length argument lists" do
# delete! takes rest args
"foo".method(:delete!).parameters.should == [[:rest]]
end
it "returns [[:rest]] or [[:opt]] for core methods with optional arguments" do
# pop takes 1 optional argument
[
[[:rest]],
[[:opt]]
].should include([].method(:pop).parameters)
end
it "returns [[:req]] for each parameter for core methods with fixed-length argument lists" do
"foo".method(:+).parameters.should == [[:req]]
end
end
|