|
1 |
| -local PhoneNumber = require('phone-number') |
| 1 | +local phone_number = require('phone-number') |
2 | 2 |
|
3 | 3 | describe('phone-number', function()
|
4 |
| - it('cleans the number (223) 456-7890', function() |
5 |
| - local phone = PhoneNumber:new('(223) 456-7890') |
6 |
| - assert.are.equals('2234567890', phone.number) |
| 4 | + it('cleans the number', function() |
| 5 | + assert.are.equal('2234567890', phone_number.clean('(223) 456-7890')) |
7 | 6 | end)
|
8 | 7 |
|
9 | 8 | it('cleans numbers with dots', function()
|
10 |
| - local phone = PhoneNumber:new('223.456.7890') |
11 |
| - assert.are.equals('2234567890', phone.number) |
| 9 | + assert.are.equal('2234567890', phone_number.clean('223.456.7890')) |
12 | 10 | end)
|
13 | 11 |
|
14 |
| - it('valid when 11 digits and first digit is 1', function() |
15 |
| - local phone = PhoneNumber:new('12234567890') |
16 |
| - assert.are.equals('2234567890', phone.number) |
| 12 | + it('cleans numbers with multiple spaces', function() |
| 13 | + assert.are.equal('2234567890', phone_number.clean('223 456 7890 ')) |
17 | 14 | end)
|
18 | 15 |
|
19 |
| - it('invalid when 11 digits', function() |
20 |
| - local phone = PhoneNumber:new('22234567890') |
21 |
| - assert.are.equals('0000000000', phone.number) |
| 16 | + it('invalid when 9 digits', function() |
| 17 | + assert.has_error(function() |
| 18 | + phone_number.clean('123456789') |
| 19 | + end) |
22 | 20 | end)
|
23 | 21 |
|
24 |
| - it('invalid when 9 digits', function() |
25 |
| - local phone = PhoneNumber:new('223456789') |
26 |
| - assert.are.equals('0000000000', phone.number) |
| 22 | + it('invalid when 11 digits does not start with a 1', function() |
| 23 | + assert.has_error(function() |
| 24 | + phone_number.clean('22234567890') |
| 25 | + end) |
| 26 | + end) |
| 27 | + |
| 28 | + it('valid when 11 digits and starting with 1', function() |
| 29 | + assert.are.equal('2234567890', phone_number.clean('12234567890')) |
| 30 | + end) |
| 31 | + |
| 32 | + it('valid when 11 digits and starting with 1 even with punctuation', function() |
| 33 | + assert.are.equal('2234567890', phone_number.clean('+1 (223) 456-7890')) |
| 34 | + end) |
| 35 | + |
| 36 | + it('invalid when more than 11 digits', function() |
| 37 | + assert.has_error(function() |
| 38 | + phone_number.clean('321234567890') |
| 39 | + end) |
| 40 | + end) |
| 41 | + |
| 42 | + it('invalid with letters', function() |
| 43 | + assert.has_error(function() |
| 44 | + phone_number.clean('523-abc-7890') |
| 45 | + end) |
| 46 | + end) |
| 47 | + |
| 48 | + it('invalid with punctuations', function() |
| 49 | + assert.has_error(function() |
| 50 | + phone_number.clean('523-@:!-7890') |
| 51 | + end) |
| 52 | + end) |
| 53 | + |
| 54 | + it('invalid if area code starts with 0', function() |
| 55 | + assert.has_error(function() |
| 56 | + phone_number.clean('(023) 456-7890') |
| 57 | + end) |
| 58 | + end) |
| 59 | + |
| 60 | + it('invalid if area code starts with 1', function() |
| 61 | + assert.has_error(function() |
| 62 | + phone_number.clean('(123) 456-7890') |
| 63 | + end) |
| 64 | + end) |
| 65 | + |
| 66 | + it('invalid if exchange code starts with 0', function() |
| 67 | + assert.has_error(function() |
| 68 | + phone_number.clean('(223) 056-7890') |
| 69 | + end) |
| 70 | + end) |
| 71 | + |
| 72 | + it('invalid if exchange code starts with 1', function() |
| 73 | + assert.has_error(function() |
| 74 | + phone_number.clean('(223) 156-7890') |
| 75 | + end) |
| 76 | + end) |
| 77 | + |
| 78 | + it('invalid if area code starts with 0 on valid 11-digit number', function() |
| 79 | + assert.has_error(function() |
| 80 | + phone_number.clean('1 (023) 456-7890') |
| 81 | + end) |
| 82 | + end) |
| 83 | + |
| 84 | + it('invalid if area code starts with 1 on valid 11-digit number', function() |
| 85 | + assert.has_error(function() |
| 86 | + phone_number.clean('1 (123) 456-7890') |
| 87 | + end) |
27 | 88 | end)
|
28 | 89 |
|
29 |
| - it('has an area code', function() |
30 |
| - local phone = PhoneNumber:new('2234567890') |
31 |
| - assert.are.equals('223', phone:areaCode()) |
| 90 | + it('invalid if exchange code starts with 0 on valid 11-digit number', function() |
| 91 | + assert.has_error(function() |
| 92 | + phone_number.clean('1 (223) 056-7890') |
| 93 | + end) |
32 | 94 | end)
|
33 | 95 |
|
34 |
| - it('formats a number', function() |
35 |
| - local phone = PhoneNumber:new('2234567890') |
36 |
| - assert.are.equals('(223) 456-7890', tostring(phone)) |
| 96 | + it('invalid if exchange code starts with 1 on valid 11-digit number', function() |
| 97 | + assert.has_error(function() |
| 98 | + phone_number.clean('1 (223) 156-7890') |
| 99 | + end) |
37 | 100 | end)
|
38 | 101 | end)
|
0 commit comments