srfi-37.test 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;;; srfi-37.test --- Test suite for SRFI 37 -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2007, 2008, 2013 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-srfi-37)
  19. #:use-module (test-suite lib)
  20. #:use-module (srfi srfi-37))
  21. (with-test-prefix "SRFI-37"
  22. (pass-if "empty calls with count-modified seeds"
  23. (equal? (list 21 42)
  24. (call-with-values
  25. (lambda ()
  26. (args-fold '("1" "3" "4") '()
  27. (lambda (opt name arg seed seed2)
  28. (values 1 2))
  29. (lambda (op seed seed2)
  30. (values (1+ seed) (+ 2 seed2)))
  31. 18 36))
  32. list)))
  33. (pass-if "short opt params"
  34. (let ((a-set #f) (b-set #f) (c-val #f) (d-val #f) (no-fail #t) (no-operands #t))
  35. (args-fold '("-abcdoit" "-ad" "whatev")
  36. (list (option '(#\a) #f #f (lambda (opt name arg)
  37. (set! a-set #t)
  38. (values)))
  39. (option '(#\b) #f #f (lambda (opt name arg)
  40. (set! b-set #t)
  41. (values)))
  42. (option '("cdoit" #\c) #f #t
  43. (lambda (opt name arg)
  44. (set! c-val arg)
  45. (values)))
  46. (option '(#\d) #f #t
  47. (lambda (opt name arg)
  48. (set! d-val arg)
  49. (values))))
  50. (lambda (opt name arg) (set! no-fail #f) (values))
  51. (lambda (oper) (set! no-operands #f) (values)))
  52. (equal? '(#t #t "doit" "whatev" #t #t)
  53. (list a-set b-set c-val d-val no-fail no-operands))))
  54. (pass-if "single unrecognized long-opt"
  55. (equal? "fake"
  56. (args-fold '("--fake" "-i2")
  57. (list (option '(#\i) #t #f
  58. (lambda (opt name arg k) k)))
  59. (lambda (opt name arg k) name)
  60. (lambda (operand k) #f)
  61. #f)))
  62. (pass-if "long req'd/optional"
  63. (equal? '(#f "bsquare" "apple")
  64. (args-fold '("--x=pple" "--y=square" "--y")
  65. (list (option '("x") #t #f
  66. (lambda (opt name arg k)
  67. (cons (string-append "a" arg) k)))
  68. (option '("y") #f #t
  69. (lambda (opt name arg k)
  70. (cons (if arg
  71. (string-append "b" arg)
  72. #f) k))))
  73. (lambda (opt name arg k) #f)
  74. (lambda (opt name arg k) #f)
  75. '())))
  76. ;; this matches behavior of getopt_long in libc 2.4
  77. (pass-if "short options absorb special markers in the next arg"
  78. (let ((arg-proc (lambda (opt name arg k)
  79. (acons name arg k))))
  80. (equal? '((#\y . "-z") (#\x . "--") (#\z . #f))
  81. (args-fold '("-zx" "--" "-y" "-z" "--")
  82. (list (option '(#\x) #f #t arg-proc)
  83. (option '(#\z) #f #f arg-proc)
  84. (option '(#\y) #t #f arg-proc))
  85. (lambda (opt name arg k) #f)
  86. (lambda (opt name arg k) #f)
  87. '()))))
  88. (pass-if "short options without arguments"
  89. ;; In Guile 1.8.4 and earlier, using short names of argument-less options
  90. ;; would lead to a stack overflow.
  91. (let ((arg-proc (lambda (opt name arg k)
  92. (acons name arg k))))
  93. (equal? '((#\x . #f))
  94. (args-fold '("-x")
  95. (list (option '(#\x) #f #f arg-proc))
  96. (lambda (opt name arg k) #f)
  97. (lambda (opt name arg k) #f)
  98. '()))))
  99. (pass-if-equal "short option with optional argument omitted" 'good
  100. ;; This would trigger an infinite loop in Guile up to 2.0.7.
  101. ;; See <http://bugs.gnu.org/13176>.
  102. (args-fold '("-I")
  103. (list (option '(#\I) #f #t
  104. (lambda (opt name arg value)
  105. (and (eqv? name #\I) (not arg)
  106. 'good))))
  107. (lambda _ (error "unrecognized"))
  108. (const #f)
  109. #f))
  110. (pass-if-equal "short option with optional argument provided"
  111. "the-argument"
  112. (args-fold '("-I" "the-argument")
  113. (list (option '(#\I) #f #t
  114. (lambda (opt name arg result)
  115. (and (eqv? name #\I) arg))))
  116. (lambda _ (error "unrecognized"))
  117. (const #f)
  118. #f))
  119. )