1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2021-2023 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (tests-style)
20 #:use-module (guix read-print)
21 #:use-module (guix gexp) ;for the reader extensions
22 #:use-module (srfi srfi-34)
23 #:use-module (srfi srfi-35)
24 #:use-module (srfi srfi-64)
25 #:use-module (ice-9 match))
27 (define-syntax-rule (test-pretty-print str args ...)
28 "Test equality after a round-trip where STR is passed to
29 'read-with-comments' and the resulting sexp is then passed to
30 'pretty-print-with-comments'."
32 (call-with-output-string
34 (let ((exp (call-with-input-string str
36 (pretty-print-with-comments port exp args ...))))))
38 (define-syntax-rule (test-pretty-print/sequence str args ...)
39 "Likewise, but read and print entire sequences rather than individual
42 (call-with-output-string
44 (let ((lst (call-with-input-string str
45 read-with-comments/sequence)))
46 (pretty-print-with-comments/splice port lst args ...))))))
49 (test-begin "read-print")
51 (test-assert "read-with-comments: missing closing paren"
52 (guard (c ((error? c) #t))
53 (call-with-input-string "(what is going on?"
56 (test-equal "read-with-comments: dot notation"
58 (call-with-input-string "(a . b)"
61 (test-equal "read-with-comments: list with blank line"
62 `(list with ,(vertical-space 1) blank line)
63 (call-with-input-string "\
69 (test-equal "read-with-comments: list with multiple blank lines"
70 `(list with ,(comment ";multiple\n" #t)
71 ,(vertical-space 3) blank lines)
72 (call-with-input-string "\
80 (test-equal "read-with-comments: top-level blank lines"
81 (list (vertical-space 2) '(a b c) (vertical-space 2))
82 (call-with-input-string "
86 (list (read-with-comments port)
87 (read-with-comments port)
88 (read-with-comments port)))))
90 (test-equal "read-with-comments: top-level page break"
91 (list (comment ";; Begin.\n") (vertical-space 1)
93 (comment ";; End.\n"))
94 (call-with-input-string "\
100 (list (read-with-comments port)
101 (read-with-comments port)
102 (read-with-comments port)
103 (read-with-comments port)))))
105 (test-pretty-print "(list 1 2 3 4)")
106 (test-pretty-print "((a . 1) (b . 2))")
107 (test-pretty-print "(a b c . boom)")
108 (test-pretty-print "(list 1
114 (test-pretty-print "\
118 (test-pretty-print "\
123 (test-pretty-print "\
128 (test-pretty-print "\
137 (test-pretty-print "\
145 (test-pretty-print "\
150 (test-pretty-print "\
152 ;; This is a procedure.
156 (test-pretty-print "\
163 (test-pretty-print "\
171 (test-pretty-print "\
172 #~(string-append #$coreutils \"/bin/uname\")")
174 (test-pretty-print "\
179 (test-pretty-print "\
180 (modify-phases %standard-phases
181 (add-after 'unpack 'post-unpack
184 (add-before 'check 'pre-check
185 (lambda* (#:key inputs #:allow-other-keys)
188 (test-pretty-print "\
189 (#:phases (modify-phases sdfsdf
194 (test-pretty-print "\
195 (string-append \"a\\tb\" \"\\n\")")
197 (test-pretty-print "\
198 (description \"abcdefghijkl
202 (test-pretty-print "\
208 (test-pretty-print "\
210 \"abcdefghijklmnopqrstuvwxyz\")"
213 (test-pretty-print "\
214 (modify-phases %standard-phases
216 ;; Nicely indented in 'modify-phases' context.
220 (test-pretty-print "\
221 (modify-inputs inputs
222 ;; Regular indentation for 'replace' here.
223 (replace \"gmp\" gmp))")
225 (test-pretty-print "\
227 ;; Here 'sha256', 'base32', and 'arguments' must be
228 ;; immediately followed by a newline.
233 \"not a real base32 string\"))))
235 '(#:phases %standard-phases
238 ;; '#:key value' is kept on the same line.
239 (test-pretty-print "\
241 (name \"keyword-value-same-line\")
243 (list #:phases #~(modify-phases %standard-phases
245 (lambda* (#:key inputs #:allow-other-keys)
247 #:make-flags #~'(\"ANSWER=42\")
250 (test-pretty-print "\
258 (test-pretty-print "\
260 (chmod \"foo\" #o750)
266 (test-pretty-print "\
267 (substitute-keyword-arguments (package-arguments x)
269 `(modify-phases ,phases
270 (add-before 'build 'do-things
273 ((#:configure-flags flags)
274 `(cons \"--without-any-problem\"
277 (test-pretty-print "\
289 (test-pretty-print "\
292 ;; Comment after blank line.
295 (test-pretty-print "\
302 (test-pretty-print "\
305 (list (service-type home-bash-service-type))))")
307 (test-pretty-print/sequence "\
308 ;;; This is a top-level comment.
311 ;; Above is a page break.
318 (test-pretty-print/sequence "
320 ;;; Notice that there are three semicolons here.
322 (define-module (foo bar)
329 (host-name \"komputilo\")
330 (locale \"eo_EO.UTF-8\")
333 (cons (service mcron-service-type) %base-services)))\n"
334 #:format-comment canonicalize-comment)
336 (test-equal "pretty-print-with-comments, canonicalize-comment"
339 ;; Not a margin comment.
342 ;; There's a blank line above.
345 (let ((sexp (call-with-input-string
348 ;Not a margin comment.
351 ; There's a blank line above.
352 def ;; margin comment
354 read-with-comments)))
355 (call-with-output-string
357 (pretty-print-with-comments port sexp
359 canonicalize-comment)))))
361 (test-equal "pretty-print-with-comments, canonicalize-vertical-space"
369 (let ((sexp (call-with-input-string
380 read-with-comments)))
381 (call-with-output-string
383 (pretty-print-with-comments port sexp
384 #:format-vertical-space
385 canonicalize-vertical-space)))))
387 (test-equal "pretty-print-with-comments, multi-line comment"
390 ;; This comment spans
393 (call-with-output-string
395 (pretty-print-with-comments port
396 `(list abc ,(comment "\
397 ;; This comment spans\n