File: humanize_spec.rb

package info (click to toggle)
ruby-humanize 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,108 kB
  • sloc: ruby: 1,779; makefile: 7; sh: 1
file content (149 lines) | stat: -rw-r--r-- 4,888 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
require 'spec_helper'

RSpec.describe "Humanize" do
  after do
    Humanize.reset_config
  end

  describe 'locale option' do
    it 'uses default locale' do
      Humanize.config.default_locale = :fr
      expect(42.humanize).to eql('quarante-deux')
    end

    it 'uses locale passed as argument if given' do
      Humanize.config.default_locale = :en
      expect(42.humanize(locale: :fr)).to eql('quarante-deux')
    end

    describe 'turkish specific rules' do
      it 'one thousand and two equals "bin iki"' do
        expect(1002.humanize(locale: :tr)).to eql('bin iki')
      end

      it 'two thousand and one equals "iki bin bir' do
        expect(2001.humanize(locale: :tr)).to eql('iki bin bir')
      end

      it 'ten thousand equals "on bin"' do
        expect(10_000.humanize(locale: :tr)).to eql('on bin')
      end
    end

    describe 'malaysia specific rules' do
      before do
        Humanize.config.default_locale = :ms
      end

      context 'one thousand' do
        it 'equals "satu ribu" when it is not the only thousand in its thousands range' do
          expect(1_101_000.humanize).to eql('satu juta seratus satu ribu')
          expect(2_201_042.humanize).to eql('dua juta dua ratus satu ribu empat puluh dua')
        end

        it 'equals "seribu" when it is the lone thousand in its thousands range' do
          expect(1_000.humanize).to eql('seribu')
          expect(1_042.humanize).to eql('seribu empat puluh dua')
          expect(1_001_042.humanize).to eql('satu juta seribu empat puluh dua')
          expect(1_000_001_042.humanize).to eql('satu bilion seribu empat puluh dua')
        end
      end
    end

    describe 'azerbaijani specific rules' do
      it 'one thousand and two equals "min iki"' do
        expect(1002.humanize(locale: :az)).to eql('min iki')
      end

      it 'two thousand and one equals "iki min bir' do
        expect(2001.humanize(locale: :az)).to eql('iki min bir')
      end

      it 'ten thousand equals "on min"' do
        expect(10_000.humanize(locale: :az)).to eql('on min')
      end
    end

    describe 'indonesian specific rules' do
      before do
        Humanize.config.default_locale = :id
      end

      context 'one thousand' do
        it 'equals "satu ribu" when it is not the only thousand in its thousands range' do
          expect(1_101_000.humanize).to eql('satu juta seratus satu ribu')
          expect(2_201_042.humanize).to eql('dua juta dua ratus satu ribu empat puluh dua')
        end

        it 'equals "seribu" when it is the lone thousand in its thousands range' do
          expect(1_000.humanize).to eql('seribu')
          expect(1_042.humanize).to eql('seribu empat puluh dua')
          expect(1_001_042.humanize).to eql('satu juta seribu empat puluh dua')
          expect(1_000_001_042.humanize).to eql('satu miliar seribu empat puluh dua')
        end
      end
    end
  end

  describe 'decimals_as option' do
    it 'uses value from configuration' do
      Humanize.config.decimals_as = :number
      expect(0.42.humanize).to eql('zero point forty-two')
    end

    it 'uses value passed as argument if given' do
      Humanize.config.decimals_as = :number
      expect(0.42.humanize(decimals_as: :digits)).to eql('zero point four two')
    end

    describe 'when set as number' do
      before do
        Humanize.config.decimals_as = :number
      end

      it 'reads the decimals as digits if led by zero(s)' do
        expect(0.042.humanize).to eql('zero point zero four two')
        expect(0.0042.humanize).to eql('zero point zero zero four two')
      end
    end
  end

  describe 'both options work together' do
    it 'work together' do
      expect(
        0.42.humanize(locale: :fr, decimals_as: :number)
      ).to eql('zéro virgule quarante-deux')
    end
  end

  describe 'when called on instances of Rational, Complex, and Date::Infinity' do
    it 'will raise NoMethodError' do
      expect { Rational(1, 3).humanize }.to raise_error(NoMethodError, /humanize/)
      expect { Complex(1 + 2i).humanize }.to raise_error(NoMethodError, /humanize/)
      if defined? Date::Infinity
        expect do
          Date::Infinity.new.humanize
        end.to raise_error(NoMethodError, /humanize/)
      end
    end
  end

  describe 'when called on conceptual number' do
    it 'reads correctly' do
      inf = Float::INFINITY
      neg_inf = -inf
      nan = inf + neg_inf

      expect(inf.humanize).to eql('infinity')
      expect(neg_inf.humanize).to eql('negative infinity')
      expect(nan.humanize).to eql('undefined')
    end
  end

  describe 'when called on bigdecimal with decimal fractions' do
    it 'reads the decimal digits' do
      expect(BigDecimal('123').humanize).to eql('one hundred and twenty-three')
      expect(BigDecimal('123.45').humanize).to eql('one hundred and twenty-three point four five')
    end
  end
end