configuration.scm 3.4 KB

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