1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2021-2022 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 "\
147 ;; This is a procedure.
151 (test-pretty-print "\
158 (test-pretty-print "\
166 (test-pretty-print "\
167 #~(string-append #$coreutils \"/bin/uname\")")
169 (test-pretty-print "\
174 (test-pretty-print "\
175 (modify-phases %standard-phases
176 (add-after 'unpack 'post-unpack
179 (add-before 'check 'pre-check
180 (lambda* (#:key inputs #:allow-other-keys)
183 (test-pretty-print "\
184 (#:phases (modify-phases sdfsdf
189 (test-pretty-print "\
190 (string-append \"a\\tb\" \"\\n\")")
192 (test-pretty-print "\
193 (description \"abcdefghijkl
197 (test-pretty-print "\
203 (test-pretty-print "\
205 \"abcdefghijklmnopqrstuvwxyz\")"
208 (test-pretty-print "\
209 (modify-phases %standard-phases
211 ;; Nicely indented in 'modify-phases' context.
215 (test-pretty-print "\
216 (modify-inputs inputs
217 ;; Regular indentation for 'replace' here.
218 (replace \"gmp\" gmp))")
220 (test-pretty-print "\
222 ;; Here 'sha256', 'base32', and 'arguments' must be
223 ;; immediately followed by a newline.
228 \"not a real base32 string\"))))
230 '(#:phases %standard-phases
233 ;; '#:key value' is kept on the same line.
234 (test-pretty-print "\
236 (name \"keyword-value-same-line\")
238 (list #:phases #~(modify-phases %standard-phases
240 (lambda* (#:key inputs #:allow-other-keys)
242 #:make-flags #~'(\"ANSWER=42\")
245 (test-pretty-print "\
253 (test-pretty-print "\
255 (chmod \"foo\" #o750)
261 (test-pretty-print "\
262 (substitute-keyword-arguments (package-arguments x)
264 `(modify-phases ,phases
265 (add-before 'build 'do-things
268 ((#:configure-flags flags)
269 `(cons \"--without-any-problem\"
272 (test-pretty-print "\
284 (test-pretty-print "\
287 ;; Comment after blank line.
290 (test-pretty-print "\
297 (test-pretty-print "\
300 (list (service-type home-bash-service-type))))")
302 (test-pretty-print/sequence "\
303 ;;; This is a top-level comment.
306 ;; Above is a page break.
313 (test-pretty-print/sequence "
315 ;;; Notice that there are three semicolons here.
317 (define-module (foo bar)
324 (host-name \"komputilo\")
325 (locale \"eo_EO.UTF-8\")
328 (cons (service mcron-service-type) %base-services)))\n"
329 #:format-comment canonicalize-comment)
331 (test-equal "pretty-print-with-comments, canonicalize-comment"
334 ;; Not a margin comment.
337 ;; There's a blank line above.
340 (let ((sexp (call-with-input-string
343 ;Not a margin comment.
346 ; There's a blank line above.
347 def ;; margin comment
349 read-with-comments)))
350 (call-with-output-string
352 (pretty-print-with-comments port sexp
354 canonicalize-comment)))))
356 (test-equal "pretty-print-with-comments, canonicalize-vertical-space"
364 (let ((sexp (call-with-input-string
375 read-with-comments)))
376 (call-with-output-string
378 (pretty-print-with-comments port sexp
379 #:format-vertical-space
380 canonicalize-vertical-space)))))
382 (test-equal "pretty-print-with-comments, multi-line comment"
385 ;; This comment spans
388 (call-with-output-string
390 (pretty-print-with-comments port
391 `(list abc ,(comment "\
392 ;; This comment spans\n