* NEWS: Update wording to say merely "should" for AS_DIRNAME
[autoconf.git] / tests / m4sh.at
blob2f51e7029f96f17d6ff98cbad1835e648510237f
1 #                                                       -*- Autotest -*-
3 AT_BANNER([M4sh.])
5 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
6 # Foundation, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # 02110-1301, USA.
23 ## ---------------- ##
24 ## LINENO support.  ##
25 ## ---------------- ##
27 AT_SETUP([LINENO])
29 # We cannot unset LINENO with Zsh, yet this test case relies on
30 # unsetting LINENO to compare its result when (i) LINENO is supported
31 # and when (ii) it is not.
32 # So just skip if the shell is ZSH.
33 AT_CHECK([test -n "${ZSH_VERSION+set}" && exit 77], ignore)
35 # AT_DATA_LINENO(FILE-NAME,
36 #                UNSET-LINENO = true | false, COUNTER, COUNTER-RE)
37 # ----------------------------------------------------------------
38 # Produce the FILE-NAME M4sh script which uses the COUNTER LINENO or
39 # _oline_, which we can recognized via COUNTER-RE.  Unset LINENO is
40 # UNSET-LINENO.
42 # Use COUNTER, COUNTER-RE = [__LINENO__], [LINENO]
43 #  or                     = [__OLINE__],  [_oline__]
45 # instead of the obvious $LINENO and __oline__, because they would
46 # be replaced in the test suite itself, even before creating these
47 # scripts.  For the same reason, grep for LINENO and _oline__ (sic).
49 # UNSET-LINENO is a shell condition to make sure the scripts have the
50 # same number of lines in the output, so that their outputs be identical.
51 m4_define([AT_DATA_LINENO],
52 [AT_DATA([$1.tas],
53 [[AS@&t@_INIT
54 if $2; then
55   AS@&t@_UNSET([LINENO])
57 _AS@&t@_PREPARE
58 echo "Line: $3"
59 grep 'Line: .*$4' $[0] >/dev/null ||
60   AS@&t@_ERROR([cannot find original script])
61 exit 0
62 ]])
63 # If occurrences of $LINENO or __oline__ were wanted, create them.
64 sed 's/__LINENO__/$''LINENO/g;s/__OLINE__/__''oline__/g' $1.tas >$1.as
65 AT_CHECK([autom4te -l m4sh $1.as -o $1])
66 ])# AT_DATA_LINENO
68 # `_oline_', once processed and ran, produces our reference.
69 # We check that we find ourselves by looking at a string which is
70 # available only in the original script: `_oline_'.
71 AT_DATA_LINENO([reference], [false], [__OLINE__], [_oline__])
72 AT_CHECK([./reference], 0, [stdout])
74 # The reference:
75 mv stdout expout
77 # Now using a maybe-functioning LINENO, with different call conventions.
78 # Be sure to be out of the PATH.
79 AT_CHECK([mkdir test || exit 77])
81 AT_DATA_LINENO([test/test-1], [false], [__LINENO__], [LINENO])
82 AT_CHECK([./test/test-1],                          0, [expout])
83 AT_CHECK([(PATH=test$PATH_SEPARATOR$PATH; export PATH; exec test-1)],
84                                                    0, [expout])
85 AT_CHECK([sh ./test/test-1],                       0, [expout])
87 # Now using a disabled LINENO, with different call conventions.
88 AT_DATA_LINENO([test/test-2], [true], [__LINENO__], [LINENO])
89 AT_CHECK([./test/test-2],                          0, [expout])
90 AT_CHECK([(PATH=test$PATH_SEPARATOR$PATH; export PATH; exec test-2)],
91                                                    0, [expout])
92 AT_CHECK([sh ./test/test-2],                       0, [expout])
94 AT_CLEANUP
98 ## -------------------- ##
99 ## AS_VAR_SET_DIRNAME.  ##
100 ## -------------------- ##
102 # Build nested dirs.
103 AT_SETUP([AS@&t@_VAR_SET_DIRNAME])
105 AT_DATA_M4SH([script.as],
106 [[AS_INIT
108 m4_define([DIRNAME_TEST],
109 [AS_VAR_SET_DIRNAME([dir], [$1])
110 test "$dir" = "$2" || (test -n "$3" && test "$dir" = "$3") ||
111   echo "dirname($1) = $dir instead of $2" >&2])
113 DIRNAME_TEST([/],               [/])
114 DIRNAME_TEST([//],              [//],   [/])
115 DIRNAME_TEST([///],             [/])
116 DIRNAME_TEST([//1],             [//],   [/])
117 DIRNAME_TEST([/1],              [/])
118 DIRNAME_TEST([./1],             [.])
119 DIRNAME_TEST([../../2],         [../..])
120 DIRNAME_TEST([//1/],            [//],   [/])
121 DIRNAME_TEST([/1/],             [/])
122 DIRNAME_TEST([./1/],            [.])
123 DIRNAME_TEST([../../2],         [../..])
124 DIRNAME_TEST([//1/3],           [//1])
125 DIRNAME_TEST([/1/3],            [/1])
126 DIRNAME_TEST([./1/3],           [./1])
127 DIRNAME_TEST([../../2/3],       [../../2])
128 DIRNAME_TEST([//1/3///],        [//1])
129 DIRNAME_TEST([/1/3///],         [/1])
130 DIRNAME_TEST([./1/3///],        [./1])
131 DIRNAME_TEST([../../2/3///],    [../../2])
132 DIRNAME_TEST([//1//3/],         [//1])
133 DIRNAME_TEST([/1//3/],          [/1])
134 DIRNAME_TEST([./1//3/],         [./1])
135 DIRNAME_TEST([../../2//3/],     [../../2])
136 AS_EXIT(0)
139 AT_CHECK_M4SH
140 AT_CHECK([./script])
142 AT_CLEANUP
146 ## --------------------- ##
147 ## AS_VAR_SET_BASENAME.  ##
148 ## --------------------- ##
150 # Build nested dirs.
151 AT_SETUP([AS@&t@_VAR_SET_BASENAME])
153 AT_DATA_M4SH([script.as],
154 [[AS_INIT
156 m4_define([BASENAME_TEST],
157 [AS_VAR_SET_BASENAME([base], [$1])
158 test "$base" = "$2" ||
159   echo "basename($1) = $base instead of $2" >&2])
161 BASENAME_TEST([//1],             [1])
162 BASENAME_TEST([/1],              [1])
163 BASENAME_TEST([./1],             [1])
164 BASENAME_TEST([../../2],         [2])
165 BASENAME_TEST([//1/],            [1])
166 BASENAME_TEST([/1/],             [1])
167 BASENAME_TEST([./1/],            [1])
168 BASENAME_TEST([../../2],         [2])
169 BASENAME_TEST([//1/3],           [3])
170 BASENAME_TEST([/1/3],            [3])
171 BASENAME_TEST([./1/3],           [3])
172 BASENAME_TEST([../../2/3],       [3])
173 BASENAME_TEST([//1/3///],        [3])
174 BASENAME_TEST([/1/3///],         [3])
175 BASENAME_TEST([./1/3///],        [3])
176 BASENAME_TEST([../../2/3///],    [3])
177 BASENAME_TEST([//1//3/],         [3])
178 BASENAME_TEST([/1//3/],          [3])
179 BASENAME_TEST([./1//3/],         [3])
180 BASENAME_TEST([a.c],             [a.c])
181 BASENAME_TEST([a.c/],            [a.c])
182 BASENAME_TEST([/a.c/],           [a.c])
183 BASENAME_TEST([/1/a.c],          [a.c])
184 BASENAME_TEST([/1/a.c/],         [a.c])
185 BASENAME_TEST([/1/../a.c],       [a.c])
186 BASENAME_TEST([/1/../a.c/],      [a.c])
187 BASENAME_TEST([./1/a.c],         [a.c])
188 BASENAME_TEST([./1/a.c/],        [a.c])
189 AS_EXIT(0)
192 AT_CHECK_M4SH
193 AT_CHECK([./script])
195 AT_CLEANUP
199 ## ------------ ##
200 ## AS_MKDIR_P.  ##
201 ## ------------ ##
203 # Build nested dirs.
204 AT_SETUP([AS@&t@_MKDIR_P])
206 AT_DATA_M4SH([script.as],
207 [[AS_INIT
209 pwd=`pwd`
210 set -e
211 # Absolute
212 AS_MKDIR_P(["$pwd/1/2/3/4/5/6"])
213 test -d "$pwd/1/2/3/4/5/6" ||
214   AS_ERROR([$pwd/1/2/3/4/5/6 has not been properly created])
215 # Relative
216 AS_MKDIR_P(["a/b/c/d/e/f"])
217 test -d a/b/c/d/e/f ||
218   AS_ERROR([a/b/c/d/e/f has not been properly created])
219 AS_EXIT(0)
222 AT_CHECK_M4SH
223 AT_CHECK([./script])
225 AT_CLEANUP
230 ## -------------------- ##
231 ## AS_VERSION_COMPARE.  ##
232 ## -------------------- ##
234 # Build nested dirs.
235 AT_SETUP([AS@&t@_VERSION_COMPARE])
237 AT_DATA_M4SH([script.as],
238 [[AS_INIT
240 m4_define([VERSION_COMPARE_TEST],
241 [AS_VERSION_COMPARE([$1], [$3], [result='<'], [result='='], [result='>'])
242 test "X$result" = "X$2" ||
243   AS_ERROR([version $1 $result $3; should be $1 $2 $3])
244 m4_if([$1], <,
245 [AS_VERSION_COMPARE([$3], [$1], [result='<'], [result='='], [result='>'])
246 test "X$result" = "X>" ||
247   AS_ERROR([version $3 $result $1; should be $3 > $1])])])
249 VERSION_COMPARE_TEST([], =, [])
250 VERSION_COMPARE_TEST([1.0], =, [1.0])
251 VERSION_COMPARE_TEST([alpha-1.0], =, [alpha-1.0])
253 # These tests are taken from libc/string/tst-svc.expect.
254 tst_svc_expect='
255   000 001 00 00a 01 01a 0 0a 2.8 2.8-0.4 20 21 22 212 CP037 CP345 CP1257
256   foo foo-0.4 foo-0.4a foo-0.4b foo-0.5 foo-0.10.5 foo-3.01 foo-3.0
257   foo-3.0.0 foo-3.0.1 foo-3.2 foo-3.10 foo00 foo0
259 test1=''
260 for test2 in $tst_svc_expect; do
261   VERSION_COMPARE_TEST([$test1], <, [$test2])
262   test1=$test2
263 done
265 AS_EXIT(0)
268 AT_CHECK_M4SH
269 AT_CHECK([./script])
271 AT_CLEANUP
276 ## ----------------------------- ##
277 ## Negated classes in globbing.  ##
278 ## ----------------------------- ##
280 # It is known that `[^...]' is not universally supported, but it is
281 # unknown for `[!...]'.
283 AT_SETUP([Negated classes in globbing])
285 AT_DATA_M4SH([script.as],
286 [[AS_INIT
288 case 'with!two!bangs' in
289   *[[!a-z]]*) ;;
290            *) AS_ERROR([[`*[!a-z]*' didn't match `with!two!bangs']]);;
291 esac
293 case without in
294   *[[!a-z]]*) AS_ERROR([[`*[!a-z]*' matched `without']]);;
295 esac
298 AT_CHECK_M4SH
299 AT_CHECK([./script])
301 AT_CLEANUP
306 ## ------------------- ##
307 ## Functions Support.  ##
308 ## ------------------- ##
310 # Hypothesis: the shell we are running, after having checked for
311 # $LINENO support, supports functions.
313 AT_SETUP([Functions Support])
315 AT_DATA_M4SH([script.as],
316 [[AS_INIT
317 _AS_LINENO_PREPARE
319 func_return () {
320   (exit $1)
323 func_success () {
324   func_return 0
327 func_failure () {
328   func_return 1
331 if func_success; then
332   if func_failure; then
333     AS_ERROR([func_failure passed])
334   fi
335 else
336   AS_ERROR([func_success failed])
340 AT_CHECK_M4SH
341 AT_CHECK([./script])
343 AT_CLEANUP
348 ## ------------------------------ ##
349 ## Functions and return Support.  ##
350 ## ------------------------------ ##
352 # Hypothesis: the shell we are running, after having checked for
353 # $LINENO support, supports functions, and the `return' keyword.
355 AT_SETUP([Functions and return Support])
357 AT_DATA_M4SH([script.as],
358 [[AS_INIT
359 _AS_LINENO_PREPARE
361 func_success () {
362   return 0
365 func_failure () {
366   return 1
369 if func_success; then
370   if func_failure; then
371     AS_ERROR([func_failure passed])
372   fi
373 else
374   AS_ERROR([func_success failed])
378 AT_CHECK_M4SH
379 AT_CHECK([./script])
381 AT_CLEANUP
384 ## ------------------------------------ ##
385 ## AS_REQUIRE_SHELL_FN and m4_require.  ##
386 ## ------------------------------------ ##
388 # Hypothesis: M4sh expands the requirements of AS_REQUIRE_SHELL_FN
389 # in the main diversion, and not in M4SH-INIT.
391 AT_SETUP([AS@&t@_REQUIRE_SHELL_FN and m4@&t@_require])
393 AT_DATA_M4SH([script.as], [[dnl
394 AS_INIT
396 m4_defun([in_m4_sh_init], still_in_m4sh_init=yes)
397 m4_defun([not_in_m4_sh_init], still_in_m4sh_init=no)
399 m4_defun([error_if_emitted_in_m4sh_init], [
400   if test x$still_in_m4sh_init = xyes; then
401     AS_ERROR([requirement emitted in M4SH-INIT])
402   fi
405 m4_defun([TEST_FUNC_BODY], [
406 m4_require([error_if_emitted_in_m4sh_init])
407 : echo in shell function, with parameter = [$]1
411 m4_defun([test_init], [
412 AS_REQUIRE([in_m4_sh_init])
413 AS_REQUIRE_SHELL_FN([test_func], [TEST_FUNC_BODY])
414 AS_REQUIRE([not_in_m4_sh_init])
417 test_init
418 test_func parameter1
421 AT_CHECK_M4SH
422 AT_CHECK([./script])
424 AT_CLEANUP
427 ## -------------- ##
428 ## AS_HELP_STRING ##
429 ## -------------- ##
431 # I'm not totally certain that we want to enforce the defaults here,
432 # but at least it is being tested.
434 AT_SETUP([AS@&t@_HELP_STRING])
436 AT_DATA_M4SH([script.as],
437 [[AS_INIT
438 _AS_LINENO_PREPARE
440 echo "AS_HELP_STRING([--an-option],[some text])"
441 echo "AS_HELP_STRING([--another-much-longer-option],
442 [some other text which should wrap at our default of 80 characters.])"
443 echo "AS_HELP_STRING([--fooT=barT], [foo bar])"
444 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@], [foo bar])"
445 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789], [foo bar])"
446 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890], [foo bar])"
447 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@12345678901], [foo bar])"
448 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789012], [foo bar])"
449 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890123], [foo bar])"
450 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@],
451 [some other text which should wrap at our default of 80 characters.])"
452 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789],
453 [some other text which should wrap at our default of 80 characters.])"
454 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890],
455 [some other text which should wrap at our default of 80 characters.])"
456 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@12345678901],
457 [some other text which should wrap at our default of 80 characters.])"
458 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789012],
459 [some other text which should wrap at our default of 80 characters.])"
460 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890123],
461 [some other text which should wrap at our default of 80 characters.])"
462 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@],
463 [some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])"
464 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789],
465 [some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])"
466 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890],
467 [some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])"
468 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@12345678901],
469 [some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])"
470 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789012],
471 [some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])"
472 echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890123],
473 [some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])"
476 AT_CHECK_M4SH
477 AT_CHECK([./script], [0],
478 [[  --an-option             some text
479   --another-much-longer-option
480                           some other text which should wrap at our default of
481                           80 characters.
482   --fooT=barT             foo bar
483   --foo[=bar]             foo bar
484   --foo[=bar]123456789    foo bar
485   --foo[=bar]1234567890   foo bar
486   --foo[=bar]12345678901  foo bar
487   --foo[=bar]123456789012 foo bar
488   --foo[=bar]1234567890123
489                           foo bar
490   --foo[=bar]             some other text which should wrap at our default of
491                           80 characters.
492   --foo[=bar]123456789    some other text which should wrap at our default of
493                           80 characters.
494   --foo[=bar]1234567890   some other text which should wrap at our default of
495                           80 characters.
496   --foo[=bar]12345678901  some other text which should wrap at our default of
497                           80 characters.
498   --foo[=bar]123456789012 some other text which should wrap at our default of
499                           80 characters.
500   --foo[=bar]1234567890123
501                           some other text which should wrap at our default of
502                           80 characters.
503   --foo[=bar]             some other [ex] which should wrap at our default of
504                           80 characters.
505   --foo[=bar]123456789    some other [ex] which should wrap at our default of
506                           80 characters.
507   --foo[=bar]1234567890   some other [ex] which should wrap at our default of
508                           80 characters.
509   --foo[=bar]12345678901  some other [ex] which should wrap at our default of
510                           80 characters.
511   --foo[=bar]123456789012 some other [ex] which should wrap at our default of
512                           80 characters.
513   --foo[=bar]1234567890123
514                           some other [ex] which should wrap at our default of
515                           80 characters.
518 AT_CLEANUP
521 ## ------------------- ##
522 ## AS_IF and AS_CASE.  ##
523 ## ------------------- ##
525 AT_SETUP([AS@&t@_IF and AS@&t@_CASE])
527 AT_DATA_M4SH([script.as], [[dnl
528 AS_INIT
529 # Syntax checks: cope with empty arguments.
530 AS_IF([:], [], [echo wrong])
531 AS_IF([:], [echo one], [echo wrong])
532 AS_IF([false], [echo wrong], [echo two])
533 AS_IF([false], [echo wrong])
534 # n-ary version
535 AS_IF([false], [echo wrong],
536       [:], [echo three])
537 AS_IF([false], [echo wrong],
538       [:], [echo four],
539       [echo wrong])
540 AS_IF([false], [echo wrong],
541       [false], [echo wrong])
542 AS_IF([false], [echo wrong],
543       [false], [echo wrong],
544       [echo five])
545 AS_IF([false], [echo wrong],
546       [false], [echo wrong],
547       [:], [echo six],
548       [echo wrong])
549 AS_CASE([foo])
550 AS_CASE([foo], [echo seven])
551 AS_CASE([foo],
552         [foo], [echo eight],
553         [echo wrong])
554 AS_CASE([foo],
555         [foo], [echo nine],
556         [*],   [echo wrong])
557 AS_CASE([foo],
558         [bar], [echo wrong],
559         [foo], [echo ten],
560         [*],   [echo wrong])
562 # check that require works correctly
563 m4_for([n], 1, 9, [],
564 [m4_defun([FOO]n, [foo]n[=]n)dnl
565 m4_defun([BAR]n,
566          [m4_require([FOO]]n[)dnl
567 bar]n[=]n)[]dnl
570 AS_IF([:], [BAR1])
571 echo "foo1=$foo1 bar1=$bar1"
572 AS_IF([:], [], [BAR2])
573 echo "foo2=$foo2 bar2=$bar2"
574 AS_IF([false], [BAR3])
575 echo "foo3=$foo3 bar3=$bar3"
576 AS_IF([false], [], [BAR4])
577 echo "foo4=$foo4 bar4=$bar4"
578 AS_CASE([x], [x], [BAR5])
579 echo "foo5=$foo5 bar5=$bar5"
580 AS_CASE([x], [y], [BAR6])
581 echo "foo6=$foo6 bar6=$bar6"
582 AS_CASE([x],
583         [x], [:],
584         [BAR7])
585 echo "foo7=$foo7 bar7=$bar7"
586 AS_CASE([x],
587         [y], [:],
588         [BAR8])
589 echo "foo8=$foo8 bar8=$bar8"
590 AS_CASE([x],
591         [y], [:],
592         [x], [BAR9])
593 echo "foo9=$foo9 bar9=$bar9"
596 AT_CHECK_M4SH
597 AT_CHECK([./script], [0], [[one
599 three
600 four
601 five
603 seven
604 eight
605 nine
607 foo1=1 bar1=1
608 foo2=2 bar2=
609 foo3=3 bar3=
610 foo4=4 bar4=4
611 foo5=5 bar5=5
612 foo6=6 bar6=
613 foo7=7 bar7=
614 foo8=8 bar8=8
615 foo9=9 bar9=9
618 AT_CLEANUP