aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/object/to_param_test.rb
blob: e446092abeec308e71981f8b91519bb772eeaeba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true
require "abstract_unit"
require "active_support/core_ext/object/to_param"

class ToParamTest < ActiveSupport::TestCase
  class CustomString < String
    def to_param
      "custom-#{ self }"
    end
  end

  def test_object
    foo = Object.new
    def foo.to_s; "foo" end
    assert_equal "foo", foo.to_param
  end

  def test_nil
    assert_nil nil.to_param
  end

  def test_boolean
    assert_equal true, true.to_param
    assert_equal false, false.to_param
  end

  def test_array
    # Empty Array
    assert_equal "", [].to_param

    array = [1, 2, 3, 4]
    assert_equal "1/2/3/4", array.to_param

    # Array of different objects
    array = [1, "3", { a: 1, b: 2 }, nil, true, false, CustomString.new("object")]
    assert_equal "1/3/a=1&b=2//true/false/custom-object", array.to_param
  end
end