Skip to content

Commit f9de53a

Browse files
authored
Update phone-number tests (#589)
1 parent 5a76d03 commit f9de53a

File tree

5 files changed

+149
-41
lines changed

5 files changed

+149
-41
lines changed
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
local PhoneNumber = {}
1+
local function clean(s)
2+
s = s:gsub('[()-. ]', '')
23

3-
function PhoneNumber:new(string_or_number)
4-
self.__index = self
5-
local n = string.gsub(string_or_number, '%D', '')
6-
n = n:match '^1?([2-9]%d%d[2-9]%d%d%d%d%d%d)$' or '0000000000'
4+
assert(not s:match('%D'))
5+
assert(#s == 10 or #s == 11)
6+
assert(#s ~= 11 or s:sub(1, 1) == '1')
77

8-
return setmetatable({ number = n }, self)
9-
end
8+
s = s:sub(#s - 9)
109

11-
function PhoneNumber:areaCode()
12-
return self.number:sub(1, 3)
13-
end
10+
local area_code, exchange_code, line_number = s:match('(...)(...)(....)')
11+
12+
assert(not area_code:match('^[01]'))
13+
assert(not exchange_code:match('^[01]'))
1414

15-
function PhoneNumber:__tostring()
16-
local phone_format = '(%d) %d-%d'
17-
local area_code, central_office_code, station_number = self:areaCode(), self.number:sub(4, 6), self.number:sub(7, 10)
18-
return phone_format:format(area_code, central_office_code, station_number)
15+
return area_code .. exchange_code .. line_number
1916
end
2017

21-
return PhoneNumber
18+
return { clean = clean }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
return {
2+
module_name = 'phone_number',
3+
4+
generate_test = function(case)
5+
if case.expected.error then
6+
local template = [[
7+
assert.has_error(function()
8+
phone_number.clean('%s')
9+
end)]]
10+
11+
return template:format(case.input.phrase)
12+
else
13+
local template = [[
14+
assert.are.equal('%s', phone_number.clean('%s'))]]
15+
16+
return template:format(case.expected, case.input.phrase)
17+
end
18+
end
19+
}

exercises/practice/phone-number/.meta/tests.toml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[79666dce-e0f1-46de-95a1-563802913c35]
613
description = "cleans the number"
@@ -13,6 +20,11 @@ description = "cleans numbers with multiple spaces"
1320

1421
[598d8432-0659-4019-a78b-1c6a73691d21]
1522
description = "invalid when 9 digits"
23+
include = false
24+
25+
[2de74156-f646-42b5-8638-0ef1d8b58bc2]
26+
description = "invalid when 9 digits"
27+
reimplements = "598d8432-0659-4019-a78b-1c6a73691d21"
1628

1729
[57061c72-07b5-431f-9766-d97da7c4399d]
1830
description = "invalid when 11 digits does not start with a 1"
@@ -25,12 +37,27 @@ description = "valid when 11 digits and starting with 1 even with punctuation"
2537

2638
[c6a5f007-895a-4fc5-90bc-a7e70f9b5cad]
2739
description = "invalid when more than 11 digits"
40+
include = false
41+
42+
[4a1509b7-8953-4eec-981b-c483358ff531]
43+
description = "invalid when more than 11 digits"
44+
reimplements = "c6a5f007-895a-4fc5-90bc-a7e70f9b5cad"
2845

2946
[63f38f37-53f6-4a5f-bd86-e9b404f10a60]
3047
description = "invalid with letters"
48+
include = false
49+
50+
[eb8a1fc0-64e5-46d3-b0c6-33184208e28a]
51+
description = "invalid with letters"
52+
reimplements = "63f38f37-53f6-4a5f-bd86-e9b404f10a60"
3153

3254
[4bd97d90-52fd-45d3-b0db-06ab95b1244e]
3355
description = "invalid with punctuations"
56+
include = false
57+
58+
[065f6363-8394-4759-b080-e6c8c351dd1f]
59+
description = "invalid with punctuations"
60+
reimplements = "4bd97d90-52fd-45d3-b0db-06ab95b1244e"
3461

3562
[d77d07f8-873c-4b17-8978-5f66139bf7d7]
3663
description = "invalid if area code starts with 0"
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
local PhoneNumber = {}
1+
local function clean(s)
22

3-
return PhoneNumber
3+
end
4+
5+
return { clean = clean }
Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,101 @@
1-
local PhoneNumber = require('phone-number')
1+
local phone_number = require('phone-number')
22

33
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'))
76
end)
87

98
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'))
1210
end)
1311

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 '))
1714
end)
1815

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)
2220
end)
2321

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)
2788
end)
2889

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)
3294
end)
3395

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)
37100
end)
38101
end)

0 commit comments

Comments
 (0)