<commit_before>require 'isolation/abstract_unit'

module ApplicationTests
  class RoutingTest < Test::Unit::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails
      require 'rack/test'
      extend Rack::Test::Methods
    end

    def app
      @app ||= begin
        require "#{app_path}/config/environment"

        Rails.application
      end
    end

    test "rails/info/properties" do
      get "/rails/info/properties"
      assert_equal 200, last_response.status
    end

    test "simple controller" do
      controller :foo, <<-RUBY
        class FooController < ActionController::Base
          def index
            render :text => "foo"
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match ':controller(/:action)'
        end
      RUBY

      get '/foo'
      assert_equal 'foo', last_response.body
    end

    test "multiple controllers" do
      controller :foo, <<-RUBY
        class FooController < ActionController::Base
          def index
            render :text => "foo"
          end
        end
      RUBY

      controller :bar, <<-RUBY
        class BarController < ActionController::Base
          def index
            render :text => "bar"
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match ':controller(/:action)'
        end
      RUBY

      get '/foo'
      assert_equal 'foo', last_response.body

      get '/bar'
      assert_equal 'bar', last_response.body
    end

    test "nested controller" do
      controller 'foo', <<-RUBY
        class FooController < ActionController::Base
          def index
            render :text => "foo"
          end
        end
      RUBY

      controller 'admin/foo', <<-RUBY
        module Admin
          class FooController < ActionController::Base
            def index
              render :text => "admin::foo"
            end
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match ':controller(/:action)'
        end
      RUBY

      get '/foo'
      assert_equal 'foo', last_response.body

      get '/admin/foo'
      assert_equal 'admin::foo', last_response.body
    end

    test "merges with plugin routes" do
      controller 'foo', <<-RUBY
        class FooController < ActionController::Base
          def index
            render :text => "foo"
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match 'foo', :to => 'foo#index'
        end
      RUBY

      plugin 'bar', 'require File.dirname(__FILE__) + "/app/controllers/bar"' do |plugin|
        plugin.write 'app/controllers/bar.rb', <<-RUBY
          class BarController < ActionController::Base
            def index
              render :text => "bar"
            end
          end
        RUBY

        plugin.write 'config/routes.rb', <<-RUBY
          AppTemplate::Application.routes.draw do |map|
            match 'bar', :to => 'bar#index'
          end
        RUBY
      end

      get '/foo'
      assert_equal 'foo', last_response.body

      get '/bar'
      assert_equal 'bar', last_response.body
    end

    test "reloads routes when configuration is changed" do
      controller :foo, <<-RUBY
        class FooController < ActionController::Base
          def bar
            render :text => "bar"
          end

          def baz
            render :text => "baz"
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match 'foo', :to => 'foo#bar'
        end
      RUBY

      get '/foo'
      assert_equal 'bar', last_response.body

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match 'foo', :to => 'foo#baz'
        end
      RUBY

      sleep 0.1

      get '/foo'
      assert_equal 'baz', last_response.body
    end
  end
end<commit_msg>Add test case for load initializers before routing behavior.<commit_after>require 'isolation/abstract_unit'

module ApplicationTests
  class RoutingTest < Test::Unit::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails
      require 'rack/test'
      extend Rack::Test::Methods
    end

    def app
      @app ||= begin
        require "#{app_path}/config/environment"

        Rails.application
      end
    end

    test "rails/info/properties" do
      get "/rails/info/properties"
      assert_equal 200, last_response.status
    end

    test "simple controller" do
      controller :foo, <<-RUBY
        class FooController < ActionController::Base
          def index
            render :text => "foo"
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match ':controller(/:action)'
        end
      RUBY

      get '/foo'
      assert_equal 'foo', last_response.body
    end

    test "multiple controllers" do
      controller :foo, <<-RUBY
        class FooController < ActionController::Base
          def index
            render :text => "foo"
          end
        end
      RUBY

      controller :bar, <<-RUBY
        class BarController < ActionController::Base
          def index
            render :text => "bar"
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match ':controller(/:action)'
        end
      RUBY

      get '/foo'
      assert_equal 'foo', last_response.body

      get '/bar'
      assert_equal 'bar', last_response.body
    end

    test "nested controller" do
      controller 'foo', <<-RUBY
        class FooController < ActionController::Base
          def index
            render :text => "foo"
          end
        end
      RUBY

      controller 'admin/foo', <<-RUBY
        module Admin
          class FooController < ActionController::Base
            def index
              render :text => "admin::foo"
            end
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match ':controller(/:action)'
        end
      RUBY

      get '/foo'
      assert_equal 'foo', last_response.body

      get '/admin/foo'
      assert_equal 'admin::foo', last_response.body
    end

    test "merges with plugin routes" do
      controller 'foo', <<-RUBY
        class FooController < ActionController::Base
          def index
            render :text => "foo"
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match 'foo', :to => 'foo#index'
        end
      RUBY

      plugin 'bar', 'require File.dirname(__FILE__) + "/app/controllers/bar"' do |plugin|
        plugin.write 'app/controllers/bar.rb', <<-RUBY
          class BarController < ActionController::Base
            def index
              render :text => "bar"
            end
          end
        RUBY

        plugin.write 'config/routes.rb', <<-RUBY
          AppTemplate::Application.routes.draw do |map|
            match 'bar', :to => 'bar#index'
          end
        RUBY
      end

      get '/foo'
      assert_equal 'foo', last_response.body

      get '/bar'
      assert_equal 'bar', last_response.body
    end

    test "reloads routes when configuration is changed" do
      controller :foo, <<-RUBY
        class FooController < ActionController::Base
          def bar
            render :text => "bar"
          end

          def baz
            render :text => "baz"
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match 'foo', :to => 'foo#bar'
        end
      RUBY

      get '/foo'
      assert_equal 'bar', last_response.body

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match 'foo', :to => 'foo#baz'
        end
      RUBY

      sleep 0.1

      get '/foo'
      assert_equal 'baz', last_response.body
    end

    test 'resource routing with irrigular inflection' do
      app_file 'config/initializers/inflection.rb', <<-RUBY
        ActiveSupport::Inflector.inflections do |inflect|
          inflect.irregular 'yazi', 'yazilar'
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          resources :yazilar
        end
      RUBY

      controller 'yazilar', <<-RUBY
        class YazilarController < ActionController::Base
          def index
            render :text => 'yazilar#index'
          end
        end
      RUBY

      get '/yazilars'
      assert_equal 404, last_response.status

      get '/yazilar'
      assert_equal 200, last_response.status
    end
  end
end
{'fraction_non_alphanumeric': 0.09705882352941177, 'fraction_numerical': 0.0017429193899782135, 'mean_word_length': 1.73081499107674, 'pattern_counts': {'":': 0, '<': 88, '<?xml version=': 0, '>': 30, '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': '1874434', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2555, 'n_words': 706}