fix enum printer for small enumerations

This commit is contained in:
Robby Findler 2014-11-26 11:35:26 -06:00
parent c79a5573f9
commit 3e9f23a2c8
2 changed files with 37 additions and 29 deletions

View File

@ -205,11 +205,15 @@
[(#f) display]
[else (lambda (p port) (print p port mode))]))
(display "#<enum" port)
(define the-size (enum-size enum))
(define more-to-go?
(parameterize ([enum-printing (+ (enum-printing) 1)])
(let loop ([i 0] [chars 0])
;; chars is an approximation on how much
;; we've printed so far.
(when (<= chars 20)
(cond
[(not (< i the-size)) #f]
[(<= chars 20)
(define ele (from-nat enum i))
(define sp (open-output-string))
(recur ele sp)
@ -219,7 +223,7 @@
;; if any enumeration values print as empty
;; strings, then we just give up so as to avoid
;; 'i' never incrementing and never terminating
(display ">" port)]
#t]
[else
(if (zero? i)
(display ": " port)
@ -235,8 +239,11 @@
(recur ele port)])
(loop (+ i 1)
(+ chars (string-length s) 1))]))))
(display "...>" port))])
(+ chars (string-length s) 1))])]
[else #t]))))
(if more-to-go?
(display "...>" port)
(display ">" port)))])
;; size : enum a -> Nat or +Inf
(define (size e)

View File

@ -468,6 +468,7 @@
(check-equal? (to-str nat/e #t) "#<enum: 0 1 2 3 4 5 6 7 8 9 10...>")
(check-equal? (to-str (cons/e nat/e nat/e) #t) "#<enum: '(0 . 0) '(0 . 1) '(1 . 0)...>")
(check-equal? (to-str (cons/e nat/e nat/e) #f) "#<enum: (0 . 0) (0 . 1) (1 . 0)...>")
(check-equal? (to-str (const/e 0) #t) "#<enum: 0>")
;; just check that it doesn't crash when we get deep nesting
;; (checks that we end up in the case that just uses the string