numbers_in_words 0.4.0 → 0.4.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.
Files changed (44) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +35 -1148
  5. data/.travis.yml +4 -4
  6. data/Gemfile +3 -1
  7. data/README.md +5 -43
  8. data/Rakefile +3 -1
  9. data/lib/numbers_in_words.rb +43 -19
  10. data/lib/numbers_in_words/duck_punch.rb +12 -8
  11. data/lib/numbers_in_words/exceptional_numbers.rb +119 -0
  12. data/lib/numbers_in_words/fraction.rb +151 -0
  13. data/lib/numbers_in_words/number_group.rb +34 -21
  14. data/lib/numbers_in_words/parsing/number_parser.rb +98 -0
  15. data/lib/numbers_in_words/parsing/pair_parsing.rb +64 -0
  16. data/lib/numbers_in_words/parsing/parse_fractions.rb +45 -0
  17. data/lib/numbers_in_words/parsing/parse_individual_number.rb +68 -0
  18. data/lib/numbers_in_words/parsing/parse_status.rb +17 -0
  19. data/lib/numbers_in_words/parsing/to_number.rb +159 -0
  20. data/lib/numbers_in_words/powers_of_ten.rb +49 -0
  21. data/lib/numbers_in_words/to_word.rb +78 -13
  22. data/lib/numbers_in_words/version.rb +3 -1
  23. data/lib/numbers_in_words/writer.rb +69 -0
  24. data/numbers_in_words.gemspec +14 -13
  25. data/spec/exceptional_numbers_spec.rb +26 -0
  26. data/spec/fraction_spec.rb +132 -0
  27. data/spec/fractions_spec.rb +31 -0
  28. data/spec/non_monkey_patch_spec.rb +39 -20
  29. data/spec/number_group_spec.rb +12 -12
  30. data/spec/number_parser_spec.rb +63 -0
  31. data/spec/numbers_in_words_spec.rb +56 -69
  32. data/spec/numerical_strings_spec.rb +28 -12
  33. data/spec/spec_helper.rb +2 -4
  34. data/spec/to_word_spec.rb +18 -0
  35. data/spec/words_in_numbers_spec.rb +130 -125
  36. data/spec/writer_spec.rb +26 -0
  37. data/spec/years_spec.rb +23 -13
  38. metadata +40 -25
  39. data/lib/numbers_in_words/english/constants.rb +0 -124
  40. data/lib/numbers_in_words/english/language_writer_english.rb +0 -116
  41. data/lib/numbers_in_words/language_writer.rb +0 -30
  42. data/lib/numbers_in_words/number_parser.rb +0 -135
  43. data/lib/numbers_in_words/to_number.rb +0 -88
  44. data/spec/language_writer_spec.rb +0 -23
@@ -1,32 +1,51 @@
1
- require 'numbers_in_words'
1
+ # frozen_string_literal: true
2
2
 
3
- describe "NumbersInWords" do
4
- describe ".in_words" do
3
+ require 'spec_helper'
4
+
5
+ describe 'NumbersInWords' do
6
+ describe '.in_words' do
5
7
  it do
6
- expect(NumbersInWords.in_words(100)).to eq "one hundred"
7
- expect(NumbersInWords.in_words(-100)).to eq "minus one hundred"
8
- expect(NumbersInWords.in_words(24)).to eq "twenty four"
9
- expect(NumbersInWords.in_words(1.2)).to eq "one point two"
10
- expect(NumbersInWords.in_words(100*10**100)).to eq "one hundred googol"
11
- expect(NumbersInWords.in_words(30 + 100*10**100)).to eq "one hundred googol and thirty"
8
+ expect(NumbersInWords.in_words(100)).to eq 'one hundred'
9
+ expect(NumbersInWords.in_words(-100)).to eq 'minus one hundred'
10
+ expect(NumbersInWords.in_words(24)).to eq 'twenty-four'
11
+ expect(NumbersInWords.in_words(1.2)).to eq 'one point two'
12
+ expect(NumbersInWords.in_words(100 * 10**100)).to eq 'one hundred googol'
13
+ expect(NumbersInWords.in_words(30 + 100 * 10**100)).to eq 'one hundred googol and thirty'
12
14
  end
15
+ end
16
+
17
+ FRACTIONS = {
18
+ [1, 2] => 'one half',
19
+ [3, 2] => 'three halves',
20
+ [1, 3] => 'one third',
21
+ [1, 4] => 'one quarter',
22
+ [1, 5] => 'one fifth',
23
+ [1, 19] => 'one nineteenth',
24
+ [2, 17] => 'two seventeenths',
25
+ [1, 21] => 'one twenty-first',
26
+ [1, 32] => 'one thirty-second',
27
+ [1, 101] => 'one one hundred and first',
28
+ [3, 101] => 'three hundred and firsts',
29
+ [73, 102] => 'seventy-three hundred and seconds',
30
+ [13, 97] => 'thirteen ninety-sevenths'
31
+ }.freeze
13
32
 
