Skip to content

Commit 4cdb174

Browse files
committed
Revised the test and example files of the "Phone Number" exercise.
- The tests expecting errors now check custom errors instead of error messages. - The example test has been modified so that it passes the modified tests.
1 parent 1dc6672 commit 4cdb174

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

exercises/practice/phone-number/.meta/example.el

+19-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
;;; Commentary:
44

5+
(define-error 'short-phone-num-error "must not be fewer than 10 digits")
6+
(define-error 'long-phone-num-error "must not be greater than 11 digits")
7+
(define-error 'letters-in-phone-num-error "letters not permitted")
8+
(define-error 'punctuations-in-phone-num-error "punctuations not permitted")
9+
(define-error 'country-code-error "country code must be 1")
10+
(define-error 'area-code-starting-with-0-error "area code cannot start with zero")
11+
(define-error 'area-code-starting-with-1-error "area code cannot start with one")
12+
(define-error 'exchange-code-starting-with-0-error "exchange code cannot start with zero")
13+
(define-error 'exchange-code-starting-with-1-error "exchange code cannot start with one")
14+
515
(defun char-digit-p (x)
616
(<= ?0 x ?9))
717

@@ -42,30 +52,30 @@
4252
"Converts a num string into a string of digits."
4353
(cond
4454
((cl-find-if #'char-alphabetic-p num)
45-
(error "letters not permitted"))
55+
(signal 'letters-in-phone-num-error num))
4656
((cl-find-if #'char-punctuation-p num)
47-
(error "punctuations not permitted")))
57+
(signal 'punctuations-in-phone-num-error num)))
4858
(let* ((digits (string-remove num (negate #'char-digit-p)))
4959
(n (length digits)))
5060
(cond
51-
((< n 10) (error "must not be fewer than 10 digits"))
52-
((> n 11) (error "must not be greater than 11 digits")))
61+
((< n 10) (signal 'short-phone-num-error num))
62+
((> n 11) (signal 'long-phone-num-error num)))
5363
(if (= n 11)
5464
(if (= (aref digits 0) ?1)
5565
(setf digits (substring digits 1))
56-
(error "11 digits must start with 1")))
66+
(signal 'country-code-error num)))
5767
(let ((y (aref digits 0)))
5868
(cond
5969
((= y ?0)
60-
(error "area code cannot start with zero"))
70+
(signal 'area-code-starting-with-0-error num))
6171
((= y ?1)
62-
(error "area code cannot start with one"))))
72+
(signal 'area-code-starting-with-1-error num)))
6373
(let ((y (aref digits 3)))
6474
(cond
6575
((= y ?0)
66-
(error "exchange code cannot start with zero"))
76+
(signal 'exchange-code-starting-with-0-error num))
6777
((= y ?1)
68-
(error "exchange code cannot start with one"))))
78+
(signal 'exchange-code-starting-with-1-error num)))))
6979
digits))
7080

7181
(defun area-code (num)

exercises/practice/phone-number/phone-number-test.el

+13-26
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
(should (string= (numbers "223 456 7890 ") "2234567890")))
2020

2121
(ert-deftest invalid-when-9-digits ()
22-
(should (string= (cadr (should-error (numbers "123456789")))
23-
"must not be fewer than 10 digits")))
22+
(should-error (numbers "123456789") :type 'short-phone-num-error))
2423

2524
(ert-deftest invalid-when-11-digits-does-not-start-with-a-1 ()
26-
(should (string= (cadr (should-error (numbers "22234567890")))
27-
"11 digits must start with 1")))
25+
(should-error (numbers "22234567890") :type 'country-code-error))
2826

2927
(ert-deftest valid-when-11-digits-and-starting-with-1 ()
3028
(should (string= (numbers "12234567890") "2234567890")))
@@ -33,48 +31,37 @@
3331
(should (string= (numbers "+1 (223) 456-7890") "2234567890")))
3432

3533
(ert-deftest invalid-when-more-than-11-digits ()
36-
(should (string= (cadr (should-error (numbers "321234567890")))
37-
"must not be greater than 11 digits")))
34+
(should-error (numbers "321234567890") :type 'long-phone-num-error))
3835

3936
(ert-deftest invalid-with-letters ()
40-
(should (string= (cadr (should-error (numbers "523-abc-7890")))
41-
"letters not permitted")))
37+
(should-error (numbers "523-abc-7890") :type 'letters-in-phone-num-error))
4238

4339
(ert-deftest invalid-with-punctuations ()
44-
(should (string= (cadr (should-error (numbers "523-@:!-7890")))
45-
"punctuations not permitted")))
40+
(should-error (numbers "523-@:!-7890") :type 'punctuations-in-phone-num-error))
4641

4742
(ert-deftest invalid-if-area-code-starts-with-0 ()
48-
(should (string= (cadr (should-error (numbers "(023) 456-7890")))
49-
"area code cannot start with zero")))
43+
(should-error (numbers "(023) 456-7890") :type 'area-code-starting-with-0-error))
5044

5145
(ert-deftest invalid-if-area-code-starts-with-1 ()
52-
(should (string= (cadr (should-error (numbers "(123) 456-7890")))
53-
"area code cannot start with one")))
46+
(should-error (numbers "(123) 456-7890") :type 'area-code-starting-with-1-error))
5447

5548
(ert-deftest invalid-if-exchange-code-starts-with-0 ()
56-
(should (string= (cadr (should-error (numbers "(223) 056-7890")))
57-
"exchange code cannot start with zero")))
49+
(should-error (numbers "(223) 056-7890") :type 'exchange-code-starting-with-0-error))
5850

5951
(ert-deftest invalid-if-exchange-code-starts-with-1 ()
60-
(should (string= (cadr (should-error (numbers "(223) 156-7890")))
61-
"exchange code cannot start with one")))
52+
(should-error (numbers "(223) 156-7890") :type 'exchange-code-starting-with-1-error))
6253

6354
(ert-deftest invalid-if-area-code-starts-with-0-on-valid-11-digit-number ()
64-
(should (string= (cadr (should-error (numbers "1 (023) 456-7890")))
65-
"area code cannot start with zero")))
55+
(should-error (numbers "1 (023) 456-7890") :type 'area-code-starting-with-0-error))
6656

6757
(ert-deftest invalid-if-area-code-starts-with-1-on-valid-11-digit-number ()
68-
(should (string= (cadr (should-error (numbers "1 (123) 456-7890")))
69-
"area code cannot start with one")))
58+
(should-error (numbers "1 (123) 456-7890") :type 'area-code-starting-with-1-error))
7059

7160
(ert-deftest invalid-if-exchange-code-starts-with-0-on-valid-11-digit-number ()
72-
(should (string= (cadr (should-error (numbers "1 (223) 056-7890")))
73-
"exchange code cannot start with zero")))
61+
(should-error (numbers "1 (223) 056-7890") :type 'exchange-code-starting-with-0-error))
7462

7563
(ert-deftest invalid-if-exchange-code-starts-with-1-on-valid-11-digit-number ()
76-
(should (string= (cadr (should-error (numbers "1 (223) 156-7890")))
77-
"exchange code cannot start with one")))
64+
(should-error (numbers "1 (223) 156-7890") :type 'exchange-code-starting-with-1-error))
7865

7966
(ert-deftest area-code-test ()
8067
(should (equal (area-code "2234567890") "223")))

0 commit comments

Comments
 (0)