require 'to_words' # Examples of unhandled numbers [-123, -1, 0, 1_000_000_000_000].each do |number| describe "#{number}.to_words" do it "should be '?'" do number.to_words.should == '?' end end end # 1-19 {1=>'one', 2=>'two', 3=>'three', 4=>'four', 5=>'five', 6=>'six', 7=>'seven', 8=>'eight', 9=>'nine', 10=>'ten', 11=>'eleven', 12=>'twelve', 13=>'thirteen', 14=>'fourteen', 15=>'fifteen', 16=>'sixteen', 17=>'seventeen', 18=>'eighteen', 19=>'nineteen'}.each do |number, word| describe "#{number}.to_words" do it "should be '#{word}'" do number.to_words.should == word end end end # 20, 30 ... 90 {20=>'twenty', 30=>'thirty', 40=>'forty', 50=>'fifty', 60=>'sixty', 70=>'seventy', 80=>'eighty', 90=>'ninety'}.each do |number, word| describe "#{number}.to_words" do it "should be '#{word}'" do number.to_words.should == word end end end describe "A two-digit number above 20 that's not divisible by ten, in words" do {21=>'twenty-one', 42=>'forty-two', 69=>'sixty-nine', 99=>'ninety-nine'}.each do |number, word| it "should be '-'" do number.to_words.should == word end end end describe "A three-digit number that's divisible by one hundred, in words" do [100, 500, 900].each do |number| it "should be ' hundred'" do number.to_words.should == (number/100).to_words + ' hundred' end end end describe "A three-digit number that's not divisible by one hundred, in words" do [101, 150, 666, 999].each do |number| it "should be ' hundred and '" do number.to_words.should == (number/100).to_words + ' hundred and ' + (number%100).to_words end end end describe "A four, five or six-digit number that's divisible by one thousand, in words" do [1_000, 23_000, 456_000, 999_000].each do |number| it "should be ' thousand'" do number.to_words.should == (number/1000).to_words + ' thousand' end end end describe "A four, five or six-digit number that's not divisible by one thousand, in words" do [1_234, 23_456, 345_678, 999_999].each do |number| it "should be ' thousand, '" do number.to_words.should == (number/1000).to_words + ' thousand, ' + (number%1000).to_words end end end describe "A four, five or six-digit number that's not divisible by one thousand but has no hundreds, in words" do [1_023, 23_001, 345_099].each do |number| it "should be ' thousand and '" do number.to_words.should == (number/1000).to_words + ' thousand and ' + (number%1000).to_words end end end describe "A seven, eight or nine-digit number that's divisible by one million, in words" do [1_000_000, 34_000_000, 567_000_000, 999_000_000].each do |number| it "should be ' million'" do number.to_words.should == (number/1_000_000).to_words + ' million' end end end describe "A seven, eight or nine-digit number that's not divisible by one million, in words" do [1_234_567, 34_567_890, 567_890_123, 999_999_999].each do |number| it "should be ' million, '" do number.to_words.should == (number/1_000_000).to_words + ' million, ' + (number%1_000_000).to_words end end end describe "A seven, eight or nine-digit number that's not divisible by one million but has zeroes " + "from hundreds of thousands down to hundreds, in words" do [1_000_023, 23_000_001, 345_000_099].each do |number| it "should be ' million and '" do number.to_words.should == (number/1_000_000).to_words + ' million and ' + (number%1_000_000).to_words end end end describe "A ten, eleven or twelve-digit number that's divisible by one billion, in words" do [1_000_000_000, 34_000_000_000, 567_000_000_000, 999_000_000_000].each do |number| it "should be ' billion'" do number.to_words.should == (number/1_000_000_000).to_words + ' billion' end end end describe "A ten, eleven or twelve-digit number that's not divisible by one billion, in words" do [1_234_567_890, 34_567_890_123, 567_890_123_456, 999_999_999_999].each do |number| it "should be ' billion, '" do number.to_words.should == (number/1_000_000_000).to_words + ' billion, ' + (number%1_000_000_000).to_words end end end describe "A ten, eleven or twelve-digit number that's not divisible by one billion but has zeroes " + "from hundreds of millions down to hundreds, in words" do [1_000_000_023, 23_000_000_001, 345_000_000_099].each do |number| it "should be ' billion and '" do number.to_words.should == (number/1_000_000_000).to_words + ' billion and ' + (number%1_000_000_000).to_words end end end