summaryrefslogtreecommitdiff
path: root/tests/modules.at
blob: 1e9b2d95c5ea6bc305c0dca71ef4bf07ed6a8224 (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
# Hand crafted tests for GNU M4.                               -*- Autotest -*-
# Copyright (C) 2001, 2006, 2007, 2008, 2010 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([Module support.])


## --------- ##
## modfreeze ##
## --------- ##

AT_SETUP([Freezing modules])
AT_KEYWORDS([frozen])
AT_CHECK_DYNAMIC_MODULE

AT_DATA([[frozen.m4]],
[[divert(1)dnl
define(`test', `local::`test'')dnl
define(`test1', defn(`test'))dnl
->test
load(`modtest')
define(`test2', defn(`test'))dnl
->test
load(`shadow')
define(`test3', defn(`test'))dnl
->test
]])

AT_DATA([[unfrozen.m4]],
[[undivert(1)dnl
test1
test2
test3
]])

# First generate the `expout' ouput by running over the sources before
# freezing.
AT_CHECK_M4([-M "$abs_builddir" -m load frozen.m4 unfrozen.m4],
            [0], [stdout], [stderr])

mv stdout expout
mv stderr experr

# Now freeze the first source file.
AT_CHECK_M4([-M "$abs_builddir" -m load -F frozen.m4f frozen.m4],
            [0], [], [ignore])

# Now rerun the original sequence, but using the frozen file.
AT_CHECK_M4([-M "$abs_builddir" -R frozen.m4f unfrozen.m4],
            [0], [expout], [experr])

AT_CLEANUP([frozen.m4f])


## ------------------ ##
## module test macros ##
## ------------------ ##

AT_SETUP([module test macros])
AT_CHECK_DYNAMIC_MODULE
AT_CHECK_GMP