14
- pending "This should be implemented" do
15
- expect(NumbersInWords.in_words(100*10**100)).to eq "one hundred googols"
33
+ FRACTIONS.each do |(numerator, denominator), string|
34
+ it "#{numerator}/#{denominator} == #{string}" do
35
+ expect(NumbersInWords.in_words(numerator.to_f / denominator.to_f, fraction: true)).to eql(string)
16
36
  end
17
37
  end
18
38
 
19
- describe ".in_numbers" do
39
+ describe '.in_numbers' do
20
40
  it do
21
- expect(NumbersInWords.in_numbers("one hundred")).to eq 100
41
+ expect(NumbersInWords.in_numbers('hundred')).to eq 100
42
+ expect(NumbersInWords.in_numbers('one hundred')).to eq 100
22
43
 
23
- expect(NumbersInWords.in_numbers("minus one hundred")).to eq -100
24
- expect(NumbersInWords.in_numbers("twenty four" )).to eq 24
25
- expect(NumbersInWords.in_numbers("one point two")).to eq 1.2
26
- expect(NumbersInWords.in_numbers("one hundred googol")).to eq 100*10**100
27
- expect(NumbersInWords.in_numbers("one hundred googol and thirty")).to eq 30 + 100*10**100
44
+ expect(NumbersInWords.in_numbers('minus one hundred')).to eq(-100)
45
+ expect(NumbersInWords.in_numbers('twenty four')).to eq 24
46
+ expect(NumbersInWords.in_numbers('one point two')).to eq 1.2
47
+ expect(NumbersInWords.in_numbers('one hundred googol')).to eq 100 * 10**100
48
+ expect(NumbersInWords.in_numbers('one hundred googol and thirty')).to eq 30 + 100 * 10**100
28
49
  end
29
50
  end
30
-
31
-
32
51
  end
@@ -1,17 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require './spec/spec_helper'
4
+ require 'numbers_in_words/number_group'
2
5
 
3
6
  describe NumbersInWords::NumberGroup do
4
- it "should split into group of three digit numbers" do
5
- g = Numeric::NumberGroup
6
-
7
- expect(g.groups_of(1 ,3 )).to eq({0=>1 })
8
- expect(g.groups_of(12 ,3 )).to eq({0=>12 })
9
- expect(g.groups_of(123 ,3 )).to eq({0=>123 })
10
- expect(g.groups_of(1111 ,3 )).to eq({3=>1 , 0=>111 })
11
- expect(g.groups_of(87654 ,3 )).to eq({3=>87 , 0=>654 })
12
- expect(g.groups_of(1_234_567 ,3 )).to eq({6=>1 , 3=>234 , 0=>567 })
13
- expect(g.groups_of(123_456_789_101_112 ,3 )).to eq({12=>123 , 9=>456 , 6=>789 , 3=>101 , 0=>112 })
7
+ it 'should split into group of three digit numbers' do
8
+ g = described_class
9
+ expect(g.groups_of(1, 3)).to eq({ 0 => 1 })
10
+ expect(g.groups_of(12, 3)).to eq({ 0 => 12 })
11
+ expect(g.groups_of(123, 3)).to eq({ 0 => 123 })
12
+ expect(g.groups_of(1111, 3)).to eq({ 3 => 1, 0 => 111 })
13
+ expect(g.groups_of(87_654, 3)).to eq({ 3 => 87, 0 => 654 })
14
+ expect(g.groups_of(1_234_567, 3)).to eq({ 6 => 1, 3 => 234, 0 => 567 })
15
+ expect(g.groups_of(123_456_789_101_112, 3)).to eq({ 12 => 123, 9 => 456, 6 => 789, 3 => 101, 0 => 112 })
14
16
  end
15
17
  end
16
-
17
-
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './spec/spec_helper'
4
+ require 'numbers_in_words/parsing/number_parser'
5
+
6
+ describe NumbersInWords::NumberParser do
7
+ subject do
8
+ super().parse(number)
9
+ end
10
+
11
+ context 'with single digit' do
12
+ let(:number) { [9] }
13
+ it { expect(subject).to eq 9 }
14
+ end
15
+
16
+ context 'with thousands' do
17
+ let(:number) { [20, 1000] }
18
+ it { expect(subject).to eq 20_000 }
19
+ end
20
+
21
+ context 'with thousands with a leading one' do
22
+ let(:number) { [1, 1000, 6, 100] }
23
+ it { expect(subject).to eq 1600 }
24
+ end
25
+
26
+ pending 'with fractions' do
27
+ context 'with only denominator' do
28
+ let(:number) { [0.5] }
29
+ it { expect(subject).to eq 0.5 }
30
+ end
31
+
32
+ context 'with numerator denominator' do
33
+ let(:number) { [1, 0.5] }
34
+ it { expect(subject).to eq 0.5 }
35
+ end
36
+
37
+ context 'with multiple denominator' do
38
+ let(:number) { [3, 0.5] }
39
+ it { expect(subject).to eq 1.5 }
40
+ end
41
+
42
+ context 'with tens denominator' do
43
+ let(:number) { [2, 3, 0.5] }
44
+ pending { expect(subject).to eq 21.5 }
45
+ end
46
+
47
+ context 'with invalid number' do
48
+ let(:number) { [2, 1, 0.5, 1] }
49
+ it { expect { subject }.to raise_error NumbersInWords::InvalidNumber }
50
+ end
51
+
52
+ context 'with numerator and denominator' do
53
+ let(:number) { [2, 0.5] }
54
+ it { expect(subject).to eq 1.0 }
55
+ end
56
+ end
57
+
58
+ context 'with hundreds' do
59
+ let(:number) { [2, 100, 45] }
60
+
61
+ it { expect(subject).to eq 245 }
62
+ end
63
+ end
@@ -1,106 +1,93 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require './spec/spec_helper'
2
4
 
