2 require File.dirname(__FILE__) + '/../abstract_unit'
3 require 'inflector_test_cases'
5 class StringInflectionsTest < Test::Unit::TestCase
6 include InflectorTestCases
9 SingularToPlural.each do |singular, plural|
10 assert_equal(plural, singular.pluralize)
13 assert_equal("plurals", "plurals".pluralize)
17 SingularToPlural.each do |singular, plural|
18 assert_equal(singular, plural.singularize)
23 MixtureToTitleCase.each do |before, titleized|
24 assert_equal(titleized, before.titleize)
29 CamelToUnderscore.each do |camel, underscore|
30 assert_equal(camel, underscore.camelize)
35 CamelToUnderscore.each do |camel, underscore|
36 assert_equal(underscore, camel.underscore)
39 assert_equal "html_tidy", "HTMLTidy".underscore
40 assert_equal "html_tidy_generator", "HTMLTidyGenerator".underscore
43 def test_underscore_to_lower_camel
44 UnderscoreToLowerCamel.each do |underscored, lower_camel|
45 assert_equal(lower_camel, underscored.camelize(:lower))
50 assert_equal "Account", "MyApplication::Billing::Account".demodulize
54 ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
55 assert_equal(foreign_key, klass.foreign_key)
58 ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
59 assert_equal(foreign_key, klass.foreign_key(false))
64 ClassNameToTableName.each do |class_name, table_name|
65 assert_equal(table_name, class_name.tableize)
70 ClassNameToTableName.each do |class_name, table_name|
71 assert_equal(class_name, table_name.classify)
76 UnderscoreToHuman.each do |underscore, human|
77 assert_equal(human, underscore.humanize)
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
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)
111 def test_access_returns_a_real_string
114 hash["hello123".at(0)] = true
115 assert_equal %w(h), hash.keys
119 hash["hello".from(2)] = true
120 assert_equal %w(llo), hash.keys
124 hash["hello".to(2)] = true
125 assert_equal %w(hel), hash.keys
129 hash["123hello".last(5)] = true
130 assert_equal %w(hello), hash.keys
134 hash["hello123".first(5)] = true
135 assert_equal %w(hello), hash.keys
138 def test_starts_ends_with
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')
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