rails-inflections 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 379a14f88bacd25bf817a5c1b2084f07b645911e
4
+ data.tar.gz: 13c584bca996326368fbc3b05b82d98c8b83c239
5
+ SHA512:
6
+ metadata.gz: 8462a688c3e1403daa515fce47711ea76a157a1d1a4e38c98db43c14db6e768905dabe1d110be8da9be3726525ba32eedf7c0dace084532c4122cd4f4ac8aa06
7
+ data.tar.gz: 912084b9d8e5d8d1af8ce244407e1d94d90a77d533a0cf33761c7a1d40e83175b4fd00f46a8261a3f847223274ccc7fee0670a73329408e1a0da74ba6c1884e6
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-inflections.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Vipul A M
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # Rails::Inflections
2
+
3
+ Rails Inflections are frozen, since
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rails-inflections'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rails-inflections
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Adding new inflections
26
+
27
+ The aim is to keep up with valid inflection rules. When requesting new inflections, please provide information
28
+ about validity and usage.
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( https://github.com/vipulnsward/rails-inflections/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create a new Pull Request
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+
5
+ task :default => :test
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.warning = true
10
+ t.verbose = true
11
+ end
@@ -0,0 +1,72 @@
1
+ require "rails/inflections/version"
2
+
3
+ require 'active_support/inflector/inflections'
4
+
5
+ #--
6
+ # Defines the standard inflection rules. These are the starting point for
7
+ # new projects and are not considered complete. The current set of inflection
8
+ # rules is frozen. This means, we do not change them to become more complete.
9
+ # This is a safety measure to keep existing applications from breaking.
10
+ #++
11
+ module ActiveSupport
12
+ Inflector.inflections(:en) do |inflect|
13
+ inflect.plural(/$/, 's')
14
+ inflect.plural(/s$/i, 's')
15
+ inflect.plural(/^(ax|test)is$/i, '\1es')
16
+ inflect.plural(/(octop|vir)us$/i, '\1i')
17
+ inflect.plural(/(octop|vir)i$/i, '\1i')
18
+ inflect.plural(/(alias|status)$/i, '\1es')
19
+ inflect.plural(/(bu)s$/i, '\1ses')
20
+ inflect.plural(/(buffal|tomat)o$/i, '\1oes')
21
+ inflect.plural(/([ti])um$/i, '\1a')
22
+ inflect.plural(/([ti])a$/i, '\1a')
23
+ inflect.plural(/sis$/i, 'ses')
24
+ inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
25
+ inflect.plural(/(hive)$/i, '\1s')
26
+ inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
27
+ inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
28
+ inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
29
+ inflect.plural(/^(m|l)ouse$/i, '\1ice')
30
+ inflect.plural(/^(m|l)ice$/i, '\1ice')
31
+ inflect.plural(/^(ox)$/i, '\1en')
32
+ inflect.plural(/^(oxen)$/i, '\1')
33
+ inflect.plural(/(quiz)$/i, '\1zes')
34
+
35
+ inflect.singular(/s$/i, '')
36
+ inflect.singular(/(ss)$/i, '\1')
37
+ inflect.singular(/(n)ews$/i, '\1ews')
38
+ inflect.singular(/([ti])a$/i, '\1um')
39
+ inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, '\1sis')
40
+ inflect.singular(/(^analy)(sis|ses)$/i, '\1sis')
41
+
42
+ inflect.singular(/(hive)s$/i, '\1')
43
+ inflect.singular(/(tive)s$/i, '\1')
44
+ inflect.singular(/([lr])ves$/i, '\1f')
45
+ inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
46
+ inflect.singular(/(s)eries$/i, '\1eries')
47
+ inflect.singular(/(m)ovies$/i, '\1ovie')
48
+ inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
49
+ inflect.singular(/^(m|l)ice$/i, '\1ouse')
50
+ inflect.singular(/(bus)(es)?$/i, '\1')
51
+ inflect.singular(/(o)es$/i, '\1')
52
+ inflect.singular(/(shoe)s$/i, '\1')
53
+ inflect.singular(/(cris|test)(is|es)$/i, '\1is')
54
+ inflect.singular(/^(a)x[ie]s$/i, '\1xis')
55
+ inflect.singular(/(octop|vir)(us|i)$/i, '\1us')
56
+ inflect.singular(/(alias|status)(es)?$/i, '\1')
57
+ inflect.singular(/^(ox)en/i, '\1')
58
+ inflect.singular(/(vert|ind)ices$/i, '\1ex')
59
+ inflect.singular(/(matr)ices$/i, '\1ix')
60
+ inflect.singular(/(quiz)zes$/i, '\1')
61
+ inflect.singular(/(database)s$/i, '\1')
62
+
63
+ inflect.irregular('person', 'people')
64
+ inflect.irregular('man', 'men')
65
+ inflect.irregular('child', 'children')
66
+ inflect.irregular('sex', 'sexes')
67
+ inflect.irregular('move', 'moves')
68
+ inflect.irregular('zombie', 'zombies')
69
+
70
+ inflect.uncountable(%w(equipment information rice money species series fish sheep jeans police))
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ module Rails
2
+ module Inflections
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails/inflections/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails-inflections"
8
+ spec.version = Rails::Inflections::VERSION
9
+ spec.authors = ["Vipul A M"]
10
+ spec.email = ["vipulnsward@gmail.com"]
11
+ spec.summary = %q{\Rails Inflections on steriods}
12
+ spec.description = %q{Inflections from Rails are frozen, and most edge cases are broken. rails-inflections adds
13
+ missing rules, or corrects broken ones.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "activesupport", "~> 4.1"
25
+ end
@@ -0,0 +1,37 @@
1
+ ORIG_ARGV = ARGV.dup
2
+
3
+ require 'active_support/core_ext/kernel/reporting'
4
+
5
+ silence_warnings do
6
+ Encoding.default_internal = "UTF-8"
7
+ Encoding.default_external = "UTF-8"
8
+ end
9
+
10
+ require 'active_support/testing/autorun'
11
+
12
+ ENV['NO_RELOAD'] = '1'
13
+ require 'active_support'
14
+
15
+ Thread.abort_on_exception = true
16
+
17
+ # Show backtraces for deprecated behavior for quicker cleanup.
18
+ ActiveSupport::Deprecation.debug = true
19
+
20
+ # Disable available locale checks to avoid warnings running the test suite.
21
+ I18n.enforce_available_locales = false
22
+
23
+ # Skips the current run on Rubinius using Minitest::Assertions#skip
24
+ def rubinius_skip(message = '')
25
+ skip message if RUBY_ENGINE == 'rbx'
26
+ end
27
+
28
+ # Skips the current run on JRuby using Minitest::Assertions#skip
29
+ def jruby_skip(message = '')
30
+ skip message if defined?(JRUBY_VERSION)
31
+ end
32
+
33
+
34
+ # FIXME: we have tests that depend on run order, we should fix that and
35
+ # remove this method call.
36
+ require 'active_support/test_case'
37
+
@@ -0,0 +1,116 @@
1
+ require 'dependencies_test_helpers'
2
+
3
+ module Ace
4
+ module Base
5
+ class Case
6
+ class Dice
7
+ end
8
+ end
9
+ class Fase < Case
10
+ end
11
+ end
12
+ class Gas
13
+ include Base
14
+ end
15
+ end
16
+
17
+ class Object
18
+ module AddtlGlobalConstants
19
+ class Case
20
+ class Dice
21
+ end
22
+ end
23
+ end
24
+ include AddtlGlobalConstants
25
+ end
26
+
27
+ module ConstantizeTestCases
28
+ include DependenciesTestHelpers
29
+
30
+ def run_constantize_tests_on
31
+ assert_equal Ace::Base::Case, yield("Ace::Base::Case")
32
+ assert_equal Ace::Base::Case, yield("::Ace::Base::Case")
33
+ assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice")
34
+ assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Fase::Dice")
35
+ assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice")
36
+
37
+ assert_equal Ace::Gas::Case, yield("Ace::Gas::Case")
38
+ assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice")
39
+ assert_equal Ace::Base::Case::Dice, yield("Ace::Gas::Case::Dice")
40
+
41
+ assert_equal Case::Dice, yield("Case::Dice")
42
+ assert_equal AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
43
+ assert_equal Object::AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
44
+
45
+ assert_equal Case::Dice, yield("Object::Case::Dice")
46
+ assert_equal AddtlGlobalConstants::Case::Dice, yield("Object::Case::Dice")
47
+ assert_equal Object::AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
48
+
49
+ assert_equal ConstantizeTestCases, yield("ConstantizeTestCases")
50
+ assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases")
51
+
52
+ assert_raises(NameError) { yield("UnknownClass") }
53
+ assert_raises(NameError) { yield("UnknownClass::Ace") }
54
+ assert_raises(NameError) { yield("UnknownClass::Ace::Base") }
55
+ assert_raises(NameError) { yield("An invalid string") }
56
+ assert_raises(NameError) { yield("InvalidClass\n") }
57
+ assert_raises(NameError) { yield("Ace::ConstantizeTestCases") }
58
+ assert_raises(NameError) { yield("Ace::Base::ConstantizeTestCases") }
59
+ assert_raises(NameError) { yield("Ace::Gas::Base") }
60
+ assert_raises(NameError) { yield("Ace::Gas::ConstantizeTestCases") }
61
+ assert_raises(NameError) { yield("") }
62
+ assert_raises(NameError) { yield("::") }
63
+ assert_raises(NameError) { yield("Ace::gas") }
64
+
65
+ assert_raises(NameError) do
66
+ with_autoloading_fixtures do
67
+ yield("RaisesNameError")
68
+ end
69
+ end
70
+
71
+ assert_raises(NoMethodError) do
72
+ with_autoloading_fixtures do
73
+ yield("RaisesNoMethodError")
74
+ end
75
+ end
76
+ end
77
+
78
+ def run_safe_constantize_tests_on
79
+ assert_equal Ace::Base::Case, yield("Ace::Base::Case")
80
+ assert_equal Ace::Base::Case, yield("::Ace::Base::Case")
81
+ assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice")
82
+ assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice")
83
+ assert_equal Ace::Gas::Case, yield("Ace::Gas::Case")
84
+ assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice")
85
+ assert_equal Case::Dice, yield("Case::Dice")
86
+ assert_equal Case::Dice, yield("Object::Case::Dice")
87
+ assert_equal ConstantizeTestCases, yield("ConstantizeTestCases")
88
+ assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases")
89
+ assert_nil yield("")
90
+ assert_nil yield("::")
91
+ assert_nil yield("UnknownClass")
92
+ assert_nil yield("UnknownClass::Ace")
93
+ assert_nil yield("UnknownClass::Ace::Base")
94
+ assert_nil yield("An invalid string")
95
+ assert_nil yield("InvalidClass\n")
96
+ assert_nil yield("blargle")
97
+ assert_nil yield("Ace::ConstantizeTestCases")
98
+ assert_nil yield("Ace::Base::ConstantizeTestCases")
99
+ assert_nil yield("Ace::Gas::Base")
100
+ assert_nil yield("Ace::Gas::ConstantizeTestCases")
101
+ assert_nil yield("#<Class:0x7b8b718b>::Nested_1")
102
+ assert_nil yield("Ace::gas")
103
+
104
+ assert_raises(NameError) do
105
+ with_autoloading_fixtures do
106
+ yield("RaisesNameError")
107
+ end
108
+ end
109
+
110
+ assert_raises(NoMethodError) do
111
+ with_autoloading_fixtures do
112
+ yield("RaisesNoMethodError")
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,27 @@
1
+ module DependenciesTestHelpers
2
+ def with_loading(*from)
3
+ old_mechanism, ActiveSupport::Dependencies.mechanism = ActiveSupport::Dependencies.mechanism, :load
4
+ this_dir = File.dirname(__FILE__)
5
+ parent_dir = File.dirname(this_dir)
6
+ path_copy = $LOAD_PATH.dup
7
+ $LOAD_PATH.unshift(parent_dir) unless $LOAD_PATH.include?(parent_dir)
8
+ prior_autoload_paths = ActiveSupport::Dependencies.autoload_paths
9
+ ActiveSupport::Dependencies.autoload_paths = from.collect { |f| "#{this_dir}/#{f}" }
10
+ yield
11
+ ensure
12
+ $LOAD_PATH.replace(path_copy)
13
+ ActiveSupport::Dependencies.autoload_paths = prior_autoload_paths
14
+ ActiveSupport::Dependencies.mechanism = old_mechanism
15
+ ActiveSupport::Dependencies.explicitly_unloadable_constants = []
16
+ end
17
+
18
+ def with_autoloading_fixtures(&block)
19
+ with_loading 'autoloading_fixtures', &block
20
+ end
21
+
22
+ def remove_constants(*constants)
23
+ constants.each do |constant|
24
+ Object.send(:remove_const, constant) if Object.const_defined?(constant)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,537 @@
1
+ require 'abstract_unit'
2
+ require 'active_support/inflector'
3
+
4
+ require 'inflector_test_cases'
5
+ require 'constantize_test_cases'
6
+
7
+ class InflectorTest < ActiveSupport::TestCase
8
+ include InflectorTestCases
9
+ include ConstantizeTestCases
10
+
11
+ def test_pluralize_plurals
12
+ assert_equal "plurals", ActiveSupport::Inflector.pluralize("plurals")
13
+ assert_equal "Plurals", ActiveSupport::Inflector.pluralize("Plurals")
14
+ end
15
+
16
+ def test_pluralize_empty_string
17
+ assert_equal "", ActiveSupport::Inflector.pluralize("")
18
+ end
19
+
20
+ ActiveSupport::Inflector.inflections.uncountable.each do |word|
21
+ define_method "test_uncountability_of_#{word}" do
22
+ assert_equal word, ActiveSupport::Inflector.singularize(word)
23
+ assert_equal word, ActiveSupport::Inflector.pluralize(word)
24
+ assert_equal ActiveSupport::Inflector.pluralize(word), ActiveSupport::Inflector.singularize(word)
25
+ end
26
+ end
27
+
28
+ def test_uncountable_word_is_not_greedy
29
+ with_dup do
30
+ uncountable_word = "ors"
31
+ countable_word = "sponsor"
32
+
33
+ ActiveSupport::Inflector.inflections.uncountable << uncountable_word
34
+
35
+ assert_equal uncountable_word, ActiveSupport::Inflector.singularize(uncountable_word)
36
+ assert_equal uncountable_word, ActiveSupport::Inflector.pluralize(uncountable_word)
37
+ assert_equal ActiveSupport::Inflector.pluralize(uncountable_word), ActiveSupport::Inflector.singularize(uncountable_word)
38
+
39
+ assert_equal "sponsor", ActiveSupport::Inflector.singularize(countable_word)
40
+ assert_equal "sponsors", ActiveSupport::Inflector.pluralize(countable_word)
41
+ assert_equal "sponsor", ActiveSupport::Inflector.singularize(ActiveSupport::Inflector.pluralize(countable_word))
42
+ end
43
+ end
44
+
45
+ SingularToPlural.each do |singular, plural|
46
+ define_method "test_pluralize_singular_#{singular}" do
47
+ assert_equal(plural, ActiveSupport::Inflector.pluralize(singular))
48
+ assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(singular.capitalize))
49
+ end
50
+ end
51
+
52
+ SingularToPlural.each do |singular, plural|
53
+ define_method "test_singularize_plural_#{plural}" do
54
+ assert_equal(singular, ActiveSupport::Inflector.singularize(plural))
55
+ assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(plural.capitalize))
56
+ end
57
+ end
58
+
59
+ SingularToPlural.each do |singular, plural|
60
+ define_method "test_pluralize_plural_#{plural}" do
61
+ assert_equal(plural, ActiveSupport::Inflector.pluralize(plural))
62
+ assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(plural.capitalize))
63
+ end
64
+
65
+ define_method "test_singularize_singular_#{singular}" do
66
+ assert_equal(singular, ActiveSupport::Inflector.singularize(singular))
67
+ assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(singular.capitalize))
68
+ end
69
+ end
70
+
71
+
72
+ def test_overwrite_previous_inflectors
73
+ with_dup do
74
+ assert_equal("series", ActiveSupport::Inflector.singularize("series"))
75
+ ActiveSupport::Inflector.inflections.singular "series", "serie"
76
+ assert_equal("serie", ActiveSupport::Inflector.singularize("series"))
77
+ end
78
+ end
79
+
80
+ MixtureToTitleCase.each_with_index do |(before, titleized), index|
81
+ define_method "test_titleize_mixture_to_title_case_#{index}" do
82
+ assert_equal(titleized, ActiveSupport::Inflector.titleize(before), "mixture \
83
+ to TitleCase failed for #{before}")
84
+ end
85
+ end
86
+
87
+ def test_camelize
88
+ CamelToUnderscore.each do |camel, underscore|
89
+ assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
90
+ end
91
+ end
92
+
93
+ def test_camelize_with_lower_downcases_the_first_letter
94
+ assert_equal('capital', ActiveSupport::Inflector.camelize('Capital', false))
95
+ end
96
+
97
+ def test_camelize_with_underscores
98
+ assert_equal("CamelCase", ActiveSupport::Inflector.camelize('Camel_Case'))
99
+ end
100
+
101
+ def test_acronyms
102
+ ActiveSupport::Inflector.inflections do |inflect|
103
+ inflect.acronym("API")
104
+ inflect.acronym("HTML")
105
+ inflect.acronym("HTTP")
106
+ inflect.acronym("RESTful")
107
+ inflect.acronym("W3C")
108
+ inflect.acronym("PhD")
109
+ inflect.acronym("RoR")
110
+ inflect.acronym("SSL")
111
+ end
112
+
113
+ # camelize underscore humanize titleize
114
+ [
115
+ ["API", "api", "API", "API"],
116
+ ["APIController", "api_controller", "API controller", "API Controller"],
117
+ ["Nokogiri::HTML", "nokogiri/html", "Nokogiri/HTML", "Nokogiri/HTML"],
118
+ ["HTTPAPI", "http_api", "HTTP API", "HTTP API"],
119
+ ["HTTP::Get", "http/get", "HTTP/get", "HTTP/Get"],
120
+ ["SSLError", "ssl_error", "SSL error", "SSL Error"],
121
+ ["RESTful", "restful", "RESTful", "RESTful"],
122
+ ["RESTfulController", "restful_controller", "RESTful controller", "RESTful Controller"],
123
+ ["Nested::RESTful", "nested/restful", "Nested/RESTful", "Nested/RESTful"],
124
+ ["IHeartW3C", "i_heart_w3c", "I heart W3C", "I Heart W3C"],
125
+ ["PhDRequired", "phd_required", "PhD required", "PhD Required"],
126
+ ["IRoRU", "i_ror_u", "I RoR u", "I RoR U"],
127
+ ["RESTfulHTTPAPI", "restful_http_api", "RESTful HTTP API", "RESTful HTTP API"],
128
+ ["HTTP::RESTful", "http/restful", "HTTP/RESTful", "HTTP/RESTful"],
129
+ ["HTTP::RESTfulAPI", "http/restful_api", "HTTP/RESTful API", "HTTP/RESTful API"],
130
+ ["APIRESTful", "api_restful", "API RESTful", "API RESTful"],
131
+
132
+ # misdirection
133
+ ["Capistrano", "capistrano", "Capistrano", "Capistrano"],
134
+ ["CapiController", "capi_controller", "Capi controller", "Capi Controller"],
135
+ ["HttpsApis", "https_apis", "Https apis", "Https Apis"],
136
+ ["Html5", "html5", "Html5", "Html5"],
137
+ ["Restfully", "restfully", "Restfully", "Restfully"],
138
+ ["RoRails", "ro_rails", "Ro rails", "Ro Rails"]
139
+ ].each do |camel, under, human, title|
140
+ assert_equal(camel, ActiveSupport::Inflector.camelize(under))
141
+ assert_equal(camel, ActiveSupport::Inflector.camelize(camel))
142
+ assert_equal(under, ActiveSupport::Inflector.underscore(under))
143
+ assert_equal(under, ActiveSupport::Inflector.underscore(camel))
144
+ assert_equal(title, ActiveSupport::Inflector.titleize(under))
145
+ assert_equal(title, ActiveSupport::Inflector.titleize(camel))
146
+ assert_equal(human, ActiveSupport::Inflector.humanize(under))
147
+ end
148
+ end
149
+
150
+ def test_acronym_override
151
+ ActiveSupport::Inflector.inflections do |inflect|
152
+ inflect.acronym("API")
153
+ inflect.acronym("LegacyApi")
154
+ end
155
+
156
+ assert_equal("LegacyApi", ActiveSupport::Inflector.camelize("legacyapi"))
157
+ assert_equal("LegacyAPI", ActiveSupport::Inflector.camelize("legacy_api"))
158
+ assert_equal("SomeLegacyApi", ActiveSupport::Inflector.camelize("some_legacyapi"))
159
+ assert_equal("Nonlegacyapi", ActiveSupport::Inflector.camelize("nonlegacyapi"))
160
+ end
161
+
162
+ def test_acronyms_camelize_lower
163
+ ActiveSupport::Inflector.inflections do |inflect|
164
+ inflect.acronym("API")
165
+ inflect.acronym("HTML")
166
+ end
167
+
168
+ assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("html_api", false))
169
+ assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("htmlAPI", false))
170
+ assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("HTMLAPI", false))
171
+ end
172
+
173
+ def test_underscore_acronym_sequence
174
+ ActiveSupport::Inflector.inflections do |inflect|
175
+ inflect.acronym("API")
176
+ inflect.acronym("JSON")
177
+ inflect.acronym("HTML")
178
+ end
179
+
180
+ assert_equal("json_html_api", ActiveSupport::Inflector.underscore("JSONHTMLAPI"))
181
+ end
182
+
183
+ def test_underscore
184
+ CamelToUnderscore.each do |camel, underscore|
185
+ assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
186
+ end
187
+ CamelToUnderscoreWithoutReverse.each do |camel, underscore|
188
+ assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
189
+ end
190
+ end
191
+
192
+ def test_camelize_with_module
193
+ CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
194
+ assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
195
+ end
196
+ end
197
+
198
+ def test_underscore_with_slashes
199
+ CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
200
+ assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
201
+ end
202
+ end
203
+
204
+ def test_demodulize
205
+ assert_equal "Account", ActiveSupport::Inflector.demodulize("MyApplication::Billing::Account")
206
+ assert_equal "Account", ActiveSupport::Inflector.demodulize("Account")
207
+ assert_equal "Account", ActiveSupport::Inflector.demodulize("::Account")
208
+ assert_equal "", ActiveSupport::Inflector.demodulize("")
209
+ end
210
+
211
+ def test_deconstantize
212
+ assert_equal "MyApplication::Billing", ActiveSupport::Inflector.deconstantize("MyApplication::Billing::Account")
213
+ assert_equal "::MyApplication::Billing", ActiveSupport::Inflector.deconstantize("::MyApplication::Billing::Account")
214
+
215
+ assert_equal "MyApplication", ActiveSupport::Inflector.deconstantize("MyApplication::Billing")
216
+ assert_equal "::MyApplication", ActiveSupport::Inflector.deconstantize("::MyApplication::Billing")
217
+
218
+ assert_equal "", ActiveSupport::Inflector.deconstantize("Account")
219
+ assert_equal "", ActiveSupport::Inflector.deconstantize("::Account")
220
+ assert_equal "", ActiveSupport::Inflector.deconstantize("")
221
+ end
222
+
223
+ def test_foreign_key
224
+ ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
225
+ assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass))
226
+ end
227
+
228
+ ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
229
+ assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass, false))
230
+ end
231
+ end
232
+
233
+ def test_tableize
234
+ ClassNameToTableName.each do |class_name, table_name|
235
+ assert_equal(table_name, ActiveSupport::Inflector.tableize(class_name))
236
+ end
237
+ end
238
+
239
+ # FIXME: get following tests to pass on jruby, currently skipped
240
+ #
241
+ # Currently this fails because ActiveSupport::Multibyte::Unicode#tidy_bytes
242
+ # required a specific Encoding::Converter(UTF-8 to UTF8-MAC) which unavailable on JRuby
243
+ # causing our tests to error out.
244
+ # related bug http://jira.codehaus.org/browse/JRUBY-7194
245
+ def test_parameterize
246
+ jruby_skip "UTF-8 to UTF8-MAC Converter is unavailable"
247
+ StringToParameterized.each do |some_string, parameterized_string|
248
+ assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
249
+ end
250
+ end
251
+
252
+ def test_parameterize_and_normalize
253
+ jruby_skip "UTF-8 to UTF8-MAC Converter is unavailable"
254
+ StringToParameterizedAndNormalized.each do |some_string, parameterized_string|
255
+ assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
256
+ end
257
+ end
258
+
259
+ def test_parameterize_with_custom_separator
260
+ jruby_skip "UTF-8 to UTF8-MAC Converter is unavailable"
261
+ StringToParameterizeWithUnderscore.each do |some_string, parameterized_string|
262
+ assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string, '_'))
263
+ end
264
+ end
265
+
266
+ def test_parameterize_with_multi_character_separator
267
+ jruby_skip "UTF-8 to UTF8-MAC Converter is unavailable"
268
+ StringToParameterized.each do |some_string, parameterized_string|
269
+ assert_equal(parameterized_string.gsub('-', '__sep__'), ActiveSupport::Inflector.parameterize(some_string, '__sep__'))
270
+ end
271
+ end
272
+
273
+ def test_classify
274
+ ClassNameToTableName.each do |class_name, table_name|
275
+ assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))
276
+ assert_equal(class_name, ActiveSupport::Inflector.classify("table_prefix." + table_name))
277
+ end
278
+ end
279
+
280
+ def test_classify_with_symbol
281
+ assert_nothing_raised do
282
+ assert_equal 'FooBar', ActiveSupport::Inflector.classify(:foo_bars)
283
+ end
284
+ end
285
+
286
+ def test_classify_with_leading_schema_name
287
+ assert_equal 'FooBar', ActiveSupport::Inflector.classify('schema.foo_bar')
288
+ end
289
+
290
+ def test_humanize
291
+ UnderscoreToHuman.each do |underscore, human|
292
+ assert_equal(human, ActiveSupport::Inflector.humanize(underscore))
293
+ end
294
+ end
295
+
296
+ def test_humanize_without_capitalize
297
+ UnderscoreToHumanWithoutCapitalize.each do |underscore, human|
298
+ assert_equal(human, ActiveSupport::Inflector.humanize(underscore, capitalize: false))
299
+ end
300
+ end
301
+
302
+ def test_humanize_by_rule
303
+ ActiveSupport::Inflector.inflections do |inflect|
304
+ inflect.human(/_cnt$/i, '\1_count')
305
+ inflect.human(/^prefx_/i, '\1')
306
+ end
307
+ assert_equal("Jargon count", ActiveSupport::Inflector.humanize("jargon_cnt"))
308
+ assert_equal("Request", ActiveSupport::Inflector.humanize("prefx_request"))
309
+ end
310
+
311
+ def test_humanize_by_string
312
+ ActiveSupport::Inflector.inflections do |inflect|
313
+ inflect.human("col_rpted_bugs", "Reported bugs")
314
+ end
315
+ assert_equal("Reported bugs", ActiveSupport::Inflector.humanize("col_rpted_bugs"))
316
+ assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs"))
317
+ end
318
+
319
+ def test_constantize
320
+ run_constantize_tests_on do |string|
321
+ ActiveSupport::Inflector.constantize(string)
322
+ end
323
+ end
324
+
325
+ def test_safe_constantize
326
+ run_safe_constantize_tests_on do |string|
327
+ ActiveSupport::Inflector.safe_constantize(string)
328
+ end
329
+ end
330
+
331
+ def test_ordinal
332
+ OrdinalNumbers.each do |number, ordinalized|
333
+ assert_equal(ordinalized, number + ActiveSupport::Inflector.ordinal(number))
334
+ end
335
+ end
336
+
337
+ def test_ordinalize
338
+ OrdinalNumbers.each do |number, ordinalized|
339
+ assert_equal(ordinalized, ActiveSupport::Inflector.ordinalize(number))
340
+ end
341
+ end
342
+
343
+ def test_dasherize
344
+ UnderscoresToDashes.each do |underscored, dasherized|
345
+ assert_equal(dasherized, ActiveSupport::Inflector.dasherize(underscored))
346
+ end
347
+ end
348
+
349
+ def test_underscore_as_reverse_of_dasherize
350
+ UnderscoresToDashes.each_key do |underscored|
351
+ assert_equal(underscored, ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.dasherize(underscored)))
352
+ end
353
+ end
354
+
355
+ def test_underscore_to_lower_camel
356
+ UnderscoreToLowerCamel.each do |underscored, lower_camel|
357
+ assert_equal(lower_camel, ActiveSupport::Inflector.camelize(underscored, false))
358
+ end
359
+ end
360
+
361
+ def test_symbol_to_lower_camel
362
+ SymbolToLowerCamel.each do |symbol, lower_camel|
363
+ assert_equal(lower_camel, ActiveSupport::Inflector.camelize(symbol, false))
364
+ end
365
+ end
366
+
367
+ %w{plurals singulars uncountables humans}.each do |inflection_type|
368
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
369
+ def test_clear_#{inflection_type}
370
+ with_dup do
371
+ ActiveSupport::Inflector.inflections.clear :#{inflection_type}
372
+ assert ActiveSupport::Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\"
373
+ end
374
+ end
375
+ RUBY
376
+ end
377
+
378
+ def test_inflector_locality
379
+ ActiveSupport::Inflector.inflections(:es) do |inflect|
380
+ inflect.plural(/$/, 's')
381
+ inflect.plural(/z$/i, 'ces')
382
+
383
+ inflect.singular(/s$/, '')
384
+ inflect.singular(/es$/, '')
385
+
386
+ inflect.irregular('el', 'los')
387
+ end
388
+
389
+ assert_equal('hijos', 'hijo'.pluralize(:es))
390
+ assert_equal('luces', 'luz'.pluralize(:es))
391
+ assert_equal('luzs', 'luz'.pluralize)
392
+
393
+ assert_equal('sociedad', 'sociedades'.singularize(:es))
394
+ assert_equal('sociedade', 'sociedades'.singularize)
395
+
396
+ assert_equal('los', 'el'.pluralize(:es))
397
+ assert_equal('els', 'el'.pluralize)
398
+
399
+ ActiveSupport::Inflector.inflections(:es) { |inflect| inflect.clear }
400
+
401
+ assert ActiveSupport::Inflector.inflections(:es).plurals.empty?
402
+ assert ActiveSupport::Inflector.inflections(:es).singulars.empty?
403
+ assert !ActiveSupport::Inflector.inflections.plurals.empty?
404
+ assert !ActiveSupport::Inflector.inflections.singulars.empty?
405
+ end
406
+
407
+ def test_clear_all
408
+ with_dup do
409
+ ActiveSupport::Inflector.inflections do |inflect|
410
+ # ensure any data is present
411
+ inflect.plural(/(quiz)$/i, '\1zes')
412
+ inflect.singular(/(database)s$/i, '\1')
413
+ inflect.uncountable('series')
414
+ inflect.human("col_rpted_bugs", "Reported bugs")
415
+
416
+ inflect.clear :all
417
+
418
+ assert inflect.plurals.empty?
419
+ assert inflect.singulars.empty?
420
+ assert inflect.uncountables.empty?
421
+ assert inflect.humans.empty?
422
+ end
423
+ end
424
+ end
425
+
426
+ def test_clear_with_default
427
+ with_dup do
428
+ ActiveSupport::Inflector.inflections do |inflect|
429
+ # ensure any data is present
430
+ inflect.plural(/(quiz)$/i, '\1zes')
431
+ inflect.singular(/(database)s$/i, '\1')
432
+ inflect.uncountable('series')
433
+ inflect.human("col_rpted_bugs", "Reported bugs")
434
+
435
+ inflect.clear
436
+
437
+ assert inflect.plurals.empty?
438
+ assert inflect.singulars.empty?
439
+ assert inflect.uncountables.empty?
440
+ assert inflect.humans.empty?
441
+ end
442
+ end
443
+ end
444
+
445
+ Irregularities.each do |singular, plural|
446
+ define_method("test_irregularity_between_#{singular}_and_#{plural}") do
447
+ with_dup do
448
+ ActiveSupport::Inflector.inflections do |inflect|
449
+ inflect.irregular(singular, plural)
450
+ assert_equal singular, ActiveSupport::Inflector.singularize(plural)
451
+ assert_equal plural, ActiveSupport::Inflector.pluralize(singular)
452
+ end
453
+ end
454
+ end
455
+ end
456
+
457
+ Irregularities.each do |singular, plural|
458
+ define_method("test_pluralize_of_irregularity_#{plural}_should_be_the_same") do
459
+ with_dup do
460
+ ActiveSupport::Inflector.inflections do |inflect|
461
+ inflect.irregular(singular, plural)
462
+ assert_equal plural, ActiveSupport::Inflector.pluralize(plural)
463
+ end
464
+ end
465
+ end
466
+ end
467
+
468
+ Irregularities.each do |singular, plural|
469
+ define_method("test_singularize_of_irregularity_#{singular}_should_be_the_same") do
470
+ with_dup do
471
+ ActiveSupport::Inflector.inflections do |inflect|
472
+ inflect.irregular(singular, plural)
473
+ assert_equal singular, ActiveSupport::Inflector.singularize(singular)
474
+ end
475
+ end
476
+ end
477
+ end
478
+
479
+ [ :all, [] ].each do |scope|
480
+ ActiveSupport::Inflector.inflections do |inflect|
481
+ define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do
482
+ # save all the inflections
483
+ singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables
484
+
485
+ # clear all the inflections
486
+ inflect.clear(*scope)
487
+
488
+ assert_equal [], inflect.singulars
489
+ assert_equal [], inflect.plurals
490
+ assert_equal [], inflect.uncountables
491
+
492
+ # restore all the inflections
493
+ singulars.reverse_each { |singular| inflect.singular(*singular) }
494
+ plurals.reverse_each { |plural| inflect.plural(*plural) }
495
+ inflect.uncountable(uncountables)
496
+
497
+ assert_equal singulars, inflect.singulars
498
+ assert_equal plurals, inflect.plurals
499
+ assert_equal uncountables, inflect.uncountables
500
+ end
501
+ end
502
+ end
503
+
504
+ %w(plurals singulars uncountables humans acronyms).each do |scope|
505
+ define_method("test_clear_inflections_with_#{scope}") do
506
+ with_dup do
507
+ # clear the inflections
508
+ ActiveSupport::Inflector.inflections do |inflect|
509
+ inflect.clear(scope)
510
+ assert_equal [], inflect.send(scope)
511
+ end
512
+ end
513
+ end
514
+ end
515
+
516
+ def test_inflections_with_uncountable_words
517
+ ActiveSupport::Inflector.inflections do |inflect|
518
+ inflect.uncountable "HTTP"
519
+ end
520
+
521
+ assert_equal "HTTP", ActiveSupport::Inflector.pluralize("HTTP")
522
+ end
523
+
524
+ # Dups the singleton and yields, restoring the original inflections later.
525
+ # Use this in tests what modify the state of the singleton.
526
+ #
527
+ # This helper is implemented by setting @__instance__ because in some tests
528
+ # there are module functions that access ActiveSupport::Inflector.inflections,
529
+ # so we need to replace the singleton itself.
530
+ def with_dup
531
+ original = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)[:en]
532
+ ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original.dup)
533
+ yield
534
+ ensure
535
+ ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original)
536
+ end
537
+ end