AT_DATA([in], [[load(`mpeval')
-__load__-__mpeval__-
unload(`mpeval')
-__load__-__mpeval__-
unload(`load')
-__load__-__mpeval__-
]])

AT_CHECK_M4([-m load in], [0], [[
---

--__mpeval__-

-__load__-__mpeval__-
]])

AT_CLEANUP


## ---------------------------- ##
## Exercising the test module.  ##
## ---------------------------- ##

# AT_CHECK_M4_MODTEST(TITLE, ENV-VARS, M4-OPTIONS)
# ------------------------------------------------
# Add a test named TITLE, running m4 with either ENV-VARS in the environment
# or M4-OPTIONS set to pick up test modules.
m4_define([AT_CHECK_M4_MODTEST],
[AT_SETUP([$1])
AT_CHECK_DYNAMIC_MODULE

AT_DATA([input.m4],
[[load(`modtest')
test
Dumpdef: dumpdef(`test').
unload(`modtest')
test
Dumpdef: dumpdef(`test').
]])

dnl Fortunately, all tests within AT_SETUP are in the same subshell, so
dnl setting the environment now will impact the AT_CHECK_M4, but not
dnl carry over to the next AT_SETUP.
m4_ifval([$2], [$2
export m4_substr([$2], [0], m4_index([$2], [=]))])

AT_CHECK_M4([-m load $3 input.m4], [0],
[[
Test module called.
Dumpdef: .

test
Dumpdef: .
]],
[[Test module loaded.
test:	<test>
Test module unloaded.
m4:input.m4:6: Warning: dumpdef: undefined macro `test'
]])

AT_CLEANUP
])



AT_CHECK_M4_MODTEST([--module-directory: absolute path],
                    [], [-M "$abs_builddir"])

AT_CHECK_M4_MODTEST([--module-directory: relative path],
                    [], [-M "$top_build_prefix/tests"])

AT_CHECK_M4_MODTEST([M4MODPATH: absolute path],
                    [M4MODPATH="$abs_builddir"], [])

AT_CHECK_M4_MODTEST([M4MODPATH: relative path],
                    [M4MODPATH="$top_build_prefix/tests"], [])

AT_CHECK_M4_MODTEST([LTDL_LIBRARY_PATH: absolute path],
                    [LTDL_LIBRARY_PATH="$abs_builddir"], [])

AT_CHECK_M4_MODTEST([LTDL_LIBRARY_PATH: relative path],
                    [LTDL_LIBRARY_PATH="$top_build_prefix/tests"], [])




## ------ ##
## shadow ##
## ------ ##

AT_SETUP([modules: shadow])
AT_CHECK_DYNAMIC_MODULE

AT_DATA([[input.m4]],
[[# no modules loaded yet
test
shadow

# define our own macros for `test' and `shadow'
define(`test', `local::`test'')
define(`shadow', `local::`shadow'')
test
shadow

# module Shadow defines `shadow' and `test' macros
load(`shadow')
dumpdef(`test')
dumpdef(`shadow')
test
shadow

# save the definition of `test' from the Shadow module
define(`Shadow::test', defn(`test'))

# module Modtest also defines a `test' macro
load(`modtest')
dumpdef(`test')
dumpdef(`shadow')
test
shadow

# Reloading Shadow shouldn't affect anything
load(`shadow')
dumpdef(`test')
dumpdef(`shadow')
test
shadow

# Unloading Modtest will unshadow the test definition in Shadow
unload(`modtest')
dumpdef(`test')
dumpdef(`shadow')
test
shadow

# Unloading Shadow once has no effect (we loaded it twice)
unload(`shadow')
dumpdef(`test')
dumpdef(`shadow')
test
shadow

# Unloading Shadow again will revert to copying `test' and the local
# `shadow' macro.
unload(`shadow')
test
shadow
]])

AT_DATA([[expout]],
[[# no modules loaded yet
test
shadow

# define our own macros for `test' and `shadow'


local::test
local::shadow

# module Shadow defines `shadow' and `test' macros
Shadow module loaded.


Shadow::test called.
Shadow::shadow called.

# save the definition of `test' from the Shadow module


# module Modtest also defines a `test' macro



Test module called.
Shadow::shadow called.

# Reloading Shadow shouldn't affect anything



Test module called.
Shadow::shadow called.

# Unloading Modtest will unshadow the test definition in Shadow



Shadow::test called.
Shadow::shadow called.

# Unloading Shadow once has no effect (we loaded it twice)



Shadow::test called.
Shadow::shadow called.

# Unloading Shadow again will revert to copying `test' and the local
# `shadow' macro.

local::test
local::shadow
]])

AT_DATA([[experr]],
[[test:	<test>
shadow:	<shadow>
Test module loaded.
test:	<test>
shadow:	<shadow>
test:	<test>
shadow:	<shadow>
Test module unloaded.
test:	<test>
shadow:	<shadow>
test:	<test>
shadow:	<shadow>
]])

AT_CHECK_M4([-M "$abs_builddir" -m load input.m4], [0],
            [expout], [experr])

AT_CLEANUP



## ------ ##
## unload ##
## ------ ##

AT_SETUP([modules: unload])
AT_CHECK_DYNAMIC_MODULE

AT_DATA([[input.m4]],
[[test
__test__
load(`modtest')
test
__test__
load(`shadow')
test
__test__
unload(`modtest')
test
__test__
load(`modtest')
test
__test__
unload(`modtest')
test
__test__
unload(`shadow')
test
__test__
]])

AT_DATA([[expout]],
[[test
__test__

Test module called.
modtest
Shadow module loaded.
Shadow::test called.
shadow

Shadow::test called.
shadow

Test module called.
modtest

Shadow::test called.
shadow

test
__test__
]])

AT_DATA([[experr]],
[[Test module loaded.
Test module unloaded.
Test module loaded.
Test module unloaded.
]])


AT_CHECK_M4([-M "$abs_builddir" -m load input.m4],
            [0], [expout], [experr])

AT_CLEANUP



## ----------------------- ##
## module symbol importing ##
## ----------------------- ##

# Importing a symbol from a not yet loaded module

# This test is ugly, because we need to canonicalize strerror strings
# to match our test.  So we save STDERR to a file, and run another check
# which edits that file and compares it to the canonical STDERR output
# from the first command:

AT_SETUP([modules: importing])
AT_CHECK_DYNAMIC_MODULE

AT_DATA([[input.m4]],
[[import
load(`import')
import
unload(`modtest')
import
symbol_fail
module_fail
]])

AT_DATA([[expout]],
[[import

import::import called.

import::import called.
import::symbol_fail called.
]])

AT_DATA([[experr]],
[[Test module loaded.

TRUE
Test module unloaded.
Test module loaded.

TRUE
m4:input.m4:6: cannot load symbol `no_such' from module `modtest'
m4:input.m4:7: cannot open module `no_such'
]])

AT_CHECK_M4([-M "$abs_builddir" -m load input.m4],
            [1], [expout], [experr])

AT_CLEANUP



## ------------------- ##
## text module symbols ##
## ------------------- ##

# Support text macros with requested numbers of parameters.

AT_SETUP([modules: text])
AT_CHECK_DYNAMIC_MODULE

AT_DATA([input.m4],
[[__test__
__test__(1)
__test__(1,2)
onearg
onearg(1)
onearg(1,2)
manyargs
manyargs(1)
manyargs(1,2)
]])

AT_CHECK_M4([-M "$abs_builddir" -m modtest input.m4], [0],
[[modtest
modtest
modtest

1
1

1
1,2
]], [[Test module loaded.
m4:input.m4:2: Warning: __test__: extra arguments ignored: 1 > 0
m4:input.m4:3: Warning: __test__: extra arguments ignored: 2 > 0
m4:input.m4:4: Warning: onearg: too few arguments: 0 < 1
m4:input.m4:6: Warning: onearg: extra arguments ignored: 2 > 1
Test module unloaded.
]])

AT_CLEANUP


## -------------------- ##
## trace module symbols ##
## -------------------- ##

# The trace bit should not be lost if a builtin is unloaded from
# memory and then redefined by a subsequent load.

AT_SETUP([modules: trace])
AT_CHECK_DYNAMIC_MODULE

AT_DATA([[input.m4]],
[[test
load(`shadow')
test
unload(`shadow')
test
load(`shadow')
test
]])

AT_DATA([[expout]],
[[test
Shadow module loaded.
Shadow::test called.

test
Shadow module loaded.
Shadow::test called.
]])

AT_DATA([[experr]],
[[m4trace: -1- test -> `Shadow::`test' called.'
m4trace: -1- test -> `Shadow::`test' called.'
]])


AT_CHECK_M4([-M "$abs_builddir" -m load -t test input.m4],
            [0], [expout], [experr])

AT_CLEANUP


## ----------------- ##
## unload gnu module ##
## ----------------- ##

AT_SETUP([unload gnu module])
AT_CHECK_DYNAMIC_MODULE

dnl Ensure that the gnu module does not leak memory.  I don't know how
dnl to portably artificially limit the heap to cause an out-of-memory
dnl condition in the case of a leak, but examining the run of this test
dnl in a debugger can show whether it is leaking.
AT_DATA([input.m4], [[divert(-1)
define(`forloop',
  `pushdef(`$1', `$2')_forloop(`$1', `$2', `$3', `$4')popdef(`$1')')
define(`_forloop',
  `$4`'ifelse($1, `$3', `',
    `define(`$1', incr($1))_forloop(`$1', `$2', `$3', `$4')')')
forloop(`i', `1', `5000', `unload(`gnu')load(`gnu')regexp(`123', `\(4\)?2')')
]])

AT_CHECK_M4([-m load input.m4], [0])

AT_CLEANUP


## ------------------ ##
## unload load module ##
## ------------------ ##

AT_SETUP([unload load module])
AT_CHECK_DYNAMIC_MODULE

dnl Ensure that the load module can be unloaded and reloaded (obviously,
dnl it can't reload itself; the reload occurs on the command line).  Since
dnl the module must be resident (after all, the `unload' builtin had better
dnl not unmap the memory for the code it is currently executing), make sure
dnl that resident modules are handled gracefully.
AT_DATA([in1.m4], [[__load__ 1
unload(`load') 2
__load__ 3
]])

AT_DATA([in2.m4], [[__load__ 4
load(`load') 5
__load__ 6
unload(`load') 7
__load__ 8
unload(`load') 9
__load__ 10
]])

AT_CHECK_M4([-m load in1.m4 -m load in2.m4], [0],
[[ 1
 2
__load__ 3
 4
 5
 6
 7
 8
 9
__load__ 10
]])

AT_CLEANUP