@@ -86,6 +86,41 @@ def perform_now(user_id); end
8686 assert_equal ( expected , rbi_for ( :NotifyJob ) )
8787 end
8888
89+ def test_generates_correct_rbi_file_for_job_with_optional_parameter
90+ add_ruby_file ( "job.rb" , <<~RUBY )
91+ class NotifyJob < ActiveJob::Base
92+ include JobIteration::Iteration
93+
94+ def build_enumerator(user_id, foo_id = nil, cursor:)
95+ # ...
96+ end
97+ end
98+ RUBY
99+
100+ expected = template ( <<~RBI )
101+ # typed: strong
102+
103+ class NotifyJob
104+ sig { params(user_id: T.untyped, foo_id: T.untyped).void }
105+ def perform(user_id, foo_id = T.unsafe(nil)); end
106+
107+ class << self
108+ <% if rails_version(">= 7.0") %>
109+ sig { params(user_id: T.untyped, foo_id: T.untyped, block: T.nilable(T.proc.params(job: NotifyJob).void)).returns(T.any(NotifyJob, FalseClass)) }
110+ def perform_later(user_id, foo_id = T.unsafe(nil), &block); end
111+ <% else %>
112+ sig { params(user_id: T.untyped, foo_id: T.untyped).returns(T.any(NotifyJob, FalseClass)) }
113+ def perform_later(user_id, foo_id = T.unsafe(nil)); end
114+ <% end %>
115+
116+ sig { params(user_id: T.untyped, foo_id: T.untyped).returns(T.any(NilClass, Exception)) }
117+ def perform_now(user_id, foo_id = T.unsafe(nil)); end
118+ end
119+ end
120+ RBI
121+ assert_equal ( expected , rbi_for ( :NotifyJob ) )
122+ end
123+
89124 def test_generates_correct_rbi_file_for_job_with_build_enumerator_method_with_keyword_parameter
90125 add_ruby_file ( "job.rb" , <<~RUBY )
91126 class NotifyJob < ActiveJob::Base
@@ -121,6 +156,41 @@ def perform_now(user_id:, profile_id:); end
121156 assert_equal ( expected , rbi_for ( :NotifyJob ) )
122157 end
123158
159+ def test_generates_correct_rbi_file_for_job_with_build_enumerator_method_with_optional_keyword_parameter
160+ add_ruby_file ( "job.rb" , <<~RUBY )
161+ class NotifyJob < ActiveJob::Base
162+ include JobIteration::Iteration
163+
164+ def build_enumerator(user_id:, profile_id: nil, cursor:)
165+ # ...
166+ end
167+ end
168+ RUBY
169+
170+ expected = template ( <<~RBI )
171+ # typed: strong
172+
173+ class NotifyJob
174+ sig { params(user_id: T.untyped, profile_id: T.untyped).void }
175+ def perform(user_id:, profile_id: T.unsafe(nil)); end
176+
177+ class << self
178+ <% if rails_version(">= 7.0") %>
179+ sig { params(user_id: T.untyped, profile_id: T.untyped, block: T.nilable(T.proc.params(job: NotifyJob).void)).returns(T.any(NotifyJob, FalseClass)) }
180+ def perform_later(user_id:, profile_id: T.unsafe(nil), &block); end
181+ <% else %>
182+ sig { params(user_id: T.untyped, profile_id: T.untyped).returns(T.any(NotifyJob, FalseClass)) }
183+ def perform_later(user_id:, profile_id: T.unsafe(nil)); end
184+ <% end %>
185+
186+ sig { params(user_id: T.untyped, profile_id: T.untyped).returns(T.any(NilClass, Exception)) }
187+ def perform_now(user_id:, profile_id: T.unsafe(nil)); end
188+ end
189+ end
190+ RBI
191+ assert_equal ( expected , rbi_for ( :NotifyJob ) )
192+ end
193+
124194 def test_generates_correct_rbi_file_for_job_with_build_enumerator_method_signature
125195 add_ruby_file ( "job.rb" , <<~RUBY )
126196 class NotifyJob < ActiveJob::Base
@@ -158,6 +228,43 @@ def perform_now(user_id); end
158228 assert_equal ( expected , rbi_for ( :NotifyJob ) )
159229 end
160230
231+ def test_generates_correct_rbi_file_for_job_with_optional_parameter_and_signature
232+ add_ruby_file ( "job.rb" , <<~RUBY )
233+ class NotifyJob < ActiveJob::Base
234+ include JobIteration::Iteration
235+
236+ extend T::Sig
237+ sig { params(user_id: Integer, foo_id: T.nilable(String), cursor: T.untyped).returns(T::Array[T.untyped]) }
238+ def build_enumerator(user_id, foo_id = nil, cursor:)
239+ # ...
240+ end
241+ end
242+ RUBY
243+
244+ expected = template ( <<~RBI )
245+ # typed: strong
246+
247+ class NotifyJob
248+ sig { params(user_id: ::Integer, foo_id: T.nilable(::String)).void }
249+ def perform(user_id, foo_id = T.unsafe(nil)); end
250+
251+ class << self
252+ <% if rails_version(">= 7.0") %>
253+ sig { params(user_id: ::Integer, foo_id: T.nilable(::String), block: T.nilable(T.proc.params(job: NotifyJob).void)).returns(T.any(NotifyJob, FalseClass)) }
254+ def perform_later(user_id, foo_id = T.unsafe(nil), &block); end
255+ <% else %>
256+ sig { params(user_id: ::Integer, foo_id: T.nilable(::String)).returns(T.any(NotifyJob, FalseClass)) }
257+ def perform_later(user_id, foo_id = T.unsafe(nil)); end
258+ <% end %>
259+
260+ sig { params(user_id: ::Integer, foo_id: T.nilable(::String)).returns(T.any(NilClass, Exception)) }
261+ def perform_now(user_id, foo_id = T.unsafe(nil)); end
262+ end
263+ end
264+ RBI
265+ assert_equal ( expected , rbi_for ( :NotifyJob ) )
266+ end
267+
161268 def test_generates_correct_rbi_file_for_job_with_build_enumerator_method_signature_with_keyword_parameter
162269 add_ruby_file ( "job.rb" , <<~RUBY )
163270 class NotifyJob < ActiveJob::Base
@@ -195,6 +302,43 @@ def perform_now(user_id:, profile_id:); end
195302 assert_equal ( expected , rbi_for ( :NotifyJob ) )
196303 end
197304
305+ def test_generates_correct_rbi_file_for_job_with_build_enumerator_method_with_optional_keyword_parameter_and_signature
306+ add_ruby_file ( "job.rb" , <<~RUBY )
307+ class NotifyJob < ActiveJob::Base
308+ include JobIteration::Iteration
309+
310+ extend T::Sig
311+ sig { params(user_id: Integer, cursor: T.untyped, profile_id: T.nilable(Integer)).returns(T::Array[T.untyped]) }
312+ def build_enumerator(user_id:, cursor:, profile_id: nil)
313+ # ...
314+ end
315+ end
316+ RUBY
317+
318+ expected = template ( <<~RBI )
319+ # typed: strong
320+
321+ class NotifyJob
322+ sig { params(user_id: ::Integer, profile_id: T.nilable(::Integer)).void }
323+ def perform(user_id:, profile_id: T.unsafe(nil)); end
324+
325+ class << self
326+ <% if rails_version(">= 7.0") %>
327+ sig { params(user_id: ::Integer, profile_id: T.nilable(::Integer), block: T.nilable(T.proc.params(job: NotifyJob).void)).returns(T.any(NotifyJob, FalseClass)) }
328+ def perform_later(user_id:, profile_id: T.unsafe(nil), &block); end
329+ <% else %>
330+ sig { params(user_id: ::Integer, profile_id: T.nilable(::Integer)).returns(T.any(NotifyJob, FalseClass)) }
331+ def perform_later(user_id:, profile_id: T.unsafe(nil)); end
332+ <% end %>
333+
334+ sig { params(user_id: ::Integer, profile_id: T.nilable(::Integer)).returns(T.any(NilClass, Exception)) }
335+ def perform_now(user_id:, profile_id: T.unsafe(nil)); end
336+ end
337+ end
338+ RBI
339+ assert_equal ( expected , rbi_for ( :NotifyJob ) )
340+ end
341+
198342 def test_generates_correct_rbi_file_for_job_with_build_enumerator_method_with_multiple_parameters
199343 add_ruby_file ( "job.rb" , <<~RUBY )
200344 class NotifyJob < ActiveJob::Base
0 commit comments