Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activesupport / test / core_ext / string_ext_test.rb
blob9c199e1e799db0bde747ad969a4ece0a409510cc
1 require 'date'
2 require File.dirname(__FILE__) + '/../abstract_unit'
3 require 'inflector_test_cases'
5 class StringInflectionsTest < Test::Unit::TestCase
6   include InflectorTestCases
8   def test_pluralize
9     SingularToPlural.each do |singular, plural|
10       assert_equal(plural, singular.pluralize)
11     end
13     assert_equal("plurals", "plurals".pluralize)
14   end
16   def test_singularize
17     SingularToPlural.each do |singular, plural|
18       assert_equal(singular, plural.singularize)
19     end
20   end
22   def test_titleize
23     MixtureToTitleCase.each do |before, titleized|
24       assert_equal(titleized, before.titleize)
25     end
26   end
28   def test_camelize
29     CamelToUnderscore.each do |camel, underscore|
30       assert_equal(camel, underscore.camelize)
31     end
32   end
34   def test_underscore
35     CamelToUnderscore.each do |camel, underscore|
36       assert_equal(underscore, camel.underscore)
37     end
39     assert_equal "html_tidy", "HTMLTidy".underscore
40     assert_equal "html_tidy_generator", "HTMLTidyGenerator".underscore
41   end
43   def test_underscore_to_lower_camel
44     UnderscoreToLowerCamel.each do |underscored, lower_camel|
45       assert_equal(lower_camel, underscored.camelize(:lower))
46     end
47   end
49   def test_demodulize
50     assert_equal "Account", "MyApplication::Billing::Account".demodulize
51   end
53   def test_foreign_key
54     ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
55       assert_equal(foreign_key, klass.foreign_key)
56     end
58     ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
59       assert_equal(foreign_key, klass.foreign_key(false))
60     end
61   end
63   def test_tableize
64     ClassNameToTableName.each do |class_name, table_name|
65       assert_equal(table_name, class_name.tableize)
66     end
67   end
69   def test_classify
70     ClassNameToTableName.each do |class_name, table_name|
71       assert_equal(class_name, table_name.classify)
72     end
73   end
75   def test_humanize
76     UnderscoreToHuman.each do |underscore, human|
77       assert_equal(human, underscore.humanize)
78     end
79   end
81   def test_string_to_time
82     assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
83     assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local)
84     assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
85     assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
86     assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
87     assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
88   end
90   def test_access
91     s = "hello"
92     assert_equal "h", s.at(0)
94     assert_equal "llo", s.from(2)
95     assert_equal "hel", s.to(2)
97     assert_equal "h", s.first
98     assert_equal "he", s.first(2)
100     assert_equal "o", s.last
101     assert_equal "llo", s.last(3)
102     assert_equal "hello", s.last(10)
104     assert_equal 'x', 'x'.first
105     assert_equal 'x', 'x'.first(4)
107     assert_equal 'x', 'x'.last
108     assert_equal 'x', 'x'.last(4)
109   end
111   def test_access_returns_a_real_string
112     hash = {}
113     hash["h"] = true
114     hash["hello123".at(0)] = true
115     assert_equal %w(h), hash.keys
117     hash = {}
118     hash["llo"] = true
119     hash["hello".from(2)] = true
120     assert_equal %w(llo), hash.keys
122     hash = {}
123     hash["hel"] = true
124     hash["hello".to(2)] = true
125     assert_equal %w(hel), hash.keys
127     hash = {}
128     hash["hello"] = true
129     hash["123hello".last(5)] = true
130     assert_equal %w(hello), hash.keys
132     hash = {}
133     hash["hello"] = true
134     hash["hello123".first(5)] = true
135     assert_equal %w(hello), hash.keys
136   end
138   def test_starts_ends_with
139     s = "hello"
140     assert s.starts_with?('h')
141     assert s.starts_with?('hel')
142     assert !s.starts_with?('el')
144     assert s.ends_with?('o')
145     assert s.ends_with?('lo')
146     assert !s.ends_with?('el')
147   end
149   # FIXME: Ruby 1.9
150   def test_each_char_with_utf8_string_when_kcode_is_utf8
151     old_kcode, $KCODE = $KCODE, 'UTF8'
152     '€2.99'.each_char do |char|
153       assert_not_equal 1, char.length
154       break
155     end
156   ensure
157     $KCODE = old_kcode
158   end