3
5
  describe NumbersInWords do
4
- describe ".language" do
5
- after do
6
- NumbersInWords.instance_variable_set(:"@language", nil)
7
- end
8
-
9
- it "has a default value" do
10
- expect(NumbersInWords.language).to eq "English"
11
- end
12
-
13
- it "can be set" do
14
- NumbersInWords.language = "some language"
15
- expect(NumbersInWords.language).to eq "some language"
16
- end
17
- end
18
-
19
- describe ".in_words" do
6
+ describe '.in_words' do
20
7
  it do
21
- expect(NumbersInWords.in_words(100)).to eq "one hundred"
8
+ expect(NumbersInWords.in_words(100)).to eq 'one hundred'
22
9
  end
23
10
  end
24
11
 
25
- describe ".in_numbers" do
12
+ describe '.in_numbers' do
26
13
  it do
27
- expect(NumbersInWords.in_numbers("one hundred")).to eq 100
14
+ expect(NumbersInWords.in_numbers('one-hundred')).to eq 100
28
15
  end
29
16
  end
30
17
 
31
- it "should print the digits 0-9 correctly" do
18
+ it 'should print the digits 0-9 correctly' do
32
19
  numbers = %w[zero one two three four five six seven eight nine]
33
20
 
34
21
  10.times { |i| expect(i.in_words).to eq(numbers[i]) }
35
22
  end
36
23
 
37
- it "should print the digits 11-19 correctly" do
24
+ it 'should print the digits 11-19 correctly' do
38
25
  words = %w[eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen]
39
- numbers = [11,12,13,14,15,16,17,18,19]
26
+ numbers = [11, 12, 13, 14, 15, 16, 17, 18, 19]
40
27
  numbers.each_with_index do |number, index|
41
28
  expect(number.in_words).to eq(words[index])
42
-
43
29
  end
44
30
  end
45
31
 
46
- it "should handle two digit numbers" do
47
- expect(21.in_words). to eq("twenty one")
32
+ it 'should handle two digit numbers' do
33
+ expect(21.in_words).to eq('twenty-one')
48
34
  end
49
35
 
50
- it "should handle three digit numbers" do
51
- expect(113.in_words). to eq("one hundred and thirteen")
52
- expect(299.in_words). to eq("two hundred and ninety nine")
53
- expect(300.in_words). to eq("three hundred")
54
- expect(101.in_words). to eq("one hundred and one")
36
+ it 'should handle three digit numbers' do
37
+ expect(113.in_words).to eq('one hundred and thirteen')
38
+ expect(299.in_words).to eq('two hundred and ninety-nine')
39
+ expect(300.in_words).to eq('three hundred')
40
+ expect(101.in_words).to eq('one hundred and one')
55
41
  end
56
42
 
