From 6bc7e27c191b76015b21c22c31da38593bd6f367 Mon Sep 17 00:00:00 2001 From: Akshay Vishnoi Date: Tue, 29 Jul 2014 20:05:36 +0530 Subject: [PATCH] Add missing test case for Array#to_sentence, collect all test cases for Object#to_param at one place and avoid repitition --- .../test/core_ext/array/conversions_test.rb | 30 +++++-------------- .../test/core_ext/object/to_param_test.rb | 10 +++++-- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/activesupport/test/core_ext/array/conversions_test.rb b/activesupport/test/core_ext/array/conversions_test.rb index 51ad9897dac48..577b889410e86 100644 --- a/activesupport/test/core_ext/array/conversions_test.rb +++ b/activesupport/test/core_ext/array/conversions_test.rb @@ -52,6 +52,14 @@ def test_does_not_modify_given_hash def test_with_blank_elements assert_equal ", one, , two, and three", [nil, 'one', '', 'two', 'three'].to_sentence end + + def test_with_invalid_options + exception = assert_raise ArgumentError do + ['one', 'two'].to_sentence(passing: 'invalid option') + end + + assert_equal exception.message, "Unknown key: :passing. Valid keys are: :words_connector, :two_words_connector, :last_word_connector, :locale" + end end class ToSTest < ActiveSupport::TestCase @@ -187,25 +195,3 @@ def test_to_xml_dups_options assert_equal({ skip_instruct: true }, options) end end - -class ToParamTest < ActiveSupport::TestCase - class ToParam < String - def to_param - "#{self}1" - end - end - - def test_string_array - assert_equal '', %w().to_param - assert_equal 'hello/world', %w(hello world).to_param - assert_equal 'hello/10', %w(hello 10).to_param - end - - def test_number_array - assert_equal '10/20', [10, 20].to_param - end - - def test_to_param_array - assert_equal 'custom1/param1', [ToParam.new('custom'), ToParam.new('param')].to_param - end -end diff --git a/activesupport/test/core_ext/object/to_param_test.rb b/activesupport/test/core_ext/object/to_param_test.rb index 25c3bf22c330d..30a7557dc2d94 100644 --- a/activesupport/test/core_ext/object/to_param_test.rb +++ b/activesupport/test/core_ext/object/to_param_test.rb @@ -2,6 +2,12 @@ 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 @@ -25,7 +31,7 @@ def test_array assert_equal "1/2/3/4", array.to_param # Array of different objects - array = [1, '3', { a: 1, b: 2 }, nil, true, false] - assert_equal "1/3/a=1&b=2//true/false", array.to_param + 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