Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add most of the missing specs to inflecto

  • Loading branch information...
commit 0f0c74ba2aa3375febc566d6498f65bcc59a7fa7 1 parent 1eaf62a
@indrekj indrekj authored
Showing with 224 additions and 175 deletions.
  1. +2 −2 config/flay.yml
  2. +1 −1  config/flog.yml
  3. +16 −14 lib/inflecto.rb
  4. +0 −9 spec/shared/invertible_method_behaviour.rb
  5. +0 −44 spec/shared/mutator_behavior.rb
  6. +4 −0 spec/spec_helper.rb
  7. +19 −0 spec/unit/inflecto/class_methods/camelize_spec.rb
  8. +19 −0 spec/unit/inflecto/class_methods/classify_spec.rb
  9. +35 −0 spec/unit/inflecto/class_methods/constantize_spec.rb
  10. +7 −0 spec/unit/inflecto/class_methods/dasherize_spec.rb
  11. +11 −0 spec/unit/inflecto/class_methods/demodulize_spec.rb
  12. +11 −0 spec/unit/inflecto/class_methods/foreign_key_spec.rb
  13. +20 −0 spec/unit/inflecto/class_methods/humanize_spec.rb
  14. +51 −0 spec/unit/inflecto/class_methods/ordinalize_spec.rb
  15. +1 −1  spec/unit/{inflector → inflecto}/class_methods/pluralize_spec.rb
  16. +2 −2 spec/unit/{inflector → inflecto}/class_methods/singularize_spec.rb
  17. +6 −6 spec/unit/{inflector/class_methods/tabelize_spec.rb → inflecto/class_methods/tableize_spec.rb}
  18. +19 −0 spec/unit/inflecto/class_methods/underscore_spec.rb
  19. +0 −21 spec/unit/inflector/class_methods/camelize_spec.rb
  20. +0 −15 spec/unit/inflector/class_methods/classify_spec.rb
  21. +0 −13 spec/unit/inflector/class_methods/demodulize_spec.rb
  22. +0 −13 spec/unit/inflector/class_methods/foreign_key_spec.rb
  23. +0 −13 spec/unit/inflector/class_methods/humanize_spec.rb
  24. +0 −21 spec/unit/inflector/class_methods/underscore_spec.rb
