|
36 | 36 | racket/syntax) |
37 | 37 | data/order |
38 | 38 | guard |
| 39 | + racket/format |
39 | 40 | racket/mutability |
40 | 41 | racket/sequence |
41 | 42 | racket/string |
|
52 | 53 |
|
53 | 54 | (module+ test |
54 | 55 | (require (submod "..") |
| 56 | + racket/format |
55 | 57 | racket/syntax |
56 | 58 | rackunit)) |
57 | 59 |
|
|
61 | 63 | (struct syntax-path (elements) |
62 | 64 | #:transparent |
63 | 65 | #:sealed |
64 | | - #:guard (λ (elements _) (sequence->treelist elements))) |
| 66 | + #:guard (λ (elements _) (sequence->treelist elements)) |
| 67 | + |
| 68 | + #:methods gen:custom-write |
| 69 | + [(define (write-proc this out mode) |
| 70 | + (write-string "#<syntax-path:" out) |
| 71 | + (write-string (syntax-path->string this) out) |
| 72 | + (write-string ">" out))]) |
65 | 73 |
|
66 | 74 |
|
67 | 75 | (define empty-syntax-path (syntax-path (treelist))) |
68 | 76 |
|
69 | 77 |
|
| 78 | +(module+ test |
| 79 | + (test-case "syntax-path custom printing" |
| 80 | + |
| 81 | + (test-case "empty path prints as #<syntax-path:/>" |
| 82 | + (check-equal? (~a empty-syntax-path) "#<syntax-path:/>") |
| 83 | + (check-equal? (~v empty-syntax-path) "#<syntax-path:/>") |
| 84 | + (check-equal? (~s empty-syntax-path) "#<syntax-path:/>")) |
| 85 | + |
| 86 | + (test-case "single element path prints compactly" |
| 87 | + (define path (syntax-path (list 0))) |
| 88 | + (check-equal? (~a path) "#<syntax-path:/0>") |
| 89 | + (check-equal? (~v path) "#<syntax-path:/0>")) |
| 90 | + |
| 91 | + (test-case "multiple element path prints with slashes" |
| 92 | + (define path (syntax-path (list 1 2 3 4))) |
| 93 | + (check-equal? (~a path) "#<syntax-path:/1/2/3/4>") |
| 94 | + (check-equal? (~v path) "#<syntax-path:/1/2/3/4>")) |
| 95 | + |
| 96 | + (test-case "path with large numbers" |
| 97 | + (define path (syntax-path (list 42 99 1000))) |
| 98 | + (check-equal? (~a path) "#<syntax-path:/42/99/1000>")))) |
| 99 | + |
| 100 | + |
70 | 101 | (define (empty-syntax-path? v) |
71 | 102 | (and (syntax-path? v) (treelist-empty? (syntax-path-elements v)))) |
72 | 103 |
|
|
0 commit comments