gnu: tryton applications and framework: Update to 6.0.x.
[jackhill/guix/guix.git] / tests / services / configuration.scm
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>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
19
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))
25
26 ;;; Tests for the (gnu services configuration) module.
27
28 (test-begin "services-configuration")
29
30 \f
31 ;;;
32 ;;; define-configuration macro.
33 ;;;
34
35 (define-configuration port-configuration
36 (port (number 80) "The port number.")
37 (no-serialization))
38
39 (test-equal "default value, no serialization"
40 80
41 (port-configuration-port (port-configuration)))
42
43 (define-configuration port-configuration-cs
44 (port (number 80) "The port number." empty-serializer))
45
46 (test-equal "default value, custom serializer"
47 80
48 (port-configuration-cs-port (port-configuration-cs)))
49
50 (define serialize-number "")
51 (define-configuration port-configuration-ndv
52 (port (number) "The port number."))
53
54 (test-equal "no default value, provided"
55 55
56 (port-configuration-ndv-port (port-configuration-ndv
57 (port 55))))
58
59 (test-assert "no default value, not provided"
60 (guard (c ((configuration-error? c)
61 #t))
62 (port-configuration-ndv-port (port-configuration-ndv))))
63
64 (define (custom-number-serializer name value)
65 (format #f "~a = ~a;" name value))
66
67 (define-configuration serializable-configuration
68 (port (number 80) "The port number." custom-number-serializer))
69
70 (test-assert "serialize-configuration"
71 (gexp?
72 (let ((config (serializable-configuration)))
73 (serialize-configuration config serializable-configuration-fields))))
74
75 (define-configuration serializable-configuration
76 (port (number 80) "The port number." custom-number-serializer)
77 (no-serialization))
78
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)))))
85
86 (define (custom-prefix-serialize-integer field-name name) name)
87
88 (define-configuration configuration-with-prefix
89 (port (integer 10) "The port number.")
90 (prefix custom-prefix-))
91
92 (test-assert "serialize-configuration with prefix"
93 (gexp?
94 (let ((config (configuration-with-prefix)))
95 (serialize-configuration config configuration-with-prefix-fields))))
96
97 \f
98 ;;;
99 ;;; define-maybe macro.
100 ;;;
101 (define-maybe number)
102
103 (define-configuration config-with-maybe-number
104 (port (maybe-number 80) "The port number."))
105
106 (define (serialize-number field value)
107 (format #f "~a=~a" field value))
108
109 (test-equal "maybe value serialization"
110 "port=80"
111 (serialize-maybe-number "port" 80))
112
113 (define-maybe/no-serialization string)
114
115 (define-configuration config-with-maybe-string/no-serialization
116 (name (maybe-string) "The name of the item.")
117 (no-serialization))
118
119 (test-assert "maybe value without serialization no procedure bound"
120 (not (defined? 'serialize-maybe-string)))