summaryrefslogtreecommitdiff
path: root/tests/options.at
blob: 8503f8fca20e96ba628075db6b659b6434d97dcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
# Hand crafted tests for GNU M4.                               -*- Autotest -*-
# Copyright (C) 2001, 2006-2010, 2013 Free Software Foundation, Inc.

# This file is part of GNU M4.
#
# GNU M4 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNU M4 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

AT_BANNER([Options.])


## ------------------ ##
## deprecated options ##
## ------------------ ##

AT_SETUP([deprecated options])

dnl -N/--diversions was removed after 1.4.x
AT_CHECK_M4([--diversions=1], [1], [], [stderr])
AT_CHECK([$SED 's/Try.*help/Try `m4 --help/' stderr], [0],
[[m4: bad option
Try `m4 --help' for more information.
]])

dnl -H/--hashsize are no-ops since 2.0
AT_CHECK_M4([-H1 --hashsize=1], [0], [],
[[m4: warning: `-H' is deprecated
m4: warning: `--hashsize' is deprecated
]])

dnl -S/-T are no-ops for compatibility
AT_CHECK_M4([-S1 -T1], [0], [],
[[m4: warning: `-S' is deprecated
m4: warning: `-T' is deprecated
]])

dnl -Bint can be confused with its no-op meaning in 1.4.x, but all other
dnl uses of -B or its long option are okay
AT_CHECK_M4([-B1], [0], [],
[[m4: warning: recommend using `-B ./1' instead
]])
AT_CHECK_M4([-B./1 --prepend-include=1], [0], [],
[[]])

dnl --error-output is a misleading name
AT_CHECK_M4([--error-output=trace], [0], [],
[[m4: warning: `--error-output' is deprecated, use `--debugfile' instead
]])
AT_CHECK([rm trace])

dnl -o will change meaning in the future
AT_CHECK_M4([-otrace], [0], [],
[[m4: warning: `-o' is deprecated, use `--debugfile' instead
]])
AT_CHECK([rm trace])

dnl -e is an odd spelling for --interactive
AT_CHECK_M4([-e], [0], [],
[[m4: warning: `-e' is deprecated, use `-i' instead
]])

dnl --arglength is an older spelling for --debuglen
AT_CHECK_M4([--arglength=10], [0], [],
[[m4: warning: `--arglength' is deprecated, use `--debuglen' instead
]])

AT_CHECK_M4([--arglength=x], [1], [],
[[m4: warning: `--arglength' is deprecated, use `--debuglen' instead
m4: invalid --arglength argument 'x'
]])

dnl --word-regexp from 1.4.x is no longer supported.  Can't test for its
dnl alternate spelling -W, as that now means --warnings.
AT_CHECK_M4([[--word-regexp='[a-zA-Z0-9_]+']], [0], [],
[[m4: warning: `--word-regexp' is deprecated
]])

AT_CLEANUP


## ---------- ##
## file names ##
## ---------- ##

AT_SETUP([file names])

dnl Check that all files are processed even after missing file
AT_DATA([in], [[hello world
]])
AT_CHECK_M4([oops in], [1], [[hello world
]], [[m4: cannot open file 'oops': No such file or directory
]])

dnl Check that '-' means stdin, even if ./- exists.
AT_DATA([-], [[hi
]])
AT_CHECK_M4([-], [0])
AT_CHECK_M4([- --], [0])
AT_CHECK_M4([-- -], [0])
AT_CHECK_M4([./-], [0], [[hi
]])
AT_CHECK_M4([./- --], [0], [[hi
]])
AT_CHECK_M4([-- ./-], [0], [[hi
]])

AT_CLEANUP


## --------------- ##
## option grouping ##
## --------------- ##

AT_SETUP([option grouping])

dnl this test group depends on correct settings of POSIXLY_CORRECT
if test -n "${POSIXLY_CORRECT+set}" ; then
  (unset POSIXLY_CORRECT) >/dev/null 2>&1 || {
    echo "Problem unsetting POSIXLY_CORRECT"
    AT_CHECK([exit 77])
  }
  unset POSIXLY_CORRECT
fi

dnl This comes from the "Invoking m4" node; it was easier to write the
dnl test separately than to let generate.awk fiddle with it.
AT_DATA([a], [[foo 1
m@&t@4_dnl()
]])

AT_DATA([-d+f], [[hi
]])

dnl First, run with GNU semantics
AT_CHECK_M4([-QPDfoo -d a -d+f], [0], [[ 1
]])

AT_CHECK_M4([-Q -P -D foo -d -d+f -- a], [0], [[ 1
]])

AT_CHECK_M4([-QPDfoo -d -- a -d+f], [0], [[ 1
hi
]])

AT_CHECK_M4([-Q -P -D foo -d ./a ./-d+f], [0], [[ 1
hi
]])

AT_CHECK_M4([--def foo --debug a], [0], [[ 1
m@&t@4_dnl()
]])

AT_CHECK_M4([--define=foo --debug= -- a], [0], [[ 1
m@&t@4_dnl()
]])

dnl Next, run with POSIX semantics
POSIXLY_CORRECT=1
export POSIXLY_CORRECT

AT_CHECK_M4([-QPDfoo -d a -d+f], [0], [[ 1
]])

AT_CHECK_M4([-Q -P -D foo -d -d+f -- ./a], [0], [[ 1
]])

AT_CHECK_M4([-QPDfoo -d -- a -d+f], [0], [[ 1
hi
]])

AT_CHECK_M4([-Q -P -D foo -d ./a ./-d+f], [0], [[ 1
hi
]])

AT_CHECK_M4([--def foo --debug a], [0], [[ 1
m@&t@4_dnl()
]])

AT_CHECK_M4([--define=foo --debug= -- a], [0], [[ 1
m@&t@4_dnl()
]])

AT_CLEANUP


## --------------- ##
## POSIXLY_CORRECT ##
## --------------- ##

AT_SETUP([POSIXLY_CORRECT])

dnl this test group depends on correct settings of POSIXLY_CORRECT
if test -n "${POSIXLY_CORRECT+set}" ; then
  (unset POSIXLY_CORRECT) >/dev/null 2>&1 || {
    echo "Problem unsetting POSIXLY_CORRECT"
    AT_CHECK([exit 77])
  }
  unset POSIXLY_CORRECT
fi

dnl By default, GNU extensions and warnings are enabled
AT_DATA([in], [[__line__
dnl()
]])
AT_CHECK_M4([in], [0], [[1
]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0
]])
AT_CHECK_M4([in -g], [0], [[1
]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0
]])
AT_CHECK_M4([in -G], [0], [[__line__
]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0
]])
AT_CHECK_M4([in -W], [0], [[1
]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0
]])
AT_CHECK_M4([in -Q], [0], [[1
]])

dnl Next, run with POSIX semantics
POSIXLY_CORRECT=1
export POSIXLY_CORRECT

dnl POSIXLY_CORRECT implies -G -Q
AT_DATA([in], [[__line__
dnl()
]])
AT_CHECK_M4([in], [0], [[__line__
]])
AT_CHECK_M4([in -g], [0], [[1
]])
AT_CHECK_M4([in -G], [0], [[__line__
]])
AT_CHECK_M4([in -W], [0], [[__line__
]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0
]])
AT_CHECK_M4([in -Q], [0], [[__line__
]])

AT_CLEANUP

## -------------- ##
## unknown option ##
## -------------- ##

AT_SETUP([unknown option])

AT_CHECK_M4([--unknown], [1], [], [stderr])
AT_CHECK([$SED 's/Try.*help/Try `m4 --help/' stderr], [0],
[[m4: bad option
Try `m4 --help' for more information.
]])

AT_CLEANUP


## --------- ##
## debugfile ##
## --------- ##

AT_SETUP([--debugfile])

dnl For a while, CVS m4 mistakenly sent debug output to stdout
dnl when stdout and stderr were the same file.

AT_DATA([[in]], [[foo
]])

AT_CHECK_M4([--debugfile=trace1 -tfoo -Dfoo=bar in 2>&1], [0], [[bar
]])

AT_CHECK_M4([--debugfile=trace2 -tfoo -Dfoo=bar in], [0], [[bar
]])

AT_CHECK([cmp trace1 trace2])

dnl m4 1.4.x mistakenly created the trace file.  --help and --version
dnl should always override, even if they come later in the command line
AT_CHECK_M4([--debugfile=trace3 --help], [0], [ignore])
AT_CHECK_M4([--debugfile=trace3 --version], [0], [ignore])
AT_CHECK([test -f trace3], [1])

dnl check that trace file failure causes an error, but allows processing
AT_CHECK_M4([--debugfile=no_such_dir/trace -tfoo -Dfoo=bar in], [1],
[[bar
]], [[m4: cannot set debug file 'no_such_dir/trace': No such file or directory
m4trace: -1- foo -> `bar'
]])

dnl check that empty trace file discards trace data
AT_CHECK_M4([--debugfile= -tfoo -Dfoo=bar in], [0],
[[bar
]])

dnl check that all tracing gets diverted to the trace file.  Don't use
dnl AT_CHECK_M4 on the first run, because sanitizing stderr breaks the
dnl comparison with the raw data in the trace file of the second run.
AT_CHECK([$M4 -dV in], [0], [[foo
]], [stderr])
mv stderr expout
AT_CHECK_M4([--debugfile=trace4 -dV in], [0], [[foo
]])
AT_CHECK([cat trace4], [0], [expout])

dnl check that order matters
AT_DATA([in1], [[bar
]])
AT_DATA([in2], [[errprint(`hi
')dnl
bar
]])
AT_CHECK_M4([-Dbar=hello -tbar --debugfile= in1 --debugfile in2], [0],
[[hello
hello
]], [[hi
m4trace: -1- bar -> `hello'
]])

AT_CLEANUP


## -------- ##
## debuglen ##
## -------- ##

AT_SETUP([--debuglen])

dnl Check for argument validation.

AT_DATA([in],
[[define(`echo', `$@')dnl
traceon(`echo')dnl
echo(`long string')
]])

AT_CHECK_M4([--debuglen=-1 in], [1], [],
[[m4: invalid --debuglen argument '-1'
]])

AT_CHECK_M4([--debuglen oops in], [1], [],
[[m4: invalid --debuglen argument 'oops'
]])

AT_CHECK_M4([-l 10oops in], [1], [],
[[m4: invalid suffix in -l argument '10oops'
]])

dnl MiB is the suffix to implict 1, resulting in 1048576
AT_CHECK_M4([-lMiB in], [0], [[long string
]], [[m4trace: -1- echo(`long string') -> ``long string''
]])

dnl this assumes size_t is no bigger than 64 bits
AT_CHECK_M4([-l 123456789012345678901234567890 in], [1], [],
[[m4: -l argument '123456789012345678901234567890' too large
]])
AT_CHECK_M4([--debugl 123456789012345678901234567890 in], [1], [],
[[m4: --debuglen argument '123456789012345678901234567890' too large
]])

dnl per POSIX guidelines, this is a decimal number 10, not octal 8
AT_CHECK_M4([-l 010 in], [0], [[long string
]], [[m4trace: -1- echo(`long strin...') -> ``long stri...'
]])

AT_CHECK_M4([-l 3 in], [0], [[long string
]], [[m4trace: -1- echo(`lon...') -> ``lo...'
]])

AT_CHECK_M4([--debuglen=3 -l0 in], [0], [[long string
]], [[m4trace: -1- echo(`long string') -> ``long string''
]])

AT_CLEANUP


## --------- ##
## debugmode ##
## --------- ##

AT_SETUP([--debugmode])

AT_DATA([[in]],
[[include(`nested')dnl
len(`abc')
]])

AT_DATA([[nested]],
[[m4wrap(`divnum
')dnl
]])

dnl AT_CHECK_M4 starts life with -d.  Make sure it looks like -d+adeq.
AT_CHECK_M4([-tlen in], [0], [[3
0
]], [[m4trace: -1- len(`abc') -> `3'
]])

AT_CHECK_M4([-tlen -dfl -d in], [0], [[3
0
]], [[m4trace:in:2: -1- len(`abc') -> `3'
]])

dnl -d after file is deferred (but this is still useful for some flags).
AT_CHECK_M4([-tlen in -dfli], [0], [[3
0
]], [[m4trace: -1- len(`abc') -> `3'
m4debug: input from m4wrap recursion level 1
m4debug: input from m4wrap exhausted
]])

dnl Test all flags.
AT_CHECK_M4([-dV in], [0], [[3
0
]], [[m4debug: module m4: opening file
m4debug: module m4: init hook called
m4debug: module m4: opened
m4debug: module m4: builtins loaded
m4debug: module gnu: opening file
m4debug: module gnu: init hook called
m4debug: module gnu: opened
m4debug: module gnu: builtins loaded
m4debug: module gnu: macros loaded
m4debug: path search for 'in' found 'in'
m4debug: input read from 'in'
m4trace:in:1: -1- id 1: include ... = <include>{m4}
m4debug:in:1: path search for 'nested' found 'nested'
m4debug:in:1: input read from 'nested'
m4trace:in:1: -1- id 1: include(`nested') -> <file: nested>
m4trace:nested:1: -1- id 2: m4wrap ... = <m4wrap>{m4}
m4trace:nested:1: -1- id 2: m4wrap(`divnum
') -> `'
m4trace:nested:2: -1- id 3: dnl ... = <dnl>{m4}
m4trace:nested:2: -1- id 3: dnl -> `'
m4debug:nested:3: input reverted to in, line 1
m4trace:in:1: -1- id 4: dnl ... = <dnl>{m4}
m4trace:in:1: -1- id 4: dnl -> `'
m4trace:in:2: -1- id 5: len ... = <len>{m4}
m4trace:in:2: -1- id 5: len(`abc') -> `3'
m4debug:in:3: input exhausted
m4debug: input from m4wrap recursion level 1
m4trace:nested:1: -1- id 6: divnum ... = <divnum>{m4}
m4trace:nested:1: -1- id 6: divnum -> `0'
m4debug: input from m4wrap exhausted
]])

dnl Test addition and subtraction of flags.
AT_CHECK_M4([--debug=-e --debugmode=+xt in], [0], [[3
0
]], [[m4trace: -1- id 1: include(`nested')
m4trace: -1- id 2: m4wrap(`divnum
')
m4trace: -1- id 3: dnl
m4trace: -1- id 4: dnl
m4trace: -1- id 5: len(`abc')
m4trace: -1- id 6: divnum
]])

dnl Test that shorter prefix is ambiguous.
AT_CHECK_M4([--debu], [1], [], [stderr])
AT_CHECK([$SED -e 's/Try.*--help/Try `m4 --help/' stderr], [0],
[[m4: bad option
Try `m4 --help' for more information.
]])

AT_CLEANUP


## ---------------- ##
## discard comments ##
## ---------------- ##

AT_SETUP([--discard-comments])

AT_DATA([[in]],
[[This is not a comment # but this is.
# This line should disappear completely.
This should not disappear.
changecom(`<!--', `-->')
html <!--
comment
 --> ends.
]])

AT_CHECK_M4([-c in], [0],
[[This is not a comment This should not disappear.

html  ends.
]])

dnl check that even when discarding, a comment must be terminated by EOF
AT_DATA([in], [[changecom(`/*', `*/')dnl
/* unterminated comment
]])
AT_CHECK_M4([--discard-comments in], [1], [],
[[m4:in:2: end of file in comment
]])

AT_CLEANUP


## -------------- ##
## fatal warnings ##
## -------------- ##

AT_SETUP([--fatal-warnings])

AT_DATA([[in]],
[[dnl()
dnl()
dnl()
]])

dnl By default, warnings don't affect exit status
AT_CHECK_M4([in], [0], [],
[[m4:in:1: warning: dnl: extra arguments ignored: 1 > 0
m4:in:2: warning: dnl: extra arguments ignored: 1 > 0
m4:in:3: warning: dnl: extra arguments ignored: 1 > 0
]])

dnl With one -E, exit status changes, but execution continues
AT_CHECK_M4([-E in], [1], [],
[[m4:in:1: warning: dnl: extra arguments ignored: 1 > 0
m4:in:2: warning: dnl: extra arguments ignored: 1 > 0
m4:in:3: warning: dnl: extra arguments ignored: 1 > 0
]])

dnl With two -E, execution halts immediately
AT_CHECK_M4([--fatal-warnings --fatal-warnings in], [1], [],
[[m4:in:1: warning: dnl: extra arguments ignored: 1 > 0
]])

dnl Exit status can't be affected if there were no warnings
AT_CHECK_M4([-EEQ in], [0])

AT_CLEANUP


## ---------------- ##
## help and version ##
## ---------------- ##

AT_SETUP([--help and --version])

AT_CHECK_M4([--help], [0], [stdout])
AT_CHECK([[$SED -n -e 's|Usage:.*\[OPTION\]... \[FILE\]...|success|p' stdout]],
[0], [success
])

AT_CHECK_M4([--version], [0], [stdout])
AT_CHECK([[$SED -n -e 's|There is NO WARRANTY.*|success|p' stdout]],
[0], [success
])

dnl make sure option specified first takes precedence
AT_CHECK_M4([--help --version], [0], [stdout])
AT_CHECK([[$SED -n -e 's|Usage:.*\[OPTION\]... \[FILE\]...|success|p' stdout]],
[0], [success
])

AT_CHECK_M4([--version --help], [0], [stdout])
AT_CHECK([[$SED -n -e 's|There is NO WARRANTY.*|success|p' stdout]],
[0], [success
])

AT_CLEANUP


## ------------------ ##
## import-environment ##
## ------------------ ##

AT_SETUP([--import-environment])

AT_DATA([[in]],
[[`TEST'=TEST
`ZAPPED'=ZAPPED
`OVERRIDE'=OVERRIDE
]])

TEST='This is an environment variable'
export TEST

ZAPPED='This is an environment variable which we will delete'
export ZAPPED

OVERRIDE='This is an environment variable which we will change'
export OVERRIDE

AT_CHECK_M4([--import-environment -UZAPPED -DOVERRIDE='It is changed.' in],
[0], [[TEST=This is an environment variable
ZAPPED=ZAPPED
OVERRIDE=It is changed.
]])

AT_CLEANUP


## ------------- ##
## nesting-limit ##
## ------------- ##

AT_SETUP([--nesting-limit])

dnl Check for argument validation.

AT_DATA([in],
[[define(`echo', `$@')dnl
echo(echo(echo(echo(`nested string'))))
echo(echo(echo(echo(echo(echo(echo(echo(echo(`nested string')))))))))
]])

AT_CHECK_M4([--nesting-limit=-1 in], [1], [],
[[m4: invalid --nesting-limit argument '-1'
]])

AT_CHECK_M4([--nesting-limit oops in], [1], [],
[[m4: invalid --nesting-limit argument 'oops'
]])

AT_CHECK_M4([-L 10oops in], [1], [],
[[m4: invalid suffix in -L argument '10oops'
]])

dnl MiB is the suffix to implict 1, resulting in 1048576
AT_CHECK_M4([-LMiB in], [0], [[nested string
nested string
]])

dnl this assumes size_t is no bigger than 64 bits
AT_CHECK_M4([-L 123456789012345678901234567890 in], [1], [],
[[m4: -L argument '123456789012345678901234567890' too large
]])
AT_CHECK_M4([--nest 123456789012345678901234567890 in], [1], [],
[[m4: --nesting-limit argument '123456789012345678901234567890' too large
]])

AT_CHECK_M4([-L 5 in], [1], [[nested string
]],
[[m4:in:3: recursion limit of 5 exceeded, use -L<N> to change it
]])

dnl per POSIX guidelines, this is a decimal number 10, not octal 8
AT_CHECK_M4([-L 010 in], [0], [[nested string
nested string
]])

AT_CHECK_M4([--nesting-limit=3 -L0 in], [0], [[nested string
nested string
]])

AT_CLEANUP


## --------------- ##
## prepend-include ##
## --------------- ##

AT_SETUP([--prepend-include])

dnl Lots of data to set up.
AT_DATA([[in]],
[[include(`foo')dnl
include(`bar')dnl
include(`bad')dnl
include(`blah')dnl
]])

AT_CHECK([mkdir pre post])

AT_DATA([[pre/foo]], [[in pre/foo
]])
AT_DATA([[foo]], [[in ./foo
]])
AT_DATA([[bar]], [[in ./bar
]])
AT_DATA([[post/bar]], [[in post/bar
]])
AT_DATA([[post/blah]], [[in post/blah
]])

dnl Make circular links in the subdirectories, to ensure that the error
dnl message when bad cannot be opened comes from the search in ., regardless
dnl of what else was searched.
AT_CHECK([ln -s bad pre/bad])
AT_CHECK([ln -s bad post/bad])

dnl Finally, see that it all works.
AT_CHECK_M4([-I post -B pre in], [1],
[[in pre/foo
in ./bar
in post/blah
]], [[m4:in:3: include: cannot open file 'bad': No such file or directory
]])

AT_CLEANUP


## ------------- ##
## regexp-syntax ##
## ------------- ##

AT_SETUP([--regexp-syntax])

dnl test argument validation

AT_DATA([[in]], [[regexp(`(', `(')
]])

AT_CHECK_M4([--regexp-syntax=unknown in], [1], [],
[[m4: bad syntax-spec: 'unknown'
]])

AT_CHECK_M4([--regexp-syntax= in], [0], [[0
]])

AT_CHECK_M4([-rEXTENDED in], [0], [[
]], [[m4:in:1: warning: regexp: bad regular expression '(': Unmatched ( or \(
]])

AT_CHECK_M4([-rgnu-m4 in], [0], [[0
]])

AT_CHECK_M4([-r"gnu M4" in], [0], [[0
]])

dnl Test behavior of -r intermixed with files
AT_CHECK_M4([-rEXTENDED in --regexp-syntax in], [0], [[
0
]], [[m4:in:1: warning: regexp: bad regular expression '(': Unmatched ( or \(
]])

AT_CLEANUP


## ----- ##
## safer ##
## ----- ##

AT_SETUP([--safer])

dnl with --safer, the debugfile macro is crippled, but --debugfile is not
AT_DATA([[in]],
[[define(`foo', `1')foo
debugfile(`trace2')
define(`foo', `2')foo
]])

AT_CHECK_M4([--safer --debugfile=trace1 -t foo in], [1],
[[1

2
]], [[m4:in:2: debugfile: disabled by --safer
]])

AT_CHECK([test -f trace2], [1])
AT_CHECK([cat trace1], [0],
[[m4trace: -1- foo -> `1'
m4trace: -1- foo -> `2'
]])

dnl make sure builtin cannot bypass --safer, and that mkstemp does not
dnl create file
AT_DATA([[in]], [[builtin(`mkstemp', `./fooXXXXXX')
]])

AT_CHECK([echo foo*], [0], [foo*
])

AT_CHECK_M4([--safer in], [1], [[
]], [[m4:in:1: mkstemp: disabled by --safer
]])

AT_CHECK([echo foo*], [0], [foo*
])

AT_CLEANUP


## ---------- ##
## syncoutput ##
## ---------- ##

AT_SETUP([--syncoutput])

dnl -s/--synclines implies --syncoutput=1
AT_DATA([in], [[hi
]])
AT_CHECK_M4([-s in], [0],
[[#line 1 "in"
hi
]])
AT_CHECK_M4([--synclines in], [0],
[[#line 1 "in"
hi
]])

dnl test intermixing option and files
AT_CHECK_M4([in --syncoutput=1 in --syncoutput in], [0],
[[hi
#line 1 "in"
hi
hi
]])

dnl test parse error
AT_CHECK_M4([--syncoutput=huh in], [0],
[[hi
]], [[m4: warning: --syncoutput: unknown directive 'huh'
]])

AT_CLEANUP

AT_SETUP([--syncoutput and diversions])

dnl synclines should always start at the beginning of a line.
dnl this does not (yet) happen reliably when diversions do not
dnl always end in newline
AT_XFAIL_IF([:])
AT_DATA([in.m4], [[divert(2)2divert(1)1
dnl
undivert
]])

AT_CHECK_M4([-s in.m4], [0], [[1
#line 1 "stdin"
2
]])

AT_CLEANUP


## -------------------- ##
## traceon and traceoff ##
## -------------------- ##

AT_SETUP([--traceon and --traceoff])

dnl test intermixing with files
AT_DATA([[in]], [[divnum
]])
AT_CHECK_M4([-d+x in --traceon=divnum in --traceoff=divnum in], [0],
[[0
0
0
]], [[m4trace: -1- id 2: divnum -> `0'
]])

dnl test that --traceoff does not warn on untraced name
AT_CHECK_M4([--traceoff=unknown], [0])

AT_CLEANUP