57
- it "should print out some random examples correctly" do
58
- expect(2999 .in_words). to eq("two thousand nine hundred and ninety nine")
59
- expect(99999 .in_words). to eq("ninety nine thousand nine hundred and ninety nine")
60
- expect(999999 .in_words). to eq("nine hundred and ninety nine thousand nine hundred and ninety nine")
61
- expect(123456 .in_words). to eq("one hundred and twenty three thousand four hundred and fifty six")
62
- expect(17054 .in_words). to eq("seventeen thousand and fifty four")
63
- expect(11004 .in_words). to eq("eleven thousand and four")
64
- expect(470154 .in_words). to eq("four hundred and seventy thousand one hundred and fifty four")
65
- expect(417155 .in_words). to eq("four hundred and seventeen thousand one hundred and fifty five")
66
- expect(999999 .in_words). to eq("nine hundred and ninety nine thousand nine hundred and ninety nine")
67
- expect(1000000 .in_words). to eq("one million")
68
- expect(1000001 .in_words). to eq("one million and one")
69
- expect(112 .in_words). to eq("one hundred and twelve")
43
+ it 'should print out some random examples correctly' do
44
+ expect(2999.in_words).to eq('two thousand nine hundred and ninety-nine')
45
+ expect(99_999.in_words).to eq('ninety-nine thousand nine hundred and ninety-nine')
46
+ expect(999_999.in_words).to eq('nine hundred and ninety-nine thousand nine hundred and ninety-nine')
47
+ expect(123_456.in_words).to eq('one hundred and twenty-three thousand four hundred and fifty-six')
48
+ expect(24_056.in_words).to eq('twenty-four thousand and fifty-six')
49
+ expect(17_054.in_words).to eq('seventeen thousand and fifty-four')
50
+ expect(11_004.in_words).to eq('eleven thousand and four')
51
+ expect(470_154.in_words).to eq('four hundred and seventy thousand one hundred and fifty-four')
52
+ expect(417_155.in_words).to eq('four hundred and seventeen thousand one hundred and fifty-five')
53
+ expect(999_999.in_words).to eq('nine hundred and ninety-nine thousand nine hundred and ninety-nine')
54
+ expect(1_000_000.in_words).to eq('one million')
55
+ expect(1_000_001.in_words).to eq('one million and one')
56
+ expect(112.in_words).to eq('one hundred and twelve')
70
57
  end
71
58
 
72
- it "should handle edge cases" do
73
- expect(1000001 .in_words). to eq("one million and one")
74
- expect((10*10**12 + 10**6 +1) .in_words). to eq("ten trillion one million and one")
75
- expect((10**75) .in_words). to eq("one quattuorvigintillion")
76
- expect(10001001 .in_words). to eq("ten million one thousand and one")
59
+ it 'should handle edge cases' do
60
+ expect(1_000_001.in_words).to eq('one million and one')
61
+ expect((10 * 10**12 + 10**6 + 1).in_words).to eq('ten trillion one million and one')
62
+ expect((10**75).in_words).to eq('one quattuorvigintillion')
63
+ expect(10_001_001.in_words).to eq('ten million one thousand and one')
77
64
  end
78
65
 
79
- it "should handle a googol and larger" do
80
- n=10**100
81
- expect((10**100 + 1) .in_words). to eq("one googol and one")
82
- expect((42*10**100 + 16777216).in_words). to eq("forty two googol sixteen million seven hundred and seventy seven thousand two hundred and sixteen")
83
- expect((42* 10**100 * 10**100).in_words). to eq("forty two googol googol")
66
+ it 'should handle a googol and larger' do
67
+ googol = 10**100
68
+ expect((googol + 1).in_words).to eq('one googol and one')
69
+ expect((42 * googol + 16_777_216).in_words).to eq('forty-two googol sixteen million seven hundred and seventy-seven thousand two hundred and sixteen')
70
+ expect((42 * googol * googol).in_words).to eq('forty-two googol googol')
84
71
  end
85
72
 
86
- it "should handle negative numbers" do
87
- expect(-1 .in_words).to eq("minus one")
88
- expect(-9 .in_words).to eq("minus nine")
89
- expect(-10 .in_words).to eq("minus ten")
90
- expect(-15 .in_words).to eq("minus fifteen")
91
- expect(-100 .in_words).to eq("minus one hundred")
92
- expect((-1*(10**100)).in_words).to eq("minus one googol")
93
- expect(-123456789 .in_words).to eq("minus one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine")
73
+ it 'should handle negative numbers' do
74
+ expect(-1.in_words).to eq('minus one')
75
+ expect(-9.in_words).to eq('minus nine')
76
+ expect(-10.in_words).to eq('minus ten')
77
+ expect(-15.in_words).to eq('minus fifteen')
78
+ expect(-100.in_words).to eq('minus one hundred')
79
+ expect((-1 * (10**100)).in_words).to eq('minus one googol')
80
+ expect(-123_456_789.in_words).to eq('minus one hundred and twenty-three million four hundred and fifty-six thousand seven hundred and eighty-nine')
94
81
  end
95
82
 
96
- it "should handle decimals" do
97
- #because of lack of absolute accuracy with floats
98
- #the output won't be exactly as you might expect
99
- #so we will match rather than find equivalents
100
- expect(1.1 .in_words).to match(/one point one/)
101
- expect(1.2345678 .in_words).to match(/one point two three four five six seven eight/)
102
- expect(1000.2345678 .in_words).to match(/one thousand point two three four five six seven eight/)
103
- expect(12345.2345678.in_words).to match(/twelve thousand three hundred and forty five point two three four five six seven eight/)
83
+ it 'should handle decimals' do
84
+ # because of lack of absolute accuracy with floats
85
+ # the output won't be exactly as you might expect
86
+ # so we will match rather than find equivalents
87
+ expect(1.1.in_words).to match(/one point one/)
88
+ expect(1.2345678.in_words).to match(/one point two three four five six seven eight/)
89
+ expect(1000.2345678.in_words).to match(/one thousand point two three four five six seven eight/)
90
+ expect(12_345.2345678.in_words).to match(/twelve thousand three hundred and forty-five point two three four five six seven eight/)
104
91
  expect((10**9 + 0.1).in_words).to match(/one billion point one/)
