<commit_before>require_relative "../test_helper"

module SmartAnswer
  class ErbRendererTest < ActiveSupport::TestCase
    test "can render a template" do
      erb_template = render_content_for(:key, "content")
      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(
          template_directory: erb_template_directory,
          template_name: "template-name",
        )
        assert_match("content", renderer.content_for(:key))
      end
    end

    test "can render a template with a .govspeak name" do
      erb_template = render_content_for(:key, "content")
      with_erb_template_file("template-name.govspeak", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(
          template_directory: erb_template_directory,
          template_name: "template-name",
        )
        assert_match("content", renderer.content_for(:key))
      end
    end

    test "#relative_erb_template_path returns a template path relative to Rails.root" do
      with_erb_template_file("template-name", "") do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name")
        Rails.stubs(:root).returns(erb_template_directory)
        assert_equal "template-name.erb", renderer.relative_erb_template_path
      end
    end

    test "#content_for raises an exception when the erb template doesn't exist" do
      renderer = ErbRenderer.new(template_directory: Pathname.new("/path/to/non-existent"), template_name: "template-name")

      assert_raise(ActionView::MissingTemplate) do
        renderer.content_for(:key)
      end
    end

    test "#content_for trims newlines by default" do
      erb_template = render_content_for(
        :key,
        '<% if true %>
Hello world
<% end %>',
      )

      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name")

        assert_match(/<p>Hello world<\/p>/, renderer.content_for(:key))
      end
    end

    test "#content_for makes local variables available to the ERB template" do
      erb_template = render_content_for(:key, "<%= state_variable %>")

      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name", locals: { state_variable: "state-variable" })

        assert_match "state-variable", renderer.content_for(:key)
      end
    end

    test "#content_for raises an exception if the ERB template references a non-existent state variable" do
      erb_template = render_content_for(:key, "<%= non_existent_state_variable %>")

      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name", locals: {})

        e = assert_raises(ActionView::Template::Error) do
          renderer.content_for(:key)
        end
        assert_match "undefined local variable or method `non_existent_state_variable'", e.message
      end
    end

    test "#content_for makes the ActionView::Helpers::NumberHelper methods available to the ERB template" do
      erb_template = render_content_for(:key, "<%= number_with_delimiter(123456789) %>")

      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name")

        assert_match "123,456,789", renderer.content_for(:key)
      end
    end

    test "#content_for passes output of ERB template through Govspeak by default" do
      erb_template = render_content_for(:key, "^information^")

      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name")

        nodes = Capybara.string(renderer.content_for(:key))
        assert nodes.has_css?(".application-notice", text: "information"), "Does not have information callout"
      end
    end

    test "#content_for returns an HTML-safe string when passed through Govspeak" do
      erb_template = render_content_for(:body, "html-unsafe-string")

      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name")

        assert renderer.content_for(:body).html_safe?
      end
    end

    test "#content_for returns an HTML-safe string" do
      erb_template = render_content_for(:key, "html-unsafe-string")

      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name")

        assert renderer.content_for(:key).html_safe?
      end
    end
<commit_msg>Remove unnecessary unit tests<commit_after>require_relative "../test_helper"

module SmartAnswer
  class ErbRendererTest < ActiveSupport::TestCase
    test "can render a template" do
      erb_template = render_content_for(:key, "content")
      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(
          template_directory: erb_template_directory,
          template_name: "template-name",
        )
        assert_match("content", renderer.content_for(:key))
      end
    end

    test "can render a template with a .govspeak name" do
      erb_template = render_content_for(:key, "content")
      with_erb_template_file("template-name.govspeak", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(
          template_directory: erb_template_directory,
          template_name: "template-name",
        )
        assert_match("content", renderer.content_for(:key))
      end
    end

    test "#relative_erb_template_path returns a template path relative to Rails.root" do
      with_erb_template_file("template-name", "") do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name")
        Rails.stubs(:root).returns(erb_template_directory)
        assert_equal "template-name.erb", renderer.relative_erb_template_path
      end
    end

    test "#content_for raises an exception when the erb template doesn't exist" do
      renderer = ErbRenderer.new(template_directory: Pathname.new("/path/to/non-existent"), template_name: "template-name")

      assert_raise(ActionView::MissingTemplate) do
        renderer.content_for(:key)
      end
    end

    test "#content_for makes local variables available to the ERB template" do
      erb_template = render_content_for(:key, "<%= state_variable %>")

      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name", locals: { state_variable: "state-variable" })

        assert_match "state-variable", renderer.content_for(:key)
      end
    end

    test "#content_for raises an exception if the ERB template references a non-existent state variable" do
      erb_template = render_content_for(:key, "<%= non_existent_state_variable %>")

      with_erb_template_file("template-name", erb_template) do |erb_template_directory|
        renderer = ErbRenderer.new(template_directory: erb_template_directory, template_name: "template-name", locals: {})

        e = assert_raises(ActionView::Template::Error) do
          renderer.content_for(:key)
        end
        assert_match "undefined local variable or method `non_existent_state_variable'", e.message
      end
    end

{'fraction_non_alphanumeric': 0.09356945491082642, 'fraction_numerical': 0.002260738507912585, 'mean_word_length': 3.909371146732429, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5501409', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2302, 'n_words': 502}