Permalink
Browse files

Add missing specs for Inflecto::Inflections

  • Loading branch information...
1 parent 3d8ab06 commit c3c58ab556c8a629be7d93095befad95dd29e93a @indrekj indrekj committed May 27, 2013
View
69 lib/inflecto/inflections.rb
@@ -84,7 +84,6 @@ def initialize
#
def plural(rule, replacement)
rule(rule, replacement, @plurals)
- @plurals.insert(0, [rule, replacement])
self
end
@@ -105,23 +104,6 @@ def singular(rule, replacement)
self
end
- # Add a new rule
- #
- # @param [String, Regexp] rule
- # @param [String, Regexp] replacement
- # @param [Array] target
- #
- # @return [undefined]
- #
- # @api private
- #
- def rule(rule, replacement, target)
- @uncountables.delete(rule) if rule.is_a?(String)
- @uncountables.delete(replacement)
- target.insert(0, [rule, replacement])
- end
- private :rule
-
# Add a new irregular pluralization
#
# Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
@@ -147,21 +129,6 @@ def irregular(singular, plural)
self
end
- # Add irregular inflection
- #
- # @param [String] rule
- # @param [String] replacement
- #
- # @return [undefined]
- #
- # @api private
- #
- def add_irregular(rule, replacement, target)
- head, *tail = rule.chars.to_a
- rule(Regexp.new("(#{head})#{tail.join}$", 'i'), '\1' + replacement[1..-1], target)
- end
- private :add_irregular
-
# Add uncountable words
#
# Uncountable will not be inflected
@@ -178,8 +145,8 @@ def add_irregular(rule, replacement, target)
#
# @api private
#
- def uncountable(words)
- @uncountables.concat(words)
+ def uncountable(*words)
+ @uncountables.concat(words.flatten)
self
end
@@ -220,5 +187,37 @@ def clear
self
end
+ private
+
+ # Add irregular inflection
+ #
+ # @param [String] rule
+ # @param [String] replacement
+ #
+ # @return [undefined]
+ #
+ # @api private
+ #
+ def add_irregular(rule, replacement, target)
+ head, *tail = rule.chars.to_a
+ rule(/(#{head})#{tail.join}$/i, '\1' + replacement[1..-1], target)
+ end
+
+ # Add a new rule
+ #
+ # @param [String, Regexp] rule
+ # @param [String, Regexp] replacement
+ # @param [Array] target
+ #
+ # @return [undefined]
+ #
+ # @api private
+ #
+ def rule(rule, replacement, target)
+ @uncountables.delete(rule)
+ @uncountables.delete(replacement)
+ target.insert(0, [rule, replacement])
+ end
+
end
end
View
11 spec/unit/inflecto/inflections/class_methods/instance_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe Inflecto::Inflections, '.instance' do
+ subject { described_class.instance }
+
+ it { should be_instance_of(described_class) }
+
+ it 'returns always same instance' do
+ subject.should equal(described_class.instance)
+ end
+end
View
21 spec/unit/inflecto/inflections/clear_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe Inflecto::Inflections, '#clear' do
+ subject { object.clear }
+
+ let(:object) { described_class.new }
+
+ before do
+ object.uncountable('apple')
+ object.plural('people', 'person')
+ object.singular('apple', 'apples')
+ object.human('person_name', 'Name')
+ end
+
+ it { should be(object) }
+
+ its(:plurals) { should be_empty }
+ its(:singulars) { should be_empty }
+ its(:uncountables) { should be_empty }
+ its(:humans) { should be_empty }
+end
View
22 spec/unit/inflecto/inflections/human_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe Inflecto::Inflections, '#human' do
+ subject { object.human(rule, replacement) }
+
+ let(:object) { described_class.new }
+ let(:rule) { mock(:rule) }
+ let(:replacement) { mock(:replacement) }
+
+ it { should be(object) }
+
+ its(:humans) { should eq([[rule, replacement]]) }
+
+ it 'adds rule as a first item' do
+ subject
+ object.human(mock, mock)
+
+ humans = object.humans
+ humans.size.should be(2)
+ humans.last.should eq([rule, replacement])
+ end
+end
View
30 spec/unit/inflecto/inflections/irregular_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe Inflecto::Inflections, '#irregular' do
+ subject { object.irregular(singular, plural) }
+
+ let(:object) { described_class.new }
+ let(:singular) { 'person' }
+ let(:plural) { 'people' }
+
+ it { should be(object) }
+
+ its(:plurals) { should include([/(p)erson$/i, "\\1eople"]) }
+ its(:singulars) { should include([/(p)eople$/i, "\\1erson"]) }
+
+ context 'when singular form is in uncountables' do
+ before { object.uncountable('person') }
+
+ it 'removes it from uncountables' do
+ subject.uncountables.should_not include('person')
+ end
+ end
+
+ context 'when plural form is in uncountables' do
+ before { object.uncountable('people') }
+
+ it 'removes it from uncountables' do
+ subject.uncountables.should_not include('people')
+ end
+ end
+end
View
38 spec/unit/inflecto/inflections/plural_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+describe Inflecto::Inflections, '#plural' do
+ subject { object.plural(rule, replacement) }
+
+ let(:object) { described_class.new }
+ let(:rule) { mock(:rule) }
+ let(:replacement) { mock(:replacement) }
+
+ it { should be(object) }
+
+ its(:plurals) { should eq([[rule, replacement]]) }
+
+ it 'adds rule as a first item' do
+ subject
+ object.plural(mock, mock)
+
+ plurals = object.plurals
+ plurals.size.should be(2)
+ plurals.last.should eq([rule, replacement])
+ end
+
+ context 'when rule is in the uncountables' do
+ before { object.uncountable(rule) }
+
+ it 'removes it from uncountables' do
+ subject.uncountables.should eq([])
+ end
+ end
+
+ context 'when replacement is in the uncountables' do
+ before { object.uncountable(replacement) }
+
+ it 'removes it from uncountables' do
+ subject.uncountables.should eq([])
+ end
+ end
+end
View
38 spec/unit/inflecto/inflections/singular_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+describe Inflecto::Inflections, '#singular' do
+ subject { object.singular(rule, replacement) }
+
+ let(:object) { described_class.new }
+ let(:rule) { mock(:rule) }
+ let(:replacement) { mock(:replacement) }
+
+ it { should be(object) }
+
+ its(:singulars) { should eq([[rule, replacement]]) }
+
+ it 'adds rule as a first item' do
+ subject
+ object.singular(mock, mock)
+
+ singulars = object.singulars
+ singulars.size.should be(2)
+ singulars.last.should eq([rule, replacement])
+ end
+
+ context 'when rule is in the uncountables' do
+ before { object.uncountable(rule) }
+
+ it 'removes it from uncountables' do
+ subject.uncountables.should eq([])
+ end
+ end
+
+ context 'when replacement is in the uncountables' do
+ before { object.uncountable(replacement) }
+
+ it 'removes it from uncountables' do
+ subject.uncountables.should eq([])
+ end
+ end
+end
View
20 spec/unit/inflecto/inflections/uncountable_spec.rb
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe Inflecto::Inflections, '#uncountable' do
+ let(:object) { described_class.new }
+
+ context 'when word given' do
+ subject { object.uncountable('water') }
+ its(:uncountables) { should eq(['water']) }
+ end
+
+ context 'when array of words given' do
+ subject { object.uncountable(['water', 'sugar']) }
+ its(:uncountables) { should eq(%w[water sugar]) }
+ end
+
+ context 'when multiple words given' do
+ subject { object.uncountable('water', 'sugar') }
+ its(:uncountables) { should eq(%w[water sugar]) }
+ end
+end

0 comments on commit c3c58ab

Please sign in to comment.