105
92
  end
106
93
  end
@@ -1,19 +1,35 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require './spec/spec_helper'
2
4
 
3
5
  describe NumbersInWords do
4
- it "should recognize numerical strings" do
5
- arr = %w(8 56 100 5789 3435356)
6
- arr.each{ |s| expect(s.in_numbers).to eql(s.to_f) }
6
+ it 'recognizes numerical strings' do
7
+ arr = %w[8 56 100 5789 3435356]
8
+ arr.each { |s| expect(s.in_numbers).to eql(s.to_f) }
7
9
  end
8
10
 
9
- it "should recognize mixed strings" do
10
- mixed = {
11
- "19 hundred" => 1_900.0,
12
- "20 thousand" => 20_000.0,
13
- "100 million" => 100_000_000.0,
14
- "7 billion" => 7_000_000_000.0,
15
- "42 trillion" => 42_000_000_000_000.0
16
- }
17
- mixed.each{ |k, v| expect(k.in_numbers).to eql(v) }
11
+ MIXED = {
12
+ '19 hundred' => 1_900,
13
+ '20 thousand' => 20_000,
14
+ '100 million' => 100_000_000,
15
+ '7 billion' => 7_000_000_000,
16
+ '42 trillion' => 42_000_000_000_000,
17
+ '20 thousand and 4' => 20_004
18
+ }.freeze
19
+
20
+ MIXED.each do |k, v|
21
+ it do
22
+ expect(k.in_numbers).to eql(v)
23
+ end
24
+ end
25
+
26
+ PENDING = {
27
+ '19 zero five' => 1_905
28
+ }.freeze
29
+
30
+ PENDING.each do |k, v|
31
+ pending do
32
+ expect(k.in_numbers).to eql(v)
33
+ end
18
34
  end
19
35
  end
@@ -1,7 +1,5 @@
1
- require "codeclimate-test-reporter"
2
- CodeClimate::TestReporter.start
1
+ # frozen_string_literal: true
3
2
 
4
3
  require 'numbers_in_words'
5
4
  require 'numbers_in_words/duck_punch'
6
-
7
-
5
+ require 'byebug'
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe NumbersInWords::ToWord do
4
+ let(:number) { 2_111 }
5
+ let(:writer) { described_class.new(number) }
6
+
7
+ describe '#in_words' do
8
+ let(:number) { 99 }
9
+
10
+ subject do
11
+ writer.in_words
12
+ end
13
+
14
+ it do
15
+ expect(subject).to eq 'ninety-nine'
16
+ end
17
+ end
18
+ end
@@ -1,145 +1,150 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require './spec/spec_helper'
2
4
 
3
- describe WordsInNumbers do
4
- it "should do the digits 0-10" do
5
- expect("zero" .in_numbers).to eq(0)
6
- expect("one" .in_numbers).to eq(1)
7
- expect("two" .in_numbers).to eq(2)
8
- expect("three" .in_numbers).to eq(3)
9
- expect("four" .in_numbers).to eq(4)
10
- expect("five" .in_numbers).to eq(5)
11
- expect("six" .in_numbers).to eq(6)
12
- expect("seven" .in_numbers).to eq(7)
13
- expect("eight" .in_numbers).to eq(8)
14
- expect("nine" .in_numbers).to eq(9)
5
+ describe NumbersInWords::StringExtension do
6
+ it 'should do the digits 0-10' do
7
+ expect('zero'.in_numbers).to eq(0)
8
+ expect('one'.in_numbers).to eq(1)
9
+ expect('two'.in_numbers).to eq(2)
10
+ expect('three'.in_numbers).to eq(3)
11
+ expect('four'.in_numbers).to eq(4)
12
+ expect('five'.in_numbers).to eq(5)
13
+ expect('six'.in_numbers).to eq(6)
14
+ expect('seven'.in_numbers).to eq(7)
15
+ expect('eight'.in_numbers).to eq(8)
16
+ expect('nine'.in_numbers).to eq(9)
17
+ end
18
+
19
+ it 'should handle numbers for which there is one word' do
20
+ expect('ten'.in_numbers).to eq(10)
21
+ expect('eleven'.in_numbers).to eq(11)
22
+ expect('twelve'.in_numbers).to eq(12)
23
+ expect('thirteen'.in_numbers).to eq(13)
24
+ expect('fourteen'.in_numbers).to eq(14)
25
+ expect('fifteen'.in_numbers).to eq(15)
26
+ expect('sixteen'.in_numbers).to eq(16)
27
+ expect('seventeen'.in_numbers).to eq(17)
28
+ expect('eighteen'.in_numbers).to eq(18)
29
+ expect('nineteen'.in_numbers).to eq(19)
30
+ expect('twenty'.in_numbers).to eq(20)
15
31
  end
