emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 01e082c9bfc53d103e5eb36d1b71b4000a0daf88 177014 bytes (raw)
name: testing/lisp/test-ox.el 	 # note: path name is non-authoritative(*)

   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
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
 
;;; test-ox.el --- Tests for ox.el                   -*- lexical-binding: t; -*-

;; Copyright (C) 2012-2016, 2019  Nicolas Goaziou

;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>

;; This file is not part of GNU Emacs.

;; This program 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.

;; This program 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 <https://www.gnu.org/licenses/>.

;;; Code:

(require 'cl-lib)
(require 'ox)
(require 'org-inlinetask)

(defun org-test-default-backend ()
  "Return a default export backend.
This backend simply returns parsed data as Org syntax."
  (org-export-create-backend
   :transcoders
   (mapcar (lambda (type)
	     (cons type
		   (lambda (o c _)
		     (funcall
		      (intern (format "org-element-%s-interpreter" type))
		      o c))))
	   (append org-element-all-elements org-element-all-objects))))

(defmacro org-test-with-parsed-data (data &rest body)
  "Execute body with parsed data available.
DATA is a string containing the data to be parsed.  BODY is the
body to execute.  Parse tree is available under the `tree'
variable, and communication channel under `info'."
  (declare (debug (form body)) (indent 1))
  `(org-test-with-temp-text ,data
     (org-export--delete-comment-trees)
     (let* ((tree (org-element-parse-buffer))
	    (info (org-combine-plists
		   (org-export--get-export-attributes)
		   (org-export-get-environment))))
       (org-export--prune-tree tree info)
       (org-export--remove-uninterpreted-data tree info)
       (let ((info (org-combine-plists
		    info (org-export--collect-tree-properties tree info))))
	 (ignore info) ;; Don't warn if the var is unused.
	 ,@body))))


\f
;;; Internal Tests

(ert-deftest test-org-export/org-export-copy-buffer ()
  "Test `org-export-copy-buffer' specifications."
  ;; The copy must not overwrite the original file.
  (org-test-with-temp-text-in-file
      "* Heading"
    (let ((file (buffer-file-name)))
      (with-current-buffer (org-export-copy-buffer)
        (insert "This must not go into the original file.")
        (save-buffer)
        (should
         (equal
          "* Heading"
          (with-temp-buffer
            (insert-file-contents file)
            (buffer-string)))))))
  ;; The copy must not show when re-opening the original file.
  (org-test-with-temp-text-in-file
      "* Heading"
    (let ((file (buffer-file-name))
          (buffer-copy (generate-new-buffer " *Org export copy*")))
      (org-export-with-buffer-copy
       :to-buffer buffer-copy
       (insert "This must not show as the original file.")
       (save-buffer))
      ;; Unassign the original buffer from file.
      (setq buffer-file-name nil)
      (should-not
       (equal buffer-copy (get-file-buffer file))))))

(ert-deftest test-org-export/bind-keyword ()
  "Test reading #+BIND: keywords."
  ;; Test with `org-export-allow-bind-keywords' set to t.
  (should
   (org-test-with-temp-text "#+BIND: test-ox-var value"
     (let ((org-export-allow-bind-keywords t))
       (org-export-get-environment)
       (eq test-ox-var 'value))))
  ;; Test with `org-export-allow-bind-keywords' set to nil.
  (should-not
   (org-test-with-temp-text "#+BIND: test-ox-var value"
     (let ((org-export-allow-bind-keywords nil))
       (org-export-get-environment)
       (boundp 'test-ox-var))))
  ;; BIND keywords are case-insensitive.
  (should
   (org-test-with-temp-text "#+bind: test-ox-var value"
     (let ((org-export-allow-bind-keywords t))
       (org-export-get-environment)
       (eq test-ox-var 'value))))
  ;; Preserve order of BIND keywords.
  (should
   (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
     (let ((org-export-allow-bind-keywords t))
       (org-export-get-environment)
       (eq test-ox-var 2))))
  ;; Read BIND keywords in setup files.
  (should
   (org-test-with-temp-text
       (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
     (let ((org-export-allow-bind-keywords t))
       (org-export-get-environment)
       ;; `variable' is bound inside the setupfile.
       (eq variable 'value))))
  ;; Verify that bound variables are seen during export.
  (should
   (equal "Yes\n"
	  (org-test-with-temp-text "#+BIND: test-ox-var value"
	    (let ((org-export-allow-bind-keywords t))
	      (org-export-as
	       (org-export-create-backend
		:transcoders
		'((section . (lambda (s c i)
			       (if (eq test-ox-var 'value) "Yes" "No")))))))))))

(ert-deftest test-org-export/parse-option-keyword ()
  "Test reading all standard #+OPTIONS: items."
  (should
   (let ((options
	  (org-export--parse-option-keyword
	   "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t \
*:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil \
stat:t title:t")))
     (and (eq (plist-get options :headline-levels) 1)
	  (eq (plist-get options :section-numbers) t)
	  (eq (plist-get options :preserve-breaks) t)
	  (eq (plist-get options :time-stamp-file) t)
	  (eq (plist-get options :with-archived-trees) t)
	  (eq (plist-get options :with-author) t)
	  (eq (plist-get options :with-drawers) t)
	  (eq (plist-get options :with-email) t)
	  (eq (plist-get options :with-emphasize) t)
	  (eq (plist-get options :with-entities) t)
	  (eq (plist-get options :with-fixed-width) t)
	  (eq (plist-get options :with-footnotes) t)
	  (eq (plist-get options :with-priority) t)
	  (eq (plist-get options :with-special-strings) t)
	  (eq (plist-get options :with-sub-superscript) t)
	  (eq (plist-get options :with-toc) t)
	  (eq (plist-get options :with-tables) t)
	  (eq (plist-get options :with-tags) t)
	  (eq (plist-get options :with-tasks) t)
	  (eq (plist-get options :with-timestamps) t)
	  (eq (plist-get options :with-todo-keywords) t)
	  (eq (plist-get options :with-inlinetasks) nil)
	  (eq (plist-get options :with-statistics-cookies) t)
	  (eq (plist-get options :with-title) t))))
  ;; Test some special values.
  (should
   (let ((options
	  (org-export--parse-option-keyword
	   "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo \
num:2 <:active")))
     (and (eq (plist-get options :with-archived-trees) 'headline)
	  (eq (plist-get options :with-sub-superscript) '{})
	  (eq (plist-get options :with-toc) 1)
	  (eq (plist-get options :with-tags) 'not-in-toc)
	  (eq (plist-get options :with-tasks) 'todo)
	  (eq (plist-get options :section-numbers) 2)
	  (eq (plist-get options :with-timestamps) 'active)
	  (equal (plist-get options :with-drawers) '("TEST")))))
  ;; Test backend specific values.
  (should
   (equal
    (org-export--parse-option-keyword
     "opt:t" (org-export-create-backend :options '((:option nil "opt"))))
    '(:option t)))
  ;; More than one property can refer to the same option item.
  (should
   (equal '(:opt1 t :opt2 t)
	  (org-export--parse-option-keyword
	   "opt:t"
	   (org-export-create-backend
	    :options '((:opt1 nil "opt") (:opt2 nil "opt"))))))
  ;; Ignore options with a missing value.
  (should
   (let ((options (org-export--parse-option-keyword "H: num:t")))
     (and (not (plist-get options :headline-levels))
          (plist-get options :section-numbers))))
  ;; Parse spaces inside brackets.
  (let ((options (org-export--parse-option-keyword "html-postamble:\"test space\"" 'html)))
    (should (equal "test space" (plist-get options :html-postamble)))))

(ert-deftest test-org-export/get-inbuffer-options ()
  "Test reading all standard export keywords."
  ;; Properties should follow buffer order.
  (should
   (equal
    (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
      (org-export--get-inbuffer-options))
    '(:language "fr" :creator "Me" :email "email")))
  ;; Test `space' behaviour.
  (should
   (equal
    (let ((backend (org-export-create-backend
		     :options '((:keyword "KEYWORD" nil nil space)))))
      (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
	(org-export--get-inbuffer-options backend)))
    '(:keyword "With spaces")))
  ;; Test `newline' behaviour.
  (should
   (equal
    (let ((backend (org-export-create-backend
		     :options '((:keyword "KEYWORD" nil nil newline)))))
      (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
	(org-export--get-inbuffer-options backend)))
    '(:keyword "With\ntwo lines")))
  ;; Test `split' behaviour.
  (should
   (equal
    (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
      (org-export--get-inbuffer-options))
    '(:select-tags ("a" "b"))))
  ;; Test `parse' behaviour.  `parse' implies `space' but preserve
  ;; line breaks.  Multi-line objects are allowed.
  (should
   (org-element-map
       (org-test-with-temp-text "#+TITLE: *bold*"
	 (plist-get (org-export--get-inbuffer-options) :title))
       'bold #'identity nil t))
  (should
   (equal
    (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
      (plist-get (org-export--get-inbuffer-options) :title))
    '("Some title with spaces")))
  (should
   (org-element-map
       (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
	 (plist-get (org-export--get-inbuffer-options) :title))
       'line-break #'identity nil t))
  (should
   (org-element-map
       (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
	 (plist-get (org-export--get-inbuffer-options) :title))
       'bold #'identity nil t))
  ;; Options set through SETUPFILE.
  (should
   (equal
    (org-test-with-temp-text
	(format "#+DESCRIPTION: l1
#+LANGUAGE: es
#+SELECT_TAGS: a
#+TITLE: a
#+SETUPFILE: \"%s/examples/setupfile.org\"
#+LANGUAGE: fr
#+SELECT_TAGS: c
#+TITLE: c"
		org-test-dir)
      (org-export--get-inbuffer-options))
    '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
  ;; Options set through SETUPFILE specified using a URL.
  (let ((buffer (generate-new-buffer "url-retrieve-output"))
        (org-resource-download-policy t))
    (unwind-protect
	;; Simulate successful retrieval of a setupfile from URL.
	(cl-letf (((symbol-function 'url-retrieve-synchronously)
		   (lambda (&rest_)
		     (with-current-buffer buffer
		       (insert "HTTP/1.1 200 OK

# Contents of http://link-to-my-setupfile.org
#+BIND: variable value
#+DESCRIPTION: l2
#+LANGUAGE: en
#+SELECT_TAGS: b
#+TITLE: b
#+PROPERTY: a 1
"))
		     buffer)))
	  (should
	   (equal
	    (org-test-with-temp-text
		"#+DESCRIPTION: l1
#+LANGUAGE: es
#+SELECT_TAGS: a
#+TITLE: a
#+SETUPFILE: \"http://link-to-my-setupfile.org\"
#+LANGUAGE: fr
#+SELECT_TAGS: c
#+TITLE: c"
	      (org-export--get-inbuffer-options))
	    '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c")))))
      (kill-buffer buffer)))
  ;; More than one property can refer to the same buffer keyword.
  (should
   (equal '(:k2 "value" :k1 "value")
	  (let ((backend (org-export-create-backend
			  :options '((:k1 "KEYWORD")
				     (:k2 "KEYWORD")))))
	    (org-test-with-temp-text "#+KEYWORD: value"
	      (org-export--get-inbuffer-options backend)))))
  ;; Keywords in commented subtrees are ignored.
  (should-not
   (equal "Me"
	  (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
				     (plist-get info :author))))
  (should-not
   (equal "Mine"
	  (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
				     (plist-get info :email))))
  ;; Keywords can be set to an empty value.
  (should-not
   (let ((user-full-name "Me"))
     (org-test-with-parsed-data "#+AUTHOR:"
				(plist-get info :author)))))

(ert-deftest test-org-export/get-subtree-options ()
  "Test setting options from headline's properties."
  ;; EXPORT_TITLE.
  (should
   (equal '("Subtree Title")
	  (org-test-with-temp-text "#+TITLE: Title
* Headline<point>
  :PROPERTIES:
  :EXPORT_TITLE: Subtree Title
  :END:
Paragraph"
	    (plist-get (org-export-get-environment nil t) :title))))
  ;; EXPORT_OPTIONS.
  (should
   (= 2
      (org-test-with-temp-text "#+OPTIONS: H:1
* Headline<point>
  :PROPERTIES:
  :EXPORT_OPTIONS: H:2
  :END:
Paragraph"
	(plist-get (org-export-get-environment nil t) :headline-levels))))
  ;; EXPORT_DATE.
  (should
   (equal '("29-03-2012")
	  (org-test-with-temp-text "#+DATE: today
* Headline<point>
  :PROPERTIES:
  :EXPORT_DATE: 29-03-2012
  :END:
Paragraph"
	    (plist-get (org-export-get-environment nil t) :date))))
  ;; Properties with `split' behaviour are stored as a list of
  ;; strings.
  (should
   (equal '("a" "b")
	  (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
* Headline<point>
  :PROPERTIES:
  :EXPORT_EXCLUDE_TAGS: a b
  :END:
Paragraph"
	    (plist-get (org-export-get-environment nil t) :exclude-tags))))
  ;; Handle :PROPERTY+: syntax.
  (should
   (equal '("a" "b")
	  (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
* Headline<point>
  :PROPERTIES:
  :EXPORT_EXCLUDE_TAGS: a
  :EXPORT_EXCLUDE_TAGS+: b
  :END:
Paragraph"
	    (plist-get (org-export-get-environment nil t) :exclude-tags))))
  ;; Export properties are case-insensitive.
  (should
   (equal '("29-03-2012")
	  (org-test-with-temp-text "* Headline
  :PROPERTIES:
  :EXPORT_Date: 29-03-2012
  :END:
Paragraph"
	    (plist-get (org-export-get-environment nil t) :date))))
  ;; Still grab correct options when section above is empty.
  (should
   (equal '("H12")
	  (org-test-with-temp-text "* H1\n** H11\n** H12<point>"
	    (plist-get (org-export-get-environment nil t) :title))))
  ;; More than one property can refer to the same node property.
  (should
   (equal '("1" "1")
	  (org-test-with-temp-text
	      "* H\n:PROPERTIES:\n:EXPORT_A: 1\n:END:\n<point>"
	    (let* ((backend (org-export-create-backend
			     :options '((:k1 "A")
					(:k2 "A"))))
		   (options (org-export-get-environment backend t)))
	      (list (plist-get options :k1) (plist-get options :k2)))))))

(ert-deftest test-org-export/get-ordinal ()
  "Test specifications for `org-export-get-ordinal'."
  ;; Table numbering with, without predicates, and with other types.
  (org-test-with-temp-text
      "#+title: Table numbering test
#+options: author:nil toc:nil

#+caption: Should be Table 1
| h1       | h2       | h3       |
|----------+----------+----------|
| abcdefgh | ijklmnop | qrstuvwx |

#+caption: Should be Table 2
| h1       | h2       | h3       |
|----------+----------+----------|
| abcdefgh | ijklmnop | qrstuvwx |

#+caption: Should be Table 3
| h1       | h2       | h3       |
|----------+----------+----------|
| abcdefgh | ijklmnop | qrstuvwx |

#+caption: Should be Table 4
| h1       | h2       | h3       |
|----------+----------+----------|
| abcdefgh | ijklmnop | qrstuvwx |"
    (org-export-as
     (org-export-create-backend
      :parent 'org
      :transcoders
      '((table
         .
         (lambda (table contents info)
           (let ((from-third (lambda (table info)
                               (<= 3 (org-export-get-ordinal table info)))))
             (pcase (org-element-interpret-data (org-export-get-caption table))
               ("Should be Table 1"
                (should (= 1 (org-export-get-ordinal table info)))
                (should (= 2 (org-export-get-ordinal table info '(section))))
                (should (= 1 (org-export-get-ordinal table info nil #'org-ascii--has-caption-p)))
                (should-not (org-export-get-ordinal table info nil from-third)))
               ("Should be Table 2"
                (should (= 2 (org-export-get-ordinal table info)))
                (should (= 3 (org-export-get-ordinal table info '(section))))
                (should (= 2 (org-export-get-ordinal table info nil #'org-ascii--has-caption-p)))
                (should-not (org-export-get-ordinal table info nil from-third)))
               ("Should be Table 3"
                (should (= 3 (org-export-get-ordinal table info)))
                (should (= 4 (org-export-get-ordinal table info '(section))))
                (should (= 3 (org-export-get-ordinal table info nil #'org-ascii--has-caption-p)))
                (should (= 1 (org-export-get-ordinal table info nil from-third))))
               ("Should be Table 4"
                (should (= 4 (org-export-get-ordinal table info)))
                (should (= 5 (org-export-get-ordinal table info '(section))))
                (should (= 4 (org-export-get-ordinal table info nil #'org-ascii--has-caption-p)))
                (should (= 2 (org-export-get-ordinal table info nil from-third))))))
           "")))))))

(ert-deftest test-org-export/set-title ()
  "Test title setting."
  ;; Without TITLE keyword.
  (should
   (equal
    ""
    (let (org-export-filter-body-functions
	  org-export-filter-final-output-functions)
      (org-test-with-temp-text "Test"
	(org-export-as
	 (org-export-create-backend
	  :transcoders
	  '((template . (lambda (text info)
			  (org-element-interpret-data
			   (plist-get info :title)))))))))))
  ;; With a blank TITLE keyword.
  (should
   (equal
    ""
    (let (org-export-filter-body-functions
	  org-export-filter-final-output-functions)
      (org-test-with-temp-text "#+TITLE:\nTest"
	(org-export-as
	 (org-export-create-backend
	  :transcoders
	  '((template . (lambda (text info)
			  (org-element-interpret-data
			   (plist-get info :title)))))))))))
  ;; With a non-empty TITLE keyword.
  (should
   (equal
    "Title"
    (org-test-with-temp-text "#+TITLE: Title\nTest"
      (org-export-as
       (org-export-create-backend
	:transcoders
	'((template . (lambda (text info)
			(org-element-interpret-data
			 (plist-get info :title))))))))))
  ;; When exporting a subtree, its heading becomes the headline of the
  ;; document...
  (should
   (equal
    "Headline"
    (org-test-with-temp-text "* Headline\nBody"
      (org-export-as
       (org-export-create-backend
	:transcoders
	'((template . (lambda (text info)
			(org-element-interpret-data
			 (plist-get info :title))))))
       'subtree))))
  ;; ... unless there is an EXPORT_TITLE property at the root of the
  ;; subtree.
  (should
   (equal
    "B"
    (org-test-with-temp-text
	"* A\n  :PROPERTIES:\n  :EXPORT_TITLE: B\n  :END:\nBody"
      (org-export-as
       (org-export-create-backend
	:transcoders
	'((template . (lambda (text info)
			(org-element-interpret-data
			 (plist-get info :title))))))
       'subtree)))))

(ert-deftest test-org-export/handle-options ()
  "Test if export options have an impact on output."
  ;; Test exclude tags for headlines and inlinetasks.
  (should
   (equal ""
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text "* Head1 :noexp:"
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:exclude-tags ("noexp")))))))
  (should
   (equal "#+filetags: noexp\n"
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text "#+FILETAGS: noexp\n* Head1"
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:exclude-tags ("noexp")))))))
  ;; Excluding a tag excludes its whole group.
  (should
   (equal ""
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text "* Head1 :baz:"
	      (let ((org-tag-alist '((:startgrouptag)
				     ("foo") (:grouptags) ("bar") ("baz")
				     (:endgrouptag))))
		(org-export-as (org-test-default-backend)
			       nil nil nil '(:exclude-tags ("foo"))))))))
  ;; Test include tags for headlines and inlinetasks.
  (should
   (equal (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
	    (let ((org-tags-column 0))
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:select-tags ("exp")))))
	  "* H2\n** Sub :exp:\n*** Sub Sub\n"))
  ;; Including a tag includes its whole group.
  (should
   (string-match-p
    "\\`\\* H2"
    (let (org-export-filter-body-functions
	  org-export-filter-final-output-functions)
      (org-test-with-temp-text "* H1\n* H2 :bar:"
	(let ((org-tag-alist '((:startgrouptag)
			       ("foo") (:grouptags) ("bar") ("baz")
			       (:endgrouptag))))
	  (org-export-as (org-test-default-backend)
			 nil nil nil '(:select-tags ("foo"))))))))
  ;; If there is an include tag, ignore the section before the first
  ;; headline, if any.
  (should
   (equal (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
	    (let ((org-tags-column 0))
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:select-tags ("exp")))))
	  "* H1 :exp:\nBody\n"))
  (should
   (equal (org-test-with-temp-text "#+FILETAGS: exp\nFirst section\n* H1\nBody"
	    (org-export-as (org-test-default-backend)
			   nil nil nil '(:select-tags ("exp"))))
	  "* H1\nBody\n"))
  (should-not
   (equal (org-test-with-temp-text "* H1 :exp:\nBody"
	    (let ((org-tags-column 0))
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:select-tags ("exp")))))
	  "* H1 :exp:\n"))
  ;; Test mixing include tags and exclude tags.
  (should
   (string-match
    "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
    (org-test-with-temp-text "
* Head1 :export:
** Sub-Head1 :noexport:
** Sub-Head2
* Head2 :noexport:
** Sub-Head1 :export:"
      (org-export-as (org-test-default-backend) nil nil nil
		     '(:select-tags ("export") :exclude-tags ("noexport"))))))
  ;; Ignore tasks.
  (should
   (equal ""
	  (let ((org-todo-keywords '((sequence "TODO" "DONE")))
		org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text "* TODO Head1"
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:with-tasks nil))))))
  (should
   (equal "* TODO Head1\n"
	  (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
	    (org-test-with-temp-text "* TODO Head1"
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:with-tasks t))))))
  ;; Archived tree.
  (should
   (equal ""
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text "* Head1 :ARCHIVE:"
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:with-archived-trees nil))))))
  (should
   (string-match
    "\\* Head1[ \t]+:ARCHIVE:"
    (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
      (org-export-as (org-test-default-backend) nil nil nil
		     '(:with-archived-trees headline)))))
  (should
   (string-match
    "\\`\\* Head1[ \t]+:ARCHIVE:\n\\'"
    (org-test-with-temp-text "* Head1 :ARCHIVE:"
      (org-export-as (org-test-default-backend)
		     nil nil nil '(:with-archived-trees t)))))
  ;; Broken links.  Depending on `org-export-with-broken-links', raise
  ;; an error, ignore link or mark is as broken in output.
  (should-error
   (org-test-with-temp-text "[[#broken][link]]"
     (let ((backend
	    (org-export-create-backend
	     :transcoders
	     '((section . (lambda (_e c _i) c))
	       (paragraph . (lambda (_e c _i) c))
	       (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
       (org-export-as backend nil nil nil '(:with-broken-links nil)))))
  (should
   (org-test-with-temp-text "[[#broken][link]]"
     (let ((backend
	    (org-export-create-backend
	     :transcoders
	     '((section . (lambda (_e c _i) c))
	       (paragraph . (lambda (_e c _i) c))
	       (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
       (org-export-as backend nil nil nil '(:with-broken-links t)))))
  (should
   (org-test-with-temp-text "[[#broken][link]]"
     (let ((backend
	    (org-export-create-backend
	     :transcoders
	     '((section . (lambda (_e c _i) c))
	       (paragraph . (lambda (_e c _i) c))
	       (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
       (org-string-nw-p
	(org-export-as backend nil nil nil '(:with-broken-links mark))))))
  ;; Clocks.
  (should
   (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
		 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
		   (org-export-as (org-test-default-backend)
				  nil nil nil '(:with-clocks t)))))
  (should
   (equal ""
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:with-clocks nil))))))
  ;; Drawers.
  (should
   (equal ""
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text ":TEST:\ncontents\n:END:"
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:with-drawers nil))))))
  (should
   (equal ":TEST:\ncontents\n:END:\n"
	  (org-test-with-temp-text ":TEST:\ncontents\n:END:"
	    (org-export-as (org-test-default-backend)
			   nil nil nil '(:with-drawers t)))))
  (should
   (equal ":FOO:\nkeep\n:END:\n"
	  (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
	    (org-export-as (org-test-default-backend)
			   nil nil nil '(:with-drawers ("FOO"))))))
  (should
   (equal ":FOO:\nkeep\n:END:\n"
	  (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
	    (org-export-as (org-test-default-backend)
			   nil nil nil '(:with-drawers (not "BAR"))))))
  ;; Fixed-width.
  (should
   (equal ": A\n"
	  (org-test-with-temp-text ": A"
	    (org-export-as (org-test-default-backend) nil nil nil
			   '(:with-fixed-width t)))))
  (should
   (equal ""
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text ": A"
	      (org-export-as (org-test-default-backend) nil nil nil
			     '(:with-fixed-width nil))))))
  ;; Footnotes.
  (should
   (equal "Footnote?"
	  (let ((org-footnote-section nil))
	    (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
	      (org-trim (org-export-as (org-test-default-backend)
				       nil nil nil '(:with-footnotes nil)))))))
  (should
   (equal "Footnote?[fn:1]\n\n[fn:1] Def"
	  (let ((org-footnote-section nil))
	    (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
	      (org-trim (org-export-as (org-test-default-backend)
				       nil nil nil '(:with-footnotes t)))))))
  ;; Inlinetasks.
  (when (featurep 'org-inlinetask)
    (should
     (equal
      ""
      (let ((org-inlinetask-min-level 15)
	    org-export-filter-body-functions
	    org-export-filter-final-output-functions)
	(org-test-with-temp-text "*************** Task"
	  (org-export-as (org-test-default-backend)
			 nil nil nil '(:with-inlinetasks nil))))))
    (should
     (equal
      ""
      (let ((org-inlinetask-min-level 15)
	    org-export-filter-body-functions
	    org-export-filter-final-output-functions)
	(org-test-with-temp-text
	    "*************** Task\nContents\n*************** END"
	  (org-export-as (org-test-default-backend)
			 nil nil nil '(:with-inlinetasks nil)))))))
  ;; Plannings.
  (should
   (string-match
    "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
    (let ((org-closed-string "CLOSED:"))
      (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
	(org-export-as (org-test-default-backend)
		       nil nil nil '(:with-planning t))))))
  (should
   (equal "* H\n"
	  (let ((org-closed-string "CLOSED:"))
	    (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
	      (org-export-as (org-test-default-backend)
			     nil nil nil '(:with-planning nil))))))
  ;; Property Drawers.
  (should
   (equal "* H1\n"
	  (org-test-with-temp-text
	      "* H1\n  :PROPERTIES:\n  :PROP: value\n  :END:"
	    (org-export-as (org-test-default-backend)
			   nil nil nil '(:with-properties nil)))))
  (should
   (equal "* H1\n:PROPERTIES:\n:PROP:     value\n:END:\n"
	  (org-test-with-temp-text
	      "* H1\n  :PROPERTIES:\n  :PROP: value\n  :END:"
	    (org-export-as (org-test-default-backend)
			   nil nil nil '(:with-properties t)))))
  (should
   (equal "* H1\n:PROPERTIES:\n:B:        2\n:END:\n"
	  (org-test-with-temp-text
	      "* H1\n  :PROPERTIES:\n  :A: 1\n  :B: 2\n:END:"
	    (org-export-as (org-test-default-backend)
			   nil nil nil '(:with-properties ("B"))))))
  ;; Statistics cookies.
  (should
   (equal "* Stats"
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-trim
	     (org-test-with-temp-text "* Stats [0/0]"
	       (org-export-as (org-test-default-backend)
			      nil nil nil '(:with-statistics-cookies nil)))))))
  ;; Tables.
  (should
   (equal "| A |\n"
	  (org-test-with-temp-text "| A |"
	    (org-export-as (org-test-default-backend) nil nil nil
			   '(:with-tables t)))))
  (should
   (equal ""
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text "| A |"
	      (org-export-as (org-test-default-backend) nil nil nil
			     '(:with-tables nil)))))))

(ert-deftest test-org-export/with-timestamps ()
  "Test `org-export-with-timestamps' specifications."
  ;; t value.
  (should
   (string-match
    "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
    (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
      (org-export-as (org-test-default-backend)
		     nil nil nil '(:with-timestamps t)))))
  ;; nil value.
  (should
   (equal
    ""
    (let (org-export-filter-body-functions
	  org-export-filter-final-output-functions)
      (org-trim
       (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
	 (org-export-as (org-test-default-backend)
			nil nil nil '(:with-timestamps nil)))))))
  ;; `active' value.
  (should
   (string-match
    "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
    (org-test-with-temp-text
	"<2012-03-29 Thu>[2012-03-29 Thu]

Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
      (org-export-as (org-test-default-backend)
		     nil nil nil '(:with-timestamps active)))))
  ;; `inactive' value.
  (should
   (string-match
    "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
    (org-test-with-temp-text
	"<2012-03-29 Thu>[2012-03-29 Thu]

Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
      (org-export-as (org-test-default-backend)
		     nil nil nil '(:with-timestamps inactive))))))

(ert-deftest test-org-export/comment-tree ()
  "Test if export process ignores commented trees."
  (should
   (equal ""
	  (let (org-export-filter-body-functions
		org-export-filter-final-output-functions)
	    (org-test-with-temp-text "* COMMENT Head1"
	      (org-export-as (org-test-default-backend)))))))

(ert-deftest test-org-export/uninterpreted ()
  "Test handling of uninterpreted elements."
  ;; Entities.
  (should
   (equal "dummy\n"
	  (org-test-with-temp-text "\\alpha"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((entity . (lambda (e c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-entities t)))))
  (should
   (equal "\\alpha\n"
	  (org-test-with-temp-text "\\alpha"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((entity . (lambda (e c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-entities nil)))))
  ;; Emphasis.
  (should
   (equal "dummy\n"
	  (org-test-with-temp-text "*bold*"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((bold . (lambda (b c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-emphasize t)))))
  (should
   (equal "*bold*\n"
	  (org-test-with-temp-text "*bold*"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((bold . (lambda (b c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-emphasize nil)))))
  (should
   (equal "/simple/ /example/\n"
	  (org-test-with-temp-text "/simple/ /example/"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((bold . (lambda (b c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-emphasize nil)))))
  ;; LaTeX environment.
  (should
   (equal "dummy\n"
	  (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((latex-environment . (lambda (l c i) "dummy"))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-latex t)))))
  (should
   (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
	  (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((latex-environment . (lambda (l c i) "dummy"))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-latex verbatim)))))
  ;; LaTeX fragment.
  (should
   (equal "dummy\n"
	  (org-test-with-temp-text "$1$"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-latex t)))))
  (should
   (equal "$1$\n"
	  (org-test-with-temp-text "$1$"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-latex verbatim)))))
  (should
   (equal "$1$ \n"
	  (org-test-with-temp-text "$1$ "
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-latex verbatim)))))
  ;; Sub/superscript.
  (should
   (equal "adummy\n"
	  (org-test-with-temp-text "a_b"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((subscript . (lambda (s c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript t)))))
  (should
   (equal "a_b\n"
	  (org-test-with-temp-text "a_b"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((subscript . (lambda (s c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript nil)))))
  (should
   (equal "a_b\n"
	  (org-test-with-temp-text "a_b"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((subscript . (lambda (s c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript {})))))
  (should
   (equal "adummy\n"
	  (org-test-with-temp-text "a_{b}"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((subscript . (lambda (s c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript {})))))
  (should
   (equal "a_entity\n"
	  (org-test-with-temp-text "a_\\alpha"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((entity . (lambda (e c i) "entity"))
			     (subscript . (lambda (s c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript nil)))))
  ;; Handle uninterpreted objects in parsed keywords.
  (should
   (equal "a_b"
	  (org-test-with-temp-text "#+TITLE: a_b"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders
	      '((subscript . (lambda (s c i) "dummy"))
		(template . (lambda (c i)
			      (org-export-data (plist-get i :title) i)))
		(section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript nil)))))
  (should
   (equal "a_b"
	  (org-test-with-temp-text "#+FOO: a_b"
	    (org-export-as
	     (org-export-create-backend
	      :options
	      '((:foo "FOO" nil nil parse))
	      :transcoders
	      '((subscript . (lambda (s c i) "dummy"))
		(template . (lambda (c i)
			      (org-export-data (plist-get i :foo) i)))
		(section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript nil)))))
  ;; Objects in parsed keywords are "uninterpreted" before filters are
  ;; applied.
  (should
   (org-test-with-temp-text "#+TITLE: a_b"
     (org-export-as
      (org-export-create-backend
       :filters
       '((:filter-options
	  (lambda (i _)
	    (org-element-map (plist-get i :title) 'subscript
	      (lambda (_) (error "There should be no subscript here")))))))
      nil nil nil '(:with-sub-superscript nil))))
  ;; Handle uninterpreted objects in captions.
  (should
   (equal "adummy\n"
	  (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders
	      '((subscript . (lambda (s c i) "dummy"))
		(paragraph . (lambda (p c i)
			       (org-export-data (org-export-get-caption p) i)))
		(section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript t)))))
  (should
   (equal "a_b\n"
	  (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders
	      '((subscript . (lambda (s c i) "dummy"))
		(paragraph . (lambda (p c i)
			       (org-export-data (org-export-get-caption p) i)))
		(section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript nil)))))
  ;; Special case: multiples uninterpreted objects in a row.
  (should
   (equal "a_b_c_d\n"
	  (org-test-with-temp-text "a_b_c_d"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((subscript . (lambda (s c i) "dummy"))
			     (paragraph . (lambda (p c i) c))
			     (section . (lambda (s c i) c))))
	     nil nil nil '(:with-sub-superscript {}))))))

(ert-deftest test-org-export/export-scope ()
  "Test all export scopes."
  ;; Subtree.
  (should
   (equal "text\n*** H3\n"
	  (org-test-with-temp-text "* H1\n<point>** H2\ntext\n*** H3"
	    (org-export-as (org-test-default-backend) 'subtree))))
  (should
   (equal "text\n*** H3\n"
	  (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
	    (org-export-as (org-test-default-backend) 'subtree))))
  ;; Subtree with a code block calling another block outside.
  (should
   (equal ": 3\n"
	  (org-test-with-temp-text "
<point>* Head1
#+BEGIN_SRC emacs-lisp :noweb yes :exports results
<<test>>
#+END_SRC
* Head2
#+NAME: test
#+BEGIN_SRC emacs-lisp
\(+ 1 2)
#+END_SRC"
	    (let ((org-export-use-babel t))
	      (org-export-as (org-test-default-backend) 'subtree)))))
  ;; Subtree export should ignore leading planning line and property
  ;; drawer.
  (should
   (equal "Text\n"
	  (org-test-with-temp-text "
<point>* H
SCHEDULED: <2012-03-29 Thu>
:PROPERTIES:
:A: 1
:END:
Text"
	    (org-export-as (org-test-default-backend)
			   'subtree nil nil
			   '(:with-planning t :with-properties t)))))
  (should
   (equal ""
	  (org-test-with-temp-text "
* H
:PROPERTIES:
:A: 1
:END:<point>
* H2"
	    (org-export-as (org-test-default-backend)
			   'subtree))))
  ;; Visible.
  (should
   (equal "* H1\n"
	  (org-test-with-temp-text "* H1\n** H2\ntext\n*** H3"
	    (org-cycle)
	    (org-export-as (org-test-default-backend) nil 'visible))))
  ;; Region.
  (should
   (equal "text\n"
	  (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
	    (transient-mark-mode 1)
	    (push-mark (point) t t)
	    (end-of-line)
	    (org-export-as (org-test-default-backend)))))
  ;; Body only.
  (should
   (equal "Text\n"
	  (org-test-with-temp-text "Text"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders
	      '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
		(section . (lambda (_s c _i) c))
		(paragraph . (lambda (_p c _i) c))))
	     nil nil 'body-only))))
  (should
   (equal "BEGIN\nText\nEND"
	  (org-test-with-temp-text "Text"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders
	      '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
		(section . (lambda (_s c _i) c))
		(paragraph . (lambda (_p c _i) c))))))))
  ;; Pathological case: Body only on an empty buffer is expected to
  ;; return an empty string, not nil.
  (should
   (org-test-with-temp-text ""
     (org-export-as (org-test-default-backend) nil nil t))))

(ert-deftest test-org-export/output-file-name ()
  "Test `org-export-output-file-name' specifications."
  ;; Export from a file: name is built from original file name.
  (should
   (org-test-with-temp-text-in-file "Test"
     (equal (file-name-base (buffer-file-name))
	    (file-name-base (org-export-output-file-name ".ext")))))
  ;; When #+EXPORT_FILE_NAME is defined, use it.
  (should
   (equal "test.ext"
	  (org-test-with-temp-text-in-file "#+EXPORT_FILE_NAME: test"
	    (org-export-output-file-name ".ext" t))))
  ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  (should
   (equal "test.ext"
	  (org-test-with-temp-text-in-file
	      "* Test\n  :PROPERTIES:\n  :EXPORT_FILE_NAME: test\n  :END:"
	    (org-export-output-file-name ".ext" t))))
  (should
   (equal "property.ext"
	  (org-test-with-temp-text
	      "#+EXPORT_FILE_NAME: keyword
* Test<point>
:PROPERTIES:
:EXPORT_FILE_NAME: property
:END:"
	    (org-export-output-file-name ".ext" t))))
  ;; From a buffer not associated to a file, too.
  (should
   (equal "test.ext"
	  (org-test-with-temp-text
	      "* Test\n  :PROPERTIES:\n  :EXPORT_FILE_NAME: test\n  :END:"
	    (org-export-output-file-name ".ext" t))))
  ;; When provided name is absolute, preserve it.
  (should
   (org-test-with-temp-text
       (format "* Test\n  :PROPERTIES:\n  :EXPORT_FILE_NAME: %s\n  :END:"
	       (expand-file-name "test"))
     (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  ;; When PUB-DIR argument is provided, use it.
  (should
   (equal "dir/"
	  (org-test-with-temp-text-in-file "Test"
	    (file-name-directory
	     (org-export-output-file-name ".ext" nil "dir/")))))
  ;; PUB-DIR has precedence over EXPORT_FILE_NAME keyword or property.
  (should
   (equal "pub-dir/"
	  (org-test-with-temp-text-in-file
	      "#+EXPORT_FILE_NAME: /dir/keyword\nTest"
	    (file-name-directory
	     (org-export-output-file-name ".ext" nil "pub-dir/")))))
  ;; When returned name would overwrite original file, add EXTENSION
  ;; another time.
  (should
   (equal "normal.org.org"
	  (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
	    (org-export-output-file-name ".org")))))

(ert-deftest test-org-export/expand-include ()
  "Test file inclusion in an Org buffer."
  ;; Error when file isn't specified.
  (should-error
   (org-test-with-temp-text "#+INCLUDE: dummy.org"
     (org-export-expand-include-keyword)))
  ;; Refuse to expand keywords in commented headings.
  (should
   (org-test-with-temp-text "* COMMENT H1\n#+INCLUDE: dummy.org"
     (org-export-expand-include-keyword)
     t))
  ;; Full insertion with recursive inclusion.
  (should
   (equal
    (with-temp-buffer
      (insert-file
       (expand-file-name "examples/include.org" org-test-dir))
      (replace-regexp-in-string
       (regexp-quote "#+INCLUDE: \"include2.org\"")
       "Success!" (buffer-string)))
    (org-test-with-temp-text
	(format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Localized insertion.
  (org-test-with-temp-text
      (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
	      org-test-dir)
    (org-export-expand-include-keyword)
    (should (equal (buffer-string)
		   "Small Org file with an include keyword.\n")))
  ;; Insertion with constraints on headlines level.
  (should
   (equal
    "* Top heading\n** Heading\nbody\n"
    (org-test-with-temp-text
	(format
	 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
	 org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  (should
   (equal
    "* Top heading\n* Heading\nbody\n"
    (org-test-with-temp-text
	(format
	 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
	 org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Inclusion within an example block.
  (should
   (equal
    "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
    (org-test-with-temp-text
	(format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
		org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Inclusion within a src-block.
  (should
   (equal
    "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
    (org-test-with-temp-text
	(format
	 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
	 org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Inclusion within an html export-block.
  (should
   (equal
    "#+BEGIN_EXPORT html\n<p>HTML!</p>\n#+END_EXPORT\n"
    (org-test-with-temp-text
	(format
	 "#+INCLUDE: \"%s/examples/include.html\" EXPORT html"
	 org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Inclusion within an center paragraph
  (should
   (equal
    "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
    (org-test-with-temp-text
	(format
	 "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
	 org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Footnotes labels are local to each included file.
  (should
   (= 6
      (length
       (delete-dups
	(let ((contents "
Footnotes[fn:1], [fn:test], [fn:test] and [fn:inline:inline footnote].
\[fn:1] Footnote 1
\[fn:test] Footnote \"test\""))
	  (org-test-with-temp-text-in-file contents
	    (let ((file1 (buffer-file-name)))
	      (org-test-with-temp-text-in-file contents
		(let ((file2 (buffer-file-name)))
		  (org-test-with-temp-text
		      (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
			      file1 file2)
		    (org-export-expand-include-keyword)
		    (org-element-map (org-element-parse-buffer)
			'footnote-reference
		      (lambda (r) (org-element-property :label r)))))))))))))
  ;; Footnotes labels are not local to each include keyword.
  (should
   (= 3
      (length
       (delete-dups
	(let ((contents "
Footnotes[fn:1], [fn:test] and [fn:inline:inline footnote].
\[fn:1] Footnote 1
\[fn:test] Footnote \"test\""))
	  (org-test-with-temp-text-in-file contents
	    (let ((file (buffer-file-name)))
	      (org-test-with-temp-text
		  (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
		(org-export-expand-include-keyword)
		(org-element-map (org-element-parse-buffer)
		    'footnote-reference
		  (lambda (ref) (org-element-property :label ref)))))))))))
  ;; Footnotes are supported by :lines-like elements and unnecessary
  ;; footnotes are dropped.
  (should
   (= 3
      (length
       (delete-dups
	(let ((contents "
* foo
Footnotes[fn:1]
* bar
Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote]

\[fn:1] Footnote 1
\[fn:2] Footnote 1
* Footnotes
\[fn:test] Footnote \"test\"
"))
	  (org-test-with-temp-text-in-file contents
	    (let ((file (buffer-file-name)))
	      (org-test-with-temp-text
		  (format "#+INCLUDE: \"%s::*bar\"\n" file)
		(org-export-expand-include-keyword)
		(org-element-map (org-element-parse-buffer)
		    'footnote-definition
		  (lambda (ref) (org-element-property :label ref)))))))))))
  ;; If only-contents is non-nil only include contents of element.
  (should
   (equal
    "body\n"
    (org-test-with-temp-text
	(concat
	 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" "
		 org-test-dir)
	 ":only-contents t")
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Headings can be included via CUSTOM_ID.
  (should
   (org-test-with-temp-text
       (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
     (org-export-expand-include-keyword)
     (goto-char (point-min))
     (looking-at "* Another heading")))
  ;; Named objects can be included.
  (should
   (equal
    "| 1 |\n"
    (org-test-with-temp-text
	(format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t"
		org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Including non-existing elements should result in an error.
  (should-error
   (org-test-with-temp-text
       (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\""
	       org-test-dir)
     (org-export-expand-include-keyword)))
  ;; Lines work relatively to an included element.
  (should
   (equal
    "2\n3\n"
    (org-test-with-temp-text
	(format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t \
:lines \"2-3\""
		org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Properties should be dropped from headlines.
  (should
   (equal
    (org-test-with-temp-text
	(format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t"
		org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))
    (org-test-with-temp-text
	(format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Properties should be dropped, drawers should not be.
  (should
   (equal
    ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
    (org-test-with-temp-text
	(format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t"
		org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
  (should
   (cl-every (lambda (level) (zerop (1- level)))
	     (org-test-with-temp-text
		 (concat
		  (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n"
			  org-test-dir)
		  (format "#+INCLUDE: \"%s/examples/include.org::*Heading\""
			  org-test-dir))
	       (org-export-expand-include-keyword)
	       (org-element-map (org-element-parse-buffer) 'headline
		 (lambda (head) (org-element-property :level head))))))
  ;; INCLUDE does not insert induced :minlevel for src-blocks.
  (should-not
   (equal
    (org-test-with-temp-text
	(format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp"
		org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))
    (org-test-with-temp-text
	(format
	 "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1"
	 org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))))
  ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
  (should
   (org-test-with-temp-text-in-file
       (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\""
	       org-test-dir)
     (narrow-to-region (point) (point-max))
     (org-export-expand-include-keyword)
     (eq 2 (org-current-level))))
  ;; If :minlevel is present do not alter it.
  (should
   (org-test-with-temp-text
       (format
	"* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3"
	org-test-dir)
     (narrow-to-region (point) (point-max))
     (org-export-expand-include-keyword)
     (eq 3 (org-current-level)))))

(ert-deftest test-org-export/expand-include/links ()
  "Test links modifications when including files."
  ;; Preserve relative plain links.
  (should
   (string-prefix-p
    "file:org-includee-"
    (let* ((subdir (make-temp-file "org-includee-" t))
	   (includee (expand-file-name "includee.org" subdir))
	   (includer (make-temp-file "org-includer-")))
      (write-region "file:foo.org" nil includee)
      (write-region (format "#+INCLUDE: %S"
			    (file-relative-name includee
						temporary-file-directory))
		    nil includer)
      (let ((buffer (find-file-noselect includer t)))
	(unwind-protect
	    (with-current-buffer buffer
              (org-mode)
	      (org-export-expand-include-keyword)
	      (org-trim (buffer-string)))
	  (when (buffer-live-p buffer)
	    (with-current-buffer buffer (set-buffer-modified-p nil))
	    (kill-buffer buffer))
	  (when (file-exists-p subdir) (delete-directory subdir t))
	  (when (file-exists-p includer) (delete-file includer)))))))
  ;; Preserve relative angular links.
  (should
   (string-prefix-p
    "<file:org-includee-"
    (let* ((subdir (make-temp-file "org-includee-" t))
	   (includee (expand-file-name "includee.org" subdir))
	   (includer (make-temp-file "org-includer-")))
      (write-region "<file:foo.org>" nil includee)
      (write-region (format "#+INCLUDE: %S"
			    (file-relative-name includee
						temporary-file-directory))
		    nil includer)
      (let ((buffer (find-file-noselect includer t)))
	(unwind-protect
	    (with-current-buffer buffer
              (org-mode)
	      (org-export-expand-include-keyword)
	      (org-trim (buffer-string)))
	  (when (buffer-live-p buffer)
	    (with-current-buffer buffer (set-buffer-modified-p nil))
	    (kill-buffer buffer))
	  (when (file-exists-p subdir) (delete-directory subdir t))
	  (when (file-exists-p includer) (delete-file includer)))))))
  ;; Preserve relative bracket links without description.
  (should
   (string-prefix-p
    "[[file:org-includee-"
    (let* ((subdir (make-temp-file "org-includee-" t))
	   (includee (expand-file-name "includee.org" subdir))
	   (includer (make-temp-file "org-includer-")))
      (write-region "[[file:foo.org]]" nil includee)
      (write-region (format "#+INCLUDE: %S"
			    (file-relative-name includee
						temporary-file-directory))
		    nil includer)
      (let ((buffer (find-file-noselect includer t)))
	(unwind-protect
	    (with-current-buffer buffer
              (org-mode)
	      (org-export-expand-include-keyword)
	      (org-trim (buffer-string)))
	  (when (buffer-live-p buffer)
	    (with-current-buffer buffer (set-buffer-modified-p nil))
	    (kill-buffer buffer))
	  (when (file-exists-p subdir) (delete-directory subdir t))
	  (when (file-exists-p includer) (delete-file includer)))))))
  ;; Preserve blanks after the link.
  (should
   (string-suffix-p
    "foo.org]] :tag:"
    (let* ((subdir (make-temp-file "org-includee-" t))
	   (includee (expand-file-name "includee.org" subdir))
	   (includer (make-temp-file "org-includer-")))
      (write-region "[[file:foo.org]] :tag:" nil includee)
      (write-region (format "#+INCLUDE: %S"
			    (file-relative-name includee
						temporary-file-directory))
		    nil includer)
      (let ((buffer (find-file-noselect includer t)))
	(unwind-protect
	    (with-current-buffer buffer
              (org-mode)
	      (org-export-expand-include-keyword)
	      (org-trim (buffer-string)))
	  (when (buffer-live-p buffer)
	    (with-current-buffer buffer (set-buffer-modified-p nil))
	    (kill-buffer buffer))
	  (when (file-exists-p subdir) (delete-directory subdir t))
	  (when (file-exists-p includer) (delete-file includer)))))))
  ;; Preserve relative bracket links with description.
  (should
   (string-prefix-p
    "[[file:org-includee-"
    (let* ((subdir (make-temp-file "org-includee-" t))
	   (includee (expand-file-name "includee.org" subdir))
	   (includer (make-temp-file "org-includer-")))
      (write-region "[[file:foo.org][description]]" nil includee)
      (write-region (format "#+INCLUDE: %S"
			    (file-relative-name includee
						temporary-file-directory))
		    nil includer)
      (let ((buffer (find-file-noselect includer t)))
	(unwind-protect
	    (with-current-buffer buffer
              (org-mode)
	      (org-export-expand-include-keyword)
	      (org-trim (buffer-string)))
	  (when (buffer-live-p buffer)
	    (with-current-buffer buffer (set-buffer-modified-p nil))
	    (kill-buffer buffer))
	  (when (file-exists-p subdir) (delete-directory subdir t))
	  (when (file-exists-p includer) (delete-file includer)))))))
  ;; Preserve absolute links.
  (should
   (string=
    "[[file:/foo/bar.org]]"
    (let* ((subdir (make-temp-file "org-includee-" t))
	   (includee (expand-file-name "includee.org" subdir))
	   (includer (make-temp-file "org-includer-")))
      (write-region "[[file:/foo/bar.org]]" nil includee)
      (write-region (format "#+INCLUDE: %S"
			    (file-relative-name includee
						temporary-file-directory))
		    nil includer)
      (let ((buffer (find-file-noselect includer t)))
	(unwind-protect
	    (with-current-buffer buffer
              (org-mode)
	      (org-export-expand-include-keyword)
	      (org-trim (buffer-string)))
	  (when (buffer-live-p buffer)
	    (with-current-buffer buffer (set-buffer-modified-p nil))
	    (kill-buffer buffer))
	  (when (file-exists-p subdir) (delete-directory subdir t))
	  (when (file-exists-p includer) (delete-file includer)))))))
  ;; Pathological case: Do not error when fixing a path in a headline.
  (should
   (let* ((subdir (make-temp-file "org-includee-" t))
	  (includee (expand-file-name "includee.org" subdir))
	  (includer (make-temp-file "org-includer-")))
     (write-region "* [[file:foo.org]]" nil includee)
     (write-region (format "#+INCLUDE: %S"
			   (file-relative-name includee
					       temporary-file-directory))
		   nil includer)
     (let ((buffer (find-file-noselect includer t)))
       (unwind-protect
	   (with-current-buffer buffer
             (org-mode)
	     (org-export-expand-include-keyword)
	     (org-trim (buffer-string)))
	 (when (buffer-live-p buffer)
	   (with-current-buffer buffer (set-buffer-modified-p nil))
	   (kill-buffer buffer))
	 (when (file-exists-p subdir) (delete-directory subdir t))
	 (when (file-exists-p includer) (delete-file includer)))))))

(ert-deftest test-org-export/expand-macro ()
  "Test macro expansion in an Org buffer."
  (require 'ox-org)
  ;; Standard macro expansion.
  (should
   (equal "#+macro: macro1 value\nvalue\n"
	  (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
	    (org-export-as (org-test-default-backend)))))
  ;; Include global macros.  However, local macros override them.
  (should
   (equal "global\n"
	  (org-test-with-temp-text "{{{M}}}"
	    (let ((org-export-global-macros '(("M" . "global"))))
	      (org-export-as (org-test-default-backend))))))
  (should
   (equal "global arg\n"
	  (org-test-with-temp-text "{{{M(arg)}}}"
	    (let ((org-export-global-macros '(("M" . "global $1"))))
	      (org-export-as (org-test-default-backend))))))
  (should
   (equal "2\n"
	  (org-test-with-temp-text "{{{M}}}"
	    (let ((org-export-global-macros '(("M" . "(eval (+ 1 1))"))))
	      (org-export-as (org-test-default-backend))))))
  (should
   (equal "#+macro: M local\nlocal\n"
	  (org-test-with-temp-text "#+macro: M local\n{{{M}}}"
	    (let ((org-export-global-macros '(("M" . "global"))))
	      (org-export-as (org-test-default-backend))))))
  ;; Allow macro in parsed keywords and associated properties.
  ;; Standard macro expansion.
  (should
   (string-match
    "#\\+k: value"
    (let ((backend (org-export-create-backend
		    :parent 'org
		    :options '((:k "K" nil nil parse)))))
      (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
	(org-export-as backend)))))
  (should
   (string-match
    ":EXPORT_K: v"
    (let ((backend (org-export-create-backend
		    :parent 'org
		    :options '((:k "K" nil nil parse)))))
      (org-test-with-temp-text
	  "#+macro: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
	(org-export-as backend nil nil nil '(:with-properties t))))))
  ;; Expand specific macros.
  (should
   (equal "me 2012-03-29 me@here Title\n"
	  (org-test-with-temp-text
	      "
#+TITLE: Title
#+DATE: 2012-03-29
#+AUTHOR: me
#+EMAIL: me@here
{{{author}}} {{{date}}} {{{email}}} {{{title}}}"
	    (let ((output (org-export-as (org-test-default-backend))))
	      (substring output (string-match ".*\n\\'" output))))))
  ;; Expand specific macros when property contained a regular macro
  ;; already.
  (should
   (equal "value\n"
	  (org-test-with-temp-text "
#+MACRO: macro1 value
#+TITLE: {{{macro1}}}
{{{title}}}"
	    (let ((output (org-export-as (org-test-default-backend))))
	      (substring output (string-match ".*\n\\'" output))))))
  ;; Expand macros with templates in included files.
  (should
   (equal "success\n"
	  (org-test-with-temp-text
	      (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
{{{included-macro}}}" org-test-dir)
	    (let ((output (org-export-as (org-test-default-backend))))
	      (substring output (string-match ".*\n\\'" output))))))
  ;; Date macro takes a optional formatting argument
  (should
   (equal "09-02-15\n"
	  (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
	    (org-export-as (org-test-default-backend)))))
  ;; Only single timestamps are formatted
  (should
   (equal "<2015-02x-09>\n"
	  (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
	    (org-export-as (org-test-default-backend)))))
  ;; Throw an error when a macro definition is missing.
  (should-error
   (org-test-with-temp-text "{{{missing}}}"
     (org-export-as (org-test-default-backend))))
  ;; Inline source blocks generate {{{results}}} macros.  Evaluate
  ;; those.
  (should
   (equal "=2=\n"
	  (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
	    (let ((org-export-use-babel t)
		  (org-babel-inline-result-wrap "=%s="))
	      (org-export-as (org-test-default-backend))))))
  ;; If inline source block is already associated to a "results"
  ;; macro, do not duplicate it.
  (should
   (equal "src_emacs-lisp{(+ 1 1)} {{{results(=2=)}}}"
	  (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)} {{{results(=2=)}}}"
	    (let ((org-export-use-babel t)
		  (org-babel-inline-result-wrap "=%s="))
	      (org-export-as (org-test-default-backend)))
	    (buffer-string)))))

(ert-deftest test-org-export/before-processing-hook ()
  "Test `org-export-before-processing-hook'."
  (should
   (equal
    "#+macro: mac val\nTest\n"
    (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
      (let ((org-export-before-processing-hook
	     '((lambda (backend)
		 (while (re-search-forward "{{{" nil t)
		   (let ((object (org-element-context)))
		     (when (org-element-type-p object 'macro)
		       (delete-region
			(org-element-property :begin object)
			(org-element-property :end object)))))))))
	(org-export-as (org-test-default-backend)))))))

(ert-deftest test-org-export/before-parsing-hook ()
  "Test `org-export-before-parsing-hook'."
  (should
   (equal "Body 1\nBody 2\n"
	  (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
	    (let ((org-export-before-parsing-hook
		   '((lambda (backend)
		       (goto-char (point-min))
		       (while (re-search-forward org-outline-regexp-bol nil t)
			 (delete-region
			  (point-at-bol) (progn (forward-line) (point))))))))
	      (org-export-as (org-test-default-backend)))))))


\f
;;; Affiliated Keywords

(ert-deftest test-org-export/read-attribute ()
  "Test `org-export-read-attribute' specifications."
  ;; Standard test.
  (should
   (equal
    (org-export-read-attribute
     :attr_html
     (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
       (org-element-at-point)))
    '(:a "1" :b "2")))
  ;; Return nil on empty attribute.
  (should-not
   (org-export-read-attribute
    :attr_html
    (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  ;; Return nil on "nil" string.
  (should
   (equal '(:a nil :b nil)
	  (org-export-read-attribute
	   :attr_html
	   (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
	     (org-element-at-point)))))
  ;; Return nil on empty string.
  (should
   (equal '(:a nil :b nil)
	  (org-export-read-attribute
	   :attr_html
	   (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
	     (org-element-at-point)))))
  ;; Return empty string when value is "".
  (should
   (equal '(:a "")
	  (org-export-read-attribute
	   :attr_html
	   (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
	     (org-element-at-point)))))
  ;; Return \"\" when value is """".
  (should
   (equal '(:a "\"\"")
	  (org-export-read-attribute
	   :attr_html
	   (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
	     (org-element-at-point)))))
  ;; Ignore text before first property.
  (should-not
   (member "ignore"
	   (org-export-read-attribute
	    :attr_html
	    (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
	      (org-element-at-point))))))

(ert-deftest test-org-export/get-caption ()
  "Test `org-export-get-caption' specifications."
  ;; Without optional argument, return long caption
  (should
   (equal
    '("l")
    (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
      (org-export-get-caption (org-element-at-point)))))
  ;; With optional argument, return short caption.
  (should
   (equal
    '("s")
    (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
      (org-export-get-caption (org-element-at-point) t))))
  ;; Multiple lines are separated by white spaces.
  (should
   (equal
    '("a" " " "b")
    (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
      (org-export-get-caption (org-element-at-point))))))


\f
;;; Backend Tools

(ert-deftest test-org-export/define-backend ()
  "Test backend definition and accessors."
  ;; Translate table.
  (should
   (equal '((headline . my-headline-test))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'test '((headline . my-headline-test)))
	    (org-export-get-all-transcoders 'test))))
  ;; Filters.
  (should
   (equal '((:filter-headline . my-filter))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'test
	      '((headline . my-headline-test))
	      :filters-alist '((:filter-headline . my-filter)))
	    (org-export-backend-filters (org-export-get-backend 'test)))))
  ;; Options.
  (should
   (equal '((:prop value))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'test
	      '((headline . my-headline-test))
	      :options-alist '((:prop value)))
	    (org-export-backend-options (org-export-get-backend 'test)))))
  ;; Menu.
  (should
   (equal '(?k "Test Export" test)
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'test
	      '((headline . my-headline-test))
	      :menu-entry '(?k "Test Export" test))
	    (org-export-backend-menu (org-export-get-backend 'test))))))

(ert-deftest test-org-export/define-derived-backend ()
  "Test `org-export-define-derived-backend' specifications."
  ;; Error when parent backend is not defined.
  (should-error
   (let (org-export-registered-backends)
     (org-export-define-derived-backend 'test 'parent)))
  ;; Append translation table to parent's.
  (should
   (equal '((:headline . test) (:headline . parent))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'parent '((:headline . parent)))
	    (org-export-define-derived-backend 'test 'parent
	      :translate-alist '((:headline . test)))
	    (org-export-get-all-transcoders 'test))))
  ;; Options defined in the new back have priority over those defined
  ;; in parent.
  (should
   (eq 'test
       (let (org-export-registered-backends)
	 (org-export-define-backend 'parent
	   '((:headline . parent))
	   :options-alist '((:a nil nil 'parent)))
	 (org-export-define-derived-backend 'test 'parent
	   :options-alist '((:a nil nil 'test)))
	 (plist-get (org-export--get-global-options
		     (org-export-get-backend 'test))
		    :a)))))

(ert-deftest test-org-export/derived-backend-p ()
  "Test `org-export-derived-backend-p' specifications."
  ;; Non-nil with direct match.
  (should
   (let (org-export-registered-backends)
     (org-export-define-backend 'test '((headline . test)))
     (org-export-derived-backend-p 'test 'test)))
  (should
   (let (org-export-registered-backends)
     (org-export-define-backend 'test '((headline . test)))
     (org-export-define-derived-backend 'test2 'test)
     (org-export-derived-backend-p 'test2 'test2)))
  ;; Non-nil with a direct parent.
  (should
   (let (org-export-registered-backends)
     (org-export-define-backend 'test '((headline . test)))
     (org-export-define-derived-backend 'test2 'test)
     (org-export-derived-backend-p 'test2 'test)))
  ;; Non-nil with an indirect parent.
  (should
   (let (org-export-registered-backends)
     (org-export-define-backend 'test '((headline . test)))
     (org-export-define-derived-backend 'test2 'test)
     (org-export-define-derived-backend 'test3 'test2)
     (org-export-derived-backend-p 'test3 'test)))
  ;; Nil otherwise.
  (should-not
   (let (org-export-registered-backends)
     (org-export-define-backend 'test '((headline . test)))
     (org-export-define-backend 'test2 '((headline . test2)))
     (org-export-derived-backend-p 'test2 'test)))
  (should-not
   (let (org-export-registered-backends)
     (org-export-define-backend 'test '((headline . test)))
     (org-export-define-backend 'test2 '((headline . test2)))
     (org-export-define-derived-backend 'test3 'test2)
     (org-export-derived-backend-p 'test3 'test))))

(ert-deftest test-org-export/get-all-transcoders ()
  "Test `org-export-get-all-transcoders' specifications."
  ;; Return nil when backend cannot be found.
  (should-not (org-export-get-all-transcoders nil))
  ;; Same as `org-export-transcoders' if no parent.
  (should
   (equal '((headline . ignore))
	  (org-export-get-all-transcoders
	   (org-export-create-backend
	    :transcoders '((headline . ignore))))))
  ;; But inherit from all ancestors whenever possible.
  (should
   (equal '((section . ignore) (headline . ignore))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'b1 '((headline . ignore)))
	    (org-export-get-all-transcoders
	     (org-export-create-backend
	      :parent 'b1 :transcoders '((section . ignore)))))))
  (should
   (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'b1 '((headline . ignore)))
	    (org-export-define-derived-backend 'b2 'b1
	      :translate-alist '((section . ignore)))
	    (org-export-get-all-transcoders
	     (org-export-create-backend
	      :parent 'b2 :transcoders '((paragraph . ignore)))))))
  ;; Backend transcoders overrule inherited ones.
  (should
   (eq 'b
       (let (org-export-registered-backends)
	 (org-export-define-backend 'b1 '((headline . a)))
	 (cdr (assq 'headline
		    (org-export-get-all-transcoders
		     (org-export-create-backend
		      :parent 'b1 :transcoders '((headline . b))))))))))

(ert-deftest test-org-export/get-all-options ()
  "Test `org-export-get-all-options' specifications."
  ;; Return nil when backend cannot be found.
  (should-not (org-export-get-all-options nil))
  ;; Same as `org-export-options' if no parent.
  (should
   (equal '((headline . ignore))
	  (org-export-get-all-options
	   (org-export-create-backend
	    :options '((headline . ignore))))))
  ;; But inherit from all ancestors whenever possible.
  (should
   (equal '((:key2 value2) (:key1 value1))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
	    (org-export-get-all-options
	     (org-export-create-backend
	      :parent 'b1 :options '((:key2 value2)))))))
  (should
   (equal '((:key3 value3) (:key2 value2) (:key1 value1))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
	    (org-export-define-derived-backend 'b2 'b1
	      :options-alist '((:key2 value2)))
	    (org-export-get-all-options
	     (org-export-create-backend
	      :parent 'b2 :options '((:key3 value3)))))))
  ;; Backend options overrule inherited ones.
  (should
   (eq 'b
       (let (org-export-registered-backends)
	 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
	 (cdr (assq :key1
		    (org-export-get-all-options
		     (org-export-create-backend
		      :parent 'b1 :options '((:key1 . b))))))))))

(ert-deftest test-org-export/get-all-filters ()
  "Test `org-export-get-all-filters' specifications."
  ;; Return nil when backend cannot be found.
  (should-not (org-export-get-all-filters nil))
  ;; Same as `org-export-filters' if no parent.
  (should
   (equal '((:filter-headline . ignore))
	  (org-export-get-all-filters
	   (org-export-create-backend
	    :filters '((:filter-headline . ignore))))))
  ;; But inherit from all ancestors whenever possible.
  (should
   (equal '((:filter-section . ignore) (:filter-headline . ignore))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'b1
	      nil :filters-alist '((:filter-headline . ignore)))
	    (org-export-get-all-filters
	     (org-export-create-backend
	      :parent 'b1 :filters '((:filter-section . ignore)))))))
  (should
   (equal '((:filter-paragraph . ignore)
	    (:filter-section . ignore)
	    (:filter-headline . ignore))
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'b1
	      nil :filters-alist '((:filter-headline . ignore)))
	    (org-export-define-derived-backend 'b2 'b1
	      :filters-alist '((:filter-section . ignore)))
	    (org-export-get-all-filters
	     (org-export-create-backend
	      :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  ;; Backend filters overrule inherited ones.
  (should
   (eq 'b
       (let (org-export-registered-backends)
	 (org-export-define-backend 'b1 '((:filter-headline . a)))
	 (cdr (assq :filter-headline
		    (org-export-get-all-filters
		     (org-export-create-backend
		      :parent 'b1 :filters '((:filter-headline . b))))))))))

(ert-deftest test-org-export/with-backend ()
  "Test `org-export-with-backend' definition."
  ;; Error when calling an undefined backend
  (should-error (org-export-with-backend nil "Test"))
  ;; Error when called backend doesn't have an appropriate
  ;; transcoder.
  (should-error
   (org-export-with-backend
    (org-export-create-backend :transcoders '((headline . ignore)))
    "Test"))
  ;; Otherwise, export using correct transcoder
  (should
   (equal "Success"
	  (let (org-export-registered-backends)
	    (org-export-define-backend 'test
	      '((verbatim . (lambda (text contents info) "Failure"))))
	    (org-export-define-backend 'test2
	      '((verbatim . (lambda (text contents info) "Success"))))
	    (org-export-with-backend 'test2 '(verbatim (:value "=Test="))))))
  ;; Corner case: plain-text transcoders have a different arity.
  (should
   (equal "Success"
	  (org-export-with-backend
	   (org-export-create-backend
	    :transcoders '((plain-text . (lambda (text info) "Success"))))
	   "Test")))
  ;; Provide correct backend if transcoder needs to use recursive
  ;; calls.
  (should
   (equal "Success\n"
	  (let ((test-backend
		 (org-export-create-backend
		  :transcoders
		  (list (cons 'headline
			      (lambda (headline _contents info)
				(org-export-data
				 (org-element-property :title headline)
				 info)))
			(cons 'plain-text (lambda (_text _info) "Success"))))))
	    (org-export-string-as
	     "* Test"
	     (org-export-create-backend
	      :transcoders
	      (list (cons 'headline
			  (lambda (headline contents info)
			    (org-export-with-backend
			     test-backend headline contents info))))))))))

(ert-deftest test-org-export/data-with-backend ()
  "Test `org-export-data-with-backend' specifications."
  ;; Error when calling an undefined backend.
  (should-error (org-export-data-with-backend nil "nil" nil))
  ;; Otherwise, export data recursively, using correct backend.
  (should
   (equal
    "Success!"
    (org-export-data-with-backend
     '(bold nil "Test")
     (org-export-create-backend
      :transcoders
      '((plain-text . (lambda (text info) "Success"))
	(bold . (lambda (bold contents info) (concat contents "!")))))
     '(:with-emphasize t)))))


\f
;;; Comments

(ert-deftest test-org-export/comments ()
  "Test comments handling during export.
In particular, structure of the document mustn't be altered after
comments removal."
  (should
   (equal "Para1\n\nPara2\n"
	  (org-test-with-temp-text
	      "Para1
# Comment

# Comment
Para2"
	    (org-export-as (org-test-default-backend)))))
  (should
   (equal "Para1\n\nPara2\n"
	  (org-test-with-temp-text
	      "Para1
# Comment
Para2"
	    (org-export-as (org-test-default-backend)))))
  (should
   (equal "[fn:1] Para1\n\n\nPara2\n"
	  (org-test-with-temp-text
	      "[fn:1] Para1
# Inside definition


# Outside definition
Para2"
	    (org-export-as (org-test-default-backend)))))
  (should
   (equal "[fn:1] Para1\n\nPara2\n"
	  (org-test-with-temp-text
	      "[fn:1] Para1

# Inside definition

# Inside definition

Para2"
	    (org-export-as (org-test-default-backend)))))
  (should
   (equal "[fn:1] Para1\n\nPara2\n"
	  (org-test-with-temp-text
	      "[fn:1] Para1
# Inside definition

Para2"
	    (org-export-as (org-test-default-backend)))))
  (should
   (equal "[fn:1] Para1\n\nPara2\n"
	  (org-test-with-temp-text
	      "[fn:1] Para1

# Inside definition
Para2"
	    (org-export-as (org-test-default-backend)))))
  (should
   (equal "- item 1\n\n- item 2\n"
	  (org-test-with-temp-text
	      "- item 1

  # Comment

- item 2"
	    (org-export-as (org-test-default-backend))))))


\f
;;; Export blocks

(ert-deftest test-org-export/export-block ()
  "Test export blocks transcoding."
  (should
   (equal "Success!\n"
	  (org-test-with-temp-text
	      "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((export-block . (lambda (b _c _i)
					       (org-element-property :value b)))
			     (section . (lambda (_s c _i) c))))))))
  (should
   (equal "Success!\n"
	  (org-test-with-temp-text
	      "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders
	      (list
	       (cons 'export-block
		     (lambda (b _c _i)
		       (and (equal (org-element-property :type b) "BACKEND")
			    (org-element-property :value b))))
	       (cons 'section (lambda (_s c _i) c)))))))))

\f
;;; Export Snippets

(ert-deftest test-org-export/export-snippet ()
  "Test export snippets transcoding."
  ;; Standard test.
  (org-test-with-temp-text "@@test:A@@@@t:B@@"
    (let ((backend (org-test-default-backend)))
      (setf (org-export-backend-name backend) 'test)
      (setf (org-export-backend-transcoders backend)
	    (cons (cons 'export-snippet
			(lambda (snippet _contents _info)
			  (when (eq (org-export-snippet-backend snippet) 'test)
			    (org-element-property :value snippet))))
		  (org-export-backend-transcoders backend)))
      (let ((org-export-snippet-translation-alist nil))
	(should (equal (org-export-as backend) "A\n")))
      (let ((org-export-snippet-translation-alist '(("t" . "test"))))
	(should (equal (org-export-as backend) "AB\n")))))
  ;; Ignored export snippets do not remove any blank.
  (should
   (equal "begin  end\n"
	  (org-test-with-parsed-data "begin @@test:A@@ end"
	    (org-export-data-with-backend
	     tree
	     (org-export-create-backend
	      :transcoders
	      '((paragraph . (lambda (paragraph contents info) contents))
		(section . (lambda (section contents info) contents))))
	     info)))))

\f
;;; Filters

(ert-deftest test-org-export/filter-apply-functions ()
  "Test `org-export-filter-apply-functions' specifications."
  ;; Functions are applied in order and return values are reduced.
  (should
   (equal "210"
	  (org-export-filter-apply-functions
	   (list (lambda (value &rest _) (concat "1" value))
		 (lambda (value &rest _) (concat "2" value)))
	   "0" nil)))
  ;; Functions returning nil are skipped.
  (should
   (equal "20"
	  (org-export-filter-apply-functions
	   (list #'ignore (lambda (value &rest _) (concat "2" value)))
	   "0" nil)))
  ;; If all functions are skipped, return the initial value.
  (should
   (equal "0"
	  (org-export-filter-apply-functions (list #'ignore) "0" nil)))
  ;; If any function returns the empty string, final value is the
  ;; empty string.
  (should
   (equal ""
	  (org-export-filter-apply-functions
	   (list (lambda (_value &rest _) "")
		 (lambda (value &rest _) (concat "2" value)))
	   "0" nil)))
  ;; Any function returning the empty string short-circuits the
  ;; process.
  (should
   (org-export-filter-apply-functions
    (list (lambda (_value &rest _) "")
	  (lambda (_value &rest _) (error "This shouldn't happen")))
    "0" nil)))

\f
;;; Footnotes

(ert-deftest test-org-export/footnote-first-reference-p ()
  "Test `org-export-footnote-first-reference-p' specifications."
  (should
   (equal
    '(t nil)
    (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
      (let (result)
	(org-export-as
	 (org-export-create-backend
	  :transcoders
	  `(,(cons 'footnote-reference
		   (lambda (f _c i)
		     (push (org-export-footnote-first-reference-p f i)
			   result)
		     ""))
	    (section . (lambda (s c i) c))
	    (paragraph . (lambda (p c i) c))))
	 nil nil nil '(:with-footnotes t))
	(nreverse result)))))
  ;; Limit check to DATA, when non-nil.
  (should
   (equal
    '(nil t)
    (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
      (let (result)
	(org-element-map tree 'footnote-reference
	  (lambda (ref)
	    (push
	     (org-export-footnote-first-reference-p
	      ref info (org-element-map tree 'headline #'identity info t))
	     result))
	  info)
	(nreverse result)))))
  (should
   (equal
    '(t nil)
    (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
      (let (result)
	(org-element-map tree 'footnote-reference
	  (lambda (ref)
	    (push (org-export-footnote-first-reference-p ref info) result))
	  info)
	(nreverse result)))))
  ;; If optional argument BODY-FIRST is non-nil, first find footnote
  ;; in the main body of the document.  Otherwise, enter footnote
  ;; definitions when they are encountered.
  (should
   (equal
    '(t nil)
    (org-test-with-temp-text
	":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
      (let (result)
	(org-export-as
	 (org-export-create-backend
	  :transcoders
	  `(,(cons 'footnote-reference
		   (lambda (f _c i)
		     (when (org-element-lineage f 'drawer)
		       (push (org-export-footnote-first-reference-p f i nil)
			     result))
		     ""))
	    (drawer . (lambda (d c i) c))
	    (footnote-definition . (lambda (d c i) c))
	    (section . (lambda (s c i) c))
	    (paragraph . (lambda (p c i) c))))
	 nil nil nil '(:with-footnotes t))
	(nreverse result)))))
  (should
   (equal
    '(t t)
    (org-test-with-temp-text
	":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
      (let (result)
	(org-export-as
	 (org-export-create-backend
	  :transcoders
	  `(,(cons 'footnote-reference
		   (lambda (f _c i)
		     (when (org-element-lineage f 'drawer)
		       (push (org-export-footnote-first-reference-p f i nil t)
			     result))
		     ""))
	    (drawer . (lambda (d c i) c))
	    (footnote-definition . (lambda (d c i) c))
	    (section . (lambda (s c i) c))
	    (paragraph . (lambda (p c i) c))))
	 nil nil nil '(:with-footnotes t))
	(nreverse result))))))

(ert-deftest test-org-export/get-footnote-number ()
  "Test `org-export-get-footnote-number' specifications."
  (should
   (equal '(1 2 1)
	  (org-test-with-parsed-data
	      "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
	    (org-element-map tree 'footnote-reference
	      (lambda (ref) (org-export-get-footnote-number ref info))
	      info))))
  ;; Anonymous footnotes all get a new number.
  (should
   (equal '(1 2)
	  (org-test-with-parsed-data
	      "Text[fn::anon1][fn::anon2]"
	    (org-element-map tree 'footnote-reference
	      (lambda (ref) (org-export-get-footnote-number ref info))
	      info))))
  ;; Test nested footnotes order.
  (should
   (equal
    '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
    (org-test-with-parsed-data
	"Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
      (org-element-map tree 'footnote-reference
	(lambda (ref)
	  (cons (org-export-get-footnote-number ref info)
		(org-element-property :label ref)))
	info))))
  ;; Limit number to provided DATA, when non-nil.
  (should
   (equal
    '(1)
    (org-test-with-parsed-data
	"Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
      (org-element-map tree 'footnote-reference
	(lambda (ref)
	  (org-export-get-footnote-number
	   ref info (org-element-map tree 'headline #'identity info t)))
	info))))
  (should
   (equal
    '(1 2)
    (org-test-with-parsed-data
	"Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
      (org-element-map tree 'footnote-reference
	(lambda (ref) (org-export-get-footnote-number ref info))
	info))))
  ;; With a non-nil BODY-FIRST optional argument, first check body,
  ;; then footnote definitions.
  (should
   (equal
    '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
    (org-test-with-parsed-data
	"Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
      (org-element-map tree 'footnote-reference
	(lambda (ref)
	  (cons (org-element-property :label ref)
		(org-export-get-footnote-number ref info nil t)))
	info))))
  (should
   (equal
    '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
    (org-test-with-parsed-data
	"Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
      (org-element-map tree 'footnote-reference
	(lambda (ref)
	  (cons (org-element-property :label ref)
		(org-export-get-footnote-number ref info nil)))
	info)))))

(ert-deftest test-org-export/get-footnote-definition ()
  "Test `org-export-get-footnote-definition' specifications."
  ;; Standard test.
  (should
   (equal "A\n"
	  (org-element-interpret-data
	   (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
	     (org-export-get-footnote-definition
	      (org-element-map tree 'footnote-reference #'identity nil t)
	      info)))))
  ;; Raise an error if no definition is found.
  (should-error
   (org-test-with-parsed-data "Text[fn:1]"
     (org-export-get-footnote-definition
      (org-element-map tree 'footnote-reference #'identity nil t)
      info)))
  ;; Find inline definitions.
  (should
   (equal "A"
	  (org-element-interpret-data
	   (org-test-with-parsed-data "Text[fn:1:A]"
	     (org-export-get-footnote-definition
	      (org-element-map tree 'footnote-reference #'identity nil t)
	      info)))))
  ;; Find anonymous definitions.
  (should
   (equal "A"
	  (org-element-interpret-data
	   (org-test-with-parsed-data "Text[fn::A]"
	     (org-export-get-footnote-definition
	      (org-element-map tree 'footnote-reference #'identity nil t)
	      info)))))
  ;; Find empty definitions.
  (should
   (equal ""
	  (org-element-interpret-data
	   (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
	     (org-export-get-footnote-definition
	      (org-element-map tree 'footnote-reference #'identity nil t)
	      info)))))
  (should
   (equal ""
	  (org-element-interpret-data
	   (org-test-with-parsed-data "Text[fn:1:]"
	     (org-export-get-footnote-definition
	      (org-element-map tree 'footnote-reference #'identity nil t)
	      info)))))
  (should
   (equal ""
	  (org-element-interpret-data
	   (org-test-with-parsed-data "Text[fn::]"
	     (org-export-get-footnote-definition
	      (org-element-map tree 'footnote-reference #'identity nil t)
	      info))))))

(ert-deftest test-org-export/collect-footnote-definitions ()
  "Test `org-export-collect-footnote-definitions' specifications."
  (should
   (= 4
      (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].

\[fn:2] B [fn:3] [fn::D].

\[fn:3] C."
	(length (org-export-collect-footnote-definitions info)))))
  ;; Limit number to provided DATA, when non-nil.
  (should
   (equal
    '((1 "2"))
    (org-test-with-parsed-data
	"Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
      (mapcar #'butlast
	      (org-export-collect-footnote-definitions
	       info (org-element-map tree 'headline #'identity info t))))))
  (should
   (equal
    '((1 "1") (2 "2"))
    (org-test-with-parsed-data
	"Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
      (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  ;; With optional argument BODY-FIRST, first check body, then
  ;; footnote definitions.
  (should
   (equal '("1" "3" "2" nil)
	  (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].

\[fn:2] B [fn:3] [fn::D].

\[fn:3] C."
	    (mapcar (lambda (e) (nth 1 e))
		    (org-export-collect-footnote-definitions info nil t)))))
  (should-not
   (equal '("1" "3" "2" nil)
	  (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].

\[fn:2] B [fn:3] [fn::D].

\[fn:3] C."
	    (mapcar (lambda (e) (nth 1 e))
		    (org-export-collect-footnote-definitions info))))))

(ert-deftest test-org-export/footnotes ()
  "Miscellaneous tests on footnotes."
  (let ((org-footnote-section nil)
	(org-export-with-footnotes t))
    ;; Read every type of footnote.
    (should
     (equal
      '((1 . "A\n") (2 . "C") (3 . "D"))
      (org-test-with-parsed-data
	  "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
	(org-element-map tree 'footnote-reference
	  (lambda (ref)
	    (let ((def (org-export-get-footnote-definition ref info)))
	      (cons (org-export-get-footnote-number ref info)
		    (if (eq (org-element-property :type ref) 'inline) (car def)
		      (car (org-element-contents
			    (car (org-element-contents def))))))))
	  info))))
    ;; Export nested footnote in invisible definitions.
    (should
     (= 2
	(org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
	  (narrow-to-region (point) (line-end-position))
	  (catch 'exit
	    (org-export-as
	     (org-export-create-backend
	      :transcoders
	      '((section
		 .
		 (lambda (s c i)
		   (throw 'exit (length
				 (org-export-collect-footnote-definitions
				  i))))))))))))
    ;; Export footnotes defined outside parsing scope.
    (should
     (string-match
      "Out of scope"
      (org-test-with-temp-text "[fn:1] Out of scope
* Title
<point>Paragraph[fn:1]"
	(org-export-as (org-test-default-backend) 'subtree))))
    (should
     (string-match
      "Out of scope"
      (org-test-with-temp-text "[fn:1] Out of scope
* Title
<point>Paragraph[fn:1]"
	(narrow-to-region (point) (point-max))
	(org-export-as (org-test-default-backend)))))
    ;; Export nested footnotes defined outside parsing scope.
    (should
     (string-match
      "Very out of scope"
      (org-test-with-temp-text "
\[fn:1] Out of scope[fn:2]

\[fn:2] Very out of scope
* Title
<point>Paragraph[fn:1]"
	(org-export-as (org-test-default-backend) 'subtree))))
    (should
     (string-match
      "Very out of scope"
      (org-test-with-temp-text "
\[fn:1] Out of scope[fn:2]

\[fn:2] Very out of scope
* Title
<point>Paragraph[fn:1]"
	(narrow-to-region (point) (point-max))
	(org-export-as (org-test-default-backend)))))
    (should
     (string-match
      "D2"
      (org-test-with-temp-text "
\[fn:1] Out of scope[fn:2:D2]
* Title
<point>Paragraph[fn:1]"
	(narrow-to-region (point) (point-max))
	(org-export-as (org-test-default-backend)))))
    ;; Export footnotes in pruned parts of tree.
    (should
     (string-match
      "Definition"
      (let ((org-export-exclude-tags '("noexport")))
	(org-test-with-temp-text
	    "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
	  (org-export-as (org-test-default-backend))))))
    (should
     (string-match
      "Definition"
      (let ((org-export-select-tags '("export")))
	(org-test-with-temp-text
	    "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
	  (org-export-as (org-test-default-backend))))))
    ;; Export nested footnotes in pruned parts of tree.
    (should
     (string-match
      "D2"
      (let ((org-export-exclude-tags '("noexport")))
	(org-test-with-temp-text
	    "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
	  (org-export-as (org-test-default-backend))))))
    (should
     (string-match
      "D2"
      (let ((org-export-select-tags '("export")))
	(org-test-with-temp-text
	    "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
	  (org-export-as (org-test-default-backend))))))
    ;; Handle uninterpreted data in pruned footnote definitions.
    (should-not
     (string-match
      "|"
      (let ((org-export-with-tables nil))
	(org-test-with-temp-text
	    "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
	  (org-export-as (org-test-default-backend))))))
    ;; Footnotes without a definition should throw an error.
    (should-error
     (org-test-with-parsed-data "Text[fn:1]"
       (org-export-get-footnote-definition
	(org-element-map tree 'footnote-reference #'identity info t) info)))
    ;; Footnote section should be ignored in TOC and in headlines
    ;; numbering.
    (should
     (= 1 (let ((org-footnote-section "Footnotes"))
	    (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
		      (org-export-collect-headlines info))))))
    (should
     (equal '(2)
	    (let ((org-footnote-section "Footnotes"))
	      (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
		(org-element-map tree 'headline
		  (lambda (hl)
		    (when (equal (org-element-property :raw-value hl) "H2")
		      (org-export-get-headline-number hl info)))
		  info t)))))))


\f
;;; Headlines and Inlinetasks

(ert-deftest test-org-export/get-relative-level ()
  "Test `org-export-get-relative-level' specifications."
  ;; Standard test.
  (should
   (equal '(1 2)
	  (let ((org-odd-levels-only nil))
	    (org-test-with-parsed-data "* Headline 1\n** Headline 2"
	      (org-element-map tree 'headline
		(lambda (h) (org-export-get-relative-level h info))
		info)))))
  ;; Missing levels
  (should
   (equal '(1 3)
	  (let ((org-odd-levels-only nil))
	    (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
	      (org-element-map tree 'headline
		(lambda (h) (org-export-get-relative-level h info))
		info))))))

(ert-deftest test-org-export/low-level-p ()
  "Test `org-export-low-level-p' specifications."
  (should
   (equal
    '(no yes)
    (let ((org-odd-levels-only nil))
      (org-test-with-parsed-data "* Headline 1\n** Headline 2"
	(org-element-map tree 'headline
	  (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
	  (plist-put info :headline-levels 1)))))))

(ert-deftest test-org-export/get-headline-number ()
  "Test `org-export-get-headline-number' specifications."
  ;; Standard test.
  (should
   (equal
    '((1) (1 1))
    (let ((org-odd-levels-only nil))
      (org-test-with-parsed-data "* Headline 1\n** Headline 2"
	(org-element-map tree 'headline
	  (lambda (h) (org-export-get-headline-number h info))
	  info)))))
  ;; Missing levels are replaced with 0.
  (should
   (equal
    '((1) (1 0 1))
    (let ((org-odd-levels-only nil))
      (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
	(org-element-map tree 'headline
	  (lambda (h) (org-export-get-headline-number h info))
	  info))))))

(ert-deftest test-org-export/numbered-headline-p ()
  "Test `org-export-numbered-headline-p' specifications."
  ;; If `:section-numbers' is nil, never number headlines.
  (should-not
   (org-test-with-parsed-data "* Headline"
     (org-element-map tree 'headline
       (lambda (h) (org-export-numbered-headline-p h info))
       (plist-put info :section-numbers nil))))
  ;; If `:section-numbers' is a number, only number headlines with
  ;; a level greater that it.
  (should
   (equal
    '(yes no)
    (org-test-with-parsed-data "* Headline 1\n** Headline 2"
      (org-element-map tree 'headline
	(lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
	(plist-put info :section-numbers 1)))))
  ;; Otherwise, headlines are always numbered.
  (should
   (org-test-with-parsed-data "* Headline"
     (org-element-map tree 'headline
       (lambda (h) (org-export-numbered-headline-p h info))
       (plist-put info :section-numbers t))))
  ;; With #+OPTIONS: num:nil all headlines are unnumbered.
  (should-not
   (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
     (org-export-numbered-headline-p
      (org-element-map tree 'headline 'identity info t)
      info)))
  ;; Headlines with a non-nil UNNUMBERED property are not numbered.
  (should-not
   (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
     (org-export-numbered-headline-p
      (org-element-map tree 'headline #'identity info t)
      info)))
  ;; UNNUMBERED is inherited.
  (should
   (equal '(unnumbered numbered unnumbered)
	  (org-test-with-parsed-data
	      "* H
:PROPERTIES:
:UNNUMBERED: t
:END:
** H2
:PROPERTIES:
:UNNUMBERED: nil
:END:
** H3"
	    (org-element-map tree 'headline
	      (lambda (h)
		(if (org-export-numbered-headline-p h info) 'numbered
		  'unnumbered))
	      info)))))

(ert-deftest test-org-export/number-to-roman ()
  "Test `org-export-number-to-roman' specifications."
  ;; If number is negative, return it as a string.
  (should (equal (org-export-number-to-roman -1) "-1"))
  ;; Otherwise, return it as a roman number.
  (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))

(ert-deftest test-org-export/get-optional-title ()
  "Test `org-export-get-alt-title' specifications."
  ;; If ALT_TITLE property is defined, use it.
  (should
   (equal '("opt")
	  (org-test-with-parsed-data
	      "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
	    (org-export-get-alt-title
	     (org-element-map tree 'headline 'identity info t)
	     info))))
  ;; Otherwise, fall-back to regular title.
  (should
   (equal '("Headline")
	  (org-test-with-parsed-data "* Headline"
	    (org-export-get-alt-title
	     (org-element-map tree 'headline 'identity info t)
	     info)))))

(ert-deftest test-org-export/get-tags ()
  "Test `org-export-get-tags' specifications."
  ;; Standard test: tags which are not a select tag, an exclude tag,
  ;; or specified as optional argument shouldn't be ignored.
  (should
   (org-test-with-parsed-data "* Headline :tag:"
     (org-export-get-tags (org-element-map tree 'headline 'identity info t)
			  info)))
  ;; Tags provided in the optional argument are ignored.
  (should-not
   (org-test-with-parsed-data "* Headline :ignore:"
     (org-export-get-tags (org-element-map tree 'headline 'identity info t)
			  info '("ignore"))))
  ;; Allow tag inheritance.
  (should
   (equal
    '(("tag") ("tag"))
    (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
      (org-element-map tree 'headline
	(lambda (hl) (org-export-get-tags hl info nil t)) info))))
  ;; Tag inheritance checks FILETAGS keywords.
  (should
   (equal
    '(("a" "b" "tag"))
    (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
      (org-element-map tree 'headline
	(lambda (hl) (org-export-get-tags hl info nil t)) info)))))

(ert-deftest test-org-export/get-node-property ()
  "Test`org-export-get-node-property' specifications."
  ;; Standard test.
  (should
   (equal "value"
	  (org-test-with-parsed-data "* Headline
  :PROPERTIES:
  :prop:     value
  :END:"
	    (org-export-get-node-property
	     :PROP (org-element-map tree 'headline 'identity nil t)))))
  ;; Test inheritance.
  (should
   (equal "value"
	  (org-test-with-parsed-data "* Parent
  :PROPERTIES:
  :prop:     value
  :END:
** Headline
   Paragraph"
	    (org-export-get-node-property
	     :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  ;; Cannot return a value before the first headline.
  (should-not
   (org-test-with-parsed-data "Paragraph
* Headline
  :PROPERTIES:
  :prop:     value
  :END:"
     (org-export-get-node-property
      :PROP (org-element-map tree 'paragraph 'identity nil t)))))

(ert-deftest test-org-export/get-category ()
  "Test `org-export-get-category' specifications."
  ;; Standard test.
  (should
   (equal "value"
	  (org-test-with-parsed-data "* Headline
  :PROPERTIES:
  :CATEGORY:     value
  :END:"
	    (org-export-get-category
	     (org-element-map tree 'headline 'identity nil t) info))))
  ;; Test inheritance from a parent headline.
  (should
   (equal '("value" "value")
	  (org-test-with-parsed-data "* Headline1
  :PROPERTIES:
  :CATEGORY:     value
  :END:
** Headline2"
	    (org-element-map tree 'headline
	      (lambda (hl) (org-export-get-category hl info)) info))))
  ;; Test inheritance from #+CATEGORY keyword
  (should
   (equal "value"
	  (org-test-with-parsed-data "#+CATEGORY: value
* Headline"
	    (org-export-get-category
	     (org-element-map tree 'headline 'identity nil t) info))))
  ;; Test inheritance from file name.
  (should
   (equal "test"
	  (org-test-with-parsed-data "* Headline"
	    (let ((info (plist-put info :input-file "~/test.org")))
	      (org-export-get-category
	       (org-element-map tree 'headline 'identity nil t) info)))))
  ;; Fall-back value.
  (should
   (equal "???"
	  (org-test-with-parsed-data "* Headline"
	    (org-export-get-category
	     (org-element-map tree 'headline 'identity nil t) info)))))

(ert-deftest test-org-export/first-sibling-p ()
  "Test `org-export-first-sibling-p' specifications."
  ;; Standard test.
  (should
   (equal
    '(yes yes no)
    (org-test-with-parsed-data "* H\n** H 2\n** H 3"
      (org-element-map tree 'headline
	(lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
	info))))
  (should
   (equal '(yes no)
	  (org-test-with-parsed-data "- item\n\n  para"
	    (org-element-map tree 'paragraph
	      (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
	      info))))
  ;; Ignore sections for headlines.
  (should
   (equal '(yes yes)
	  (org-test-with-parsed-data "* H\nSection\n** H 2"
	    (org-element-map tree 'headline
	      (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
	      info))))
  ;; Ignore headlines not exported.
  (should
   (equal
    '(yes)
    (let ((org-export-exclude-tags '("ignore")))
      (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
	(org-element-map tree 'headline
	  (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
	  info))))))

(ert-deftest test-org-export/last-sibling-p ()
  "Test `org-export-last-sibling-p' specifications."
  ;; Standard test.
  (should
   (equal
    '(yes no yes)
    (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
      (org-element-map tree 'headline
	(lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
	info))))
  (should
   (equal '(no yes)
	  (org-test-with-parsed-data "- item\n\n  para"
	    (org-element-map tree 'paragraph
	      (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
	      info))))
  ;; Ignore headlines not exported.
  (should
   (equal
    '(yes)
    (let ((org-export-exclude-tags '("ignore")))
      (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
	(org-element-map tree 'headline
	  (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
	  info)))))
  ;; Handle gracefully discontinuous headings.
  (should
   (equal '(yes yes)
	  (org-test-with-parsed-data "** S\n* H"
	    (org-element-map tree 'headline
	      (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no)))))))

(ert-deftest test-org-export/handle-inlinetasks ()
  "Test inlinetask export."
  ;; Inlinetask with an exclude tag.
  (when (featurep 'org-inlinetask)
    (should
     (equal
      ""
      (let ((org-inlinetask-min-level 3)
	    org-export-filter-body-functions
	    org-export-filter-final-output-functions)
	(org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
	  (org-export-as (org-test-default-backend)
			 nil nil nil '(:exclude-tags ("noexp")))))))
    ;; Inlinetask with an include tag.
    (should
     (equal
      "* H2\n*** Inline :exp:\n"
      (let ((org-inlinetask-min-level 3)
	    (org-tags-column 0))
	(org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
	  (org-export-as (org-test-default-backend)
			 nil nil nil '(:select-tags ("exp")))))))
    ;; Ignore inlinetask with a TODO keyword and tasks excluded.
    (should
     (equal ""
	    (let ((org-todo-keywords '((sequence "TODO" "DONE")))
		  (org-inlinetask-min-level 3)
		  org-export-filter-body-functions
		  org-export-filter-final-output-functions)
	      (org-test-with-temp-text "*** TODO Inline"
		(org-export-as (org-test-default-backend)
			       nil nil nil '(:with-tasks nil))))))))


\f
;;; Keywords

(ert-deftest test-org-export/get-date ()
  "Test `org-export-get-date' specifications."
  ;; Return a properly formatted string when
  ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  ;; consists in a single timestamp.
  (should
   (equal "29 03 2012"
	  (let ((org-export-date-timestamp-format "%d %m %Y"))
	    (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
	      (org-export-get-date info)))))
  ;; Return a secondary string otherwise.
  (should-not
   (stringp
    (let ((org-export-date-timestamp-format nil))
      (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
	(org-export-get-date info)))))
  (should
   (equal '("Date")
	  (org-test-with-parsed-data "#+DATE: Date"
	    (org-export-get-date info))))
  ;; Optional argument has precedence over
  ;; `org-export-date-timestamp-format'.
  (should
   (equal "29 03"
	  (let ((org-export-date-timestamp-format "%d %m %Y"))
	    (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
	      (org-export-get-date info "%d %m"))))))


\f
;;; Links

(ert-deftest test-org-export/custom-protocol-maybe ()
  "Test `org-export-custom-protocol-maybe' specifications."
  (should
   (string-match
    "success"
    (progn
      (org-link-set-parameters "foo" :export (lambda (_p _d _f _i) "success"))
      (org-export-string-as
       "[[foo:path]]"
       (org-export-create-backend
	:name 'test
	:transcoders
	'((section . (lambda (s c i) c))
	  (paragraph . (lambda (p c i) c))
	  (link . (lambda (l c i)
		    (or (org-export-custom-protocol-maybe l c 'test i)
			"failure")))))))))
  (should-not
   (string-match
    "success"
    (progn
      (org-link-set-parameters
       "foo" :export (lambda (_p _d f _i) (and (eq f 'test) "success")))
      (org-export-string-as
       "[[foo:path]]"
       (org-export-create-backend
	:name 'no-test
	:transcoders
	'((section . (lambda (s c i) c))
	  (paragraph . (lambda (p c i) c))
	  (link . (lambda (l c i)
		    (or (org-export-custom-protocol-maybe l c 'no-test i)
			"failure")))))))))
  ;; Ignore anonymous backends.
  (should-not
   (string-match
    "success"
    (progn
      (org-link-set-parameters
       "foo" :export (lambda (_p _d f _i) (and (eq f 'test) "success")))
      (org-export-string-as
       "[[foo:path]]"
       (org-export-create-backend
	:transcoders
	'((section . (lambda (s c i) c))
	  (paragraph . (lambda (p c i) c))
	  (link . (lambda (l c i)
		    (or (org-export-custom-protocol-maybe l c nil i)
			"failure"))))))))))

(ert-deftest test-org-export/get-coderef-format ()
  "Test `org-export-get-coderef-format' specifications."
  ;; A link without description returns "%s"
  (should (equal (org-export-get-coderef-format "(ref:line)" nil)
		 "%s"))
  ;; Return "%s" when path is matched within description.
  (should (equal (org-export-get-coderef-format "path" "desc (path)")
		 "desc %s"))
  ;; Otherwise return description.
  (should (equal (org-export-get-coderef-format "path" "desc")
		 "desc")))

(ert-deftest test-org-export/inline-image-p ()
  "Test `org-export-inline-image-p' specifications."
  (should
   (org-export-inline-image-p
    (org-test-with-temp-text "[[#id]]"
      (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
    '(("custom-id" . "id")))))

(ert-deftest test-org-export/insert-image-links ()
  "Test `org-export-insert-image-links' specifications."
  (should-not
   (member "file"
	   (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
	     (org-element-map tree 'link
	       (lambda (l) (org-element-property :type l))))))
  (should
   (member "file"
	   (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
	     (org-element-map (org-export-insert-image-links tree info) 'link
	       (lambda (l) (org-element-property :type l))))))
  ;; Properly set `:parent' property when replace contents with image
  ;; link.
  (should
   (memq 'link
	 (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
	   (org-element-map (org-export-insert-image-links tree info) 'link
	     (lambda (l)
	       (org-element-type (org-element-property :parent l)))))))
  ;; With optional argument RULES, recognize different links as
  ;; images.
  (should-not
   (member "file"
	   (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
	     (org-element-map (org-export-insert-image-links tree info) 'link
	       (lambda (l) (org-element-property :type l))))))
  (should
   (member "file"
	   (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
	     (org-element-map
		 (org-export-insert-image-links tree info '(("file" . "xxx")))
		 'link
	       (lambda (l) (org-element-property :type l))))))
  ;; If an image link was included from another file, make sure to
  ;; shift any relative path accordingly.
  (should
   (string-prefix-p
    "file:org-includee-"
    (let* ((subdir (make-temp-file "org-includee-" t))
	   (includee (expand-file-name "includee.org" subdir))
	   (includer (make-temp-file "org-includer-")))
      (write-region "file:foo.png" nil includee)
      (write-region (format "#+INCLUDE: %S"
			    (file-relative-name includee
						temporary-file-directory))
		    nil includer)
      (let ((buffer (find-file-noselect includer t)))
	(unwind-protect
	    (with-current-buffer buffer
	      (org-export-as
	       (org-export-create-backend
		:transcoders
		'((section . (lambda (_s c _i) c))
		  (paragraph . (lambda (_p c _i) c))
		  (link . (lambda (l c _i) (org-element-link-interpreter l c))))
		:filters
		'((:filter-parse-tree
		   (lambda (d _b i) (org-export-insert-image-links d i)))))))
	  (when (buffer-live-p buffer)
	    (with-current-buffer buffer (set-buffer-modified-p nil))
	    (kill-buffer buffer))
	  (when (file-exists-p subdir) (delete-directory subdir t))
	  (when (file-exists-p includer) (delete-file includer)))))))
  (should
   (string-match-p
    "file:org-includee-.+?foo\\.png"
    (let* ((subdir (make-temp-file "org-includee-" t))
	   (includee (expand-file-name "includee.org" subdir))
	   (includer (make-temp-file "org-includer-")))
      (write-region "[[https://orgmode.org][file:foo.png]]" nil includee)
      (write-region (format "#+INCLUDE: %S"
			    (file-relative-name includee
						temporary-file-directory))
		    nil includer)
      (let ((buffer (find-file-noselect includer t)))
	(unwind-protect
	    (with-current-buffer buffer
	      (org-export-as
	       (org-export-create-backend
		:transcoders
		'((section . (lambda (_s c _i) c))
		  (paragraph . (lambda (_p c _i) c))
		  (link . (lambda (l c _i) (org-element-link-interpreter l c))))
		:filters
		'((:filter-parse-tree
		   (lambda (d _b i) (org-export-insert-image-links d i)))))))
	  (when (buffer-live-p buffer)
	    (with-current-buffer buffer (set-buffer-modified-p nil))
	    (kill-buffer buffer))
	  (when (file-exists-p subdir) (delete-directory subdir t))
	  (when (file-exists-p includer) (delete-file includer)))))))
  (should
   (string-match-p
    "file:org-includee.+?file:org-includee"
    (let* ((subdir (make-temp-file "org-includee-" t))
	   (includee (expand-file-name "includee.org" subdir))
	   (includer (make-temp-file "org-includer-")))
      (write-region "[[file:bar.png][file:foo.png]]" nil includee)
      (write-region (format "#+INCLUDE: %S"
			    (file-relative-name includee
						temporary-file-directory))
		    nil includer)
      (let ((buffer (find-file-noselect includer t)))
	(unwind-protect
	    (with-current-buffer buffer
	      (org-export-as
	       (org-export-create-backend
		:transcoders
		'((section . (lambda (_s c _i) c))
		  (paragraph . (lambda (_p c _i) c))
		  (link . (lambda (l c _i) (org-element-link-interpreter l c))))
		:filters
		'((:filter-parse-tree
		   (lambda (d _b i) (org-export-insert-image-links d i)))))))
	  (when (buffer-live-p buffer)
	    (with-current-buffer buffer (set-buffer-modified-p nil))
	    (kill-buffer buffer))
	  (when (file-exists-p subdir) (delete-directory subdir t))
	  (when (file-exists-p includer) (delete-file includer))))))))

(ert-deftest test-org-export/fuzzy-link ()
  "Test fuzzy links specifications."
  ;; Link to an headline should return headline's number.
  (should
   ;; Note: Headline's number is in fact a list of numbers.
   (equal '(2)
	  (org-test-with-parsed-data
	      "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
	    (org-element-map tree 'link
	      (lambda (link)
		(org-export-get-ordinal
		 (org-export-resolve-fuzzy-link link info) info)) info t))))
  ;; Link to a target in an item should return item's number.
  (should
   ;; Note: Item's number is in fact a list of numbers.
   (equal '(1 2)
	  (org-test-with-parsed-data
	      "- Item1\n  - Item11\n  - <<test>>Item12\n- Item2\n\n\n[[test]]"
	    (org-element-map tree 'link
	      (lambda (link)
		(org-export-get-ordinal
		 (org-export-resolve-fuzzy-link link info) info)) info t))))
  ;; Link to a target in a footnote should return footnote's number.
  (should
   (equal '(2 3)
	  (org-test-with-parsed-data "
Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
\[fn:1] A

\[fn:2] <<test>>B"
	    (org-element-map tree 'link
	      (lambda (link)
		(org-export-get-ordinal
		 (org-export-resolve-fuzzy-link link info) info)) info))))
  ;; Link to a named element should return sequence number of that
  ;; element.
  (should
   (= 2
      (org-test-with-parsed-data
	  "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
	(org-element-map tree 'link
	  (lambda (link)
	    (org-export-get-ordinal
	     (org-export-resolve-fuzzy-link link info) info)) info t))))
  ;; Link to a target not within an item, a table, a footnote
  ;; reference or definition should return section number.
  (should
   (equal '(2)
	  (org-test-with-parsed-data
	      "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
	    (org-element-map tree 'link
	      (lambda (link)
		(org-export-get-ordinal
		 (org-export-resolve-fuzzy-link link info) info)) info t))))
  ;; Space are not significant when matching a fuzzy link.
  (should
   (org-test-with-parsed-data "* Head 1\n[[Head\n  1]]"
     (org-element-map tree 'link
       (lambda (link) (org-export-resolve-fuzzy-link link info))
       info t)))
  ;; Statistics cookies are ignored for headline match.
  (should
   (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
     (org-element-map tree 'link
       (lambda (link) (org-export-resolve-fuzzy-link link info))
       info t)))
  (should
   (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
     (org-element-map tree 'link
       (lambda (link) (org-export-resolve-fuzzy-link link info))
       info t)))
  ;; Case is not significant when matching headings and radio targets.
  (should
   (org-test-with-parsed-data "* Head line\n[[head line]]"
     (org-element-map tree 'link
       (lambda (link) (org-export-resolve-fuzzy-link link info))
       info t))))

(ert-deftest test-org-export/resolve-link ()
  "Test `org-export-resolve-link' specifications."
  (should
   ;; Match ID links
   (equal
    "Headline1"
    (org-test-with-parsed-data "* Headline1
:PROPERTIES:
:ID: aaaa
:END:
* Headline2"
      (org-element-property
       :raw-value (org-export-resolve-link "#aaaa" info)))))
   ;; Match Custom ID links
  (should
   (equal
    "Headline1"
    (org-test-with-parsed-data
	"* Headline1
:PROPERTIES:
:CUSTOM_ID: test
:END:
* Headline2"
      (org-element-property
       :raw-value (org-export-resolve-link "#test" info)))))
  ;; Match fuzzy links
  (should
   (equal
    "B"
    (org-test-with-parsed-data
	"* A\n* B\n* C"
      (org-element-property
       :raw-value (org-export-resolve-link "B" info))))))

(defun test-org-gen-loc-list(text type)
  (org-test-with-parsed-data text
    (org-element-map tree type
      (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))

(ert-deftest test-org-export/get-loc ()
  "Test `org-export-get-loc' specifications."
  (should
   ;; "-n" resets line number.
   (equal '(0)
	  (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n  Text\n#+END_EXAMPLE"
				 'example-block)))
  ;; The first "+n" has 0 lines before it
  (should
   (equal '(0)
	  (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n  Text\n#+END_EXAMPLE"
				 'example-block)))
  ;; "-n 10" resets line number but has "9 lines" before it.
  (should
   (equal '(9)
	  (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n  Text\n#+END_EXAMPLE"
				 'example-block)))
  ;; -n10 with two lines then +n 15
  (should
   (equal '(9 25)
	  (test-org-gen-loc-list "
#+BEGIN_EXAMPLE -n 10
  Text_10
  Second line(11)
#+END_EXAMPLE
#+BEGIN_EXAMPLE +n 15
  Text line (11 + 15)
#+END_EXAMPLE"
				 'example-block)))
  (should
   (equal '(9 19 0)
	  (test-org-gen-loc-list "
#+BEGIN_EXAMPLE -n 10
  Text
#+END_EXAMPLE
#+BEGIN_EXAMPLE +n 10
  Text
#+END_EXAMPLE

#+BEGIN_EXAMPLE -n
  Text
#+END_EXAMPLE"
				 'example-block)))
  ;; an Example Block without -n does not add to the line count.
  (should
   (equal '(9 no-loc 19)
	  (test-org-gen-loc-list "
#+BEGIN_EXAMPLE -n 10
  Text
#+END_EXAMPLE
#+BEGIN_EXAMPLE
  Text
#+END_EXAMPLE
#+BEGIN_EXAMPLE +n 10
  Text
#+END_EXAMPLE"
				 'example-block)))
  ;; "-n" resets line number.
  (should
   (equal
    '(0)
    (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n  (- 1 1) \n#+END_SRC"
			   'src-block)))
  ;; The first "+n" has 0 lines before it.
  (should
   (equal '(0)
	  (test-org-gen-loc-list
	   "#+BEGIN_SRC emacs-lisp +n \n  (+ 0 (- 1 1))\n#+END_SRC"
	   'src-block)))
  ;; "-n 10" resets line number but has "9 lines" before it.
  (should
   (equal '(9)
	  (test-org-gen-loc-list
	   "#+BEGIN_SRC emacs-lisp -n 10\n  (- 10 1)\n#+END_SRC"
	   'src-block)))
  (should
   (equal '(9 25)
	  (test-org-gen-loc-list "
#+BEGIN_SRC emacs-lisp -n 10
  (- 10 1)
  (+ (- 10 1) 1)
#+END_SRC
#+BEGIN_SRC emacs-lisp +n 15
  (+ (- 10 1) 2 (- 15 1))
#+END_SRC"
				 'src-block)))
  (should
   (equal '(9 19 0)
	  (test-org-gen-loc-list "
#+BEGIN_SRC emacs-lisp -n 10
  (- 10 1)
#+END_SRC
#+BEGIN_SRC emacs-lisp +n 10
  (+ (- 10 1) 1 (- 10 1))
#+END_SRC
#+BEGIN_SRC emacs-lisp -n
  (- 1 1)
#+END_SRC"
				 'src-block)))
  ;; A SRC Block without -n does not add to the line count.
  (should
   (equal '(9 no-loc 19)
	  (test-org-gen-loc-list
	   "#+BEGIN_SRC emacs-lisp -n 10
  (+ (-10 1) 1)
#+END_SRC
#+BEGIN_SRC emacs-lisp
  (+ 2 2)
#+END_SRC
#+BEGIN_SRC emacs-lisp +n 10
  (+ (- 10 1) 1 (- 10 1))
#+END_SRC"
	   'src-block))))

(ert-deftest test-org-export/resolve-coderef ()
  "Test `org-export-resolve-coderef' specifications."
  (let ((org-coderef-label-format "(ref:%s)"))
    ;; A link to a "-n -k -r" block returns line number.
    (should
     (= 1
	(org-test-with-parsed-data
	    "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
	  (org-export-resolve-coderef "coderef" info))))
    (should
     (= 10
	(org-test-with-parsed-data
	 "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
	 (org-export-resolve-coderef "coderef" info))))
    (should
     (= 135
	(org-test-with-parsed-data
	 "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
#+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
	 (org-export-resolve-coderef "coderef" info))))
    (should
     (= 1
	(org-test-with-parsed-data
	    "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
	  (org-export-resolve-coderef "coderef" info))))
    (should
     (= 10
	(org-test-with-parsed-data
	 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
	 (org-export-resolve-coderef "coderef" info))))
    (should
     (= 135
	(org-test-with-parsed-data
	 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
#+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
	 (org-export-resolve-coderef "coderef" info))))
    ;; A link to a "-n -r" block returns line number.
    (should
     (= 1
	(org-test-with-parsed-data
	    "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
	  (org-export-resolve-coderef "coderef" info))))
    (should
     (= 10
	(org-test-with-parsed-data
	 "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
	 (org-export-resolve-coderef "coderef" info))))
    (should
     (= 135
	(org-test-with-parsed-data
	 "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
#+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
	 (org-export-resolve-coderef "coderef" info))))

    (should
     (= 1
	(org-test-with-parsed-data
	    "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
	  (org-export-resolve-coderef "coderef" info))))
    (should
     (= 10
	(org-test-with-parsed-data
	 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
	 (org-export-resolve-coderef "coderef" info))))
    (should
     (= 135
	(org-test-with-parsed-data
	 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
#+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
	 (org-export-resolve-coderef "coderef" info))))
    ;; A link to a "-n" block returns coderef.
    (should
     (equal "coderef"
	    (org-test-with-parsed-data
		"#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
	      (org-export-resolve-coderef "coderef" info))))
    (should
     (equal "coderef"
	    (org-test-with-parsed-data
		"#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
	      (org-export-resolve-coderef "coderef" info))))
    ;; A link to a "-r" block returns line number.
    (should
     (= 1
	(org-test-with-parsed-data
	    "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
	  (org-export-resolve-coderef "coderef" info))))
    (should
     (= 1
	(org-test-with-parsed-data
	    "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
	  (org-export-resolve-coderef "coderef" info))))
    ;; A link to a block without a switch returns coderef.
    (should
     (equal "coderef"
	    (org-test-with-parsed-data
		"#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
	      (org-export-resolve-coderef "coderef" info))))
    (org-test-with-parsed-data
	"#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
      (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
    ;; Correctly handle continued line numbers.  A "+n" switch should
    ;; resume numbering from previous block with numbered lines,
    ;; ignoring blocks not numbering lines in the process.  A "-n"
    ;; switch resets count.
    (should
     (equal '(2 1)
	    (org-test-with-parsed-data "
#+BEGIN_EXAMPLE -n
Text.
#+END_EXAMPLE

#+BEGIN_SRC emacs-lisp
\(- 1 1)
#+END_SRC

#+BEGIN_SRC emacs-lisp +n -r
\(+ 1 1) (ref:addition)
#+END_SRC

#+BEGIN_EXAMPLE -n -r
Another text. (ref:text)
#+END_EXAMPLE"
	      (list (org-export-resolve-coderef "addition" info)
		    (org-export-resolve-coderef "text" info)))))
    ;; Recognize coderef with user-specified syntax.
    (should
     (equal
      "text"
      (org-test-with-parsed-data
	  "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
	(org-export-resolve-coderef "text" info))))
    ;; Unresolved coderefs raise a `org-link-broken' signal.
    (should
     (condition-case nil
	 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
	   (org-export-resolve-coderef "unknown" info))
       (org-link-broken t)))))

(ert-deftest test-org-export/resolve-fuzzy-link ()
  "Test `org-export-resolve-fuzzy-link' specifications."
  ;; Match target objects.
  (should
   (org-test-with-parsed-data "<<target>> [[target]]"
     (org-export-resolve-fuzzy-link
      (org-element-map tree 'link 'identity info t) info)))
  ;; Match named elements.
  (should
   (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
     (org-export-resolve-fuzzy-link
      (org-element-map tree 'link 'identity info t) info)))
  ;; Match exact headline's name.
  (should
   (org-test-with-parsed-data "* My headline\n[[My headline]]"
     (org-export-resolve-fuzzy-link
      (org-element-map tree 'link 'identity info t) info)))
  ;; Targets objects have priority over headline titles.
  (should
   (eq 'target
       (org-test-with-parsed-data "* target\n<<target>>[[target]]"
	 (org-element-type
	  (org-export-resolve-fuzzy-link
	   (org-element-map tree 'link 'identity info t) info)))))
  ;; Named elements have priority over headline titles.
  (should
   (eq 'paragraph
       (org-test-with-parsed-data
	   "* target\n#+NAME: target\nParagraph\n\n[[target]]"
	 (org-element-type
	  (org-export-resolve-fuzzy-link
	   (org-element-map tree 'link 'identity info t) info)))))
  ;; If link's path starts with a "*", only match headline titles,
  ;; though.
  (should
   (eq 'headline
       (org-test-with-parsed-data
	   "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
	 (org-element-type
	  (org-export-resolve-fuzzy-link
	   (org-element-map tree 'link 'identity info t) info)))))
  ;; Raise a `org-link-broken' signal if no match.
  (should
   (org-test-with-parsed-data "[[target]]"
     (condition-case nil
	 (org-export-resolve-fuzzy-link
	  (org-element-map tree 'link #'identity info t) info)
       (org-link-broken t))))
  ;; Match fuzzy link even when before first headline.
  (should
   (eq 'headline
       (org-test-with-parsed-data "[[hl]]\n* hl"
	 (org-element-type
	  (org-export-resolve-fuzzy-link
	   (org-element-map tree 'link 'identity info t) info)))))
  ;; Handle escaped fuzzy links.
  (should
   (org-test-with-parsed-data "* [foo]\n[[\\[foo\\]]]"
     (org-export-resolve-fuzzy-link
      (org-element-map tree 'link #'identity info t) info))))

(ert-deftest test-org-export/resolve-id-link ()
  "Test `org-export-resolve-id-link' specifications."
  ;; Regular test for custom-id link.
  (should
   (equal '("Headline1")
	  (org-test-with-parsed-data "* Headline1
:PROPERTIES:
:CUSTOM_ID: test
:END:
* Headline 2
\[[#test]]"
	    (org-element-property
	     :title
	     (org-export-resolve-id-link
	      (org-element-map tree 'link 'identity info t) info)))))
  ;; Raise a `org-link-broken' signal on failing searches.
  (should
   (org-test-with-parsed-data "* Headline1
:PROPERTIES:
:CUSTOM_ID: test
:END:
* Headline 2
\[[#no-match]]"
     (condition-case nil
	 (org-export-resolve-id-link
	  (org-element-map tree 'link #'identity info t) info)
       (org-link-broken t))))
  ;; Test for internal id target.
  (should
   (equal '("Headline1")
	  (org-test-with-parsed-data "* Headline1
:PROPERTIES:
:ID: aaaa
:END:
* Headline 2
\[[id:aaaa]]"
	    (org-element-property
	     :title
	     (org-export-resolve-id-link
	      (org-element-map tree 'link 'identity info t) info)))))
  ;; Test for external id target.
  (should
   (equal
    "external-file"
    (org-test-with-parsed-data "[[id:aaaa]]"
      (org-export-resolve-id-link
       (org-element-map tree 'link 'identity info t)
       (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))

(ert-deftest test-org-export/resolve-radio-link ()
  "Test `org-export-resolve-radio-link' specifications."
  ;; Standard test.
  (should
   (org-test-with-temp-text "<<<radio>>> radio"
     (org-update-radio-target-regexp)
     (let* ((tree (org-element-parse-buffer))
	    (info `(:parse-tree ,tree)))
       (org-export-resolve-radio-link
	(org-element-map tree 'link 'identity info t)
	info))))
  ;; Radio targets are case-insensitive.
  (should
   (org-test-with-temp-text "<<<RADIO>>> radio"
     (org-update-radio-target-regexp)
     (let* ((tree (org-element-parse-buffer))
	    (info `(:parse-tree ,tree)))
       (org-export-resolve-radio-link
	(org-element-map tree 'link 'identity info t)
	info))))
  ;; Radio target with objects.
  (should
   (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
     (org-update-radio-target-regexp)
     (let* ((tree (org-element-parse-buffer))
	    (info `(:parse-tree ,tree)))
       (org-export-resolve-radio-link
	(org-element-map tree 'link 'identity info t)
	info))))
  ;; Radio target with objects at its beginning.
  (should
   (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
     (org-update-radio-target-regexp)
     (let* ((tree (org-element-parse-buffer))
	    (info `(:parse-tree ,tree)))
       (org-export-resolve-radio-link
	(org-element-map tree 'link 'identity info t)
	info))))
  ;; Radio link next to an apostrophe.
  (should
   (org-test-with-temp-text "<<<radio>>> radio's"
     (org-update-radio-target-regexp)
     (let* ((tree (org-element-parse-buffer))
	    (info `(:parse-tree ,tree)))
       (org-export-resolve-radio-link
	(org-element-map tree 'link 'identity info t)
	info))))
  ;; Multiple radio targets.
  (should
   (equal '("radio1" "radio2")
	  (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
	    (org-update-radio-target-regexp)
	    (let* ((tree (org-element-parse-buffer))
		   (info `(:parse-tree ,tree)))
	      (org-element-map tree 'link
		(lambda (link)
		  (org-element-property
		   :value (org-export-resolve-radio-link link info)))
		info)))))
  ;; Radio target is whitespace insensitive.
  (should
   (org-test-with-temp-text "<<<a radio>>> a\n  radio"
     (org-update-radio-target-regexp)
     (let* ((tree (org-element-parse-buffer))
	    (info `(:parse-tree ,tree)))
       (org-element-map tree 'link
	 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))

(ert-deftest test-org-export/file-uri ()
  "Test `org-export-file-uri' specifications."
  ;; Preserve relative filenames.
  (should (equal "relative.org" (org-export-file-uri "relative.org")))
  ;; Local files start with "file://"
  (should (equal (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "/local.org"))
		 (org-export-file-uri "/local.org")))
  ;; Remote files start with "file://"
  (should (equal "file://ssh:myself@some.where:papers/last.pdf"
		 (org-export-file-uri "/ssh:myself@some.where:papers/last.pdf")))
  (should (equal "file://localhost/etc/fstab"
		 (org-export-file-uri "//localhost/etc/fstab")))
  ;; Expand filename starting with "~".
  (should (equal (org-export-file-uri "~/file.org")
		 (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "~/file.org")))))

(ert-deftest test-org-export/get-reference ()
  "Test `org-export-get-reference' specifications."
  (should
   (org-test-with-parsed-data "* Headline"
     (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
			       info)))
  ;; For a given element always return the same reference.
  (should
   (org-test-with-parsed-data "* Headline"
     (let ((headline (org-element-map tree 'headline #'identity nil t)))
       (equal (org-export-get-reference headline info)
	      (org-export-get-reference headline info)))))
  ;; References get through local export backends.
  (should
   (org-test-with-parsed-data "* Headline"
     (let ((headline (org-element-map tree 'headline #'identity nil t))
	   (backend
	    (org-export-create-backend
	     :transcoders
	     '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
       (equal (org-trim (org-export-data-with-backend headline backend info))
	      (org-export-get-reference headline info)))))
  (should
   (org-test-with-parsed-data "* Headline"
     (let ((headline (org-element-map tree 'headline #'identity nil t))
	   (backend
	    (org-export-create-backend
	     :transcoders
	     '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
       (equal (org-export-with-backend backend headline nil info)
	      (org-export-get-reference headline info)))))
  ;; Use search cells defined in `:crossrefs'.  However, handle
  ;; duplicate search cells.
  (should
   (equal "org0000001"
	  (org-test-with-parsed-data "* Headline"
	    (let* ((headline (org-element-map tree 'headline #'identity nil t))
		   (search-cell (car (org-export-search-cells headline))))
	      (setq info
		    (plist-put info :crossrefs (list (cons search-cell 1))))
	      (org-export-get-reference headline info)))))
  (should-not
   (equal '("org0000001" "org0000001")
	  (org-test-with-parsed-data "* H\n** H"
	    (org-element-map tree 'headline
	      (lambda (h)
		(let* ((search-cell (car (org-export-search-cells h)))
		       (info (plist-put info :crossrefs
					(list (cons search-cell 1)))))
		  (org-export-get-reference h info))))))))

\f
;;; Pseudo objects and pseudo elements

(ert-deftest test-org-export/pseudo-elements ()
  "Test exporting pseudo-elements."
  ;; Handle blank lines after pseudo-elements.  In particular, do not
  ;; replace them with white spaces.
  (should
   (equal "contents\n\nparagraph\n"
	  (let ((backend (org-export-create-backend
			  :transcoders
			  '((pseudo-element . (lambda (_p c _i) c))
			    (paragraph . (lambda (_p c _i) c))
			    (plain-text . (lambda (c _i) c)))))
		(element '(pseudo-element (:post-blank 1) "contents"))
		(paragraph '(paragraph nil "paragraph"))
		(data '(org-data nil)))
	    (org-element-adopt data element paragraph)
	    (org-export-data-with-backend data backend nil)))))

(ert-deftest test-org-export/pseudo-objects ()
  "Test exporting pseudo-objects."
  ;; Handle blank spaces after pseudo-objects.  In particular, do not
  ;; replace them with newlines.
  (should
   (equal "begin x end\n"
	  (let ((backend (org-export-create-backend
			  :transcoders
			  '((pseudo-object . (lambda (_p c _i) c))
			    (paragraph . (lambda (_p c _i) c))
			    (plain-text . (lambda (c _i) c)))))
		(object '(pseudo-object (:post-blank 1) "x"))
		(paragraph '(paragraph nil)))
	    (org-element-adopt paragraph "begin " object "end")
	    (org-export-data-with-backend paragraph backend nil)))))

\f
;;; Raw objects

(ert-deftest test-org-export/raw-strings ()
  "Test exporting raw objects."
  (should
   (equal "foo"
          (let ((backend (org-export-create-backend))
                (object (org-export-raw-string "foo")))
            (org-export-data-with-backend object backend nil)))))

\f
;;; Src-block and example-block

(ert-deftest test-org-export/unravel-code ()
  "Test `org-export-unravel-code' function."
  ;; Code without reference.
  (should
   (equal '("(+ 1 1)")
	  (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
	    (org-export-unravel-code (org-element-at-point)))))
  ;; Code with reference.
  (should
   (equal '("(+ 1 1)" (1 . "test"))
	  (org-test-with-temp-text
	      "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
	    (let  ((org-coderef-label-format "(ref:%s)"))
	      (org-export-unravel-code (org-element-at-point))))))
  ;; Code with user-defined reference.
  (should
   (equal
    '("(+ 1 1)" (1 . "test"))
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
      (let ((org-coderef-label-format "(ref:%s)"))
	(org-export-unravel-code (org-element-at-point))))))
  ;; Code references keys are relative to the current block.
  (should
   (equal '("(+ 2 2)\n(+ 3 3)" (2 . "one"))
	  (org-test-with-temp-text "
#+BEGIN_EXAMPLE -n
\(+ 1 1)
#+END_EXAMPLE
#+BEGIN_EXAMPLE +n
\(+ 2 2)
\(+ 3 3) (ref:one)
#+END_EXAMPLE"
	    (goto-line 5)
	    (let ((org-coderef-label-format "(ref:%s)"))
	      (org-export-unravel-code (org-element-at-point)))))\x14))

(ert-deftest test-org-export/format-code-default ()
  "Test `org-export-format-code-default' specifications."
  ;; Preserve blank lines, even when code is empty.
  (should
   (equal "\n\n"
	  (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
	    (org-export-format-code-default
	     (org-element-map tree 'src-block #'identity info t) info))))
  ;; Likewise, preserve leading and trailing blank lines in the code.
  (should
   (equal "\n(+ 1 1)\n"
	  (org-test-with-parsed-data
	      "#+BEGIN_SRC emacs-lisp\n\n(+ 1 1)\n#+END_SRC"
	    (org-export-format-code-default
	     (org-element-map tree 'src-block #'identity info t) info))))
  (should
   (equal "(+ 1 1)\n\n"
	  (org-test-with-parsed-data
	      "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n\n#+END_SRC"
	    (org-export-format-code-default
	     (org-element-map tree 'src-block #'identity info t) info))))
  ;; Number lines, two whitespace characters before the actual loc.
  (should
   (equal "1  a\n2  b\n"
	  (org-test-with-parsed-data
	      "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
	    (org-export-format-code-default
	     (org-element-map tree 'src-block #'identity info t) info))))
  ;; Numbering includes blank lines.
  (should
   (equal "1  \n2  a\n3  \n4  b\n5  \n"
	  (org-test-with-parsed-data
	      "#+BEGIN_SRC emacs-lisp +n\n\na\n\nb\n\n#+END_SRC"
	    (org-export-format-code-default
	     (org-element-map tree 'src-block #'identity info t) info))))
  ;; Put references 6 whitespace characters after the widest line,
  ;; wrapped within parenthesis.
  (should
   (equal "123      (a)\n1        (b)\n"
	  (let ((org-coderef-label-format "(ref:%s)"))
	    (org-test-with-parsed-data
		"#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
	      (org-export-format-code-default
	       (org-element-map tree 'src-block #'identity info t) info))))))

(ert-deftest test-org-export/latex-src-block-verbatim-caption ()
  "Test `org-latex-src-block' caption for verbatim environment.
Check that percent sign does not become a part of format.
This test does not cover listings and custom environments."
  (let ((export
	 (lambda (buffer-text)
	   (org-test-with-parsed-data
	       buffer-text
	     (let* ((backend (org-export-get-backend 'latex))
		    (info (org-combine-plists
			   (org-export--get-export-attributes backend)
			   (org-export-get-environment backend)))
		    (result (org-latex-src-block
			     (org-element-map tree 'src-block #'identity info t)
			     t info)))
	       ;; Remove properties to make failure reports more clear.
	       (set-text-properties 0 (length result) nil result)
	       result)))))

    (should (equal
	     "\
\\begin{verbatim}
\"No float, no listings, 20%S\"
\\end{verbatim}
\\captionof{figure}{Caption of verbatim is below, 20\\%s}
"
	     (funcall export
		      "\
#+CAPTION: Caption of verbatim is below, 20%s
#+BEGIN_SRC emacs-lisp
  \"No float, no listings, 20%S\"
#+END_SRC")))

    ;; `org-latex-caption-above' has no associated property or keyword.
    (should (equal
	     "\
\\captionof{figure}{Caption of verbatim is above, 40\\%s}
\\begin{verbatim}
\"No float, no listings, 40%S\"
\\end{verbatim}"
	     (let ((org-latex-caption-above t))
	       (funcall export
			"\
#+CAPTION: Caption of verbatim is above, 40%s
#+BEGIN_SRC emacs-lisp
  \"No float, no listings, 40%S\"
#+END_SRC"))))

    (should (equal
	     "\
\\begin{figure*}[tp]
\\caption{Caption is above, 60\\%s}
\\begin{verbatim}
\"Float, no listings, 60%S\"
\\end{verbatim}
\\end{figure*}"
	     (let ((org-latex-caption-above t)
		   (org-latex-default-figure-position "tp"))
	       (funcall export
			"\
#+CAPTION: Caption is above, 60%s
#+ATTR_LATEX: :float multicolumn
#+BEGIN_SRC emacs-lisp
  \"Float, no listings, 60%S\"
#+END_SRC"))))

    (should (equal
	     "\
\\begin{figure*}[tp]
\\begin{verbatim}
\"Float, no lang, listings, 80%S\"
\\end{verbatim}
\\caption{Caption is below, 60\\%s}
\\end{figure*}"
	     (let ((org-latex-src-block-backend 'minted) ; inactive due to missing lang
		   (org-latex-default-figure-position "tp"))
	       ;; Namely "multicolumn" value to get just figure environment
	       ;; looks like a bug.
	       (funcall export
			"\
#+CAPTION: Caption is below, 60%s
#+ATTR_LATEX: :float multicolumn
#+BEGIN_SRC
  \"Float, no lang, listings, 80%S\"
#+END_SRC"))))

    (should (equal
	     "\
\\begin{verbatim}
\"No caption, no float, no listings, 100%S\"
\\end{verbatim}"
	     (funcall export
		      "\
#+BEGIN_SRC emacs-lisp
  \"No caption, no float, no listings, 100%S\"
#+END_SRC")))))


\f
;;; Smart Quotes

(ert-deftest test-org-export/activate-smart-quotes ()
  "Test `org-export-activate-smart-quotes' specifications."
  ;; Double quotes: standard test.
  (should
   (equal
    '("some &ldquo;quoted&rdquo; text")
    (let ((org-export-default-language "en"))
      (org-test-with-parsed-data "some \"quoted\" text"
	(org-element-map tree 'plain-text
	  (lambda (s) (org-export-activate-smart-quotes s :html info))
	  info)))))
  ;; Opening quotes: at the beginning of a paragraph.
  (should
   (equal
    '("&ldquo;begin")
    (let ((org-export-default-language "en"))
      (org-test-with-parsed-data "\"begin"
	(org-element-map tree 'plain-text
	  (lambda (s) (org-export-activate-smart-quotes s :html info))
	  info)))))
  ;; Opening quotes: after an object.
  (should
   (equal
    '("&ldquo;quoted&rdquo; text")
    (let ((org-export-default-language "en"))
      (org-test-with-parsed-data "=verb= \"quoted\" text"
	(org-element-map tree 'plain-text
	  (lambda (s) (org-export-activate-smart-quotes s :html info))
	  info)))))
  ;; Closing quotes: at the end of a paragraph.
  (should
   (equal
    '("Quoted &ldquo;text&rdquo;")
    (let ((org-export-default-language "en"))
      (org-test-with-parsed-data "Quoted \"text\""
	(org-element-map tree 'plain-text
	  (lambda (s) (org-export-activate-smart-quotes s :html info))
	  info)))))
  ;; Inner quotes: standard test.
  (should
   (equal '("« outer “inner” outer »")
	  (let ((org-export-default-language "fr"))
	    (org-test-with-parsed-data "\"outer 'inner' outer\""
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
		info)))))
  ;; Inner quotes: close to special symbols.
  (should
   (equal '("« outer (“inner”) outer »")
	  (let ((org-export-default-language "fr"))
	    (org-test-with-parsed-data "\"outer ('inner') outer\""
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
		info)))))
  (should
   (equal '("« “inner” »")
	  (let ((org-export-default-language "fr"))
	    (org-test-with-parsed-data "\"'inner'\""
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
		info)))))
  ;; Apostrophe: standard test.
  (should
   (equal '("It « shouldn’t » fail")
	  (let ((org-export-default-language "fr"))
	    (org-test-with-parsed-data "It \"shouldn't\" fail"
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
		info)))))
  (should
   (equal '("It shouldn’t fail")
	  (let ((org-export-default-language "fr"))
	    (org-test-with-parsed-data "It shouldn't fail"
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
		info)))))
  ;; Apostrophe: before an object.
  (should
   (equal
    '("« a’" " »")
    (let ((org-export-default-language "fr"))
      (org-test-with-parsed-data "\"a'=b=\""
	(org-element-map tree 'plain-text
	  (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
	  info)))))
  ;; Apostrophe: after an object.
  (should
   (equal '("« " "’s »")
	  (let ((org-export-default-language "fr"))
	    (org-test-with-parsed-data "\"=code='s\""
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
		info)))))
  ;; Special case: isolated quotes.
  (should
   (equal '("&ldquo;" "&rdquo;")
	  (let ((org-export-default-language "en"))
	    (org-test-with-parsed-data "\"$x$\""
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :html info))
		info)))))
  ;; Smart quotes in secondary strings.
  (should
   (equal '("&ldquo;" "&rdquo;")
	  (let ((org-export-default-language "en"))
	    (org-test-with-parsed-data "* \"$x$\""
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :html info))
		info)))))
  ;; Smart quotes when object has multiple secondary strings.
  (should
   (equal '(" &ldquo;prefix&rdquo; " " &ldquo;suffix&rdquo;")
	  (let ((org-export-default-language "en"))
	    (org-test-with-parsed-data "[cite:; \"prefix\" @key \"suffix\";]"
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :html info))
		info)))))
  ;; Smart quotes in document keywords.
  (should
   (equal '("&ldquo;" "&rdquo;")
	  (let ((org-export-default-language "en"))
	    (org-test-with-parsed-data "#+TITLE: \"$x$\""
	      (org-element-map (plist-get info :title) 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :html info))
		info)))))
  ;; Smart quotes in parsed affiliated keywords.
  (should
   (equal '("&ldquo;" "&rdquo;" "Paragraph")
	  (let ((org-export-default-language "en"))
	    (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :html info))
		info nil nil t)))))
  ;; Smart quotes within objects.
  (should
   (equal '("&ldquo;foo&rdquo;")
	  (let ((org-export-default-language "en"))
	    (org-test-with-parsed-data "| \"foo\" |"
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :html info))
		info nil nil t)))))
  (should
   (equal '("&ldquo;foo&rdquo;")
	  (let ((org-export-default-language "en"))
	    (org-test-with-parsed-data "*\"foo\"*"
	      (org-element-map tree 'plain-text
		(lambda (s) (org-export-activate-smart-quotes s :html info))
		info nil nil t))))))


\f
;;; Tables

(ert-deftest test-org-export/special-column ()
  "Test if the table's special column is properly recognized."
  ;; 1. First column is special if it contains only a special marking
  ;;    characters or empty cells.
  (org-test-with-temp-text "
| ! | 1 |
|   | 2 |"
    (should
     (org-export-table-has-special-column-p
      (org-element-map
	  (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  ;; 2. If the column contains anything else, it isn't special.
  (org-test-with-temp-text "
| ! | 1 |
| b | 2 |"
    (should-not
     (org-export-table-has-special-column-p
      (org-element-map
	  (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  ;;    and "!".
  (org-test-with-temp-text "
| # | 1 |
| ^ | 2 |
| * | 3 |
| _ | 4 |
| / | 5 |
| $ | 6 |
| ! | 7 |"
    (should
     (org-export-table-has-special-column-p
      (org-element-map
	  (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  ;; 4. A first column with only empty cells isn't considered as
  ;;    special.
  (org-test-with-temp-text "
|   | 1 |
|   | 2 |"
    (should-not
     (org-export-table-has-special-column-p
      (org-element-map
	  (org-element-parse-buffer) 'table 'identity nil 'first-match)))))

(ert-deftest test-org-export/table-row-is-special-p ()
  "Test `org-export-table-row-is-special-p' specifications."
  ;; 1. A row is special if it has a special marking character in the
  ;;    special column.
  (org-test-with-parsed-data "| ! | 1 |"
    (should
     (org-export-table-row-is-special-p
      (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  ;; 2. A row is special when its first field is "/"
  (org-test-with-parsed-data "
| / | 1 |
| a | b |"
    (should
     (org-export-table-row-is-special-p
      (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  ;; 3. A row only containing alignment cookies is also considered as
  ;;    special.
  (org-test-with-parsed-data "| <5> |   | <l> | <l22> |"
    (should
     (org-export-table-row-is-special-p
      (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  ;; 4. Everything else isn't considered as special.
  (org-test-with-parsed-data "| \alpha |   | c |"
    (should-not
     (org-export-table-row-is-special-p
      (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  ;; 5. Table's rules are never considered as special rows.
  (org-test-with-parsed-data "|---+---|"
    (should-not
     (org-export-table-row-is-special-p
      (org-element-map tree 'table-row 'identity nil 'first-match) info))))

(ert-deftest test-org-export/has-header-p ()
  "Test `org-export-table-has-header-p' specifications."
  ;; With an header.
  (should
   (org-test-with-parsed-data "
| a | b |
|---+---|
| c | d |"
     (org-export-table-has-header-p
      (org-element-map tree 'table 'identity info 'first-match)
      info)))
  ;; With a multi-line header.
  (should
   (org-test-with-parsed-data "
| a | b |
| 0 | 1 |
|---+---|
| a | w |"
     (org-export-table-has-header-p
      (org-element-map tree 'table 'identity info 'first-match)
      info)))
  ;; Without an header.
  (should-not
   (org-test-with-parsed-data "
| a | b |
| c | d |"
     (org-export-table-has-header-p
      (org-element-map tree 'table 'identity info 'first-match)
      info)))
  ;; Don't get fooled with starting and ending rules.
  (should-not
   (org-test-with-parsed-data "
|---+---|
| a | b |
| c | d |
|---+---|"
     (org-export-table-has-header-p
      (org-element-map tree 'table 'identity info 'first-match)
      info))))

(ert-deftest test-org-export/table-row-group ()
  "Test `org-export-table-row-group' specifications."
  ;; A rule creates a new group.
  (should
   (equal '(1 rule 2)
	  (org-test-with-parsed-data "
| a | b |
|---+---|
| 1 | 2 |"
	    (org-element-map tree 'table-row
	      (lambda (row)
		(if (eq (org-element-property :type row) 'rule) 'rule
		  (org-export-table-row-group row info)))))))
  ;; Special rows are ignored in count.
  (should
   (equal
    '(rule 1)
    (org-test-with-parsed-data "
| / | < | > |
|---|---+---|
|   | 1 | 2 |"
      (org-element-map tree 'table-row
	(lambda (row)
	  (if (eq (org-element-property :type row) 'rule) 'rule
	    (org-export-table-row-group row info)))
	info))))
  ;; Double rules also are ignored in count.
  (should
   (equal '(1 rule rule 2)
	  (org-test-with-parsed-data "
| a | b |
|---+---|
|---+---|
| 1 | 2 |"
	    (org-element-map tree 'table-row
	      (lambda (row)
		(if (eq (org-element-property :type row) 'rule) 'rule
		  (org-export-table-row-group row info))))))))

(ert-deftest test-org-export/table-row-number ()
  "Test `org-export-table-row-number' specifications."
  ;; Standard test.  Number is 0-indexed.
  (should
   (equal '(0 1)
	  (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
	    (org-element-map tree 'table-row
	      (lambda (row) (org-export-table-row-number row info)) info))))
  ;; Number ignores separators.
  (should
   (equal '(0 1)
	  (org-test-with-parsed-data "
| a | b | c |
|---+---+---|
| d | e | f |"
	    (org-element-map tree 'table-row
	      (lambda (row) (org-export-table-row-number row info)) info))))
  ;; Number ignores special rows.
  (should
   (equal '(0 1)
	  (org-test-with-parsed-data "
| / | <   | >   |
|   | b   | c   |
|---+-----+-----|
|   | <c> | <c> |
|   | e   | f   |"
	    (org-element-map tree 'table-row
	      (lambda (row) (org-export-table-row-number row info)) info)))))

(ert-deftest test-org-export/table-cell-width ()
  "Test `org-export-table-cell-width' specifications."
  ;; Width is primarily determined by width cookies.  If no cookie is
  ;; found, cell's width is nil.
  (should
   (equal '(nil 6 7)
	  (org-test-with-parsed-data "
| / | <l> | <6> | <l7> |
|   |  a  |  b  |  c   |"
	    (mapcar (lambda (cell) (org-export-table-cell-width cell info))
		    (org-element-map tree 'table-cell 'identity info)))))
  ;; Valid width cookies must have a specific row.
  (should
   (equal '(nil nil)
	  (org-test-with-parsed-data "| <6> | cell |"
	    (mapcar (lambda (cell) (org-export-table-cell-width cell info))
		    (org-element-map tree 'table-cell 'identity)))))
  ;; Do not error on malformed tables.
  (should
   (org-test-with-parsed-data "
| a |
| b | c |"
     (mapcar (lambda (cell) (org-export-table-cell-width cell info))
	     (org-element-map tree 'table-cell 'identity info)))))

(ert-deftest test-org-export/table-cell-alignment ()
  "Test `org-export-table-cell-alignment' specifications."
  ;; 1. Alignment is primarily determined by alignment cookies.
  (should
   (equal '(left center right)
	  (let ((org-table-number-fraction 0.5)
		(org-table-number-regexp "^[0-9]+$"))
	    (org-test-with-parsed-data "| <l> | <c> | <r> |"
	      (mapcar (lambda (cell)
			(org-export-table-cell-alignment cell info))
		      (org-element-map tree 'table-cell 'identity))))))
  ;; 2. The last alignment cookie has precedence.
  (should
   (equal '(right right right)
	  (org-test-with-parsed-data "
| <l8> |
| cell |
| <r9> |"
	    (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
		    (org-element-map tree 'table-cell 'identity)))))
  ;; 3. If there's no cookie, cell's contents determine alignment.
  ;;    A column mostly made of cells containing numbers will align
  ;;    its cells to the right.
  (should
   (equal '(right right right)
	  (let ((org-table-number-fraction 0.5)
		(org-table-number-regexp "^[0-9]+$"))
	    (org-test-with-parsed-data "
| 123       |
| some text |
| 12345     |"
	      (mapcar (lambda (cell)
			(org-export-table-cell-alignment cell info))
		      (org-element-map tree 'table-cell 'identity))))))
  ;; 4. Otherwise, they will be aligned to the left.
  (should
   (equal '(left left left)
	  (org-test-with-parsed-data "
| text      |
| some text |
| \alpha    |"
	    (mapcar (lambda (cell)
		      (org-export-table-cell-alignment cell info))
		    (org-element-map tree 'table-cell 'identity info))))))

(ert-deftest test-org-export/table-cell-borders ()
  "Test `org-export-table-cell-borders' specifications."
  ;; 1. Recognize various column groups indicators.
  (org-test-with-parsed-data "| / | < | > | <> |"
    (should
     (equal
      '((right bottom top) (left bottom top) (right bottom top)
	(right left bottom top))
      (mapcar (lambda (cell)
		(org-export-table-cell-borders cell info))
	      (org-element-map tree 'table-cell 'identity)))))
  ;; 2. Accept shortcuts to define column groups.
  (org-test-with-parsed-data "| / | < | < |"
    (should
     (equal
      '((right bottom top) (right left bottom top) (left bottom top))
      (mapcar (lambda (cell)
		(org-export-table-cell-borders cell info))
	      (org-element-map tree 'table-cell 'identity)))))
  ;; 3. A valid column groups row must start with a "/".
  (org-test-with-parsed-data "
|   | < |
| a | b |"
    (should
     (equal '((top) (top) (bottom) (bottom))
	    (mapcar (lambda (cell)
		      (org-export-table-cell-borders cell info))
		    (org-element-map tree 'table-cell 'identity)))))
  ;; 4. Take table rules into consideration.
  (org-test-with-parsed-data "
| 1 |
|---|
| 2 |"
    (should
     (equal '((below top) (bottom above))
	    (mapcar (lambda (cell)
		      (org-export-table-cell-borders cell info))
		    (org-element-map tree 'table-cell 'identity)))))
  ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  ;;    (resp. `bottom' and `below') borders.  Any special row is
  ;;    ignored.
  (org-test-with-parsed-data "
|---+----|
| / |    |
|   |  1 |
|---+----|"
    (should
     (equal '((bottom below top above))
	    (last
	     (mapcar (lambda (cell)
		       (org-export-table-cell-borders cell info))
		     (org-element-map tree 'table-cell 'identity)))))))

(ert-deftest test-org-export/table-dimensions ()
  "Test `org-export-table-dimensions' specifications."
  ;; 1. Standard test.
  (org-test-with-parsed-data "
| 1 | 2 | 3 |
| 4 | 5 | 6 |"
    (should
     (equal '(2 . 3)
	    (org-export-table-dimensions
	     (org-element-map tree 'table 'identity info 'first-match) info))))
  ;; 2. Ignore horizontal rules and special columns.
  (org-test-with-parsed-data "
| / | < | > |
| 1 | 2 | 3 |
|---+---+---|
| 4 | 5 | 6 |"
    (should
     (equal '(2 . 3)
	    (org-export-table-dimensions
	     (org-element-map tree 'table 'identity info 'first-match) info)))))

(ert-deftest test-org-export/table-cell-address ()
  "Test `org-export-table-cell-address' specifications."
  ;; 1. Standard test: index is 0-based.
  (org-test-with-parsed-data "| a | b |"
    (should
     (equal '((0 . 0) (0 . 1))
	    (org-element-map tree 'table-cell
	      (lambda (cell) (org-export-table-cell-address cell info))
	      info))))
  ;; 2. Special column isn't counted, nor are special rows.
  (org-test-with-parsed-data "
| / | <> |
|   | c  |"
    (should
     (equal '(0 . 0)
	    (org-export-table-cell-address
	     (car (last (org-element-map tree 'table-cell 'identity info)))
	     info))))
  ;; 3. Tables rules do not count either.
  (org-test-with-parsed-data "
| a |
|---|
| b |
|---|
| c |"
    (should
     (equal '(2 . 0)
	    (org-export-table-cell-address
	     (car (last (org-element-map tree 'table-cell 'identity info)))
	     info))))
  ;; 4. Return nil for special cells.
  (org-test-with-parsed-data "| / | a |"
    (should-not
     (org-export-table-cell-address
      (org-element-map tree 'table-cell 'identity nil 'first-match)
      info))))

(ert-deftest test-org-export/get-table-cell-at ()
  "Test `org-export-get-table-cell-at' specifications."
  ;; 1. Address ignores special columns, special rows and rules.
  (org-test-with-parsed-data "
| / | <> |
|   | a  |
|---+----|
|   | b  |"
    (should
     (equal '("b")
	    (org-element-contents
	     (org-export-get-table-cell-at
	      '(1 . 0)
	      (org-element-map tree 'table 'identity info 'first-match)
	      info)))))
  ;; 2. Return value for a non-existent address is nil.
  (org-test-with-parsed-data "| a |"
    (should-not
     (org-export-get-table-cell-at
      '(2 . 2)
      (org-element-map tree 'table 'identity info 'first-match)
      info)))
  (org-test-with-parsed-data "| / |"
    (should-not
     (org-export-get-table-cell-at
      '(0 . 0)
      (org-element-map tree 'table 'identity info 'first-match)
      info))))

(ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  "Test `org-export-table-cell-starts-colgroup-p' specifications."
  ;; 1. A cell at a beginning of a row always starts a column group.
  (org-test-with-parsed-data "| a |"
    (should
     (org-export-table-cell-starts-colgroup-p
      (org-element-map tree 'table-cell 'identity info 'first-match)
      info)))
  ;; 2. Special column should be ignored when determining the
  ;;    beginning of the row.
  (org-test-with-parsed-data "
| / |   |
|   | a |"
    (should
     (org-export-table-cell-starts-colgroup-p
      (org-element-map tree 'table-cell 'identity info 'first-match)
      info)))
  ;; 2. Explicit column groups.
  (org-test-with-parsed-data "
| / |   | < |
| a | b | c |"
    (should
     (equal
      '(yes no yes)
      (org-element-map tree 'table-cell
	(lambda (cell)
	  (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
	info)))))

(ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  "Test `org-export-table-cell-ends-colgroup-p' specifications."
  ;; 1. A cell at the end of a row always ends a column group.
  (org-test-with-parsed-data "| a |"
    (should
     (org-export-table-cell-ends-colgroup-p
      (org-element-map tree 'table-cell 'identity info 'first-match)
      info)))
  ;; 2. Special column should be ignored when determining the
  ;;    beginning of the row.
  (org-test-with-parsed-data "
| / |   |
|   | a |"
    (should
     (org-export-table-cell-ends-colgroup-p
      (org-element-map tree 'table-cell 'identity info 'first-match)
      info)))
  ;; 3. Explicit column groups.
  (org-test-with-parsed-data "
| / | < |   |
| a | b | c |"
    (should
     (equal
      '(yes no yes)
      (org-element-map tree 'table-cell
	(lambda (cell)
	  (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
	info)))))

(ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  "Test `org-export-table-row-starts-rowgroup-p' specifications."
  ;; 1. A row at the beginning of a table always starts a row group.
  ;;    So does a row following a table rule.
  (org-test-with-parsed-data "
| a |
|---|
| b |"
    (should
     (equal
      '(yes no yes)
      (org-element-map tree 'table-row
	(lambda (row)
	  (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
	info))))
  ;; 2. Special rows should be ignored when determining the beginning
  ;;    of the row.
  (org-test-with-parsed-data "
| / | < |
|   | a |
|---+---|
| / | < |
|   | b |"
    (should
     (equal
      '(yes no yes)
      (org-element-map tree 'table-row
	(lambda (row)
	  (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
	info)))))

(ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  "Test `org-export-table-row-ends-rowgroup-p' specifications."
  ;; 1. A row at the end of a table always ends a row group.  So does
  ;;    a row preceding a table rule.
  (org-test-with-parsed-data "
| a |
|---|
| b |"
    (should
     (equal
      '(yes no yes)
      (org-element-map tree 'table-row
	(lambda (row)
	  (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
	info))))
  ;; 2. Special rows should be ignored when determining the beginning
  ;;    of the row.
  (org-test-with-parsed-data "
|   | a |
| / | < |
|---+---|
|   | b |
| / | < |"
    (should
     (equal
      '(yes no yes)
      (org-element-map tree 'table-row
	(lambda (row)
	  (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
	info)))))

(ert-deftest test-org-export/table-row-in-header-p ()
  "Test `org-export-table-row-in-header-p' specifications."
  ;; Standard test.  Separators are always nil.
  (should
   (equal
    '(yes no no)
    (org-test-with-parsed-data "| a |\n|---|\n| b |"
      (org-element-map tree 'table-row
	(lambda (row)
	  (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  ;; Nil when there is no header.
  (should
   (equal
    '(no no)
    (org-test-with-parsed-data "| a |\n| b |"
      (org-element-map tree 'table-row
	(lambda (row)
	  (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))

(ert-deftest test-org-export/table-row-starts-header-p ()
  "Test `org-export-table-row-starts-header-p' specifications."
  ;; 1. Only the row starting the first row group starts the table
  ;;    header.
  (org-test-with-parsed-data "
| a |
| b |
|---|
| c |"
    (should
     (equal
      '(yes no no no)
      (org-element-map tree 'table-row
	(lambda (row)
	  (if (org-export-table-row-starts-header-p row info) 'yes 'no))
	info))))
  ;; 2. A row cannot start an header if there's no header in the
  ;;    table.
  (org-test-with-parsed-data "
| a |
|---|"
    (should-not
     (org-export-table-row-starts-header-p
      (org-element-map tree 'table-row 'identity info 'first-match)
      info))))

(ert-deftest test-org-export/table-row-ends-header-p ()
  "Test `org-export-table-row-ends-header-p' specifications."
  ;; 1. Only the row starting the first row group starts the table
  ;;    header.
  (org-test-with-parsed-data "
| a |
| b |
|---|
| c |"
    (should
     (equal
      '(no yes no no)
      (org-element-map tree 'table-row
	(lambda (row)
	  (if (org-export-table-row-ends-header-p row info) 'yes 'no))
	info))))
  ;; 2. A row cannot start an header if there's no header in the
  ;;    table.
  (org-test-with-parsed-data "
| a |
|---|"
    (should-not
     (org-export-table-row-ends-header-p
      (org-element-map tree 'table-row 'identity info 'first-match)
      info))))


\f
;;; Tables of Contents

(ert-deftest test-org-export/collect-headlines ()
  "Test `org-export-collect-headlines' specifications."
  ;; Standard test.
  (should
   (equal '("H1" "H2")
	  (org-test-with-parsed-data "* H1\n** H2"
	    (mapcar (lambda (h) (org-element-property :raw-value h))
		    (org-export-collect-headlines info)))))
  ;; Do not collect headlines below optional argument.
  (should
   (equal '("H1")
	  (org-test-with-parsed-data "* H1\n** H2"
	    (mapcar (lambda (h) (org-element-property :raw-value h))
		    (org-export-collect-headlines info 1)))))
  ;; Never collect headlines below maximum headline level.
  (should
   (equal '("H1")
	  (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
	    (mapcar (lambda (h) (org-element-property :raw-value h))
		    (org-export-collect-headlines info)))))
  (should
   (equal '("H1")
	  (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
	    (mapcar (lambda (h) (org-element-property :raw-value h))
		    (org-export-collect-headlines info 2)))))
  ;; Do not collect footnote section.
  (should
   (equal '("H1")
	  (let ((org-footnote-section "Footnotes"))
	    (org-test-with-parsed-data "* H1\n** Footnotes"
	      (mapcar (lambda (h) (org-element-property :raw-value h))
		      (org-export-collect-headlines info))))))
  ;; Do not collect headlines with UNNUMBERED property set to "notoc".
  ;; Headlines with another value for the property are still
  ;; collected.  UNNUMBERED property is inherited.
  (should
   (equal '("H1")
	  (org-test-with-parsed-data
	      "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
	    (mapcar (lambda (h) (org-element-property :raw-value h))
		    (org-export-collect-headlines info)))))
  (should-not
   (org-test-with-parsed-data
       "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
     (mapcar (lambda (h) (org-element-property :raw-value h))
	     (org-export-collect-headlines info))))
  (should
   (equal '("H1" "H2")
	  (org-test-with-parsed-data
	      "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
	    (mapcar (lambda (h) (org-element-property :raw-value h))
		    (org-export-collect-headlines info)))))
  ;; Collect headlines locally.
  (should
   (equal '("H2" "H3")
	  (org-test-with-parsed-data "* H1\n** H2\n** H3"
	    (let ((scope (org-element-map tree 'headline #'identity info t)))
	      (mapcar (lambda (h) (org-element-property :raw-value h))
		      (org-export-collect-headlines info nil scope))))))
  ;; Collect headlines from a scope specified by a fuzzy match
  (should
   (equal '("H3" "H4")
	  (org-test-with-parsed-data "* HA
** H1
** H2
* Target
  :PROPERTIES:
  :CUSTOM_ID: TargetSection
  :END:
** H3
** H4
* HB
** H5
"
	    (mapcar
	     (lambda (h) (org-element-property :raw-value h))
	     (org-export-collect-headlines
	      info
	      nil
	      (org-export-resolve-fuzzy-link
	       (with-temp-buffer
		 (save-excursion (insert "[[Target]]"))
		 (org-element-link-parser))
	       info))))))
  ;; Collect headlines from a scope specified by CUSTOM_ID
  (should
   (equal '("H3" "H4")
	  (org-test-with-parsed-data "* Not this section
** H1
** H2
* Target
  :PROPERTIES:
  :CUSTOM_ID: TargetSection
  :END:
** H3
** H4
* Another
** H5
"
	    (mapcar
	     (lambda (h) (org-element-property :raw-value h))
	     (org-export-collect-headlines
	      info
	      nil
	      (org-export-resolve-id-link
	       (with-temp-buffer
		 (save-excursion (insert "[[#TargetSection]]"))
		 (org-element-link-parser))
	       info))))))
  ;; When collecting locally, optional level is relative.
  (should
   (equal '("H2")
	  (org-test-with-parsed-data "* H1\n** H2\n*** H3"
	    (let ((scope (org-element-map tree 'headline #'identity info t)))
	      (mapcar (lambda (h) (org-element-property :raw-value h))
		      (org-export-collect-headlines info 1 scope)))))))

(ert-deftest test-org-export/excluded-from-toc-p ()
  "Test `org-export-excluded-from-toc-p' specifications."
  ;; By default, headlines are not excluded.
  (should-not
   (org-test-with-parsed-data "* H1"
     (org-element-map tree 'headline
       (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
  ;; Exclude according to a maximum level.
  (should
   (equal '(in out)
	  (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
	    (org-element-map tree 'headline
	      (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
	      info))))
  ;; Exclude according to UNNUMBERED property.
  (should
   (org-test-with-parsed-data "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
     (org-element-map tree 'headline
       (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
  ;; UNNUMBERED property is inherited, so is "notoc" value.
  (should
   (equal '(out out)
	  (org-test-with-parsed-data
	      "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
	    (org-element-map tree 'headline
	      (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
	      info)))))

(ert-deftest test-org-export/toc-entry-backend ()
  "Test `org-export-toc-entry-backend' specifications."
  ;; Ignore targets.
  (should
   (equal "H \n"
	  (org-test-with-temp-text "* H <<target>>"
	    (let (org-export-registered-backends)
	      (org-export-define-backend 'test
		'((headline . (lambda (h _c i) (org-export-data-with-backend
					   (org-element-property :title h)
					   (org-export-toc-entry-backend 'test)
					   i)))))
	      (org-export-as 'test)))))
  ;; Ignore footnote references.
  (should
   (equal "H \n"
	  (org-test-with-temp-text "[fn:1] Definition\n* H [fn:1]"
	    (let (org-export-registered-backends)
	      (org-export-define-backend 'test
		'((headline . (lambda (h _c i) (org-export-data-with-backend
					   (org-element-property :title h)
					   (org-export-toc-entry-backend 'test)
					   i)))))
	      (org-export-as 'test)))))
  ;; Replace plain links with contents, or with path.
  (should
   (equal "H Org mode\n"
	  (org-test-with-temp-text "* H [[https://orgmode.org][Org mode]]"
	    (let (org-export-registered-backends)
	      (org-export-define-backend 'test
		'((headline . (lambda (h _c i) (org-export-data-with-backend
					   (org-element-property :title h)
					   (org-export-toc-entry-backend 'test)
					   i)))))
	      (org-export-as 'test)))))
  (should
   (equal "H https://orgmode.org\n"
	  (org-test-with-temp-text "* H [[https://orgmode.org]]"
	    (let (org-export-registered-backends)
	      (org-export-define-backend 'test
		'((headline . (lambda (h _c i) (org-export-data-with-backend
					   (org-element-property :title h)
					   (org-export-toc-entry-backend 'test)
					   i)))))
	      (org-export-as 'test)))))
  ;; Replace radio targets with contents.
  (should
   (equal "H radio\n"
	  (org-test-with-temp-text "* H <<<radio>>>"
	    (let (org-export-registered-backends)
	      (org-export-define-backend 'test
		'((headline . (lambda (h _c i) (org-export-data-with-backend
					   (org-element-property :title h)
					   (org-export-toc-entry-backend 'test)
					   i)))))
	      (org-export-as 'test)))))
  ;; With optional argument TRANSCODERS, specify other
  ;; transformations.
  (should
   (equal "H bold\n"
	  (org-test-with-temp-text "* H *bold*"
	    (let (org-export-registered-backends)
	      (org-export-define-backend 'test
		'((headline . (lambda (h _c i) (org-export-data-with-backend
						(org-element-property :title h)
						(org-export-toc-entry-backend 'test
						  '(bold . (lambda (_b c _i) c)))
						i)))))
	      (org-export-as 'test))))))


\f
;;; Templates

(ert-deftest test-org-export/inner-template ()
  "Test `inner-template' translator specifications."
  (should
   (equal "Success!"
	  (org-test-with-temp-text "* Headline"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders
	      '((inner-template . (lambda (contents info) "Success!"))
		(headline . (lambda (h c i) "Headline"))))))))
  ;; Inner template is applied even in a "body-only" export.
  (should
   (equal "Success!"
	  (org-test-with-temp-text "* Headline"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((inner-template . (lambda (c i) "Success!"))
			     (headline . (lambda (h c i) "Headline"))))
	     nil nil 'body-only)))))

(ert-deftest test-org-export/template ()
  "Test `template' translator specifications."
  (should
   (equal "Success!"
	  (org-test-with-temp-text "* Headline"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((template . (lambda (contents info) "Success!"))
			     (headline . (lambda (h c i) "Headline"))))))))
  ;; Template is not applied in a "body-only" export.
  (should-not
   (equal "Success!"
	  (org-test-with-temp-text "* Headline"
	    (org-export-as
	     (org-export-create-backend
	      :transcoders '((template . (lambda (contents info) "Success!"))
			     (headline . (lambda (h c i) "Headline"))))
	     nil nil 'body-only)))))


\f
;;; Topology

(ert-deftest test-org-export/get-next-element ()
  "Test `org-export-get-next-element' specifications."
  ;; Standard test.
  (should
   (equal "b"
	  (org-test-with-parsed-data "* Headline\n*a* b"
	    (org-export-get-next-element
	     (org-element-map tree 'bold 'identity info t) info))))
  ;; Return nil when no previous element.
  (should-not
   (org-test-with-parsed-data "* Headline\na *b*"
     (org-export-get-next-element
      (org-element-map tree 'bold 'identity info t) info)))
  ;; Non-exportable elements are ignored.
  (should-not
   (let ((org-export-with-timestamps nil))
     (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
       (org-export-get-next-element
	(org-element-map tree 'entity 'identity info t) info))))
  ;; Find next element in secondary strings.
  (should
   (eq 'verbatim
       (org-test-with-parsed-data "* a =verb="
	 (org-element-type
	  (org-export-get-next-element
	   (org-element-map tree 'plain-text 'identity info t) info)))))
  (should
   (eq 'verbatim
       (org-test-with-parsed-data "* /italic/ =verb="
	 (org-element-type
	  (org-export-get-next-element
	   (org-element-map tree 'italic 'identity info t) info)))))
  ;; Find next element in document keywords.
  (should
   (eq 'verbatim
       (org-test-with-parsed-data "#+TITLE: a =verb="
	 (org-element-type
	  (org-export-get-next-element
	   (org-element-map
	       (plist-get info :title) 'plain-text 'identity info t)
	   info)))))
  ;; Find next element in parsed affiliated keywords.
  (should
   (eq 'verbatim
       (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
	 (org-element-type
	  (org-export-get-next-element
	   (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  ;; With optional argument N, return a list containing all the
  ;; following elements.
  (should
   (equal
    '(bold code underline)
    (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
      (mapcar #'car
	      (org-export-get-next-element
	       (org-element-map tree 'italic 'identity info t) info t)))))
  ;; When N is a positive integer, return a list containing up to
  ;; N following elements.
  (should
   (equal
    '(bold code)
    (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
      (mapcar #'car
	      (org-export-get-next-element
	       (org-element-map tree 'italic 'identity info t) info 2))))))

(ert-deftest test-org-export/get-previous-element ()
  "Test `org-export-get-previous-element' specifications."
  ;; Standard test.
  (should
   (equal "a "
	  (org-test-with-parsed-data "* Headline\na *b*"
	    (org-export-get-previous-element
	     (org-element-map tree 'bold 'identity info t) info))))
  ;; Return nil when no previous element.
  (should-not
   (org-test-with-parsed-data "* Headline\n*a* b"
     (org-export-get-previous-element
      (org-element-map tree 'bold 'identity info t) info)))
  ;; Non-exportable elements are ignored.
  (should-not
   (let ((org-export-with-timestamps nil))
     (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
       (org-export-get-previous-element
	(org-element-map tree 'entity 'identity info t) info))))
  ;; Find previous element in secondary strings.
  (should
   (eq 'verbatim
       (org-test-with-parsed-data "* =verb= a"
	 (org-element-type
	  (org-export-get-previous-element
	   (org-element-map tree 'plain-text 'identity info t) info)))))
  (should
   (eq 'verbatim
       (org-test-with-parsed-data "* =verb= /italic/"
	 (org-element-type
	  (org-export-get-previous-element
	   (org-element-map tree 'italic 'identity info t) info)))))
  ;; Find previous element in document keywords.
  (should
   (eq 'verbatim
       (org-test-with-parsed-data "#+TITLE: =verb= a"
	 (org-element-type
	  (org-export-get-previous-element
	   (org-element-map
	       (plist-get info :title) 'plain-text 'identity info t)
	   info)))))
  ;; Find previous element in parsed affiliated keywords.
  (should
   (eq 'verbatim
       (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
	 (org-element-type
	  (org-export-get-previous-element
	   (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  ;; With optional argument N, return a list containing up to
  ;; N previous elements.
  (should
   (equal '(underline italic bold)
	  (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
	    (mapcar #'car
		    (org-export-get-previous-element
		     (org-element-map tree 'code 'identity info t) info t)))))
  ;; When N is a positive integer, return a list containing up to
  ;; N previous elements.
  (should
   (equal '(italic bold)
	  (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
	    (mapcar #'car
		    (org-export-get-previous-element
		     (org-element-map tree 'code 'identity info t) info 2))))))


(provide 'test-ox)
;;; test-org-export.el end here

debug log:

solving 01e082c9b ...
found 01e082c9b in https://git.savannah.gnu.org/cgit/emacs/org-mode.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).