Skip to content

Commit 6d4cb05

Browse files
Copilotjackfirth
andcommitted
Simplify syntax-path->string and enhance error test assertions
Co-authored-by: jackfirth <[email protected]>
1 parent 2045eee commit 6d4cb05

File tree

1 file changed

+53
-18
lines changed

1 file changed

+53
-18
lines changed

private/syntax-path.rkt

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,11 @@
218218

219219

220220
(define (syntax-path->string path)
221-
(if (empty-syntax-path? path)
222-
"/"
223-
(string-append
224-
"/"
225-
(string-join
226-
(for/list ([elem (in-treelist (syntax-path-elements path))])
227-
(number->string elem))
228-
"/"))))
221+
(string-join
222+
(for/list ([elem (in-treelist (syntax-path-elements path))])
223+
(number->string elem))
224+
"/"
225+
#:before-first "/"))
229226

230227

231228
(module+ test
@@ -284,24 +281,62 @@
284281
(check-equal? (string->syntax-path "/42/99/1000") (syntax-path (list 42 99 1000))))
285282

286283
(test-case "error on missing leading slash"
287-
(check-exn exn:fail:contract?
288-
(λ () (string->syntax-path "0/1/2"))))
284+
(define input "0/1/2")
285+
(define thrown
286+
(with-handlers ([(λ (_) #true) values])
287+
(string->syntax-path input)
288+
#false))
289+
(check-pred exn:fail:contract? thrown)
290+
(check-regexp-match #rx"string->syntax-path:" (exn-message thrown))
291+
(check-regexp-match #rx"given: \"0/1/2\"" (exn-message thrown))
292+
(check-regexp-match #rx"syntax path string must start with /" (exn-message thrown)))
289293

290294
(test-case "error on trailing slash"
291-
(check-exn exn:fail:contract?
292-
(λ () (string->syntax-path "/0/1/"))))
295+
(define input "/0/1/")
296+
(define thrown
297+
(with-handlers ([(λ (_) #true) values])
298+
(string->syntax-path input)
299+
#false))
300+
(check-pred exn:fail:contract? thrown)
301+
(check-regexp-match #rx"string->syntax-path:" (exn-message thrown))
302+
(check-regexp-match #rx"given: \"/0/1/\"" (exn-message thrown))
303+
(check-regexp-match #rx"syntax path string must not end with /" (exn-message thrown)))
293304

294305
(test-case "error on invalid element"
295-
(check-exn exn:fail:contract?
296-
(λ () (string->syntax-path "/0/abc/2"))))
306+
(define input "/0/abc/2")
307+
(define thrown
308+
(with-handlers ([(λ (_) #true) values])
309+
(string->syntax-path input)
310+
#false))
311+
(check-pred exn:fail:contract? thrown)
312+
(check-regexp-match #rx"string->syntax-path:" (exn-message thrown))
313+
(check-regexp-match #rx"given: \"/0/abc/2\"" (exn-message thrown))
314+
(check-regexp-match #rx"syntax path string contains invalid element" (exn-message thrown))
315+
(check-regexp-match #rx"invalid element: \"abc\"" (exn-message thrown)))
297316

298317
(test-case "error on negative number"
299-
(check-exn exn:fail:contract?
300-
(λ () (string->syntax-path "/0/-1/2"))))
318+
(define input "/0/-1/2")
319+
(define thrown
320+
(with-handlers ([(λ (_) #true) values])
321+
(string->syntax-path input)
322+
#false))
323+
(check-pred exn:fail:contract? thrown)
324+
(check-regexp-match #rx"string->syntax-path:" (exn-message thrown))
325+
(check-regexp-match #rx"given: \"/0/-1/2\"" (exn-message thrown))
326+
(check-regexp-match #rx"syntax path string contains invalid element" (exn-message thrown))
327+
(check-regexp-match #rx"invalid element: \"-1\"" (exn-message thrown)))
301328

302329
(test-case "error on float"
303-
(check-exn exn:fail:contract?
304-
(λ () (string->syntax-path "/0/1.5/2"))))))
330+
(define input "/0/1.5/2")
331+
(define thrown
332+
(with-handlers ([(λ (_) #true) values])
333+
(string->syntax-path input)
334+
#false))
335+
(check-pred exn:fail:contract? thrown)
336+
(check-regexp-match #rx"string->syntax-path:" (exn-message thrown))
337+
(check-regexp-match #rx"given: \"/0/1.5/2\"" (exn-message thrown))
338+
(check-regexp-match #rx"syntax path string contains invalid element" (exn-message thrown))
339+
(check-regexp-match #rx"invalid element: \"1.5\"" (exn-message thrown)))))
305340

306341

307342
(module+ test

0 commit comments

Comments
 (0)