16
32
 
17
- it "should handle numbers for which there is one word" do
18
- expect("ten" .in_numbers).to eq(10)
19
- expect("eleven" .in_numbers).to eq(11)
20
- expect("twelve" .in_numbers).to eq(12)
21
- expect("thirteen" .in_numbers).to eq(13)
22
- expect("fourteen" .in_numbers).to eq(14)
23
- expect("fifteen" .in_numbers).to eq(15)
24
- expect("sixteen" .in_numbers).to eq(16)
25
- expect("seventeen" .in_numbers).to eq(17)
26
- expect("eighteen" .in_numbers).to eq(18)
27
- expect("nineteen" .in_numbers).to eq(19)
28
- expect("twenty" .in_numbers).to eq(20)
33
+ it 'should handle two word numbers up to 100' do
34
+ expect('twenty-one'.in_numbers).to eq(21)
35
+ expect('twenty-two'.in_numbers).to eq(22)
36
+ expect('twenty-three'.in_numbers).to eq(23)
37
+ expect('twenty-four'.in_numbers).to eq(24)
38
+ expect('twenty-five'.in_numbers).to eq(25)
39
+ expect('twenty-six'.in_numbers).to eq(26)
40
+ expect('twenty-seven'.in_numbers).to eq(27)
41
+ expect('twenty-eight'.in_numbers).to eq(28)
42
+ expect('seventy-six'.in_numbers).to eq(76)
43
+ expect('ninety-nine'.in_numbers).to eq(99)
29
44
  end
30
45
 
31
- it "should handle two word numbers up to 100" do
32
- expect("twenty one" .in_numbers).to eq(21)
33
- expect("twenty two" .in_numbers).to eq(22)
34
- expect("twenty three" .in_numbers).to eq(23)
35
- expect("twenty four" .in_numbers).to eq(24)
36
- expect("twenty five" .in_numbers).to eq(25)
37
- expect("twenty six" .in_numbers).to eq(26)
38
- expect("twenty seven" .in_numbers).to eq(27)
39
- expect("twenty eight" .in_numbers).to eq(28)
40
- expect("seventy six" .in_numbers).to eq(76)
41
- expect("ninety nine" .in_numbers).to eq(99)
46
+ it 'should handle hundreds' do
47
+ expect('one hundred'.in_numbers).to eq(100)
48
+ expect('two hundred'.in_numbers).to eq(200)
49
+ expect('three hundred'.in_numbers).to eq(300)
50
+ expect('nine hundred'.in_numbers).to eq(900)
51
+ expect('one hundred and seventy six'.in_numbers).to eq(176)
52
+ expect('one hundred and seventy nine'.in_numbers).to eq(179)
53
+ expect('nine hundred and ninety nine'.in_numbers).to eq(999)
42
54
  end
43
55
 
44
- it "should handle hundreds" do
45
- expect("one hundred" .in_numbers).to eq(100)
46
- expect("two hundred" .in_numbers).to eq(200)
47
- expect("three hundred" .in_numbers).to eq(300)
48
- expect("nine hundred" .in_numbers).to eq(900)
49
- expect("one hundred and seventy six" .in_numbers).to eq(176)
50
- expect("one hundred and seventy nine" .in_numbers).to eq(179)
51
- expect("nine hundred and ninety nine" .in_numbers).to eq(999)
56
+ it 'should handle unusual hundreds' do
57
+ expect('eleven hundred'.in_numbers).to eq(1100)
58
+ expect('twelve hundred'.in_numbers).to eq(1200)
59
+ expect('thirteen hundred'.in_numbers).to eq(1300)
60
+ expect('fifteen hundred'.in_numbers).to eq(1500)
61
+ expect('nineteen hundred'.in_numbers).to eq(1900)
62
+ end
63
+ it 'should handle thousands' do
64
+ expect('two thousand and one'.in_numbers).to eq(2001)
65
+ expect('one thousand'.in_numbers).to eq(1000)
66
+ expect('two thousand'.in_numbers).to eq(2000)
67
+ expect('three thousand'.in_numbers).to eq(3000)
68
+ expect('nine thousand'.in_numbers).to eq(9000)
69
+ expect('nine thousand two hundred'.in_numbers).to eq(9200)
70
+ expect('nine thousand two hundred and seven'.in_numbers).to eq(9207)
71
+ expect('nine thousand two hundred and ninety seven'.in_numbers).to eq(9297)
52
72
  end
53
73
 
