1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
3 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (tests services configuration)
21 #:use-module (gnu services configuration)
22 #:use-module (guix gexp)
23 #:use-module (srfi srfi-34)
24 #:use-module (srfi srfi-64))
26 ;;; Tests for the (gnu services configuration) module.
28 (test-begin "services-configuration")
32 ;;; define-configuration macro.
35 (define-configuration port-configuration
36 (port (number 80) "The port number.")
39 (test-equal "default value, no serialization"
41 (port-configuration-port (port-configuration)))
43 (define-configuration port-configuration-cs
44 (port (number 80) "The port number." empty-serializer))
46 (test-equal "default value, custom serializer"
48 (port-configuration-cs-port (port-configuration-cs)))
50 (define serialize-number "")
51 (define-configuration port-configuration-ndv
52 (port (number) "The port number."))
54 (test-equal "no default value, provided"
56 (port-configuration-ndv-port (port-configuration-ndv
59 (test-assert "no default value, not provided"
60 (guard (c ((configuration-error? c)
62 (port-configuration-ndv-port (port-configuration-ndv))))
64 (define (custom-number-serializer name value)
65 (format #f "~a = ~a;" name value))
67 (define-configuration serializable-configuration
68 (port (number 80) "The port number." custom-number-serializer))
70 (test-assert "serialize-configuration"
72 (let ((config (serializable-configuration)))
73 (serialize-configuration config serializable-configuration-fields))))
75 (define-configuration serializable-configuration
76 (port (number 80) "The port number." custom-number-serializer)
79 (test-assert "serialize-configuration with no-serialization"
80 ;; When serialization is disabled, the serializer is set to #f, so
81 ;; attempting to use it fails with a 'wrong-type-arg' error.
82 (not (false-if-exception
83 (let ((config (serializable-configuration)))
84 (serialize-configuration config serializable-configuration-fields)))))
86 (define (custom-prefix-serialize-integer field-name name) name)
88 (define-configuration configuration-with-prefix
89 (port (integer 10) "The port number.")
90 (prefix custom-prefix-))
92 (test-assert "serialize-configuration with prefix"
94 (let ((config (configuration-with-prefix)))
95 (serialize-configuration config configuration-with-prefix-fields))))
99 ;;; define-maybe macro.
101 (define-maybe number)
103 (define-configuration config-with-maybe-number
104 (port (maybe-number 80) "The port number."))
106 (define (serialize-number field value)
107 (format #f "~a=~a" field value))
109 (test-equal "maybe value serialization"
111 (serialize-maybe-number "port" 80))
113 (define-maybe/no-serialization string)
115 (define-configuration config-with-maybe-string/no-serialization
116 (name (maybe-string) "The name of the item.")
119 (test-assert "maybe value without serialization no procedure bound"
120 (not (defined? 'serialize-maybe-string)))