configuration.scm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. (define-module (tests services configuration)
  20. #:use-module (gnu services configuration)
  21. #:use-module (guix gexp)
  22. #:use-module (srfi srfi-34)
  23. #:use-module (srfi srfi-64))
  24. ;;; Tests for the (gnu services configuration) module.
  25. (test-begin "services-configuration")
  26. ;;;
  27. ;;; define-configuration macro.
  28. ;;;
  29. (define-configuration port-configuration
  30. (port (number 80) "The port number.")
  31. (no-serialization))
  32. (test-equal "default value, no serialization"
  33. 80
  34. (port-configuration-port (port-configuration)))
  35. (define-configuration port-configuration-cs
  36. (port (number 80) "The port number." empty-serializer))
  37. (test-equal "default value, custom serializer"
  38. 80
  39. (port-configuration-cs-port (port-configuration-cs)))
  40. (define serialize-number "")
  41. (define-configuration port-configuration-ndv
  42. (port (number) "The port number."))
  43. (test-equal "no default value, provided"
  44. 55
  45. (port-configuration-ndv-port (port-configuration-ndv
  46. (port 55))))
  47. (test-assert "no default value, not provided"
  48. (guard (c ((configuration-error? c)
  49. #t))
  50. (port-configuration-ndv-port (port-configuration-ndv))))
  51. (define (custom-number-serializer name value)
  52. (format #f "~a = ~a;" name value))
  53. (define-configuration serializable-configuration
  54. (port (number 80) "The port number." custom-number-serializer))
  55. (test-assert "serialize-configuration"
  56. (gexp?
  57. (let ((config (serializable-configuration)))
  58. (serialize-configuration config serializable-configuration-fields))))
  59. (define-configuration serializable-configuration
  60. (port (number 80) "The port number." custom-number-serializer)
  61. (no-serialization))
  62. (test-assert "serialize-configuration with no-serialization"
  63. ;; When serialization is disabled, the serializer is set to #f, so
  64. ;; attempting to use it fails with a 'wrong-type-arg' error.
  65. (not (false-if-exception
  66. (let ((config (serializable-configuration)))
  67. (serialize-configuration config serializable-configuration-fields)))))
  68. (define (custom-prefix-serialize-integer field-name name) name)
  69. (define-configuration configuration-with-prefix
  70. (port (integer 10) "The port number.")
  71. (prefix custom-prefix-))
  72. (test-assert "serialize-configuration with prefix"
  73. (gexp?
  74. (let ((config (configuration-with-prefix)))
  75. (serialize-configuration config configuration-with-prefix-fields))))
  76. ;;;
  77. ;;; define-maybe macro.
  78. ;;;
  79. (define-maybe number)
  80. (define-configuration config-with-maybe-number
  81. (port (maybe-number 80) "The port number."))
  82. (define (serialize-number field value)
  83. (format #f "~a=~a" field value))
  84. (test-equal "maybe value serialization"
  85. "port=80"
  86. (serialize-maybe-number "port" 80))
  87. (define-maybe/no-serialization string)
  88. (define-configuration config-with-maybe-string/no-serialization
  89. (name (maybe-string) "The name of the item.")
  90. (no-serialization))
  91. (test-assert "maybe value without serialization no procedure bound"
  92. (not (defined? 'serialize-maybe-string)))