string_ext_test.rb 4.5 KB
Newer Older
1
# encoding: utf-8
2
require 'date'
3
require 'abstract_unit'
4
require 'inflector_test_cases'
5 6

class StringInflectionsTest < Test::Unit::TestCase
7 8
  include InflectorTestCases

9
  def test_pluralize
10
    SingularToPlural.each do |singular, plural|
11 12 13 14 15 16 17
      assert_equal(plural, singular.pluralize)
    end

    assert_equal("plurals", "plurals".pluralize)
  end

  def test_singularize
18
    SingularToPlural.each do |singular, plural|
19 20 21 22
      assert_equal(singular, plural.singularize)
    end
  end

23
  def test_titleize
24
    MixtureToTitleCase.each do |before, titleized|
25 26 27 28
      assert_equal(titleized, before.titleize)
    end
  end

29
  def test_camelize
30
    CamelToUnderscore.each do |camel, underscore|
31 32 33 34 35
      assert_equal(camel, underscore.camelize)
    end
  end

  def test_underscore
36
    CamelToUnderscore.each do |camel, underscore|
37 38
      assert_equal(underscore, camel.underscore)
    end
39

40 41 42 43
    assert_equal "html_tidy", "HTMLTidy".underscore
    assert_equal "html_tidy_generator", "HTMLTidyGenerator".underscore
  end

44
  def test_underscore_to_lower_camel
45
    UnderscoreToLowerCamel.each do |underscored, lower_camel|
46 47 48 49
      assert_equal(lower_camel, underscored.camelize(:lower))
    end
  end

50
  def test_demodulize
51
    assert_equal "Account", "MyApplication::Billing::Account".demodulize
52 53 54
  end

  def test_foreign_key
55
    ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
56 57 58
      assert_equal(foreign_key, klass.foreign_key)
    end

59
    ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
60 61 62 63 64
      assert_equal(foreign_key, klass.foreign_key(false))
    end
  end

  def test_tableize
65
    ClassNameToTableName.each do |class_name, table_name|
66 67 68 69 70
      assert_equal(table_name, class_name.tableize)
    end
  end

  def test_classify
71
    ClassNameToTableName.each do |class_name, table_name|
72 73 74
      assert_equal(class_name, table_name.classify)
    end
  end
75

76
  def test_humanize
77
    UnderscoreToHuman.each do |underscore, human|
78 79 80 81
      assert_equal(human, underscore.humanize)
    end
  end

J
Jeremy Kemper 已提交
82 83 84 85 86
  def test_ord
    assert_equal 97, 'a'.ord
    assert_equal 97, 'abc'.ord
  end

87 88 89
  def test_string_to_time
    assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
    assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local)
90 91
    assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
    assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
92 93 94
  end
  
  def test_string_to_datetime
95
    assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
96 97 98 99 100 101
    assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
    assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value
  end
  
  def test_string_to_date
    assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
102
  end
103

104 105 106
  def test_access
    s = "hello"
    assert_equal "h", s.at(0)
107

108 109
    assert_equal "llo", s.from(2)
    assert_equal "hel", s.to(2)
110

111 112 113 114 115
    assert_equal "h", s.first
    assert_equal "he", s.first(2)

    assert_equal "o", s.last
    assert_equal "llo", s.last(3)
116
    assert_equal "hello", s.last(10)
117

118 119 120 121 122
    assert_equal 'x', 'x'.first
    assert_equal 'x', 'x'.first(4)

    assert_equal 'x', 'x'.last
    assert_equal 'x', 'x'.last(4)
123
  end
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  def test_access_returns_a_real_string
    hash = {}
    hash["h"] = true
    hash["hello123".at(0)] = true
    assert_equal %w(h), hash.keys

    hash = {}
    hash["llo"] = true
    hash["hello".from(2)] = true
    assert_equal %w(llo), hash.keys

    hash = {}
    hash["hel"] = true
    hash["hello".to(2)] = true
    assert_equal %w(hel), hash.keys

    hash = {}
    hash["hello"] = true
    hash["123hello".last(5)] = true
    assert_equal %w(hello), hash.keys

    hash = {}
    hash["hello"] = true
    hash["hello123".first(5)] = true
    assert_equal %w(hello), hash.keys
  end

152
  def test_starts_ends_with_alias
153 154 155 156 157
    s = "hello"
    assert s.starts_with?('h')
    assert s.starts_with?('hel')
    assert !s.starts_with?('el')

158 159 160 161
    assert s.start_with?('h')
    assert s.start_with?('hel')
    assert !s.start_with?('el')

162 163 164
    assert s.ends_with?('o')
    assert s.ends_with?('lo')
    assert !s.ends_with?('el')
165 166 167 168

    assert s.end_with?('o')
    assert s.end_with?('lo')
    assert !s.end_with?('el')
169
  end
170

171 172 173 174 175 176 177 178 179
  if RUBY_VERSION < '1.9'
    def test_each_char_with_utf8_string_when_kcode_is_utf8
      old_kcode, $KCODE = $KCODE, 'UTF8'
      '€2.99'.each_char do |char|
        assert_not_equal 1, char.length
        break
      end
    ensure
      $KCODE = old_kcode
180 181
    end
  end
182
end