comparison test/unit/initializers/patches_test.rb @ 1526:404aa68d4227

Merge from live branch
author Chris Cannam
date Thu, 11 Sep 2014 12:46:20 +0100
parents dffacf8a6908
children
comparison
equal deleted inserted replaced
1493:a5f2bdf3b486 1526:404aa68d4227
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
18 require File.expand_path('../../../test_helper', __FILE__) 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 class PatchesTest < ActiveSupport::TestCase 20 class PatchesTest < ActiveSupport::TestCase
21 include Redmine::I18n 21 include Redmine::I18n
22 22
23 context "ActiveRecord::Base.human_attribute_name" do 23 def setup
24 setup do 24 Setting.default_language = 'en'
25 Setting.default_language = 'en' 25 @symbols = { :a => 1, :b => 2 }
26 @keys = %w( blue green red pink orange )
27 @values = %w( 000099 009900 aa0000 cc0066 cc6633 )
28 @hash = Hash.new
29 @ordered_hash = ActiveSupport::OrderedHash.new
30
31 @keys.each_with_index do |key, index|
32 @hash[key] = @values[index]
33 @ordered_hash[key] = @values[index]
34 end
35 end
36
37 test "ActiveRecord::Base.human_attribute_name should transform name to field_name" do
38 assert_equal l('field_last_login_on'), ActiveRecord::Base.human_attribute_name('last_login_on')
39 end
40
41 test "ActiveRecord::Base.human_attribute_name should cut extra _id suffix for better validation" do
42 assert_equal l('field_last_login_on'), ActiveRecord::Base.human_attribute_name('last_login_on_id')
43 end
44
45 test "ActiveRecord::Base.human_attribute_name should default to humanized value if no translation has been found (useful for custom fields)" do
46 assert_equal 'Patch name', ActiveRecord::Base.human_attribute_name('Patch name')
47 end
48
49 # https://github.com/rails/rails/pull/14198/files
50 if RUBY_VERSION >= "1.9"
51 def test_indifferent_select
52 hash = ActiveSupport::HashWithIndifferentAccess.new(@symbols).select { |_ ,v| v == 1 }
53 assert_equal({ 'a' => 1 }, hash)
54 assert_instance_of ((Rails::VERSION::MAJOR < 4 && RUBY_VERSION < "2.1") ?
55 Hash : ActiveSupport::HashWithIndifferentAccess),
56 hash
26 end 57 end
27 58
28 should "transform name to field_name" do 59 def test_indifferent_select_bang
29 assert_equal l('field_last_login_on'), ActiveRecord::Base.human_attribute_name('last_login_on') 60 indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@symbols)
30 end 61 indifferent_strings.select! { |_, v| v == 1 }
31 62 assert_equal({ 'a' => 1 }, indifferent_strings)
32 should "cut extra _id suffix for better validation" do 63 assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
33 assert_equal l('field_last_login_on'), ActiveRecord::Base.human_attribute_name('last_login_on_id')
34 end
35
36 should "default to humanized value if no translation has been found (useful for custom fields)" do
37 assert_equal 'Patch name', ActiveRecord::Base.human_attribute_name('Patch name')
38 end 64 end
39 end 65 end
66
67 def test_indifferent_reject
68 hash = ActiveSupport::HashWithIndifferentAccess.new(@symbols).reject { |_, v| v != 1 }
69 assert_equal({ 'a' => 1 }, hash)
70 assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash
71 end
72
73 def test_indifferent_reject_bang
74 indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@symbols)
75 indifferent_strings.reject! { |_, v| v != 1 }
76 assert_equal({ 'a' => 1 }, indifferent_strings)
77 assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
78 end
79
80 if RUBY_VERSION >= "1.9"
81 def test_select
82 assert_equal @keys, @ordered_hash.select { true }.map(&:first)
83 new_ordered_hash = @ordered_hash.select { true }
84 assert_equal @keys, new_ordered_hash.map(&:first)
85 assert_instance_of ((Rails::VERSION::MAJOR < 4 && RUBY_VERSION < "2.1") ?
86 Hash : ActiveSupport::OrderedHash),
87 new_ordered_hash
88 end
89 end
90
91 def test_reject
92 copy = @ordered_hash.dup
93 new_ordered_hash = @ordered_hash.reject { |k, _| k == 'pink' }
94 assert_equal copy, @ordered_hash
95 assert !new_ordered_hash.keys.include?('pink')
96 assert @ordered_hash.keys.include?('pink')
97 assert_instance_of ActiveSupport::OrderedHash, new_ordered_hash
98 end
40 end 99 end