File: test_numerizer.rb

package info (click to toggle)
ruby-numerizer 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 100 kB
  • sloc: ruby: 253; makefile: 3
file content (130 lines) | stat: -rw-r--r-- 3,996 bytes parent folder | download | duplicates (2)
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
require File.join(File.dirname(__FILE__), 'test_helper')

class NumerizerTest < Test::Unit::TestCase
  def test_straight_parsing
    strings = {
      1 => 'one',
      5 => 'five',
      10 => 'ten',
      11 => 'eleven',
      12 => 'twelve',
      13 => 'thirteen',
      14 => 'fourteen',
      15 => 'fifteen',
      16 => 'sixteen',
      17 => 'seventeen',
      18 => 'eighteen',
      19 => 'nineteen',
      20 => 'twenty',
      27 => 'twenty seven',
      31 => 'thirty-one',
      37 => 'thirty-seven',
      41 => 'forty one',
      42 => 'fourty two',
      59 => 'fifty nine',
      100 => 'a hundred',
      100 => 'one hundred',
      150 => 'one hundred and fifty',
      # 150 => 'one fifty',
      200 => 'two-hundred',
      500 => '5 hundred',
      999 => 'nine hundred and ninety nine',
      1_000 => 'one thousand',
      1_200 => 'twelve hundred',
      1_200 => 'one thousand two hundred',
      17_000 => 'seventeen thousand',
      21_473 => 'twentyone-thousand-four-hundred-and-seventy-three',
      74_002 => 'seventy four thousand and two',
      99_999 => 'ninety nine thousand nine hundred ninety nine',
      100_000 => '100 thousand',
      250_000 => 'two hundred fifty thousand',
      1_000_000 => 'one million',
      1_250_007 => 'one million two hundred fifty thousand and seven',
      1_000_000_000 => 'one billion',
      1_000_000_001 => 'one billion and one'
    }

    strings.keys.sort.each do |key|
      assert_equal key, Numerizer.numerize(strings[key]).to_i
    end

    assert_equal "2.5", Numerizer.numerize("two and a half")
    assert_equal "1/2", Numerizer.numerize("one half")
  end

  def test_combined_double_digets
    assert_equal "21", Numerizer.numerize("twentyone")
    assert_equal "37", Numerizer.numerize("thirtyseven")
  end

  def test_fractions_in_words
    assert_equal "1/4", Numerizer.numerize("1 quarter")
    assert_equal "1/4", Numerizer.numerize("one quarter")
    assert_equal "1/4", Numerizer.numerize("a quarter")
    assert_equal "1/8", Numerizer.numerize("one eighth")

    assert_equal "3/4", Numerizer.numerize("three quarters")
    assert_equal "2/4", Numerizer.numerize("two fourths")
    assert_equal "3/8", Numerizer.numerize("three eighths")
  end

  def test_fractional_addition
    assert_equal "1.25", Numerizer.numerize("one and a quarter")
    assert_equal "2.375", Numerizer.numerize("two and three eighths")
    assert_equal "3.5 hours", Numerizer.numerize("three and a half hours")
  end

  def test_word_with_a_number
    assert_equal "pennyweight", Numerizer.numerize("pennyweight")
  end

  def test_edges
    assert_equal "27 Oct 2006 7:30am", Numerizer.numerize("27 Oct 2006 7:30am")
  end

  def test_multiple_slashes_should_not_be_evaluated
    assert_equal '11/02/2007', Numerizer.numerize('11/02/2007')
  end

  def test_compatability
    assert_equal '1/2', Numerizer.numerize('1/2')
    assert_equal '05/06', Numerizer.numerize('05/06')
    assert_equal "3.5 hours", Numerizer.numerize("three and a half hours")
  end

  def test_ordinal_strings
    {
      'first' => '1st',
      'second' => 'second',
      'third' => '3rd',
      'fifth' => '5th',
      'seventh' => '7th',
      'eighth' => '8th',
      'tenth' => '10th',
      'eleventh' => '11th',
      'twelfth' => '12th',
      'thirteenth' => '13th',
      'sixteenth' => '16th',
      'twentieth' => '20th',
      'twenty-third' => '23rd',
      'thirtieth' => '30th',
      'thirty-first' => '31st',
      'fourtieth' => '40th',
      'fourty ninth' => '49th',
      'fiftieth' => '50th',
      'sixtieth' => '60th',
      'seventieth' => '70th',
      'eightieth' => '80th',
      'ninetieth' => '90th',
      'hundredth' => '100th',
      'thousandth' => '1000th',
      'millionth' => '1000000th',
      'billionth' => '1000000000th',
      'trillionth' => '1000000000000th',
      'first day month two' => '1st day month 2'
    }.each do |key, val|
      assert_equal val, Numerizer.numerize(key)
    end
  end

end