Skip to content
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

Add ActionView::TestCase DSL generator #614

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions lib/tapioca/compilers/dsl/action_view_test_helpers.rb
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
38 changes: 38 additions & 0 deletions manual/generator_actionviewtesthelpers.md
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
~~~
1 change: 1 addition & 0 deletions manual/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ In the following section you will find all available DSL generators:
* [AASM](generator_aasm.md)
* [ActionControllerHelpers](generator_actioncontrollerhelpers.md)
* [ActionMailer](generator_actionmailer.md)
* [ActionViewTestHelpers](generator_actionviewtesthelpers.md)
* [ActiveJob](generator_activejob.md)
* [ActiveModelAttributes](generator_activemodelattributes.md)
* [ActiveModelSecurePassword](generator_activemodelsecurepassword.md)
Expand Down
101 changes: 101 additions & 0 deletions spec/tapioca/compilers/dsl/action_view_test_helpers_spec.rb
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
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