getopt-long.test 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ;;;; getopt-long.test --- long options processing -*- scheme -*-
  2. ;;;; Thien-Thi Nguyen <ttn@gnu.org> --- August 2001
  3. ;;;;
  4. ;;;; Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This program is free software; you can redistribute it and/or modify
  7. ;;;; it under the terms of the GNU General Public License as published by
  8. ;;;; the Free Software Foundation; either version 2, or (at your option)
  9. ;;;; any later version.
  10. ;;;;
  11. ;;;; This program is distributed in the hope that it will be useful,
  12. ;;;; but 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 this software; see the file COPYING. If not, write to
  18. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. ;;;; Boston, MA 02110-1301 USA
  20. (use-modules (test-suite lib)
  21. (ice-9 getopt-long)
  22. (ice-9 regex))
  23. (defmacro deferr (name-frag re)
  24. (let ((name (symbol-append 'exception: name-frag)))
  25. `(define ,name (cons 'misc-error ,re))))
  26. (deferr no-such-option "^no such option")
  27. (deferr option-predicate-failed "^option predicate failed")
  28. (deferr option-does-not-support-arg "^option does not support argument")
  29. (deferr option-must-be-specified "^option must be specified")
  30. (deferr option-must-have-arg "^option must be specified with argument")
  31. (with-test-prefix "exported procs"
  32. (pass-if "`option-ref' defined" (defined? 'option-ref))
  33. (pass-if "`getopt-long' defined" (defined? 'getopt-long)))
  34. (with-test-prefix "specifying predicate"
  35. (define (test1 . args)
  36. (getopt-long args
  37. `((test (value #t)
  38. (predicate ,(lambda (x)
  39. (string-match "^[0-9]+$" x)))))))
  40. (pass-if "valid arg"
  41. (equal? (test1 "foo" "bar" "--test=123")
  42. '((() "bar") (test . "123"))))
  43. (pass-if-exception "invalid arg"
  44. exception:option-predicate-failed
  45. (test1 "foo" "bar" "--test=foo"))
  46. (pass-if-exception "option has no arg"
  47. exception:option-must-have-arg
  48. (test1 "foo" "bar" "--test"))
  49. )
  50. (with-test-prefix "not specifying predicate"
  51. (define (test2 . args)
  52. (getopt-long args `((test (value #t)))))
  53. (pass-if "option has arg"
  54. (equal? (test2 "foo" "bar" "--test=foo")
  55. '((() "bar") (test . "foo"))))
  56. (pass-if "option has no arg"
  57. (equal? (test2 "foo" "bar")
  58. '((() "bar"))))
  59. )
  60. (with-test-prefix "value optional"
  61. (define (test3 . args)
  62. (getopt-long args '((foo (value optional) (single-char #\f))
  63. (bar))))
  64. (pass-if "long option `foo' w/ arg, long option `bar'"
  65. (equal? (test3 "prg" "--foo" "fooval" "--bar")
  66. '((()) (bar . #t) (foo . "fooval"))))
  67. (pass-if "short option `foo' w/ arg, long option `bar'"
  68. (equal? (test3 "prg" "-f" "fooval" "--bar")
  69. '((()) (bar . #t) (foo . "fooval"))))
  70. (pass-if "short option `foo', long option `bar', no args"
  71. (equal? (test3 "prg" "-f" "--bar")
  72. '((()) (bar . #t) (foo . #t))))
  73. (pass-if "long option `foo', long option `bar', no args"
  74. (equal? (test3 "prg" "--foo" "--bar")
  75. '((()) (bar . #t) (foo . #t))))
  76. (pass-if "long option `bar', short option `foo', no args"
  77. (equal? (test3 "prg" "--bar" "-f")
  78. '((()) (foo . #t) (bar . #t))))
  79. (pass-if "long option `bar', long option `foo', no args"
  80. (equal? (test3 "prg" "--bar" "--foo")
  81. '((()) (foo . #t) (bar . #t))))
  82. )
  83. (with-test-prefix "option-ref"
  84. (define (test4 option-arg . args)
  85. (equal? option-arg (option-ref (getopt-long
  86. (cons "prog" args)
  87. '((foo
  88. (value optional)
  89. (single-char #\f))
  90. (bar)))
  91. 'foo #f)))
  92. (pass-if "option-ref `--foo 4'"
  93. (test4 "4" "--foo" "4"))
  94. (pass-if "option-ref `-f 4'"
  95. (test4 "4" "-f" "4"))
  96. (pass-if "option-ref `-f4'"
  97. (test4 "4" "-f4"))
  98. (pass-if "option-ref `--foo=4'"
  99. (test4 "4" "--foo=4"))
  100. )
  101. (with-test-prefix "required"
  102. (define (test5 args specs)
  103. (getopt-long (cons "foo" args) specs))
  104. (pass-if "not mentioned, not given"
  105. (equal? (test5 '() '())
  106. '((()))))
  107. (pass-if-exception "not mentioned, given"
  108. exception:no-such-option
  109. (test5 '("--req") '((something))))
  110. (pass-if "not specified required, not given"
  111. (equal? (test5 '() '((req (required? #f))))
  112. '((()))))
  113. (pass-if "not specified required, given anyway"
  114. (equal? (test5 '("--req") '((req (required? #f))))
  115. '((()) (req . #t))))
  116. (pass-if "not specified required, but w/ value, given anyway w/ \"=\" val"
  117. (equal? (test5 '("--req=7") '((req (required? #f) (value #t))))
  118. '((()) (req . "7"))))
  119. (pass-if "not specified required, but w/ value, given anyway w/ non-\"=\" val"
  120. (equal? (test5 '("--req" "7") '((req (required? #f) (value #t))))
  121. '((()) (req . "7"))))
  122. (pass-if-exception "specified required, not given"
  123. exception:option-must-be-specified
  124. (test5 '() '((req (required? #t)))))
  125. )
  126. (with-test-prefix "specified no-value, given anyway"
  127. (define (test6 args specs)
  128. (getopt-long (cons "foo" args) specs))
  129. (pass-if-exception "using \"=\" syntax"
  130. exception:option-does-not-support-arg
  131. (test6 '("--maybe=yes") '((maybe))))
  132. )
  133. (with-test-prefix "specified arg required"
  134. (define (test7 args)
  135. (getopt-long (cons "foo" args) '((hmm (value #t) (single-char #\H))
  136. (ignore))))
  137. (pass-if "short opt, arg given"
  138. (equal? (test7 '("-H" "99"))
  139. '((()) (hmm . "99"))))
  140. (pass-if "long non-\"=\" opt, arg given"
  141. (equal? (test7 '("--hmm" "100"))
  142. '((()) (hmm . "100"))))
  143. (pass-if "long \"=\" opt, arg given"
  144. (equal? (test7 '("--hmm=101"))
  145. '((()) (hmm . "101"))))
  146. (pass-if-exception "short opt, arg not given"
  147. exception:option-must-have-arg
  148. (test7 '("-H")))
  149. (pass-if-exception "long non-\"=\" opt, arg not given (next arg an option)"
  150. exception:option-must-have-arg
  151. (test7 '("--hmm" "--ignore")))
  152. (pass-if-exception "long \"=\" opt, arg not given"
  153. exception:option-must-have-arg
  154. (test7 '("--hmm")))
  155. )
  156. (with-test-prefix "apples-blimps-catalexis example"
  157. (define (test8 . args)
  158. (equal? (sort (getopt-long (cons "foo" args)
  159. '((apples (single-char #\a))
  160. (blimps (single-char #\b) (value #t))
  161. (catalexis (single-char #\c) (value #t))))
  162. (lambda (a b)
  163. (cond ((null? (car a)) #t)
  164. ((null? (car b)) #f)
  165. (else (string<? (symbol->string (car a))
  166. (symbol->string (car b)))))))
  167. '((())
  168. (apples . #t)
  169. (blimps . "bang")
  170. (catalexis . "couth"))))
  171. (pass-if "normal 1" (test8 "-a" "-b" "bang" "-c" "couth"))
  172. (pass-if "normal 2" (test8 "-ab" "bang" "-c" "couth"))
  173. (pass-if "normal 3" (test8 "-ac" "couth" "-b" "bang"))
  174. (pass-if-exception "bad ordering causes missing option"
  175. exception:option-must-have-arg
  176. (test8 "-abc" "couth" "bang"))
  177. )
  178. (with-test-prefix "multiple occurrances"
  179. (define (test9 . args)
  180. (equal? (getopt-long (cons "foo" args)
  181. '((inc (single-char #\I) (value #t))
  182. (foo (single-char #\f))))
  183. '((()) (inc . "2") (foo . #t) (inc . "1"))))
  184. ;; terminology:
  185. ;; sf -- single-char free
  186. ;; sa -- single-char abutted
  187. ;; lf -- long free
  188. ;; la -- long abutted (using "=")
  189. (pass-if "sf/sf" (test9 "-I" "1" "-f" "-I" "2"))
  190. (pass-if "sa/sa" (test9 "-I1" "-f" "-I2"))
  191. (pass-if "sf/sa" (test9 "-I" "1" "-f" "-I2"))
  192. (pass-if "sa/sf" (test9 "-I1" "-f" "-I" "2"))
  193. (pass-if "lf/lf" (test9 "--inc" "1" "-f" "--inc" "2"))
  194. (pass-if "la/la" (test9 "--inc=1" "-f" "--inc=2"))
  195. (pass-if "lf/la" (test9 "--inc" "1" "-f" "--inc=2"))
  196. (pass-if "la/lf" (test9 "--inc=1" "-f" "--inc" "2"))
  197. (pass-if "sf/lf" (test9 "-I" "1" "-f" "--inc" "2"))
  198. (pass-if "lf/sf" (test9 "--inc" "1" "-f" "-I" "2"))
  199. (pass-if "sf/la" (test9 "-I" "1" "-f" "--inc=2"))
  200. (pass-if "la/sf" (test9 "--inc=1" "-f" "-I" "2"))
  201. (pass-if "sa/lf" (test9 "-I1" "-f" "--inc" "2"))
  202. (pass-if "lf/sa" (test9 "--inc" "1" "-f" "-I2"))
  203. (pass-if "sa/la" (test9 "-I1" "-f" "--inc=2"))
  204. (pass-if "la/sa" (test9 "--inc=1" "-f" "-I2"))
  205. )
  206. ;;; getopt-long.test ends here