-
Notifications
You must be signed in to change notification settings - Fork 148
Add ActionView::TestCase DSL generator #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
begin | ||
require "action_view/test_case" | ||
rescue LoadError | ||
return | ||
end | ||
|
||
module Tapioca | ||
module Compilers | ||
module Dsl | ||
# `Tapioca::Compilers::Dsl::ActionViewTestHelpers` decorates RBI files for all | ||
# subclasses of `ActionView::TestCase` with the helpers that are included dynamically. | ||
# | ||
# For example, considering the `UsersHelper` module: | ||
# | ||
# ~~~rb | ||
# module UsersHelper | ||
# def current_user_name | ||
# # ... | ||
# end | ||
# end | ||
# ~~~ | ||
# | ||
# and its respective test: | ||
# | ||
# ~~~rb | ||
# class UsersHelperTest < ActionView::TestCase | ||
# test "current_user_name works" do | ||
# assert_equal("John", current_user_name) | ||
# end | ||
# end | ||
# ~~~ | ||
# | ||
# this generator will produce an RBI file `users_helper_test.rbi` with the following content: | ||
# | ||
# ~~~rbi | ||
# # users_helper_test.rbi | ||
# # typed: strong | ||
# class UsersHelperTest | ||
# include HelperMethods | ||
# | ||
# module HelperMethods | ||
# include ::UsersHelper | ||
# end | ||
# end | ||
# ~~~ | ||
class ActionViewTestHelpers < Base | ||
extend T::Sig | ||
|
||
sig do | ||
override | ||
.params(root: RBI::Tree, constant: T.class_of(ActionView::TestCase)) | ||
.void | ||
end | ||
def decorate(root, constant) | ||
# The helpers are dynamically included in the helpers module when `include_helper_modules!` is called. | ||
constant.send(:include_helper_modules!) | ||
helpers_module = constant._helpers | ||
|
||
helper_methods_name = "HelperMethods" | ||
|
||
root.create_path(constant) do |klass| | ||
klass.create_module(helper_methods_name) do |helper_methods| | ||
# Find all the included helper modules and generate an include | ||
# for each of those helper modules | ||
gather_includes(helpers_module).each do |helper_module_name| | ||
helper_methods.create_include(helper_module_name) | ||
end | ||
|
||
# Generate a method definition in the helper module for each | ||
# helper method defined via the `helper_method` call in the test case. | ||
helpers_module.instance_methods(false).each do |method_name| | ||
helper_methods.create_method( | ||
method_name.to_s, | ||
parameters: [ | ||
create_rest_param("args", type: "T.untyped"), | ||
create_kw_rest_param("kwargs", type: "T.untyped"), | ||
create_block_param("blk", type: "T.untyped"), | ||
], | ||
return_type: "T.untyped" | ||
) | ||
end | ||
end | ||
|
||
klass.create_include(helper_methods_name) | ||
end | ||
end | ||
|
||
sig { override.returns(T::Enumerable[Module]) } | ||
def gather_constants | ||
descendants_of(ActionView::TestCase) | ||
end | ||
|
||
private | ||
|
||
sig { params(mod: Module).returns(T::Array[String]) } | ||
def gather_includes(mod) | ||
mod.ancestors | ||
.reject { |ancestor| ancestor.is_a?(Class) || ancestor == mod || name_of(ancestor).nil? } | ||
.map { |ancestor| T.must(qualified_name_of(ancestor)) } | ||
.reverse | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
## ActionViewTestHelpers | ||
|
||
`Tapioca::Compilers::Dsl::ActionViewTestHelpers` decorates RBI files for all | ||
subclasses of `ActionView::TestCase` with the helpers that are included dynamically. | ||
|
||
For example, considering the `UsersHelper` module: | ||
|
||
~~~rb | ||
module UsersHelper | ||
def current_user_name | ||
# ... | ||
end | ||
end | ||
~~~ | ||
|
||
and its respective test: | ||
|
||
~~~rb | ||
class UsersHelperTest < ActionView::TestCase | ||
test "current_user_name works" do | ||
assert_equal("John", current_user_name) | ||
end | ||
end | ||
~~~ | ||
|
||
this generator will produce an RBI file `users_helper_test.rbi` with the following content: | ||
|
||
~~~rbi | ||
# users_helper_test.rbi | ||
# typed: strong | ||
class UsersHelperTest | ||
include HelperMethods | ||
|
||
module HelperMethods | ||
include ::UsersHelper | ||
end | ||
end | ||
~~~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
spec/tapioca/compilers/dsl/action_view_test_helpers_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
class Tapioca::Compilers::Dsl::ActionViewTestHelpersSpec < DslSpec | ||
describe("#initialize") do | ||
after(:each) do | ||
T.unsafe(self).assert_no_generated_errors | ||
end | ||
|
||
it("doesn't gather anything if there are no ActionView::TestCase tests") do | ||
assert_empty(gathered_constants) | ||
end | ||
|
||
it("gathers ActionView::TestCase tests") do | ||
add_ruby_file("users_helper_test.rb", <<~RUBY) | ||
class UsersHelperTest < ActionView::TestCase | ||
end | ||
RUBY | ||
|
||
assert_equal(["UsersHelperTest"], gathered_constants) | ||
end | ||
end | ||
|
||
describe("#decorate") do | ||
paracycle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
after(:each) do | ||
T.unsafe(self).assert_no_generated_errors | ||
end | ||
|
||
it("generates a module with all dynamic helper inclusions") do | ||
add_ruby_file("users_helper.rb", <<~RUBY) | ||
module UsersHelper | ||
def current_user_name | ||
"John" | ||
end | ||
end | ||
|
||
module SomeOtherHelperModule | ||
end | ||
RUBY | ||
|
||
add_ruby_file("users_helper_test.rb", <<~RUBY) | ||
class UsersHelperTest < ActionView::TestCase | ||
helper SomeOtherHelperModule | ||
helper_method :foo | ||
end | ||
RUBY | ||
|
||
expected = <<~RBI | ||
# typed: strong | ||
|
||
class UsersHelperTest | ||
include HelperMethods | ||
|
||
module HelperMethods | ||
include ::ActionView::TestCase::HelperMethods | ||
include ::SomeOtherHelperModule | ||
include ::UsersHelper | ||
|
||
sig { params(args: T.untyped, kwargs: T.untyped, blk: T.untyped).returns(T.untyped) } | ||
def foo(*args, **kwargs, &blk); end | ||
end | ||
end | ||
RBI | ||
|
||
assert_equal(expected, rbi_for(:UsersHelperTest)) | ||
end | ||
|
||
it("generates a module with a custom helper module under test definition") do | ||
add_ruby_file("users_helper.rb", <<~RUBY) | ||
module SomeOtherHelperModule | ||
def current_user_name | ||
"John" | ||
end | ||
end | ||
RUBY | ||
|
||
add_ruby_file("users_helper_test.rb", <<~RUBY) | ||
class UsersHelperTest < ActionView::TestCase | ||
tests SomeOtherHelperModule | ||
end | ||
RUBY | ||
|
||
expected = <<~RBI | ||
# typed: strong | ||
|
||
class UsersHelperTest | ||
include HelperMethods | ||
|
||
module HelperMethods | ||
include ::ActionView::TestCase::HelperMethods | ||
include ::SomeOtherHelperModule | ||
end | ||
end | ||
RBI | ||
|
||
assert_equal(expected, rbi_for(:UsersHelperTest)) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.