Skip to content

Instantly share code, notes, and snippets.

@sxross
Created January 13, 2013 23:54
Show Gist options
  • Save sxross/4526913 to your computer and use it in GitHub Desktop.
Save sxross/4526913 to your computer and use it in GitHub Desktop.
Spec for core_extensions.rb
# See https://gist.github.com/4526905 for the code this tests
describe "core helpers" do
it "has a 'to_us_phone' method" do
'hello'.should.respond_to? :to_us_phone
end
describe "7-digit phone numbers" do
it "formats string" do
'1112222'.to_us_phone.should == '111-2222'
end
it "formats phone number stored as a number" do
1112222.to_us_phone.should == '111-2222'
end
end
describe "10-digit phone numbers" do
it "formats string" do
'111-222-3333'.to_us_phone.should == '(111) 222-3333'
end
it "formats phone number stored as a number" do
1112223333.to_us_phone.should == '(111) 222-3333'
end
end
describe "long distance numbers" do
it "formats string" do
'1 111-222-3333'.to_us_phone.should == '+1 (111) 222-3333'
end
it "formats phone number stored as a number" do
11112223333.to_us_phone.should == '+1 (111) 222-3333'
end
end
describe "numbers in a class" do
class Foo
attr_reader :result
include MotionModel::Model
include MotionModel::Formotion
include MotionModel::Validatable
include Comparable
columns :foo_phone => :string
def before_save(o)
@result = o.foo_phone.to_us_phone
end
def get_number(num)
num.to_us_phone
end
end
it "get a phone number" do
Foo.new.get_number(11112223333).should == '+1 (111) 222-3333'
end
it "also gets one in callback" do
f = Foo.new(:foo_phone => '111-222-3333')
f.save
f.result.should == '(111) 222-3333'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment