<commit_before>require "abstract_unit"

class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
  class Linkable
    attr_reader :id

    def initialize(id)
      @id = id
    end

    def linkable_type
      self.class.name.demodulize.underscore
    end
  end

  class Category < Linkable; end
  class Collection < Linkable; end
  class Product < Linkable; end

  Routes = ActionDispatch::Routing::RouteSet.new
  Routes.draw do
    default_url_options host: "www.example.com"

    root to: "pages#index"
    get "/basket", to: "basket#show", as: :basket

    resources :categories, :collections, :products

    namespace :admin do
      get "/dashboard", to: "dashboard#index"
    end

    direct(:website)  { "http://www.rubyonrails.org" }
    direct("string")  { "http://www.rubyonrails.org" }
    direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] }
    direct(:params)   { |params| params }
    direct(:symbol)   { :basket }
    direct(:hash)     { { controller: "basket", action: "show" } }
    direct(:array)    { [:admin, :dashboard] }
    direct(:options)  { |options| [:products, options] }
    direct(:defaults, size: 10) { |options| [:products, options] }
  end

  APP = build_app Routes

  def app
    APP
  end

  include Routes.url_helpers

  def setup
    @category = Category.new("1")
    @collection = Collection.new("2")
    @product = Product.new("3")
    @path_params = { "controller" => "pages", "action" => "index" }
    @unsafe_params = ActionController::Parameters.new(@path_params)
    @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action)
  end

  def test_direct_paths
    assert_equal "http://www.rubyonrails.org", website_path
    assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path

    assert_equal "http://www.rubyonrails.org", string_path
    assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path

    assert_equal "/categories/1", linkable_path(@category)
    assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category)
    assert_equal "/collections/2", linkable_path(@collection)
    assert_equal "/collections/2", Routes.url_helpers.linkable_path(@collection)
    assert_equal "/products/3", linkable_path(@product)
    assert_equal "/products/3", Routes.url_helpers.linkable_path(@product)

    assert_equal "/", params_path(@safe_params)
    assert_equal "/", Routes.url_helpers.params_path(@safe_params)
    assert_raises(ArgumentError) { params_path(@unsafe_params) }
    assert_raises(ArgumentError) { Routes.url_helpers.params_path(@unsafe_params) }

    assert_equal "/basket", symbol_path
    assert_equal "/basket", Routes.url_helpers.symbol_path
    assert_equal "/basket", hash_path
    assert_equal "/basket", Routes.url_helpers.hash_path
    assert_equal "/admin/dashboard", array_path
    assert_equal "/admin/dashboard", Routes.url_helpers.array_path

    assert_equal "/products?page=2", options_path(page: 2)
    assert_equal "/products?page=2", Routes.url_helpers.options_path(page: 2)
    assert_equal "/products?size=10", defaults_path
    assert_equal "/products?size=10", Routes.url_helpers.defaults_path
    assert_equal "/products?size=20", defaults_path(size: 20)
    assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20)
  end

  def test_direct_urls
    assert_equal "http://www.rubyonrails.org", website_url
    assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url

    assert_equal "http://www.rubyonrails.org", string_url
    assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url
<commit_msg>Add test for calling a url helper in Mapper#direct<commit_after>require "abstract_unit"

class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
  class Linkable
    attr_reader :id

    def initialize(id)
      @id = id
    end

    def linkable_type
      self.class.name.demodulize.underscore
    end
  end

  class Category < Linkable; end
  class Collection < Linkable; end
  class Product < Linkable; end

  Routes = ActionDispatch::Routing::RouteSet.new
  Routes.draw do
    default_url_options host: "www.example.com"

    root to: "pages#index"
    get "/basket", to: "basket#show", as: :basket

    resources :categories, :collections, :products

    namespace :admin do
      get "/dashboard", to: "dashboard#index"
    end

    direct(:website)  { "http://www.rubyonrails.org" }
    direct("string")  { "http://www.rubyonrails.org" }
    direct(:helper)   { basket_url }
    direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] }
    direct(:params)   { |params| params }
    direct(:symbol)   { :basket }
    direct(:hash)     { { controller: "basket", action: "show" } }
    direct(:array)    { [:admin, :dashboard] }
    direct(:options)  { |options| [:products, options] }
    direct(:defaults, size: 10) { |options| [:products, options] }
  end

  APP = build_app Routes

  def app
    APP
  end

  include Routes.url_helpers

  def setup
    @category = Category.new("1")
    @collection = Collection.new("2")
    @product = Product.new("3")
    @path_params = { "controller" => "pages", "action" => "index" }
    @unsafe_params = ActionController::Parameters.new(@path_params)
    @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action)
  end

  def test_direct_paths
    assert_equal "http://www.rubyonrails.org", website_path
    assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path

    assert_equal "http://www.rubyonrails.org", string_path
    assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path

    assert_equal "http://www.example.com/basket", helper_url
    assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url

    assert_equal "/categories/1", linkable_path(@category)
    assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category)
    assert_equal "/collections/2", linkable_path(@collection)
    assert_equal "/collections/2", Routes.url_helpers.linkable_path(@collection)
    assert_equal "/products/3", linkable_path(@product)
    assert_equal "/products/3", Routes.url_helpers.linkable_path(@product)

    assert_equal "/", params_path(@safe_params)
    assert_equal "/", Routes.url_helpers.params_path(@safe_params)
    assert_raises(ArgumentError) { params_path(@unsafe_params) }
    assert_raises(ArgumentError) { Routes.url_helpers.params_path(@unsafe_params) }

    assert_equal "/basket", symbol_path
    assert_equal "/basket", Routes.url_helpers.symbol_path
    assert_equal "/basket", hash_path
    assert_equal "/basket", Routes.url_helpers.hash_path
    assert_equal "/admin/dashboard", array_path
    assert_equal "/admin/dashboard", Routes.url_helpers.array_path

    assert_equal "/products?page=2", options_path(page: 2)
    assert_equal "/products?page=2", Routes.url_helpers.options_path(page: 2)
    assert_equal "/products?size=10", defaults_path
    assert_equal "/products?size=10", Routes.url_helpers.defaults_path
    assert_equal "/products?size=20", defaults_path(size: 20)
    assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20)
  end

  def test_direct_urls
    assert_equal "http://www.rubyonrails.org", website_url
    assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url

    assert_equal "http://www.rubyonrails.org", string_url
    assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url

    assert_equal "http://www.example.com/basket", helper_url
    assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url

{'fraction_non_alphanumeric': 0.13044612575006523, 'fraction_numerical': 0.007044090790503522, 'mean_word_length': 4.262182566918326, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 26, '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': '6269814', 'n_tokens_mistral': 2562, 'n_tokens_neox': 2489, 'n_words': 452}