Skip to content

Instantly share code, notes, and snippets.

@dogweather
Created January 2, 2014 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dogweather/8216141 to your computer and use it in GitHub Desktop.
Save dogweather/8216141 to your computer and use it in GitHub Desktop.
A passing spec for the simple number parser.
# -*- coding: utf-8 -*-
require 'spec_helper'
describe Parsers::NumberParser do
before(:each) do
@p = Parsers::NumberParser.new
end
it 'can be instantiated' do
@p.should be_instance_of Parsers::NumberParser
end
context '#number?' do
it 'correctly determines that text is a number' do
@p.number?('fifty-five').should be_true
end
it 'correctly determines that text is not a number' do
@p.number?('53x9').should be_false
end
end
context '#parse' do
it 'rejects bad input' do
expect { @p.parse('xxx') }.to raise_error
end
it 'can parse a number regardless of case' do
@p.parse('one').should == 1
@p.parse('One').should == 1
@p.parse('ONE').should == 1
end
it 'parses a number with a leading space' do
@p.parse(' thirteen hundred').should == @p.parse('thirteen hundred')
end
specify { @p.parse('two').should == 2 }
specify { @p.parse('nine').should == 9 }
specify { @p.parse('fifteen').should == 15 }
specify { @p.parse('forty-one').should == 41 }
specify { @p.parse('one hundred three').should == 103 }
specify { @p.parse('four hundred').should == 400 }
specify { @p.parse('four hundred one').should == 401 }
specify { @p.parse('thirteen hundred one').should == 1301 }
specify { @p.parse('thirteen hundred thirteen').should == 1313 }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment