1# -*- Autotest -*- 2 3AT_BANNER([M4sh.]) 4 5# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software 6# Foundation, Inc. 7# 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. 12# 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. 17# 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. 22 23## ---------------- ## 24## LINENO support. ## 25## ---------------- ## 26 27AT_SETUP([LINENO]) 28 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. 33AT_CHECK([test -n "${ZSH_VERSION+set}" && exit 77], ignore) 34 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. 41# 42# Use COUNTER, COUNTER-RE = [__LINENO__], [LINENO] 43# or = [__OLINE__], [_oline__] 44# 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). 48# 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. 51m4_define([AT_DATA_LINENO], 52[AT_DATA([$1.tas], 53[[AS@&t@_INIT 54if $2; then 55 AS@&t@_UNSET([LINENO]) 56fi 57_AS@&t@_PREPARE 58echo "Line: $3" 59grep 'Line: .*$4' $[0] >/dev/null || 60 AS@&t@_ERROR([cannot find original script]) 61exit 0 62]]) 63# If occurrences of $LINENO or __oline__ were wanted, create them. 64sed 's/__LINENO__/$''LINENO/g;s/__OLINE__/__''oline__/g' $1.tas >$1.as 65AT_CHECK([autom4te -l m4sh $1.as -o $1]) 66])# AT_DATA_LINENO 67 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_'. 71AT_DATA_LINENO([reference], [false], [__OLINE__], [_oline__]) 72AT_CHECK([./reference], 0, [stdout]) 73 74# The reference: 75mv stdout expout 76 77# Now using a maybe-functioning LINENO, with different call conventions. 78# Be sure to be out of the PATH. 79AT_CHECK([mkdir test || exit 77]) 80 81AT_DATA_LINENO([test/test-1], [false], [__LINENO__], [LINENO]) 82AT_CHECK([./test/test-1], 0, [expout]) 83AT_CHECK([(PATH=test$PATH_SEPARATOR$PATH; export PATH; exec test-1)], 84 0, [expout]) 85AT_CHECK([sh ./test/test-1], 0, [expout]) 86 87# Now using a disabled LINENO, with different call conventions. 88AT_DATA_LINENO([test/test-2], [true], [__LINENO__], [LINENO]) 89AT_CHECK([./test/test-2], 0, [expout]) 90AT_CHECK([(PATH=test$PATH_SEPARATOR$PATH; export PATH; exec test-2)], 91 0, [expout]) 92AT_CHECK([sh ./test/test-2], 0, [expout]) 93 94AT_CLEANUP 95 96 97 98## ------------ ## 99## AS_DIRNAME. ## 100## ------------ ## 101 102# Build nested dirs. 103AT_SETUP([AS@&t@_DIRNAME]) 104 105AT_DATA_M4SH([script.as], 106[[AS_INIT 107 108# The EXPR variant is allowed to fail if `expr' was considered as too 109# weak for us, in which case `as_expr=false'. 110m4_define([DIRNAME_TEST], 111[dir=`AS_DIRNAME([$1])` 112test "$dir" = "$2" || (test -n "$3" && test "$dir" = "$3") || 113 echo "dirname($1) = $dir instead of $2" >&2 114 115if test "$as_expr" != false; then 116 dir=`_AS_DIRNAME_EXPR([$1])` 117 test "$dir" = "$2" || (test -n "$3" && test "$dir" = "$3") || 118 echo "dirname_expr($1) = $dir instead of $2" >&2 119fi 120 121dir=`_AS_DIRNAME_SED([$1])` 122test "$dir" = "$2" || (test -n "$3" && test "$dir" = "$3") || 123 echo "dirname_sed($1) = $dir instead of $2" >&2]) 124 125DIRNAME_TEST([/], [/]) 126DIRNAME_TEST([//], [//], [/]) 127DIRNAME_TEST([///], [/]) 128DIRNAME_TEST([//1], [//], [/]) 129DIRNAME_TEST([/1], [/]) 130DIRNAME_TEST([./1], [.]) 131DIRNAME_TEST([../../2], [../..]) 132DIRNAME_TEST([//1/], [//], [/]) 133DIRNAME_TEST([/1/], [/]) 134DIRNAME_TEST([./1/], [.]) 135DIRNAME_TEST([../../2], [../..]) 136DIRNAME_TEST([//1/3], [//1]) 137DIRNAME_TEST([/1/3], [/1]) 138DIRNAME_TEST([./1/3], [./1]) 139DIRNAME_TEST([../../2/3], [../../2]) 140DIRNAME_TEST([//1/3///], [//1]) 141DIRNAME_TEST([/1/3///], [/1]) 142DIRNAME_TEST([./1/3///], [./1]) 143DIRNAME_TEST([../../2/3///], [../../2]) 144DIRNAME_TEST([//1//3/], [//1]) 145DIRNAME_TEST([/1//3/], [/1]) 146DIRNAME_TEST([./1//3/], [./1]) 147DIRNAME_TEST([../../2//3/], [../../2]) 148AS_EXIT(0) 149]]) 150 151AT_CHECK_M4SH 152AT_CHECK([./script]) 153 154AT_CLEANUP 155 156 157 158## ------------- ## 159## AS_BASENAME. ## 160## ------------- ## 161 162# Build nested dirs. 163AT_SETUP([AS@&t@_BASENAME]) 164 165AT_DATA_M4SH([script.as], 166[[AS_INIT 167 168m4_define([BASENAME_TEST], 169[base=`AS_BASENAME([$1])` 170test "$base" = "$2" || 171 echo "basename($1) = $base instead of $2" >&2 172 173base=`_AS_BASENAME_SED([$1])` 174test "$base" = "$2" || 175 echo "basename_sed($1) = $base instead of $2" >&2]) 176 177BASENAME_TEST([//1], [1]) 178BASENAME_TEST([/1], [1]) 179BASENAME_TEST([./1], [1]) 180BASENAME_TEST([../../2], [2]) 181BASENAME_TEST([//1/], [1]) 182BASENAME_TEST([/1/], [1]) 183BASENAME_TEST([./1/], [1]) 184BASENAME_TEST([../../2], [2]) 185BASENAME_TEST([//1/3], [3]) 186BASENAME_TEST([/1/3], [3]) 187BASENAME_TEST([./1/3], [3]) 188BASENAME_TEST([../../2/3], [3]) 189BASENAME_TEST([//1/3///], [3]) 190BASENAME_TEST([/1/3///], [3]) 191BASENAME_TEST([./1/3///], [3]) 192BASENAME_TEST([../../2/3///], [3]) 193BASENAME_TEST([//1//3/], [3]) 194BASENAME_TEST([/1//3/], [3]) 195BASENAME_TEST([./1//3/], [3]) 196BASENAME_TEST([a.c], [a.c]) 197BASENAME_TEST([a.c/], [a.c]) 198BASENAME_TEST([/a.c/], [a.c]) 199BASENAME_TEST([/1/a.c], [a.c]) 200BASENAME_TEST([/1/a.c/], [a.c]) 201BASENAME_TEST([/1/../a.c], [a.c]) 202BASENAME_TEST([/1/../a.c/], [a.c]) 203BASENAME_TEST([./1/a.c], [a.c]) 204BASENAME_TEST([./1/a.c/], [a.c]) 205AS_EXIT(0) 206]]) 207 208AT_CHECK_M4SH 209AT_CHECK([./script]) 210 211AT_CLEANUP 212 213 214 215## ------------ ## 216## AS_MKDIR_P. ## 217## ------------ ## 218 219# Build nested dirs. 220AT_SETUP([AS@&t@_MKDIR_P]) 221 222AT_DATA_M4SH([script.as], 223[[AS_INIT 224 225pwd=`pwd` 226set -e 227# Absolute 228AS_MKDIR_P(["$pwd/1/2/3/4/5/6"]) 229test -d "$pwd/1/2/3/4/5/6" || 230 AS_ERROR([$pwd/1/2/3/4/5/6 has not been properly created]) 231# Relative 232AS_MKDIR_P(["a/b/c/d/e/f"]) 233test -d a/b/c/d/e/f || 234 AS_ERROR([a/b/c/d/e/f has not been properly created]) 235AS_EXIT(0) 236]]) 237 238AT_CHECK_M4SH 239AT_CHECK([./script]) 240 241AT_CLEANUP 242 243 244 245 246## -------------------- ## 247## AS_VERSION_COMPARE. ## 248## -------------------- ## 249 250# Build nested dirs. 251AT_SETUP([AS@&t@_VERSION_COMPARE]) 252 253AT_DATA_M4SH([script.as], 254[[AS_INIT 255 256m4_define([VERSION_COMPARE_TEST], 257[AS_VERSION_COMPARE([$1], [$3], [result='<'], [result='='], [result='>']) 258test "X$result" = "X$2" || 259 AS_ERROR([version $1 $result $3; should be $1 $2 $3]) 260m4_if([$1], <, 261[AS_VERSION_COMPARE([$3], [$1], [result='<'], [result='='], [result='>']) 262test "X$result" = "X>" || 263 AS_ERROR([version $3 $result $1; should be $3 > $1])])]) 264 265VERSION_COMPARE_TEST([], =, []) 266VERSION_COMPARE_TEST([1.0], =, [1.0]) 267VERSION_COMPARE_TEST([alpha-1.0], =, [alpha-1.0]) 268 269# These tests are taken from libc/string/tst-svc.expect. 270tst_svc_expect=' 271 000 001 00 00a 01 01a 0 0a 2.8 2.8-0.4 20 21 22 212 CP037 CP345 CP1257 272 foo foo-0.4 foo-0.4a foo-0.4b foo-0.5 foo-0.10.5 foo-3.01 foo-3.0 273 foo-3.0.0 foo-3.0.1 foo-3.2 foo-3.10 foo00 foo0 274' 275test1='' 276for test2 in $tst_svc_expect; do 277 VERSION_COMPARE_TEST([$test1], <, [$test2]) 278 test1=$test2 279done 280 281AS_EXIT(0) 282]]) 283 284AT_CHECK_M4SH 285AT_CHECK([./script]) 286 287AT_CLEANUP 288 289 290 291 292## ----------------------------- ## 293## Negated classes in globbing. ## 294## ----------------------------- ## 295 296# It is known that `[^...]' is not universally supported, but it is 297# unknown for `[!...]'. 298 299AT_SETUP([Negated classes in globbing]) 300 301AT_DATA_M4SH([script.as], 302[[AS_INIT 303 304case 'with!two!bangs' in 305 *[[!a-z]]*) ;; 306 *) AS_ERROR([[`*[!a-z]*' didn't match `with!two!bangs']]);; 307esac 308 309case without in 310 *[[!a-z]]*) AS_ERROR([[`*[!a-z]*' matched `without']]);; 311esac 312]]) 313 314AT_CHECK_M4SH 315AT_CHECK([./script]) 316 317AT_CLEANUP 318 319 320 321 322## ------------------- ## 323## Functions Support. ## 324## ------------------- ## 325 326# Hypothesis: the shell we are running, after having checked for 327# $LINENO support, supports functions. 328 329AT_SETUP([Functions Support]) 330 331AT_DATA_M4SH([script.as], 332[[AS_INIT 333_AS_LINENO_PREPARE 334 335func_return () { 336 (exit $1) 337} 338 339func_success () { 340 func_return 0 341} 342 343func_failure () { 344 func_return 1 345} 346 347if func_success; then 348 if func_failure; then 349 AS_ERROR([func_failure passed]) 350 fi 351else 352 AS_ERROR([func_success failed]) 353fi 354]]) 355 356AT_CHECK_M4SH 357AT_CHECK([./script]) 358 359AT_CLEANUP 360 361 362 363 364## ------------------------------ ## 365## Functions and return Support. ## 366## ------------------------------ ## 367 368# Hypothesis: the shell we are running, after having checked for 369# $LINENO support, supports functions, and the `return' keyword. 370 371AT_SETUP([Functions and return Support]) 372 373AT_DATA_M4SH([script.as], 374[[AS_INIT 375_AS_LINENO_PREPARE 376 377func_success () { 378 return 0 379} 380 381func_failure () { 382 return 1 383} 384 385if func_success; then 386 if func_failure; then 387 AS_ERROR([func_failure passed]) 388 fi 389else 390 AS_ERROR([func_success failed]) 391fi 392]]) 393 394AT_CHECK_M4SH 395AT_CHECK([./script]) 396 397AT_CLEANUP 398 399 400## ------------------------------------ ## 401## AS_REQUIRE_SHELL_FN and m4_require. ## 402## ------------------------------------ ## 403 404# Hypothesis: M4sh expands the requirements of AS_REQUIRE_SHELL_FN 405# in the main diversion, and not in M4SH-INIT. 406 407AT_SETUP([AS@&t@_REQUIRE_SHELL_FN and m4@&t@_require]) 408 409AT_DATA_M4SH([script.as], [[dnl 410AS_INIT 411 412m4_defun([in_m4_sh_init], still_in_m4sh_init=yes) 413m4_defun([not_in_m4_sh_init], still_in_m4sh_init=no) 414 415m4_defun([error_if_emitted_in_m4sh_init], [ 416 if test x$still_in_m4sh_init = xyes; then 417 AS_ERROR([requirement emitted in M4SH-INIT]) 418 fi 419]) 420 421m4_defun([TEST_FUNC_BODY], [ 422m4_require([error_if_emitted_in_m4sh_init]) 423: echo in shell function, with parameter = [$]1 424]) 425 426 427m4_defun([test_init], [ 428AS_REQUIRE([in_m4_sh_init]) 429AS_REQUIRE_SHELL_FN([test_func], [TEST_FUNC_BODY]) 430AS_REQUIRE([not_in_m4_sh_init]) 431]) 432 433test_init 434test_func parameter1 435]]) 436 437AT_CHECK_M4SH 438AT_CHECK([./script]) 439 440AT_CLEANUP 441 442 443## -------------- ## 444## AS_HELP_STRING ## 445## -------------- ## 446 447# I'm not totally certain that we want to enforce the defaults here, 448# but at least it is being tested. 449 450AT_SETUP([AS@&t@_HELP_STRING]) 451 452AT_DATA_M4SH([script.as], 453[[AS_INIT 454_AS_LINENO_PREPARE 455 456echo "AS_HELP_STRING([--an-option],[some text])" 457echo "AS_HELP_STRING([--another-much-longer-option], 458[some other text which should wrap at our default of 80 characters.])" 459echo "AS_HELP_STRING([--fooT=barT], [foo bar])" 460echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@], [foo bar])" 461echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789], [foo bar])" 462echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890], [foo bar])" 463echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@12345678901], [foo bar])" 464echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789012], [foo bar])" 465echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890123], [foo bar])" 466echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@], 467[some other text which should wrap at our default of 80 characters.])" 468echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789], 469[some other text which should wrap at our default of 80 characters.])" 470echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890], 471[some other text which should wrap at our default of 80 characters.])" 472echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@12345678901], 473[some other text which should wrap at our default of 80 characters.])" 474echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789012], 475[some other text which should wrap at our default of 80 characters.])" 476echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890123], 477[some other text which should wrap at our default of 80 characters.])" 478echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@], 479[some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])" 480echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789], 481[some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])" 482echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890], 483[some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])" 484echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@12345678901], 485[some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])" 486echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@123456789012], 487[some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])" 488echo "AS_HELP_STRING([--foo@<][:@=bar@:][>@1234567890123], 489[some other @<][:@ex@:][>@ which should wrap at our default of 80 characters.])" 490]]) 491 492AT_CHECK_M4SH 493AT_CHECK([./script], [0], 494[[ --an-option some text 495 --another-much-longer-option 496 some other text which should wrap at our default of 497 80 characters. 498 --fooT=barT foo bar 499 --foo[=bar] foo bar 500 --foo[=bar]123456789 foo bar 501 --foo[=bar]1234567890 foo bar 502 --foo[=bar]12345678901 foo bar 503 --foo[=bar]123456789012 foo bar 504 --foo[=bar]1234567890123 505 foo bar 506 --foo[=bar] some other text which should wrap at our default of 507 80 characters. 508 --foo[=bar]123456789 some other text which should wrap at our default of 509 80 characters. 510 --foo[=bar]1234567890 some other text which should wrap at our default of 511 80 characters. 512 --foo[=bar]12345678901 some other text which should wrap at our default of 513 80 characters. 514 --foo[=bar]123456789012 some other text which should wrap at our default of 515 80 characters. 516 --foo[=bar]1234567890123 517 some other text which should wrap at our default of 518 80 characters. 519 --foo[=bar] some other [ex] which should wrap at our default of 520 80 characters. 521 --foo[=bar]123456789 some other [ex] which should wrap at our default of 522 80 characters. 523 --foo[=bar]1234567890 some other [ex] which should wrap at our default of 524 80 characters. 525 --foo[=bar]12345678901 some other [ex] which should wrap at our default of 526 80 characters. 527 --foo[=bar]123456789012 some other [ex] which should wrap at our default of 528 80 characters. 529 --foo[=bar]1234567890123 530 some other [ex] which should wrap at our default of 531 80 characters. 532]]) 533 534AT_CLEANUP 535 536 537## ------------------- ## 538## AS_IF and AS_CASE. ## 539## ------------------- ## 540 541AT_SETUP([AS@&t@_IF and AS@&t@_CASE]) 542 543AT_DATA_M4SH([script.as], [[dnl 544AS_INIT 545# Syntax checks: cope with empty arguments. 546AS_IF([:], [], [echo wrong]) 547AS_IF([:], [echo one], [echo wrong]) 548AS_IF([false], [echo wrong], [echo two]) 549AS_IF([false], [echo wrong]) 550# n-ary version 551AS_IF([false], [echo wrong], 552 [:], [echo three]) 553AS_IF([false], [echo wrong], 554 [:], [echo four], 555 [echo wrong]) 556AS_IF([false], [echo wrong], 557 [false], [echo wrong]) 558AS_IF([false], [echo wrong], 559 [false], [echo wrong], 560 [echo five]) 561AS_IF([false], [echo wrong], 562 [false], [echo wrong], 563 [:], [echo six], 564 [echo wrong]) 565AS_CASE([foo]) 566AS_CASE([foo], [echo seven]) 567AS_CASE([foo], 568 [foo], [echo eight], 569 [echo wrong]) 570AS_CASE([foo], 571 [foo], [echo nine], 572 [*], [echo wrong]) 573AS_CASE([foo], 574 [bar], [echo wrong], 575 [foo], [echo ten], 576 [*], [echo wrong]) 577 578# check that require works correctly 579m4_for([n], 1, 9, [], 580[m4_defun([FOO]n, [foo]n[=]n)dnl 581m4_defun([BAR]n, 582 [m4_require([FOO]]n[)dnl 583bar]n[=]n)[]dnl 584]) 585 586AS_IF([:], [BAR1]) 587echo "foo1=$foo1 bar1=$bar1" 588AS_IF([:], [], [BAR2]) 589echo "foo2=$foo2 bar2=$bar2" 590AS_IF([false], [BAR3]) 591echo "foo3=$foo3 bar3=$bar3" 592AS_IF([false], [], [BAR4]) 593echo "foo4=$foo4 bar4=$bar4" 594AS_CASE([x], [x], [BAR5]) 595echo "foo5=$foo5 bar5=$bar5" 596AS_CASE([x], [y], [BAR6]) 597echo "foo6=$foo6 bar6=$bar6" 598AS_CASE([x], 599 [x], [:], 600 [BAR7]) 601echo "foo7=$foo7 bar7=$bar7" 602AS_CASE([x], 603 [y], [:], 604 [BAR8]) 605echo "foo8=$foo8 bar8=$bar8" 606AS_CASE([x], 607 [y], [:], 608 [x], [BAR9]) 609echo "foo9=$foo9 bar9=$bar9" 610]]) 611 612AT_CHECK_M4SH 613AT_CHECK([./script], [0], [[one 614two 615three 616four 617five 618six 619seven 620eight 621nine 622ten 623foo1=1 bar1=1 624foo2=2 bar2= 625foo3=3 bar3= 626foo4=4 bar4=4 627foo5=5 bar5=5 628foo6=6 bar6= 629foo7=7 bar7= 630foo8=8 bar8=8 631foo9=9 bar9=9 632]]) 633 634AT_CLEANUP 635 636 637## --------------- ## 638## AS_LITERAL_IF. ## 639## --------------- ## 640 641AT_SETUP([AS@&t@_LITERAL_IF]) 642 643AT_DATA_M4SH([script.as], [[dnl 644AS_INIT 645echo AS_LITERAL_IF([lit], [ok], [ERR]) 1 646echo AS_LITERAL_IF([l$it], [ERR], [ok]) 2 647echo AS_LITERAL_IF([l``it], [ERR], [ok]) 3 648m4_define([mac], [lit]) 649echo AS_LITERAL_IF([mac], [ok], [ERR]) 4 650echo AS_LITERAL_IF([mac($, ``)], [ok], [ERR]) 5 651m4_define([mac], [l$it]) 652echo AS_LITERAL_IF([mac], [ERR], [ok]) 6 653m4_define([mac], [l`it]) 654echo AS_LITERAL_IF([mac], [ERR], [ok]) 7 655]]) 656 657AT_CHECK_M4SH 658AT_CHECK([./script], [], 659[[ok 1 660ok 2 661ok 3 662ok 4 663ok 5 664ok 6 665ok 7 666]]) 667 668AT_CLEANUP 669