54
- it "should handle unusual hundreds" do
55
- expect("eleven hundred" .in_numbers).to eq(1100)
56
- expect("twelve hundred" .in_numbers).to eq(1200)
57
- expect("thirteen hundred" .in_numbers).to eq(1300)
58
- expect("fifteen hundred" .in_numbers).to eq(1500)
59
- expect("nineteen hundred" .in_numbers).to eq(1900)
74
+ it 'handles numbers that begin with a instead of one', :aggregate_failures do
75
+ expect('a thousand six hundred'.in_numbers).to eq(1600)
76
+ expect('a thousand six hundred and fifty-five'.in_numbers).to eq(1655)
77
+ end
78
+
79
+ it 'should handle larger numbers' do
80
+ expect('one million'.in_numbers).to eq(1_000_000)
81
+ expect('two googol five billion and seventy six'.in_numbers).to eq(2 * 10**100 + 5 * 10**9 + 76)
82
+ expect('thirty seven million'.in_numbers).to eq(37 * 10**6)
83
+ expect('twenty six googol'.in_numbers).to eq(26 * 10**100)
84
+ end
60
85
 
86
+ it 'should handle numbers in hundreds of thousands etc' do
87
+ expect('nine hundred thousand'.in_numbers).to eq(900_000)
88
+ expect('three hundred and fifty seven thousand'.in_numbers).to eq(357_000)
89
+ expect('five million three hundred and fifty seven thousand'.in_numbers).to eq(5_357_000)
90
+ expect('nine hundred and ninety nine trillion'.in_numbers).to eq(999 * 10**12)
61
91
  end
62
- it "should handle thousands" do
63
- expect("two thousand and one" .in_numbers) .to eq(2001)
64
- expect("one thousand" .in_numbers) .to eq(1000)
65
- expect("two thousand" .in_numbers) .to eq(2000)
66
- expect("three thousand" .in_numbers) .to eq(3000)
67
- expect("nine thousand" .in_numbers) .to eq(9000)
68
- expect("nine thousand two hundred" .in_numbers) .to eq(9200)
69
- expect("nine thousand two hundred and seven" .in_numbers) .to eq(9207)
70
- expect("nine thousand two hundred and ninety seven" .in_numbers) .to eq(9297)
71
- end
72
-
73
- it "should handle larger numbers" do
74
- expect("one million" .in_numbers) .to eq(1000000)
75
- expect("two googol five billion and seventy six" .in_numbers) .to eq(2*10**100 + 5*10**9 + 76)
76
- expect("thirty seven million" .in_numbers) .to eq(37 * 10**6)
77
- expect("twenty six googol" .in_numbers) .to eq(26 * 10**100)
78
- end
79
-
80
- it "should handle numbers in hundreds of thousands etc" do
81
- expect("nine hundred thousand" .in_numbers) .to eq(900000)
82
- expect("three hundred and fifty seven thousand" .in_numbers) .to eq(357000)
83
- expect("five million three hundred and fifty seven thousand" .in_numbers) .to eq(5357000)
84
- expect("nine hundred and ninety nine trillion" .in_numbers) .to eq(999 * 10**12)
85
- end
86
- it "should handle negative numbers" do
87
- expect("minus one" .in_numbers) .to eq(-1)
88
- expect("minus two googol" .in_numbers) .to eq(-2 * 10**100)
89
- expect("minus nine hundred and ninety nine trillion" .in_numbers) .to eq(-999 * 10**12)
90
- end
91
-
92
- it "should ignore punctuation and capitalisation" do
93
- expect("Minus one" .in_numbers) .to eq(-1)
94
- expect("FIVE Million, three hundred and fifty-seVen Thousand" .in_numbers) .to eq(5357000)
95
- expect("FIVE,,./';';';[] Million, three hundred and fifty-seVen Thousand" .in_numbers) .to eq(5357000)
96
-
97
- end
98
-
99
- it "should handle decimal points" do
100
- expect("one point one" .in_numbers) .to eq(1.1)
101
-
102
- expect("zero point seven six five three four" .in_numbers) .to eq(0.76534)
103
- expect("one trillion point six" .in_numbers) .to eq(10**12 + 0.6)
104
-
105
- long_number = <<-NUMBER
106
- nine duotrigintillion seven hundred and seventy seven untrigintillion
107
- fifty nine trigintillion one hundred and sixty novemvigintillion
108
- eight hundred and six octovigintillion seven hundred and thirty six
109
- septenvigintillion four hundred and seventy one sexvigintillion
110
- nine hundred and seventy quinvigintillion six hundred and thirty two
111
- quattuorvigintillion eight hundred and twenty seven trevigintillion
112
- eight hundred and thirty six duovigintillion nine hundred and fifty
113
- two unvigintillion seven hundred and ten vigintillion eight hundred and one
114
- novemdecillion nine hundred and forty eight octodecillion seven hundred
115
- and five septendecillion six hundred and eighty three sexdecillion
116
- one hundred and six quindecillion seven hundred and seven quattuordecillion
117
- seven hundred and fifty seven tredecillion four hundred and twenty six
118
- duodecillion seven hundred and ninety five undecillion seven hundred
119
- and forty six decillion eight hundred and thirteen nonillion one hundred
120
- and twenty seven octillion four hundred and sixty five septillion two
121
- hundred and thirty seven sextillion one hundred and thirty nine
122
- quintillion one hundred and fifty three quadrillion forty six
123
- trillion seven hundred and fifty two billion eight hundred and three
124
- million ninety three thousand seven hundred and ninety one point
125
- eight nine five six four three two one eight nine five six seven eight
92
+ it 'should handle negative numbers' do
93
+ expect('minus one'.in_numbers).to eq(-1)
94
+ expect('minus two googol'.in_numbers).to eq(-2 * 10**100)
95
+ expect('minus nine hundred and ninety nine trillion'.in_numbers).to eq(-999 * 10**12)
96
+ end
97
+
98
+ it 'should ignore punctuation and capitalisation' do
99
+ expect('Minus one'.in_numbers).to eq(-1)
100
+ expect('FIVE Million, three hundred and fifty-seVen Thousand'.in_numbers).to eq(5_357_000)
101
+ expect("FIVE,,./';';';[] Million, three hundred and fifty-seVen Thousand".in_numbers).to eq(5_357_000)
102
+ end
103
+
104
+ it 'should handle decimal points' do
105
+ expect('one point one'.in_numbers).to eq(1.1)
106
+
107
+ expect('zero point seven six five three four'.in_numbers).to eq(0.76534)
108
+ expect('one trillion point six'.in_numbers).to eq(10**12 + 0.6)
109
+
110
+ long_number = <<~NUMBER
111
+ nine duotrigintillion seven hundred and seventy seven untrigintillion
112
+ fifty nine trigintillion one hundred and sixty novemvigintillion
113
+ eight hundred and six octovigintillion seven hundred and thirty six
114
+ septenvigintillion four hundred and seventy one sexvigintillion
115
+ nine hundred and seventy quinvigintillion six hundred and thirty two
116
+ quattuorvigintillion eight hundred and twenty seven trevigintillion
117
+ eight hundred and thirty six duovigintillion nine hundred and fifty
118
+ two unvigintillion seven hundred and ten vigintillion eight hundred and one
119
+ novemdecillion nine hundred and forty eight octodecillion seven hundred
120
+ and five septendecillion six hundred and eighty three sexdecillion
121
+ one hundred and six quindecillion seven hundred and seven quattuordecillion
122
+ seven hundred and fifty seven tredecillion four hundred and twenty six
123
+ duodecillion seven hundred and ninety five undecillion seven hundred
124
+ and forty six decillion eight hundred and thirteen nonillion one hundred
125
+ and twenty seven octillion four hundred and sixty five septillion two
126
+ hundred and thirty seven sextillion one hundred and thirty nine
127
+ quintillion one hundred and fifty three quadrillion forty six
128
+ trillion seven hundred and fifty two billion eight hundred and three
129
+ million ninety three thousand seven hundred and ninety one point
130
+ eight nine five six four three two one eight nine five six seven eight
126
131
  NUMBER