View
4 config/flay.yml
@@ -1,3 +1,3 @@
---
-threshold: 14
-total_score: 71
+threshold: 13
+total_score: 73
View
2  config/flog.yml
@@ -1,2 +1,2 @@
---
-threshold: 16.7
+threshold: 14.7
View
30 lib/inflecto.rb
@@ -23,7 +23,7 @@ module Inflecto
# @api public
#
def self.camelize(input)
- input.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
+ input.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end
# Convert input to underscored, lowercase string
@@ -41,7 +41,7 @@ def self.camelize(input)
# @api public
#
def self.underscore(input)
- word = input.to_s.dup
+ word = input.dup
word.gsub!(/::/, '/')
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
@@ -79,7 +79,7 @@ def self.dasherize(input)
# @api public
#
def self.demodulize(input)
- input.to_s.gsub(/^.*::/, '')
+ input.gsub(/^.*::/, '')
end
# Creates a foreign key name
@@ -144,10 +144,10 @@ def self.constantize(input)
# @api private
#
def self.ordinalize(number)
- if (11..13).include?(number.to_i % 100)
+ if (11..13).include?(number.abs % 100)
"#{number}th"
else
- case number.to_i % 10
+ case number.abs % 10
when 1; "#{number}st"
when 2; "#{number}nd"
when 3; "#{number}rd"
@@ -193,12 +193,12 @@ def self.inflections
# @api public
#
def self.pluralize(word)
- result = word.to_s.dup
+ result = word.dup
if result.empty? || inflections.uncountables.include?(result.downcase)
result
else
- inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
+ inflections.plurals.each { |rule, replacement| break if result.gsub!(rule, replacement) }
result
end
end
@@ -220,12 +220,12 @@ def self.pluralize(word)
# @api public
#
def self.singularize(word)
- result = word.to_s.dup
+ result = word.dup
if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i }
result
else
- inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
+ inflections.singulars.each { |rule, replacement| break if result.gsub!(rule, replacement) }
result
end
end
@@ -247,13 +247,15 @@ def self.singularize(word)
# @api private
#
def self.humanize(input)
- result = input.to_s.dup
+ result = input.dup
- inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
- result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
+ inflections.humans.each { |rule, replacement| break if result.gsub!(rule, replacement) }
+ result.gsub!(/_id$/, "")
+ result.gsub!(/_/, " ")
+ result.capitalize!
+ result
end
-
# Tabelize input string
#
# @param [String] input
@@ -296,7 +298,7 @@ def self.tableize(input)
#
def self.classify(table_name)
# strip out any leading schema name
- camelize(singularize(table_name.to_s.sub(/.*\./, '')))
+ camelize(singularize(table_name.sub(/.*\./, '')))
end
end
View
9 spec/shared/invertible_method_behaviour.rb
@@ -1,9 +0,0 @@
-# encoding: utf-8
-
-shared_examples_for 'an invertible method' do
- it_should_behave_like 'an idempotent method'
-
- it 'is invertible' do
- subject.inverse.should equal(object)
- end
-end
View
44 spec/shared/mutator_behavior.rb
@@ -1,44 +0,0 @@
-shared_examples_for 'a mutator' do
- subject { object.each(node) { |item| yields << item } }
-
- let(:yields) { [] }
- let(:object) { described_class }
-
- unless instance_methods.map(&:to_s).include?('node')
- let(:node) { source.to_ast }
- end
-
- it_should_behave_like 'a command method'
-
- context 'with no block' do
- subject { object.each(node) }
-
- it { should be_instance_of(to_enum.class) }
-
- let(:expected_mutations) do
- mutations.map do |mutation|
- if mutation.respond_to?(:to_ast)
- mutation.to_ast.to_sexp
- else
- mutation
- end
- end.to_set
- end
-
- it 'generates the expected mutations' do
- subject = self.subject.map(&:to_sexp).to_set
-
- unless subject == expected_mutations
- message = "Missing mutations: %s\nUnexpected mutations: %s" %
- [expected_mutations - subject, subject - expected_mutations ].map(&:to_a).map(&:inspect)
- fail message
- end
- end
- end
-end
-
-shared_examples_for 'a noop mutator' do
- let(:mutations) { [] }
-
- it_should_behave_like 'a mutator'
-end
View
4 spec/spec_helper.rb
@@ -29,4 +29,8 @@
end
RSpec.configure do |config|
+ # Helps to ensure that inflecto does not modify original input
+ def i(object)
+ object.freeze
+ end
end
View
19 spec/unit/inflecto/class_methods/camelize_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe Inflecto, '.camelize' do
+ it 'camelizes data_mapper as DataMapper' do
+ Inflecto.camelize(i('data_mapper')).should == 'DataMapper'
+ end
+
+ it 'camelizes merb as Merb' do
+ Inflecto.camelize(i('merb')).should == 'Merb'
+ end
+
+ it 'camelizes data_mapper/resource as DataMapper::Resource' do
+ Inflecto.camelize(i('data_mapper/resource')).should == 'DataMapper::Resource'
+ end
+
+ it 'camelizes data_mapper/associations/one_to_many as DataMapper::Associations::OneToMany' do
+ Inflecto.camelize(i('data_mapper/associations/one_to_many')).should == 'DataMapper::Associations::OneToMany'
+ end
+end
View
19 spec/unit/inflecto/class_methods/classify_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe Inflecto, '.classify' do
+ it 'classifies data_mapper as DataMapper' do
+ Inflecto.classify(i('data_mapper')).should == 'DataMapper'
+ end
+
+ it 'classifies data.mapper as Mapper' do
+ Inflecto.classify(i('data.mapper')).should == 'Mapper'
+ end
+
+ it 'classifies enlarged_testes as EnlargedTestis' do
+ Inflecto.classify(i('enlarged_testes')).should == 'EnlargedTestis'
+ end
+
+ it 'singularizes string first: classifies data_mappers as egg_and_hams as EggAndHam' do
+ Inflecto.classify(i('egg_and_hams')).should == 'EggAndHam'
+ end
+end
View
35 spec/unit/inflecto/class_methods/constantize_spec.rb
@@ -0,0 +1,35 @@
+require 'spec_helper'
+
+describe Inflecto, '.constantize' do
+ it 'constantizes Module' do
+ Inflecto.constantize(i('Module')).should == Module
+ end
+
+ it 'constantizes ::Module' do
+ Inflecto.constantize(i('::Module')).should == Module
+ end
+
+ it 'constantizes nested constant Inflecto::Inflections' do
+ Inflecto.constantize(i('Inflecto::Inflections')).should == Inflecto::Inflections
+ end
+
+ it 'does not search ancestors' do
+ module Foo
+ class Bar
+ VAL = 10
+ end
+
+ class Baz < Bar; end
+ end
+
+ expect {
+ Inflecto.constantize(i('Foo::Baz::VAL'))
+ }.to raise_error(NameError)
+ end
+
+ it 'raises exception when constant not found' do
+ expect {
+ Inflecto.constantize(i('Qwerty'))
+ }.to raise_error(NameError)
+ end
+end
View
7 spec/unit/inflecto/class_methods/dasherize_spec.rb
@@ -0,0 +1,7 @@
+require 'spec_helper'
+
+describe Inflecto, '.dasherize' do
+ it 'dasherizes data_mapper as data-mapper' do
+ Inflecto.dasherize(i('data_mapper')).should == 'data-mapper'
+ end
+end
View
11 spec/unit/inflecto/class_methods/demodulize_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe Inflecto, '.demodulize' do
+ it 'demodulizes module name: DataMapper::Inflecto => Inflecto' do
+ Inflecto.demodulize(i('DataMapper::Inflecto')).should == 'Inflecto'
+ end
+
+ it 'demodulizes module name: A::B::C::D::E => E' do
+ Inflecto.demodulize(i('A::B::C::D::E')).should == 'E'
+ end
+end
View
11 spec/unit/inflecto/class_methods/foreign_key_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe Inflecto, '.foreign_key' do
+ it 'adds _id to downcased string: Message => message_id' do
+ Inflecto.foreign_key(i('Message')).should == 'message_id'
+ end
+
+ it 'demodulizes string first: Admin::Post => post_id' do
+ Inflecto.foreign_key(i('Admin::Post')).should == 'post_id'
+ end
+end
View
20 spec/unit/inflecto/class_methods/humanize_spec.rb
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe Inflecto, '.humanize' do
+ it 'replaces _ with space: humanizes employee_salary as Employee salary' do
+ Inflecto.humanize(i('employee_salary')).should == 'Employee salary'
+ end
+
+ it 'strips _id endings: humanizes author_id as Author' do
+ Inflecto.humanize(i('author_id')).should == 'Author'
+ end
+
+ it 'uses user added rules when possible' do
+ Inflecto.inflections do |inflect|
+ inflect.human('Question', 'Fancy question')
+ inflect.human('questionary', 'Questionnaire')
+ end
+
+ Inflecto.humanize(i('questionary')).should == 'Questionnaire'
+ end
+end
View
51 spec/unit/inflecto/class_methods/ordinalize_spec.rb
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+describe Inflecto, '.ordinalize' do
+ context 'when number ends with digit 1' do
+ it 'adds -th suffix when number ends with 11' do
+ [-1011, -111, -11, 11, 111, 1011].each do |number|
+ Inflecto.ordinalize(number).should eq("#{number}th")
+ end
+ end
+
+ it 'adds -st suffix when number does not end with 11' do
+ [-1001, -101, -21, -1, 1, 21, 101, 1001].each do |number|
+ Inflecto.ordinalize(number).should eq("#{number}st")
+ end
+ end
+ end
+
+ context 'when number ends with digit 2' do
+ it 'adds -th suffix when number ends with 12' do
+ [-1012, -112, -12, 12, 112, 1012].each do |number|
+ Inflecto.ordinalize(number).should eq("#{number}th")
+ end
+ end
+
+ it 'adds -nd suffix when number does not end with 12' do
+ [-1002, -102, -22, -2, 2, 22, 102, 1002].each do |number|
+ Inflecto.ordinalize(number).should eq("#{number}nd")
+ end
+ end
+ end
+
+ context 'when number ends with digit 3' do
+ it 'adds -th suffix when number ends with 13' do
+ [-1013, -113, -13, 13, 113, 1013].each do |number|
+ Inflecto.ordinalize(number).should eq("#{number}th")
+ end
+ end
+
+ it 'adds -rd suffix when number does not end with 13' do
+ [-1003, -103, -23, -3, 3, 23, 103, 1003].each do |number|
+ Inflecto.ordinalize(number).should eq("#{number}rd")
+ end
+ end
+ end
+
+ it 'ordinalizes other numbers with -th suffix' do
+ [-4, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16].each do |number|
+ Inflecto.ordinalize(number).should eq("#{number}th")
+ end
+ end
+end
View
2  spec/unit/inflector/class_methods/pluralize_spec.rb → spec/unit/inflecto/class_methods/pluralize_spec.rb
@@ -186,7 +186,7 @@
SINGULAR_TO_PLURAL.each do |singular, plural|
it "pluralizes #{singular} => #{plural}" do
- Inflecto.pluralize(singular).should eql(plural)
+ Inflecto.pluralize(i(singular)).should eql(plural)
end
end
View
4 .../unit/inflector/class_methods/singularize_spec.rb → spec/unit/inflecto/class_methods/singularize_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'
-describe Inflecto, '#singular' do
+describe Inflecto, '.singularize' do
# ==== exceptional cases
PLURAL_TO_SINGULAR = {
@@ -147,7 +147,7 @@
PLURAL_TO_SINGULAR.each do |plural, singular|
it "should signularize #{plural} => #{singular}" do
- Inflecto.singularize(plural).should eql(singular)
+ Inflecto.singularize(i(plural)).should eql(singular)
end
end
end
View
12 spec/unit/inflector/class_methods/tabelize_spec.rb → spec/unit/inflecto/class_methods/tableize_spec.rb
@@ -2,26 +2,26 @@
describe Inflecto, '.tableize' do
it 'pluralizes last word in snake_case strings: fancy_category => fancy_categories' do
- Inflecto.tableize('fancy_category').should == 'fancy_categories'
+ Inflecto.tableize(i('fancy_category')).should == 'fancy_categories'
end
it 'underscores CamelCase strings before pluralization: enlarged_testis => enlarged_testes' do
- Inflecto.tableize('enlarged_testis').should == 'enlarged_testes'
+ Inflecto.tableize(i('enlarged_testis')).should == 'enlarged_testes'
end
it 'underscores CamelCase strings before pluralization: FancyCategory => fancy_categories' do
- Inflecto.tableize('FancyCategory').should == 'fancy_categories'
+ Inflecto.tableize(i('FancyCategory')).should == 'fancy_categories'
end
it 'underscores CamelCase strings before pluralization: EnlargedTestis => enlarged_testes' do
- Inflecto.tableize('EnlargedTestis').should == 'enlarged_testes'
+ Inflecto.tableize(i('EnlargedTestis')).should == 'enlarged_testes'
end
it 'replaces :: with underscores: Fancy::Category => fancy_categories' do
- Inflecto.tableize('Fancy::Category').should == 'fancy_categories'
+ Inflecto.tableize(i('Fancy::Category')).should == 'fancy_categories'
end
it 'underscores CamelCase strings before pluralization: Enlarged::Testis => enlarged_testes' do
- Inflecto.tableize('Enlarged::Testis').should == 'enlarged_testes'
+ Inflecto.tableize(i('Enlarged::Testis')).should == 'enlarged_testes'
end
end
View
19 spec/unit/inflecto/class_methods/underscore_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe Inflecto, '.underscore' do
+ it 'underscores DataMapper as data_mapper' do
+ Inflecto.underscore(i('DataMapper')).should == 'data_mapper'
+ end
+
+ it 'underscores Merb as merb' do
+ Inflecto.underscore(i('Merb')).should == 'merb'
+ end
+
+ it 'underscores DataMapper::Resource as data_mapper/resource' do
+ Inflecto.underscore(i('DataMapper::Resource')).should == 'data_mapper/resource'
+ end
+
+ it 'underscores Merb::BootLoader::Rackup as merb/boot_loader/rackup' do
+ Inflecto.underscore(i('Merb::BootLoader::Rackup')).should == 'merb/boot_loader/rackup'
+ end
+end
View
21 spec/unit/inflector/class_methods/camelize_spec.rb
@@ -1,21 +0,0 @@
-require 'spec_helper'
-
-describe Inflecto do
- describe '.camelize' do
- it 'camelizes data_mapper as DataMapper' do
- Inflecto.camelize('data_mapper').should == 'DataMapper'
- end
-
- it 'camelizes merb as Merb' do
- Inflecto.camelize('merb').should == 'Merb'
- end
-
- it 'camelizes data_mapper/resource as DataMapper::Resource' do
- Inflecto.camelize('data_mapper/resource').should == 'DataMapper::Resource'
- end
-
- it 'camelizes data_mapper/associations/one_to_many as DataMapper::Associations::OneToMany' do
- Inflecto.camelize('data_mapper/associations/one_to_many').should == 'DataMapper::Associations::OneToMany'
- end
- end
-end
View
15 spec/unit/inflector/class_methods/classify_spec.rb
@@ -1,15 +0,0 @@
-require 'spec_helper'
-
-describe Inflecto, '.classify' do
- it 'classifies data_mapper as DataMapper' do
- Inflecto.classify('data_mapper').should == 'DataMapper'
- end
-
- it 'classifies enlarged_testes as EnlargedTestis' do
- Inflecto.classify('enlarged_testes').should == 'EnlargedTestis'
- end
-
- it 'singularizes string first: classifies data_mappers as egg_and_hams as EggAndHam' do
- Inflecto.classify('egg_and_hams').should == 'EggAndHam'
- end
-end
View
13 spec/unit/inflector/class_methods/demodulize_spec.rb
@@ -1,13 +0,0 @@
-require 'spec_helper'
-
-describe Inflecto do
- describe '.demodulize' do
- it 'demodulizes module name: DataMapper::Inflecto => Inflecto' do
- Inflecto.demodulize('DataMapper::Inflecto').should == 'Inflecto'
- end
-
- it 'demodulizes module name: A::B::C::D::E => E' do
- Inflecto.demodulize('A::B::C::D::E').should == 'E'
- end
- end
-end
View
13 spec/unit/inflector/class_methods/foreign_key_spec.rb
@@ -1,13 +0,0 @@
-require 'spec_helper'
-
-describe Inflecto do
- describe '.foreign_key' do
- it 'adds _id to downcased string: Message => message_id' do
- Inflecto.foreign_key('Message').should == 'message_id'
- end
-
- it 'demodulizes string first: Admin::Post => post_id' do
- Inflecto.foreign_key('Admin::Post').should == 'post_id'
- end
- end
-end
View
13 spec/unit/inflector/class_methods/humanize_spec.rb
@@ -1,13 +0,0 @@
-require 'spec_helper'
-
-describe Inflecto do
- describe '.humanize' do
- it 'replaces _ with space: humanizes employee_salary as Employee salary' do
- Inflecto.humanize('employee_salary').should == 'Employee salary'
- end
-
- it 'strips _id endings: humanizes author_id as Author' do
- Inflecto.humanize('author_id').should == 'Author'
- end
- end
-end
View
21 spec/unit/inflector/class_methods/underscore_spec.rb
@@ -1,21 +0,0 @@
-require 'spec_helper'
-
-describe Inflecto do
- describe '.underscore' do
- it 'underscores DataMapper as data_mapper' do
- Inflecto.underscore('DataMapper').should == 'data_mapper'
- end
-
- it 'underscores Merb as merb' do
- Inflecto.underscore('Merb').should == 'merb'
- end
-
- it 'underscores DataMapper::Resource as data_mapper/resource' do
- Inflecto.underscore('DataMapper::Resource').should == 'data_mapper/resource'
- end
-
- it 'underscores Merb::BootLoader::Rackup as merb/boot_loader/rackup' do
- Inflecto.underscore('Merb::BootLoader::Rackup').should == 'merb/boot_loader/rackup'
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.