Repository: rails/rails
File: actionview/test/template/template_test.rb (Ruby)

Code before:

  def test_locals_cannot_be_specified_with_positional_arguments
    @template = new_template("<%# locals: (argument = 'content') -%>\n<%= argument %>")
    assert_raises ActionView::Template::Error, match: "`argument` set as non-keyword argument for hello template. Locals can only be set as keyword arguments." do
      render
    end
  end

  def test_locals_cannot_be_specified_with_block_arguments
    @template = new_template("<%# locals: (&block) -%>\n<%= tag.div(&block) %>")
    assert_raises ActionView::Template::Error, match: "`block` set as non-keyword argument for hello template. Locals can only be set as keyword arguments." do
      render { "content" }
    end
  end

  def test_locals_can_be_specified
    @template = new_template("<%# locals: (message:) -%>\n<%= message %>")
    assert_equal "Hello", render(message: "Hello")
  end

  def test_default_locals_can_be_specified
    @template = new_template("<%# locals: (message: 'Hello') -%>\n<%= message %>")
    assert_equal "Hello", render
  end

  def test_multiline_locals_can_be_specified
    @template = new_template("<%# locals: (arg_1:,\n             arg_2: nil,\n             arg_3: []) -%>\n<%= arg_1 %><%= arg_2 %><%= arg_3 %>")
    assert_equal "First[]", render(arg_1: "First")
  end

  def test_required_locals_must_be_specified
    error = assert_raises(ActionView::Template::Error) do
      @template = new_template("<%# locals: (message:) -%>")
      render
    end

    assert_match(/missing local: :message for hello template/, error.message)
    assert_instance_of ActionView::StrictLocalsError, error.cause
  end

  def test_extra_locals_raises_strict_locals_error
    error = assert_raises(ActionView::Template::Error) do
      @template = new_template("<%# locals: (message:) -%>")
      render(message: "Hi", foo: "bar")
    end

    assert_match(/unknown local: :foo for hello template/, error.message)
    assert_instance_of ActionView::StrictLocalsError, error.cause
  end

  def test_argument_error_in_the_template_is_not_hijacked_by_strict_locals_checking

Review comment (suggestion):
```suggestion
  def test_locals_can_spread_to_multiple_lines
    template = <<~ERB.chomp
        <%# locals: (arg_1:,
                     arg_2: nil,
                     arg_3: []) -%>
        <%= arg_1 %><%= arg_2 %><%= arg_3 %>
    ERB
    @template = new_template(template)
```

Looks great! 👍🏼
This is just a non-blocking suggestion to slightly improve readability and formatting.

Code after:

  def test_locals_cannot_be_specified_with_positional_arguments
    @template = new_template("<%# locals: (argument = 'content') -%>\n<%= argument %>")
    assert_raises ActionView::Template::Error, match: "`argument` set as non-keyword argument for hello template. Locals can only be set as keyword arguments." do
      render
    end
  end

  def test_locals_cannot_be_specified_with_block_arguments
    @template = new_template("<%# locals: (&block) -%>\n<%= tag.div(&block) %>")
    assert_raises ActionView::Template::Error, match: "`block` set as non-keyword argument for hello template. Locals can only be set as keyword arguments." do
      render { "content" }
    end
  end

  def test_locals_can_be_specified
    @template = new_template("<%# locals: (message:) -%>\n<%= message %>")
    assert_equal "Hello", render(message: "Hello")
  end

  def test_default_locals_can_be_specified
    @template = new_template("<%# locals: (message: 'Hello') -%>\n<%= message %>")
    assert_equal "Hello", render
  end

  def test_locals_with_parentheses
    @template = new_template("<%# locals: (message: '(Hello)') -%>\n<%= message %>")
    assert_equal "(Hello)", render

    @template = new_template("<%# locals: (message: '(Hello') -%>\n<%= message %>")
    assert_equal "(Hello", render

    @template = new_template("<%# locals: (message: 'Hello)') -%>\n<%= message %>")
    assert_equal "Hello)", render
  end

  def test_locals_can_spread_to_multiple_lines
    template = <<~ERB.chomp
        <%# locals: (arg_1:,
                     arg_2: nil,
                     arg_3: []) -%>
        <%= arg_1 %><%= arg_2 %><%= arg_3 %>
    ERB
    @template = new_template(template)
    assert_equal "First[]", render(arg_1: "First")
  end

  def test_required_locals_must_be_specified
    error = assert_raises(ActionView::Template::Error) do
      @template = new_template("<%# locals: (message:) -%>")
      render