132
+
127
133
  expect(long_number.in_numbers).to eq(
128
- 9777059160806736471970632827836952710801948705683106707757426795746813127465237139153046752803093791.89564321895678
134
+ 9_777_059_160_806_736_471_970_632_827_836_952_710_801_948_705_683_106_707_757_426_795_746_813_127_465_237_139_153_046_752_803_093_791.89564321895678
129
135
  )
130
136
 
131
- expect("seventy five point eight four three two seven six nine four five one eight".
132
- in_numbers).to eq(75.84327694518)
137
+ expect('seventy five point eight four three two seven six nine four five one eight'
138
+ .in_numbers).to eq(75.84327694518)
133
139
  end
134
140
 
135
- it "should handle years notation" do
136
- expect("fifteen sixteen".in_numbers) .to eq(1516)
137
- expect("eighty five sixteen".in_numbers) .to eq(8516)
138
- expect("nineteen ninety six".in_numbers) .to eq(1996)
139
- expect("forty nine ninety eight forty seven seventy nine".in_numbers) .to eq(49984779)
140
- expect("fifteen sixteen".in_numbers) .to eq(1516)
141
- expect("fifteen sixteen seven".in_numbers) .to eq(15167)
142
- expect("fifteen sixteen seventeen".in_numbers) .to eq(151617)
141
+ it 'should handle years notation' do
142
+ expect('fifteen sixteen'.in_numbers).to eq(1516)
143
+ expect('eighty five sixteen'.in_numbers).to eq(8516)
144
+ expect('nineteen ninety six'.in_numbers).to eq(1996)
145
+ expect('forty nine ninety eight forty seven seventy nine'.in_numbers).to eq(49_984_779)
146
+ expect('fifteen sixteen'.in_numbers).to eq(1516)
147
+ expect('fifteen sixteen seven'.in_numbers).to eq(15_167)
148
+ expect('fifteen sixteen seventeen'.in_numbers).to eq(151_617)
143
149
  end
144
-
145
150
  end