From f687e27e0385c7f9bab8d967293061158fc3f504 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Tue, 2 Aug 2022 11:57:39 +0200 Subject: read-print: Read and render vertical space. * guix/read-print.scm (, vertical-space?) (vertical-space, vertical-space-height): New variables. (combine-vertical-space, canonicalize-vertical-space) (read-vertical-space): New procedures. (read-with-comments): Use it in the #\newline case. (pretty-print-with-comments): Add #:format-vertical-space and honor it. Add case for 'vertical-space?'. * guix/scripts/style.scm (format-package-definition): Pass #:format-vertical-space to 'object->string*'. * tests/read-print.scm ("read-with-comments: list with blank line") ("read-with-comments: list with multiple blank lines") ("read-with-comments: top-level blank lines") ("pretty-print-with-comments, canonicalize-vertical-space"): New tests. Add a couple of additional round-trip tests. --- tests/read-print.scm | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) (limited to 'tests/read-print.scm') diff --git a/tests/read-print.scm b/tests/read-print.scm index e9ba1127d4..f915b7e2d2 100644 --- a/tests/read-print.scm +++ b/tests/read-print.scm @@ -19,7 +19,8 @@ (define-module (tests-style) #:use-module (guix read-print) #:use-module (guix gexp) ;for the reader extensions - #:use-module (srfi srfi-64)) + #:use-module (srfi srfi-64) + #:use-module (ice-9 match)) (define-syntax-rule (test-pretty-print str args ...) "Test equality after a round-trip where STR is passed to @@ -40,6 +41,35 @@ (call-with-input-string "(a . b)" read-with-comments)) +(test-equal "read-with-comments: list with blank line" + `(list with ,(vertical-space 1) blank line) + (call-with-input-string "\ +(list with + + blank line)\n" + read-with-comments)) + +(test-equal "read-with-comments: list with multiple blank lines" + `(list with ,(comment ";multiple\n" #t) + ,(vertical-space 3) blank lines) + (call-with-input-string "\ +(list with ;multiple + + + + blank lines)\n" + read-with-comments)) + +(test-equal "read-with-comments: top-level blank lines" + (list (vertical-space 2) '(a b c) (vertical-space 2)) + (call-with-input-string " + +(a b c)\n\n" + (lambda (port) + (list (read-with-comments port) + (read-with-comments port) + (read-with-comments port))))) + (test-pretty-print "(list 1 2 3 4)") (test-pretty-print "((a . 1) (b . 2))") (test-pretty-print "(a b c . boom)") @@ -181,6 +211,24 @@ mnopqrstuvwxyz.\")" `(cons \"--without-any-problem\" ,flags)))") +(test-pretty-print "\ +(vertical-space one: + + two: + + + three: + + + + end)") + +(test-pretty-print "\ +(vertical-space one + + ;; Comment after blank line. + two)") + (test-equal "pretty-print-with-comments, canonicalize-comment" "\ (list abc @@ -206,4 +254,30 @@ mnopqrstuvwxyz.\")" #:format-comment canonicalize-comment))))) +(test-equal "pretty-print-with-comments, canonicalize-vertical-space" + "\ +(list abc + + def + + ;; last one + ghi)" + (let ((sexp (call-with-input-string + "\ +(list abc + + + + def + + +;; last one + ghi)" + read-with-comments))) + (call-with-output-string + (lambda (port) + (pretty-print-with-comments port sexp + #:format-vertical-space + canonicalize-vertical-space))))) + (test-end) -- cgit 1.4.1