<commit_before>require 'test_helper'

describe SweetNotifications::ControllerRuntime do
  class TestLogSubscriber < SweetNotifications::LogSubscriber
  end

  ControllerRuntime = SweetNotifications.controller_runtime('Test',
                                                            TestLogSubscriber)
  ActionController::Base.send :include, ControllerRuntime

  class LogSubscribersController < ActionController::Base
    def create
      TestLogSubscriber.runtime += 100
      render_text '100'
    end

    def show
      render_text '0'
    end

    def destroy
      TestLogSubscriber.runtime += 50
      render_text 'OK'
      TestLogSubscriber.runtime += 5
    end

    private

    def render_text(text)
      if Gem.loaded_specs['rails'].version < Gem::Version.new('4.2')
        render text: 'ok'
      else
        render plain: 'ok'
      end
    end
  end

  include ActiveSupport::LogSubscriber::TestHelper
  tests LogSubscribersController

  before do
    @old_logger = ActionController::Base.logger
    ActionController::LogSubscriber.attach_to :action_controller
  end

  after do
    ActiveSupport::LogSubscriber.log_subscribers.clear
    ActionController::Base.logger = @old_logger
  end

  # rubocop:disable AccessorMethodName
  def set_logger(logger)
    ActionController::Base.logger = logger
  end
  # rubocop:enable AccessorMethodName

  describe '.log_process_action' do
    it 'emits runtime to log messages' do
      out = LogSubscribersController.log_process_action(test_runtime: 1234)
      assert_equal('Test: 1234.0ms', out[0])
    end
  end

  describe 'runtime logging' do
    it 'does not append runtime when it is 0' do
      get :show, id: 1
      wait
      assert_no_match(/Test:/, @logger.logged(:info)[2])
    end

    it 'appends non-zero runtime' do
      post :create, test: 1
      wait
      expected_message = /\(Views: [\d.]+ms \| Test: 100.0ms\)/
      assert_match(expected_message, @logger.logged(:info)[2])
    end

    it 'resets runtime before request' do
      TestLogSubscriber.runtime += 1000
      post :create, test: 1
      wait
      expected_message = /\(Views: [\d.]+ms \| Test: 100.0ms\)/
      assert_match(expected_message, @logger.logged(:info)[2])
    end

    it 'includes runtime after render' do
      post :destroy, id: 1
      wait
      expected_message = /\(Views: [\d.]+ms \| Test: 55.0ms\)/
      assert_match(expected_message, @logger.logged(:info)[2])
    end
  end
end
<commit_msg>Use Rails 5.0 style keyword arguments in specs<commit_after>require 'test_helper'

describe SweetNotifications::ControllerRuntime do
  class TestLogSubscriber < SweetNotifications::LogSubscriber
  end

  ControllerRuntime = SweetNotifications.controller_runtime('Test',
                                                            TestLogSubscriber)
  ActionController::Base.send :include, ControllerRuntime

  class LogSubscribersController < ActionController::Base
    def create
      TestLogSubscriber.runtime += 100
      render_text '100'
    end

    def show
      render_text '0'
    end

    def destroy
      TestLogSubscriber.runtime += 50
      render_text 'OK'
      TestLogSubscriber.runtime += 5
    end

    private

    def render_text(text)
      if Gem.loaded_specs['rails'].version < Gem::Version.new('4.2')
        render text: 'ok'
      else
        render plain: 'ok'
      end
    end
  end

  include ActiveSupport::LogSubscriber::TestHelper
  tests LogSubscribersController

  before do
    @old_logger = ActionController::Base.logger
    ActionController::LogSubscriber.attach_to :action_controller
  end

  after do
    ActiveSupport::LogSubscriber.log_subscribers.clear
    ActionController::Base.logger = @old_logger
  end

  # rubocop:disable AccessorMethodName
  def set_logger(logger)
    ActionController::Base.logger = logger
  end
  # rubocop:enable AccessorMethodName

  describe '.log_process_action' do
    it 'emits runtime to log messages' do
      out = LogSubscribersController.log_process_action(test_runtime: 1234)
      assert_equal('Test: 1234.0ms', out[0])
    end
  end

  describe 'runtime logging' do
    it 'does not append runtime when it is 0' do
      get :show, params: { id: 1 }
      wait
      assert_no_match(/Test:/, @logger.logged(:info)[2])
    end

    it 'appends non-zero runtime' do
      post :create, params: { test: 1 }
      wait
      expected_message = /\(Views: [\d.]+ms \| Test: 100.0ms\)/
      assert_match(expected_message, @logger.logged(:info)[2])
    end

    it 'resets runtime before request' do
      TestLogSubscriber.runtime += 1000
      post :create, params: { test: 1 }
      wait
      expected_message = /\(Views: [\d.]+ms \| Test: 100.0ms\)/
      assert_match(expected_message, @logger.logged(:info)[2])
    end

    it 'includes runtime after render' do
      post :destroy, params: { id: 1 }
      wait
      expected_message = /\(Views: [\d.]+ms \| Test: 55.0ms\)/
      assert_match(expected_message, @logger.logged(:info)[2])
    end
  end
end

{'fraction_non_alphanumeric': 0.09610389610389611, 'fraction_numerical': 0.018781218781218783, 'mean_word_length': 2.841903300076746, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4869388', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1465, 'n_words': 373}