Skip to content

Commit

Permalink
Merge pull request #1895 from Shopify/ko/fixtures
Browse files Browse the repository at this point in the history
ActiveRecordFixtures compiler should support passing no args
  • Loading branch information
KaanOzkan committed May 8, 2024
2 parents fa49b51 + 86cf1aa commit 3166ada
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
11 changes: 9 additions & 2 deletions lib/tapioca/dsl/compilers/active_record_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ module Compilers
# # test_case.rbi
# # typed: true
# class ActiveSupport::TestCase
# sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Post]) }
# sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Post) }
# sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol))
# .returns(T::Array[Post]) }
# def posts(fixture_name, *other_fixtures); end
# def posts(fixture_name = nil, *other_fixtures); end
# end
# ~~~
class ActiveRecordFixtures < Compiler
Expand Down Expand Up @@ -110,9 +111,15 @@ def method_names_from_eager_fixture_loader
def create_fixture_method(mod, name)
return_type = return_type_for_fixture(name)
mod << RBI::Method.new(name) do |node|
node.add_param("fixture_name")
node.add_opt_param("fixture_name", "nil")
node.add_rest_param("other_fixtures")

node.add_sig do |sig|
sig.add_param("fixture_name", "NilClass")
sig.add_param("other_fixtures", "NilClass")
sig.return_type = "T::Array[#{return_type}]"
end

node.add_sig do |sig|
sig.add_param("fixture_name", "T.any(String, Symbol)")
sig.add_param("other_fixtures", "NilClass")
Expand Down
3 changes: 2 additions & 1 deletion manual/compiler_activerecordfixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ The generated RBI by this compiler will produce the following
# test_case.rbi
# typed: true
class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Post]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Post) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol))
.returns(T::Array[Post]) }
def posts(fixture_name, *other_fixtures); end
def posts(fixture_name = nil, *other_fixtures); end
end
~~~
15 changes: 10 additions & 5 deletions spec/tapioca/dsl/compilers/active_record_fixtures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ class Post < ActiveRecord::Base
# typed: strong
class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Post]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Post) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[Post]) }
def posts(fixture_name, *other_fixtures); end
def posts(fixture_name = nil, *other_fixtures); end
end
RBI

Expand Down Expand Up @@ -111,13 +112,15 @@ class User < ActiveRecord::Base
# typed: strong
class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Blog::Post]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Blog::Post) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[Blog::Post]) }
def blog_posts(fixture_name, *other_fixtures); end
def blog_posts(fixture_name = nil, *other_fixtures); end
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[User]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(User) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[User]) }
def users(fixture_name, *other_fixtures); end
def users(fixture_name = nil, *other_fixtures); end
end
RBI

Expand All @@ -144,9 +147,10 @@ class Post < ActiveRecord::Base
# typed: strong
class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Post]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Post) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[Post]) }
def posts_with_other_names(fixture_name, *other_fixtures); end
def posts_with_other_names(fixture_name = nil, *other_fixtures); end
end
RBI

Expand All @@ -166,9 +170,10 @@ def posts_with_other_names(fixture_name, *other_fixtures); end
# typed: strong
class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[T.untyped]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(T.untyped) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[T.untyped]) }
def posts(fixture_name, *other_fixtures); end
def posts(fixture_name = nil, *other_fixtures); end
end
RBI

Expand Down

0 comments on commit 3166ada

Please sign in to comment.