emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 8b2cf16429e6b6ded644580db9e5cacb8c332825 198882 bytes (raw)
name: testing/lisp/test-org-element.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
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
 
;;; test-org-element.el --- Tests for org-element.el  -*- lexical-binding: t; -*-

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

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

;; 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:

(eval-and-compile (require 'cl-lib))

(require 'org-element)
(require 'org)
(require 'org-inlinetask)

(defun org-test-parse-and-interpret (text)
  "Parse TEXT as Org syntax and interpret it.
Return interpreted string."
  (with-temp-buffer
    (org-mode)
    (insert text)
    (org-element-interpret-data (org-element-parse-buffer))))

\f
;;; Test getters.

(ert-deftest test-org-element/type ()
  "Test `org-element-type' specifications."
  (should (eq 'plain-text (org-element-type "string")))
  (should-not (org-element-type nil))
  (should-not (org-element-type 1))
  (should (eq 'dummy (org-element-type '(dummy))))
  (should (eq 'dummy (org-element-type '(dummy nil 'foo))))
  (should (eq 'dummy (org-element-type '(dummy (:a a :b b) 'foo))))
  ;; anonymous node.
  (should-not (org-element-type '((dummy))))
  (should (eq 'anonymous (org-element-type '((dummy)) t)))
  (should (eq 'anonymous (org-element-type '("string") t)))
  (should-not (org-element-type '(1 2) t)))

(ert-deftest test-org-element/type-p ()
  "Test `org-element-type-p' specifications."
  (should (org-element-type-p '(foo) 'foo))
  (should (org-element-type-p '(foo) '(foo)))
  (should (org-element-type-p '(foo) '(foo bar)))
  (should-not (org-element-type-p '(foo) 'bar))
  (should-not (org-element-type-p '(foo) '(bar baz)))
  (should (org-element-type-p "string" 'plain-text))
  (should (org-element-type-p '((foo)) 'anonymous)))

(ert-deftest test-org-element/org-element-property-raw ()
  "Test `org-element-property-raw' specifications."
  ;; No properties.
  (dolist (element `( nil
                      (headline nil)
                      (headline nil (headline))
                      "string"))
    (should-not (org-element-property-raw :begin element))
    (should (eq 'default (org-element-property-raw :begin element 'default)))
    (should-not (org-element-property-raw :begin1 element))
    (should (eq 'default (org-element-property-raw :begin1 element 'default)))
    (dolist (prop '(:begin))
      (should-not (org-element-property-raw prop element))
      (should (eq 'default (org-element-property-raw prop element 'default))))
    (dolist (prop '(:begin1))
      (should-not (org-element-property-raw prop element))
      (should (eq 'default (org-element-property-raw prop element 'default)))))
  ;; Only non-standard properties.
  (dolist (element `((headline (:begin1 1))
                     (headline (:begin1 1) (headline))
                     ,(propertize "string" :begin1 1)))
    (should-not (org-element-property-raw :begin element))
    (should (eq 'default (org-element-property-raw :begin element 'default)))
    (should (= 1 (org-element-property-raw :begin1 element)))
    (should (= 1 (org-element-property-raw :begin1 element 'default)))
    (dolist (prop '(:begin))
      (should-not (org-element-property-raw prop element))
      (should (eq 'default (org-element-property-raw prop element 'default))))
    (dolist (prop '(:begin1))
      (should (= 1 (org-element-property-raw prop element)))
      (should (= 1 (org-element-property-raw prop element 'default)))))
  ;; Only standard properties.
  (dolist (element `((headline (:standard-properties ,(make-vector 10 'test)))
                     (headline (:standard-properties ,(make-vector 10 'test)) (headline))))
    (should (eq 'test (org-element-property-raw :begin element)))
    (should (eq 'test (org-element-property-raw :begin element 'default)))
    (should-not (org-element-property-raw :begin1 element))
    (should (eq 'default (org-element-property-raw :begin1 element 'default)))
    (dolist (prop '(:begin))
      (should (eq 'test (org-element-property-raw prop element)))
      (should (eq 'test (org-element-property-raw prop element 'default))))
    (dolist (prop '(:begin1))
      (should-not (org-element-property-raw prop element))
      (should (eq 'default (org-element-property-raw prop element 'default)))))
  ;; Standard properties in the plist.
  (dolist (element `((headline (:begin 1))
                     (headline (:begin 1) (headline))
                     ,(propertize "string" :begin 1)))
    (should (= 1 (org-element-property-raw :begin element)))
    (should (= 1 (org-element-property-raw :begin element 'default)))
    (should-not (org-element-property-raw :begin1 element))
    (should (eq 'default (org-element-property-raw :begin1 element 'default)))
    (dolist (prop '(:begin))
      (should (= 1 (org-element-property-raw prop element)))
      (should (= 1 (org-element-property-raw prop element 'default))))
    (dolist (prop '(:begin1))
      (should-not (org-element-property-raw prop element))
      (should (eq 'default (org-element-property-raw prop element 'default)))))
  ;; Standard properties mixed in the plist and standard array.
  (dolist (element `((headline (:standard-properties ,(make-vector 10 'test) :begin 1))
                     (headline (:begin 1 :standard-properties ,(make-vector 10 'test)))
                     (headline (:standard-properties ,(make-vector 10 'test) :begin 1) (headline))))
    (should (eq 'test (org-element-property-raw :begin element)))
    (should (eq 'test (org-element-property-raw :begin element 'default)))
    (should-not (org-element-property-raw :begin1 element))
    (should (eq 'default (org-element-property-raw :begin1 element 'default)))
    (dolist (prop '(:begin))
      (should (eq 'test (org-element-property-raw prop element)))
      (should (eq 'test (org-element-property-raw prop element 'default))))
    (dolist (prop '(:begin1))
      (should-not (org-element-property-raw prop element))
      (should (eq 'default (org-element-property-raw prop element 'default)))))
  ;; General case.
  (dolist (element `((headline (:standard-properties ,(make-vector 10 'test) :begin1 1))
                     (headline (:begin1 1 :standard-properties ,(make-vector 10 'test)))
                     (headline (:standard-properties ,(make-vector 10 'test) :begin1 1) (headline))))
    (should (eq 'test (org-element-property-raw :begin element)))
    (should (eq 'test (org-element-property-raw :begin element 'default)))
    (should (= 1 (org-element-property-raw :begin1 element)))
    (should (= 1 (org-element-property-raw :begin1 element 'default)))
    (dolist (prop '(:begin))
      (should (eq 'test (org-element-property-raw prop element)))
      (should (eq 'test (org-element-property-raw prop element 'default))))
    (dolist (prop '(:begin1))
      (should (= 1 (org-element-property-raw prop element)))
      (should (= 1 (org-element-property-raw prop element 'default))))))

(ert-deftest test-org-element/property ()
  "Test resolving deferred properties."
  ;; Resolve `:deferred' property.
  (let ((el (org-element-create
             'dummy
             `(:deferred
               ,(org-element-deferred-create
                 t (lambda (el) (org-element-put-property el :foo 'bar) nil))))))
    (should (eq 'bar (org-element-property :foo el)))
    (should-not (org-element-property :foo2 el)))
  ;; Deferred value.
  (let ((el (org-element-create
             'dummy
             `(:foo
               ,(org-element-deferred-create
                 nil (lambda (_) 'bar))))))
    (should (eq 'bar (org-element-property :foo el))))
  ;; Auto-undefer.
  (let ((el (org-element-create
             'dummy
             `(:foo
               ,(org-element-deferred-create
                 t (lambda (_) 'bar))))))
    (should (eq 'bar (org-element-property :foo el)))
    (should (eq 'bar (org-element-property-raw :foo el))))
  ;; Force undefer.
  (let ((el (org-element-create
             'dummy
             `(:foo
               ,(org-element-deferred-create
                 nil (lambda (_) 'bar))))))
    (should (eq 'bar (org-element-property :foo el)))
    (should-not (eq 'bar (org-element-property-raw :foo el)))
    (should (eq 'bar (org-element-property :foo el nil 'force)))
    (should (eq 'bar (org-element-property-raw :foo el))))
  ;; Test deferred alias.
  (let ((el (org-element-create
             'dummy
             `( :foo 1
                :bar
                ,(org-element-deferred-create-alias :foo)))))
    (should (equal 1 (org-element-property :foo el)))
    (should (equal 1 (org-element-property :bar el))))
  ;; Test deferred list.
  (let ((el (org-element-create
             'dummy
             `(:foo
               ,(org-element-deferred-create-list
                 (list 1 2 (org-element-deferred-create nil (lambda (_) 3))))))))
    (should (equal '(1 2 3) (org-element-property :foo el))))
  ;; Test deferred property with side effects.
  (let ((el (org-element-create
             'dummy
             `(:foo
               ,(org-element-deferred-create
                 nil (lambda (el)
                     (org-element-put-property el :foo 1)
                     (throw :org-element-deferred-retry nil)))))))
    (should (eq 1 (org-element-property :foo el))))
  ;; Test recursive undefer.
  (let ((el (org-element-create
             'dummy
             `(:foo
               ,(org-element-deferred-create
                 nil (lambda (el)
                     (org-element-deferred-create
                      nil (lambda (_) 1))))))))
    (should (eq 1 (org-element-property :foo el)))))

(ert-deftest test-org-element/property-2 ()
  "Test `org-element-property-2' specifications."
  (let ((el (org-element-create 'dummy '(:foo bar))))
    (should (eq (org-element-property :foo el)
                (org-element-property-2 el :foo)))))

(ert-deftest test-org-element/parent ()
  "Test `org-element-parent' specifications."
  (let ((el (org-element-create 'dummy '(:parent bar))))
    (should (eq (org-element-property :parent el)
                (org-element-parent el)))))

(ert-deftest test-org-element/properties-resolve ()
  "Test `org-element-properties-resolve' specifications."
  (let ((el (org-element-create
             'dummy
             `( :foo ,(org-element-deferred-create t (lambda (_) 1))
                :bar ,(org-element-deferred-create nil (lambda (_) 2))
                :deferred
                ,(org-element-deferred-create
                  nil (lambda (el)
                      (org-element-put-property el :baz 3)))))))
    ;; Resolve conditionally.
    (setq el (org-element-properties-resolve el))
    (should (eq 1 (org-element-property-raw :foo el)))
    (should-not (eq 2 (org-element-property-raw :bar el)))
    (should (eq 2 (org-element-property :bar el)))
    (should (eq 3 (org-element-property-raw :baz el)))
    ;; Resolve unconditionally.
    (setq el (org-element-properties-resolve el 'force))
    (should (eq 2 (org-element-property-raw :bar el)))))

(ert-deftest test-org-element/secondary-p ()
  "Test `org-element-secondary-p' specifications."
  ;; In a secondary string, return property name.
  (should
   (eq :title
       (org-test-with-temp-text "* Headline *object*"
	 (org-element-map (org-element-parse-buffer) 'bold
	   (lambda (object) (org-element-secondary-p object))
	   nil t))))
  (should
   (eq :foo
       (org-element-secondary-p
        (let* ((el (org-element-create
                    'dummy '(:secondary (:foo))))
               (child (org-element-create "string" `(:parent ,el))))
          (org-element-put-property
           el :foo (list child))
          child))))
  ;; Outside a secondary string, return nil.
  (should-not
   (org-test-with-temp-text "Paragraph *object*"
     (org-element-map (org-element-parse-buffer) 'bold
       (lambda (object) (org-element-type (org-element-secondary-p object)))
       nil t)))
  (should-not
   (eq :foo
       (org-element-secondary-p
        (let* ((el (org-element-create
                    'dummy '(:secondary (:foo))))
               (child (org-element-create "string" `(:parent ,el))))
          (org-element-put-property
           el :bar (list child))
          child)))))

(ert-deftest test-org-element/class ()
  "Test `org-element-class' specifications."
  ;; Regular tests.
  (should (eq 'element (org-element-class '(paragraph nil) nil)))
  (should (eq 'object (org-element-class '(target nil) nil)))
  ;; Special types.
  (should (eq 'element (org-element-class '(org-data nil) nil)))
  (should (eq 'object (org-element-class "text" nil)))
  (should (eq 'object (org-element-class '("secondary " "string") nil)))
  ;; Pseudo elements.
  (should (eq 'element (org-element-class '(foo nil) nil)))
  (should (eq 'element (org-element-class '(foo nil) '(center-block nil))))
  (should (eq 'element (org-element-class '(foo nil) '(org-data nil))))
  ;; Pseudo objects.
  (should (eq 'object (org-element-class '(foo nil) '(bold nil))))
  (should (eq 'object (org-element-class '(foo nil) '(paragraph nil))))
  (should (eq 'object (org-element-class '(foo nil) '("secondary"))))
  (should
   (eq 'object
       (let* ((datum '(foo nil))
	      (headline `(headline (:title (,datum) :secondary (:title)))))
	 (org-element-put-property datum :parent headline)
	 (org-element-class datum)))))

\f
;;; Test `org-element-map' and `org-element-properties-map'

(ert-deftest test-org-element/map ()
  "Test `org-element-map'."
  ;; Can map to `plain-text' objects.
  (should
   (= 2
      (org-test-with-temp-text "Some text \alpha
#+BEGIN_CENTER
Some other text
#+END_CENTER"
	(let ((count 0))
	  (org-element-map
	      (org-element-parse-buffer) 'plain-text
	    (lambda (s) (when (string-match "text" s) (cl-incf count))))
	  count))))
  ;; Applies to secondary strings
  (should
   (org-element-map '("some " (bold nil "bold") "text") 'bold 'identity))
  ;; Enter secondary strings before entering contents.
  (should
   (equal
    "alpha"
    (org-element-property
     :name
     (org-test-with-temp-text "* Some \\alpha headline\n\\beta entity."
       (org-element-map (org-element-parse-buffer) 'entity 'identity nil t)))))
  ;; Apply NO-RECURSION argument.
  (should-not
   (org-test-with-temp-text "#+BEGIN_CENTER\n\\alpha\n#+END_CENTER"
     (org-element-map
         (org-element-parse-buffer) 'entity 'identity nil nil 'center-block)))
  ;; Use WITH-AFFILIATED argument.
  (should
   (equal
    '("1" "a" "2" "b")
    (org-test-with-temp-text "#+CAPTION[a]: 1\n#+CAPTION[b]: 2\nParagraph"
      (org-element-map
          (org-element-at-point) 'plain-text 'identity nil nil nil t)))))

(ert-deftest test-org-element/ast-map ()
  "Test `org-element-ast-map' specifications."
  ;; TYPES = t
  (should
   (equal
    '(plain-text plain-text bold)
    (org-element-ast-map
        (org-element-create 'anonymous nil "a" "b" (org-element-create 'bold))
        t #'org-element-type)))
  ;; IGNORE
  (should
   (equal
    '(plain-text plain-text)
    (let ((bold (org-element-create 'bold)))
      (org-element-ast-map
          (org-element-create 'anonymous nil "a" "b" bold)
          t #'org-element-type (list bold)))))
  ;; FUN as a list form
  (org-test-with-temp-text "* H1\n* H2"
    (should
     (equal
      '("H1" "H2")
      (org-element-map
          (org-element-parse-buffer)
          t '(org-element-property :raw-value node)))))
  ;; Extra secondary properties.
  (should
   (equal
    '(bold bold)
    (org-element-ast-map
        (org-element-create
         'dummy
         `(:foo ,(org-element-create 'bold))
         (org-element-create 'bold))
        'bold #'org-element-type
        nil nil nil '(:foo))))
  (should-not
   (equal
    '(bold bold)
    (org-element-ast-map
        (org-element-create
         'dummy
         `(:foo ,(org-element-create 'bold))
         (org-element-create 'bold))
        'bold #'org-element-type)))
  ;; No secondary.
  (should-not
   (equal
    '(bold bold)
    (org-element-ast-map
        (org-element-create
         'dummy
         `(:secondary (:foo) :foo ,(org-element-create 'bold))
         (org-element-create 'bold))
        'bold #'org-element-type
        nil nil nil nil 'no-secondary)))
  (should
   (equal
    '(bold bold)
    (org-element-ast-map
        (org-element-create
         'dummy
         `(:secondary (:foo) :foo ,(org-element-create 'bold))
         (org-element-create 'bold))
        'bold #'org-element-type)))
  ;; Deferred values.
  (should
   (equal
    '(dummy bold)
    (org-element-ast-map
        (org-element-create
         'dummy
         `(:secondary (:foo) :foo ,(org-element-deferred-create nil (lambda (_) "a")))
         (org-element-create 'bold))
        t #'org-element-type
        nil nil nil nil nil 'no-undefer)))
  (should
   (equal
    '(dummy plain-text bold)
    (org-element-ast-map
        (org-element-create
         'dummy
         `(:secondary (:foo) :foo ,(org-element-deferred-create nil (lambda (_) "a")))
         (org-element-create 'bold))
        t #'org-element-type))))

(ert-deftest test-org-element/properties-mapc ()
  "Test `org-element-properties-mapc' specifications."
  (let ((el (org-element-create
             'dummy
             `( :foo ,(org-element-deferred-create t (lambda (_) 1))
                :bar 2))))
    (should
     (catch :found
       (org-element-properties-mapc
        (lambda (_ val _)
          (when (org-element-deferred-p val)
            (throw :found t)))
        el)))
    (should
     (catch :found
       (org-element-properties-mapc
        (lambda (prop val _)
          (when (and (eq prop :foo) (eq 1 val))
            (throw :found t)))
        el 'undefer)))))

(ert-deftest test-org-element/properties-map ()
  "Test `org-element-properties-map' specifications."
  ;; Check resolving deferred properties.
  (let ((el (org-element-create
             'dummy
             `( :foo ,(org-element-deferred-create t (lambda (_) 1))
                :bar 2))))
    (should
     (equal '(2)
            (cdr
             (org-element-properties-map
              (lambda (_ val _) val) el))))
    (should-not
     (equal '(1 2)
            (org-element-properties-map
             (lambda (_ val _) val) el)))
    (should
     (equal '(1 2)
            (org-element-properties-map
             (lambda (_ val _) val) el 'undefer))))
  ;; Check functions with different arity.
  (let ((el (org-element-create 'dummy '(:foo 1 :bar 2 :baz 3))))
    (should
     ;; Single argument.
     (equal '(1 2 3)
            (org-element-properties-map #'identity el)))
    ;; Two arguments.
    (should
     (equal '(1 2 nil)
            (org-element-properties-map
             (lambda (prop val) (unless (eq prop :baz) val)) el)))
    ;; Three arguments.
    (should
     (equal '(1 2 4)
            (org-element-properties-map
             (lambda (prop val node)
               (if (eq prop :baz)
                   (1+ (org-element-property-raw :baz node))
                 val))
             el)))))

\f
;;; Test Setters

(ert-deftest test-org-element/org-element-create ()
  "Test `org-element-create' specifications."
  (should
   (pcase (org-element-create 'foo '(:a 1 :b 2))
     (`(foo (:standard-properties ,_ :a 1 :b 2)) t)))
  (should
   (pcase (org-element-create 'foo '(:begin 10))
     (`(foo (:standard-properties ,vec))
      (= 10 (aref vec (org-element--property-idx :begin))))))
  ;; Strings
  (should (equal "foo" (org-element-create "foo")))
  (should (equal "foo" (org-element-create 'plain-text nil "foo")))
  (should (get-text-property 0 :a (org-element-create 'plain-text '(:a 1) "foo")))
  (should (get-text-property 0 :begin (org-element-create 'plain-text '(:begin 1) "foo")))
  ;; Children
  (let ((children '("a" "b" (org-element-create 'foo))))
    (should (equal (cddr (apply #'org-element-create 'bar nil children))
                   children))))

(ert-deftest test-org-element/put-property ()
  "Test `org-element-put-property' specifications."
  ;; Standard test.
  (org-test-with-temp-text "* Headline\n *a*"
    (let ((tree (org-element-parse-buffer)))
      (org-element-put-property
       (org-element-map tree 'bold 'identity nil t) :test 1)
      (should (org-element-property
	       :test (org-element-map tree 'bold 'identity nil t)))))
  ;; Put property on a string.
  (should
   (org-element-property :test (org-element-put-property "Paragraph" :test t)))
  ;; No properties.
  (let ((element (list 'heading nil))
        vec)
    (setq vec (make-vector (length org-element--standard-properties) nil))
    (aset vec 0 1)
    (should
     (equal
      (list 'heading (list :standard-properties vec))
      (org-element-put-property element :begin 1))))
  (let ((element (list 'heading nil)))
    (should
     (equal
      (list 'heading (list :begin1 1))
      (org-element-put-property element :begin1 1))))
  ;; Standard properties.
  (let ((element (list 'heading (list :standard-properties (make-vector (length org-element--standard-properties) 'foo)))))
    (should
     (= 1
        (org-element-property-raw :begin (org-element-put-property element :begin 1)))))
  ;; Adding standard properties when other standard properties are defined manually in the plist.
  (let ((element (list 'heading (list :begin 1 :end 20 :foo 'foo))))
    (should
     (= 2
        (org-element-property-raw :begin (org-element-put-property element :begin 2))))
    ;; Check setter.
    (cl-incf (org-element-property-raw :begin element))
    (should
     (= 3 (org-element-property-raw :begin element)))
    (should
     (= 20
        (org-element-property-raw :end element)))
    (should
     (eq 'foo
         (org-element-property-raw :foo element)))))

(ert-deftest test-org-element/put-property-2 ()
  "Test `org-element-put-property-2' specifications."
  (should
   (equal (org-element-put-property (org-element-create 'foo) :test 'value)
          (org-element-put-property-2 :test 'value  (org-element-create 'foo)))))

(ert-deftest test-org-element/set-contents ()
  "Test `org-element-set-contents' specifications."
  ;; Accept multiple entries.
  (should
   (equal '("b" (italic nil "a"))
	  (org-test-with-temp-text "* Headline\n *a*"
	    (let ((tree (org-element-parse-buffer)))
	      (org-element-set-contents
	       (org-element-map tree 'bold 'identity nil t) "b" '(italic nil "a"))
	      (org-element-contents
	       (org-element-map tree 'bold 'identity nil t))))))
  ;; Accept atoms and elements.
  (should
   (equal '("b")
	  (org-test-with-temp-text "* Headline\n *a*"
	    (let ((tree (org-element-parse-buffer)))
	      (org-element-set-contents
	       (org-element-map tree 'bold 'identity nil t) "b")
	      (org-element-contents
	       (org-element-map tree 'bold 'identity nil t))))))
  (should
   (equal '((italic nil "b"))
	  (org-test-with-temp-text "* Headline\n *a*"
	    (let ((tree (org-element-parse-buffer)))
	      (org-element-set-contents
	       (org-element-map tree 'bold 'identity nil t) '(italic nil "b"))
	      (org-element-contents
	       (org-element-map tree 'bold 'identity nil t))))))
  ;; Allow nil contents.
  (should-not
   (org-test-with-temp-text "* Headline\n *a*"
     (let ((tree (org-element-parse-buffer)))
       (org-element-set-contents (org-element-map tree 'bold 'identity nil t))
       (org-element-contents (org-element-map tree 'bold 'identity nil t)))))
  ;; Set contents of anonymous elements.
  (should
   (equal '#1=((b (:parent #1#)))
          (let ((element '#1=((a (:parent #1#)) (b (:parent #1#)))))
            (org-element-set-contents element `(b (:parent ,element)))
            element))))

(ert-deftest test-org-element/adopt-elements ()
  "Test `org-element-adopt' specifications."
  ;; Adopt an element.
  (should
   (equal '(plain-text italic)
	  (org-test-with-temp-text "* Headline\n *a*"
	    (let ((tree (org-element-parse-buffer)))
	      (org-element-adopt
	          (org-element-map tree 'bold 'identity nil t) '(italic nil "a"))
	      (mapcar (lambda (blob) (org-element-type blob))
		      (org-element-contents
		       (org-element-map tree 'bold 'identity nil t)))))))
  ;; Adopt a string.
  (should
   (equal '("a" "b")
	  (org-test-with-temp-text "* Headline\n *a*"
	    (let ((tree (org-element-parse-buffer)))
	      (org-element-adopt
	          (org-element-map tree 'bold 'identity nil t) "b")
	      (org-element-contents
	       (org-element-map tree 'bold 'identity nil t)))))))

(ert-deftest test-org-element/extract-element ()
  "Test `org-element-extract' specifications."
  ;; Extract a greater element.
  (should
   (eq 'org-data
       (org-test-with-temp-text "* Headline"
	 (let* ((tree (org-element-parse-buffer))
		(element (org-element-map tree 'headline 'identity nil t)))
	   (org-element-extract element)
	   (org-element-type tree)))))
  ;; Extract an element.
  (should-not
   (org-element-map
       (org-test-with-temp-text "Paragraph"
	 (let* ((tree (org-element-parse-buffer))
		(element (org-element-map tree 'paragraph 'identity nil t)))
	   (org-element-extract element)
	   tree))
       'paragraph
     'identity))
  ;; Extract an object, even in a secondary string.
  (should-not
   (org-element-map
       (org-test-with-temp-text "*bold*"
	 (let* ((tree (org-element-parse-buffer))
		(element (org-element-map tree 'bold 'identity nil t)))
	   (org-element-extract element)
	   tree))
       'bold
     'identity))
  (should-not
   (org-element-map
       (org-test-with-temp-text "* Headline *bold*"
	 (let* ((tree (org-element-parse-buffer))
		(element (org-element-map tree 'bold 'identity nil t)))
	   (org-element-extract element)
	   tree))
       'bold
     'identity))
  ;; Return value doesn't have any :parent set.
  (should-not
   (org-element-property
    :parent
    (org-test-with-temp-text "* Headline\n  Paragraph with *bold* text."
      (let* ((tree (org-element-parse-buffer))
	     (element (org-element-map tree 'bold 'identity nil t)))
	(org-element-extract element))))))

(ert-deftest test-org-element/insert-before ()
  "Test `org-element-insert-before' specifications."
  ;; Standard test.
  (should
   (equal
    '(italic entity bold)
    (org-test-with-temp-text "/some/ *paragraph*"
      (let* ((tree (org-element-parse-buffer))
	     (_paragraph (org-element-map tree 'paragraph #'identity nil t))
	     (bold (org-element-map tree 'bold 'identity nil t)))
	(org-element-insert-before '(entity (:name "\\alpha")) bold)
	(org-element-map tree '(bold entity italic) #'org-element-type nil)))))
  ;; Insert an object in a secondary string.
  (should
   (equal
    '(entity italic)
    (org-test-with-temp-text "* /A/\n  Paragraph."
      (let* ((tree (org-element-parse-buffer))
	     (headline (org-element-map tree 'headline 'identity nil t))
	     (italic (org-element-map tree 'italic 'identity nil t)))
	(org-element-insert-before '(entity (:name "\\alpha")) italic)
	(org-element-map (org-element-property :title headline) '(entity italic)
	  #'org-element-type))))))

(ert-deftest test-org-element/set ()
  "Test `org-element-set' specifications."
  ;; Check if new element is inserted.
  (should
   (org-test-with-temp-text "* Headline\n*a*"
     (let* ((tree (org-element-parse-buffer))
	    (bold (org-element-map tree 'bold 'identity nil t)))
       (org-element-set bold '(italic nil "b"))
       (org-element-map tree 'italic 'identity))))
  ;; Check if old element is removed.
  (should-not
   (org-test-with-temp-text "* Headline\n*a*"
     (let* ((tree (org-element-parse-buffer))
	    (bold (org-element-map tree 'bold 'identity nil t)))
       (org-element-set bold '(italic nil "b"))
       (org-element-map tree 'bold 'identity))))
  ;; Check if :parent property is correctly set.
  (should
   (eq 'paragraph
       (org-test-with-temp-text "* Headline\n*a*"
	 (let* ((tree (org-element-parse-buffer))
		(bold (org-element-map tree 'bold 'identity nil t)))
	   (org-element-set bold '(italic nil "b"))
	   (org-element-type
	    (org-element-property
	     :parent (org-element-map tree 'italic 'identity nil t)))))))
  ;; Allow to replace strings with elements.
  (should
   (equal '("b")
	  (org-test-with-temp-text "* Headline"
	    (let* ((tree (org-element-parse-buffer))
		   (text (org-element-map tree 'plain-text 'identity nil t)))
	      (org-element-set text (list 'bold nil "b"))
	      (org-element-map tree 'plain-text 'identity)))))
  ;; Allow to replace elements with strings.
  (should
   (equal "a"
	  (org-test-with-temp-text "* =verbatim="
	    (let* ((tree (org-element-parse-buffer))
		   (verb (org-element-map tree 'verbatim 'identity nil t)))
	      (org-element-set verb "a")
	      (org-element-map tree 'plain-text 'identity nil t)))))
  ;; Allow to replace strings with strings.
  (should
   (equal "b"
	  (org-test-with-temp-text "a"
	    (let* ((tree (org-element-parse-buffer))
		   (text (org-element-map tree 'plain-text 'identity nil t)))
	      (org-element-set text "b")
	      (org-element-map tree 'plain-text 'identity nil t)))))
  ;; Replace string inside anonymous element with another string.
  (let* ((parent (org-element-create 'anonymous nil "test"))
	 (str (car (org-element-contents parent))))
    (let ((return (org-element-set str "repl"))
          (new (car (org-element-contents parent))))
      ;; Return the modified value.
      (should (eq return new))
      (should (equal new "repl"))
      (should (eq (org-element-parent new) parent))))
  ;; KEEP-PROPS
  (should
   (org-element-property
    :foo
    (org-element-set
     (org-element-create 'dummy '(:foo bar))
     (org-element-create 'dummy '(:foo2 bar2))
     '(:foo)))))

(ert-deftest test-org-element/copy ()
  "Test `org-element-copy' specifications."
  ;; Preserve type.
  (should (eq 'bold
	      (org-test-with-temp-text "*bold*"
		(org-element-type (org-element-copy (org-element-context))))))
  (should (eq 'plain-text
	      (org-test-with-temp-text "*bold*"
		(org-element-type
		 (org-element-map (org-element-parse-buffer) 'plain-text
		   #'org-element-copy nil t)))))
  ;; Preserve properties except `:parent'.
  (should (= 7
	     (org-test-with-temp-text "*bold*"
	       (org-element-property
		:end (org-element-copy (org-element-context))))))
  (should-not
   (org-test-with-temp-text "*bold*"
     (org-element-property
      :parent (org-element-copy (org-element-context)))))
  (should-not
   (org-test-with-temp-text "*bold*"
     (org-element-property
      :parent
      (org-element-map (org-element-parse-buffer) 'plain-text
	#'org-element-copy nil t))))
  ;; Copying nil returns nil.
  (should-not (org-element-copy nil))
  ;; Return a copy secondary strings.
  (should (equal '("text") (org-element-copy '("text"))))
  (should-not (eq '("text") (org-element-copy '("text"))))
  ;; Do not alter the source.
  (org-test-with-temp-text "*bold*"
    (let* ((source (org-element-context))
           (copy (org-element-copy source)))
      (should-not (org-element-parent copy))
      (should (org-element-parent source)))))


\f
;;; Test Parsers

;;;; Affiliated Keywords

(ert-deftest test-org-element/affiliated-keywords-parser ()
  "Test affiliated keywords parsing."
  ;; Read simple keywords.
  (should
   (equal "para"
	  (org-element-property
	   :name
	   (org-test-with-temp-text "#+NAME: para\nParagraph"
	     (org-element-at-point)))))
  (should
   (= 1
      (org-element-property
       :begin
       (org-test-with-temp-text "#+NAME: para\nParagraph"
	 (org-element-at-point)))))
  ;; Parse multiple keywords.
  (should
   (equal
    '("line1" "line2")
    (org-element-property
     :attr_ascii
     (org-test-with-temp-text
	 "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph"
       (org-element-at-point)))))
  ;; Parse "parsed" keywords, unless granularity prevents it.
  (should
   (equal
    '(("caption"))
    (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
      (car (org-element-property :caption (org-element-at-point))))))
  (should
   (org-test-with-temp-text "#+CAPTION: *caption*\nParagraph"
     (org-element-map (org-element-map (org-element-parse-buffer)
			  'paragraph
			(lambda (e) (org-element-property :caption e)) nil t)
	 'bold
       #'org-element-type nil t)))
  (should-not
   (org-test-with-temp-text "#+CAPTION: *caption*\nParagraph"
     (org-element-map (org-element-map (org-element-parse-buffer 'element)
			  'paragraph
			(lambda (e) (org-element-property :caption e)) nil t)
	 'bold
       #'org-element-type nil t)))
  ;; Parse dual keywords.
  (should
   (equal
    '((("long") "short"))
    (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
      (org-element-property :caption (org-element-at-point)))))
  ;; Allow multiple caption keywords.
  (should
   (equal
    '((("l1") "s1") (("l2") "s2"))
    (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
      (org-element-property :caption (org-element-at-point)))))
  (should
   (equal
    '((nil "s1") (("l1")))
    (org-test-with-temp-text "#+CAPTION[s1]:\n#+CAPTION: l1\nParagraph"
      (org-element-property :caption (org-element-at-point)))))
  ;; Corner case: orphaned keyword at the end of an element.
  (should
   (eq 'keyword
       (org-test-with-temp-text "- item\n  #+name: name\nSome paragraph"
	 (progn (search-forward "name")
		(org-element-type (org-element-at-point))))))
  (should-not
   (org-test-with-temp-text "- item\n  #+name: name\nSome paragraph"
     (progn (search-forward "Some")
	    (org-element-property :name (org-element-at-point)))))
  ;; Corner case: orphaned keyword before comment.
  ;; Comments cannot have affiliated keywords.
  (should-not
   (org-test-with-temp-text "#+name: foo\n# bar"
     (progn (search-forward "bar")
	    (org-element-property :name (org-element-at-point)))))
  ;; Headlines cannot have affiliated keywords.
  (should
   (org-test-with-temp-text "<point>#+name: foo\n* Heading"
     (org-element-type-p (org-element-at-point) 'keyword)))
  ;; Clocks cannot have affiliated keywords.
  (should
   (org-test-with-temp-text "<point>#+name: foo
CLOCK: [2023-10-13 Fri 14:40]--[2023-10-13 Fri 14:51] =>  0:11"
     (org-element-type-p (org-element-at-point) 'keyword)))
  ;; Inlinetasks cannot have affiliated keywords.
  (should
   (let ((org-inlinetask-min-level 4))
     (org-test-with-temp-text "<point>#+name: foo
**** Inlinetask"
       (org-element-type-p (org-element-at-point) 'keyword)))))


;;;; Babel Call

(ert-deftest test-org-element/babel-call-parser ()
  "Test `babel-call' parsing."
  ;; Standard test.
  (should
   (eq 'babel-call
       (org-test-with-temp-text "#+CALL: test()"
	 (org-element-type (org-element-at-point)))))
  ;; Ignore case.
  (should
   (eq 'babel-call
       (org-test-with-temp-text "#+call: test()"
	 (org-element-type (org-element-at-point)))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+CALL: test()\n "
     (= (org-element-property :end (org-element-at-point)) (point-max))))
  ;; Parse call name.
  (should
   (equal "test"
	  (org-test-with-temp-text "#+CALL: test()"
	    (org-element-property :call (org-element-at-point)))))
  ;; Parse inside header.  It may contain paired square brackets.
  (should
   (equal ":results output"
	  (org-test-with-temp-text "#+CALL: test[:results output]()"
	    (org-element-property :inside-header (org-element-at-point)))))
  (should
   (equal ":results output, a=table[1:2], b=2"
	  (org-test-with-temp-text
	      "#+CALL: test[:results output, a=table[1:2], b=2]()"
	    (org-element-property :inside-header (org-element-at-point)))))
  ;; Parse arguments, which can be nested.  However, stop at paired
  ;; parenthesis, even when, e.g.,end header contains some.
  (should
   (equal "n=4"
	  (org-test-with-temp-text "#+CALL: test(n=4)"
	    (org-element-property :arguments (org-element-at-point)))))
  (should
   (equal "test()"
	  (org-test-with-temp-text "#+CALL: test(test())"
	    (org-element-property :arguments (org-element-at-point)))))
  (should
   (equal "a=1"
	  (org-test-with-temp-text "#+CALL: test(a=1) :post another-call()"
	    (org-element-property :arguments (org-element-at-point)))))
  ;; Parse end header.
  (should
   (equal ":results html"
	  (org-test-with-temp-text "#+CALL: test() :results html"
	    (org-element-property :end-header (org-element-at-point))))))


;;;; Bold

(ert-deftest test-org-element/bold-parser ()
  "Test `bold' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text "*bold*"
     (org-element-map (org-element-parse-buffer) 'bold #'identity nil t)))
  ;; Multi-line markup.
  (should
   (equal
    (org-element-contents
     (org-test-with-temp-text "*first line\nsecond line*"
       (org-element-map (org-element-parse-buffer) 'bold #'identity nil t)))
    '("first line\nsecond line"))))


;;;; Center Block

(ert-deftest test-org-element/center-block-parser ()
  "Test `center-block' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
     (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  ;; Ignore case.
  (should
   (org-test-with-temp-text "#+begin_center\nText\n#+end_center"
     (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  ;; Ignore incomplete block.
  (should-not
   (org-test-with-temp-text "#+BEGIN_CENTER"
     (org-element-map (org-element-parse-buffer) 'center-block
       'identity nil t)))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Citation

(ert-deftest test-org-element/citation-parser ()
  "Test `citation' parser"
  ;; Parse citations.  They must contain at least a bare key.
  (should
   (eq 'citation
       (org-test-with-temp-text "[cite:@key]"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'citation
       (org-test-with-temp-text "[cite:text]"
	 (org-element-type (org-element-context)))))
  ;; Citation may contain a style.
  (should
   (eq 'citation
       (org-test-with-temp-text "[cite/style:@key]"
	 (org-element-type (org-element-context)))))
  (should
   (equal "style"
	  (org-test-with-temp-text "[cite/style:@key]"
	    (org-element-property :style (org-element-context)))))
  ;; Handle multi citations separated with semi-columns.
  (should
   (eq 'citation
       (org-test-with-temp-text "[cite:@a;@b;@c]"
	 (org-element-type (org-element-context)))))
  (should
   (equal '("a" "b" "c")
	  (org-test-with-temp-text "[cite:@a;@b;@c]"
	    (org-element-map (org-element-parse-buffer) 'citation-reference
	      (lambda (r) (org-element-property :key r))))))
  (should
   (eq 'citation
       (org-test-with-temp-text "[cite:@a;-@b]"
	 (org-element-type (org-element-context)))))
  (should
   (equal '("a" "b")
	  (org-test-with-temp-text "[cite:@a;-@b]"
	    (org-element-map (org-element-parse-buffer) 'citation-reference
	      (lambda (r) (org-element-property :key r))))))
  ;; Multi citations accept `:prefix' and `:suffix' properties.
  (should
   (equal '("common-prefix")
	  (org-test-with-temp-text "[cite:common-prefix;@a]"
	    (org-element-property :prefix (org-element-context)))))
  (should
   (equal '("common-suffix")
	  (org-test-with-temp-text "[cite:@a;common-suffix]"
	    (org-element-property :suffix (org-element-context)))))
  ;; White spaces right after "cite" tags are ignored. So are white
  ;; spaces at the end of the citation.
  (should
   (equal '("common-prefix ")
	  (org-test-with-temp-text "[cite: common-prefix ;@a]"
	    (org-element-property :prefix (org-element-context)))))
  (should
   (equal '(" common-suffix")
	  (org-test-with-temp-text "[cite: @a; common-suffix ]"
	    (org-element-property :suffix (org-element-context)))))
  ;; Allow citations in a table cell.
  (should
   (eq 'citation
       (org-test-with-temp-text "| <point>[cite:@key] |"
	 (org-element-type (org-element-context))))))


;;;; Citation Reference

(ert-deftest test-org-element/citation-reference-parser ()
  "Test `citation' reference parser."
  ;; Parse bare keys.
  (should
   (eq 'citation-reference
       (org-test-with-temp-text "[cite:<point>@key]"
	 (org-element-type (org-element-context)))))
  ;; Bare keys can contain any word character, and some punctuation,
  ;; but not semicolon, square brackets, and space.
  (should
   (equal "_key"
	  (org-test-with-temp-text "[cite:@_k<point>ey]"
	    (org-element-property :key (org-element-context)))))
  (should
   (eq 'citation-reference
       (org-test-with-temp-text "[cite:<point>@a]"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'citation-reference
       (org-test-with-temp-text "[cite:<point>@ö]"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'citation-reference
       (org-test-with-temp-text "[cite:<point>@_]"
	 (org-element-type (org-element-context)))))
  (should
   (equal "a:.#$%&-+?<>~/1"
	  (org-test-with-temp-text "[cite:<point>@a:.#$%&-+?<>~/1]"
	    (org-element-property :key (org-element-context)))))
  (should-not
   (eq 'citation-reference
       (org-test-with-temp-text "[cite:<point>@;]"
	 (org-element-type (org-element-context)))))
  (should-not
   (equal "key"
	  (org-test-with-temp-text "[cite:<point>@[]]"
	    (org-element-property :key (org-element-context)))))
  ;; References in citations accept optional `:prefix' and `:suffix'
  ;; properties.
  (should
   (equal '("pre ")
	  (org-test-with-temp-text "[cite:pre <point>@key]"
	    (org-element-property :prefix (org-element-context)))))
  (should
   (equal '(" post")
	  (org-test-with-temp-text "[cite:<point>@key post]"
	    (org-element-property :suffix (org-element-context)))))
  ;; White spaces between "cite" tag and prefix are ignored.
  (should
   (equal '("pre ")
	  (org-test-with-temp-text "[cite: pre <point>@key]"
	    (org-element-property :prefix (org-element-context)))))
  ;; Semicolons do not belong to prefix or suffix.
  (should
   (equal '("pre ")
	  (org-test-with-temp-text "[cite:@key1;pre <point>@key2]"
	    (org-element-property :prefix (org-element-context)))))
  (should
   (equal '(" post")
	  (org-test-with-temp-text "[cite:@key1 <point>post;@key2]"
	    (org-element-property :suffix (org-element-context)))))
  (should
   (equal '("pre ")
	  (org-test-with-temp-text "[cite:global prefix;pre<point> @key1]"
	    (org-element-property :prefix (org-element-context)))))
  (should
   (equal '(" post")
	  (org-test-with-temp-text "[cite:@key1 <point>post; global suffix]"
	    (org-element-property :suffix (org-element-context))))))

;;;; Clock

(ert-deftest test-org-element/clock-parser ()
  "Test `clock' parser."
  ;; Running clock.
  (let ((clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
		 (org-element-at-point))))
    (should (eq (org-element-property :status clock) 'running))
    (should
     (equal (org-element-property :raw-value
				  (org-element-property :value clock))
	    "[2012-01-01 sun. 00:01]"))
    (should-not (org-element-property :duration clock)))
  ;; clock string should not be case-sensitive.
  (let ((clock (org-test-with-temp-text "Clock: [2012-01-01 sun. 00:01]"
		 (org-element-at-point))))
    (should (eq (org-element-property :status clock) 'running))
    (should
     (equal (org-element-property :raw-value
				  (org-element-property :value clock))
	    "[2012-01-01 sun. 00:01]"))
    (should-not (org-element-property :duration clock)))
  ;; Closed clock.
  (let ((clock
	 (org-test-with-temp-text
	     "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] =>  0:01"
	   (org-element-at-point))))
    (should (eq (org-element-property :status clock) 'closed))
    (should (equal (org-element-property :raw-value
					 (org-element-property :value clock))
		   "[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
    (should (equal (org-element-property :duration clock) "0:01")))
  ;; Closed clock without timestamp.
  (let ((clock
	 (org-test-with-temp-text
	     "CLOCK: =>  0:11"
	   (org-element-at-point))))
    (should (eq (org-element-property :status clock) 'closed))
    (should-not (org-element-property :value clock))
    (should (equal (org-element-property :duration clock) "0:11"))))


;;;; Code

(ert-deftest test-org-element/code-parser ()
  "Test `code' parser."
  ;; Regular test.
  (should
   (org-test-with-temp-text "~code~"
     (org-element-map (org-element-parse-buffer) 'code #'identity)))
  ;; Multi-line markup.
  (should
   (equal
    (org-element-property
     :value
     (org-test-with-temp-text "~first line\nsecond line~"
       (org-element-map
	   (org-element-parse-buffer) 'code #'identity nil t)))
    "first line\nsecond line")))


;;;; Comment

(ert-deftest test-org-element/comment-parser ()
  "Test `comment' parser."
  ;; Regular comment.
  (should (eq 'comment
	      (org-test-with-temp-text "# Comment"
		(org-element-type (org-element-at-point)))))
  ;; Inline comment.
  (should (eq 'comment
	      (org-test-with-temp-text "  # Comment"
		(org-element-type (org-element-at-point)))))
  ;; Preserve indentation.
  (should
   (equal "No blank\n One blank"
	  (org-element-property
	   :value
	   (org-test-with-temp-text "# No blank\n#  One blank"
	     (org-element-at-point)))))
  ;; Comment with blank lines.
  (should
   (equal "First part\n\n\nSecond part"
	  (org-element-property
	   :value
	   (org-test-with-temp-text "# First part\n# \n#\n# Second part"
	     (org-element-at-point)))))
  ;; Do not mix comments and keywords.
  (should
   (eq 1
       (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
	 (length (org-element-map (org-element-parse-buffer) 'comment
		   #'identity)))))
  (should
   (equal "comment"
	  (org-test-with-temp-text
	      "#+key: value\n<point># comment\n#+key: value"
	    (org-element-property :value (org-element-at-point)))))
  ;; Correctly handle non-empty blank lines at the end of buffer.
  (should
   (org-test-with-temp-text "# A\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Comment Block

(ert-deftest test-org-element/comment-block-parser ()
  "Test `comment-block' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
     (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  ;; Ignore case.
  (should
   (org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
     (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  ;; Ignore incomplete block.
  (should-not
   (org-test-with-temp-text "#+BEGIN_COMMENT"
     (org-element-map (org-element-parse-buffer) 'comment-block
       'identity nil t)))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN_COMMENT\nC\n#+END_COMMENT\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Diary Sexp

(ert-deftest test-org-element/diary-sexp-parser ()
  "Test `diary-sexp' parser."
  ;; Standard test.
  (should
   (eq 'diary-sexp
       (org-test-with-temp-text
	   "%%(org-anniversary 1956  5 14)(2) Arthur Dent is %d years old"
	 (org-element-type (org-element-at-point)))))
  ;; Diary sexp must live at beginning of line
  (should-not
   (eq 'diary-sexp
       (org-test-with-temp-text " %%(org-bbdb-anniversaries)"
	 (org-element-type (org-element-at-point)))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "%%(org-bbdb-anniversaries)\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Drawer

(ert-deftest test-org-element/drawer-parser ()
  "Test `drawer' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text ":TEST:\nText\n:END:"
     (org-element-map (org-element-parse-buffer) 'drawer 'identity)))
  ;; Ignore incomplete drawer.
  (should-not
   (org-test-with-temp-text ":TEST:"
     (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
  (should-not
   (org-test-with-temp-text ":END:"
     (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text ":TEST:\nC\n:END:\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Dynamic Block

(ert-deftest test-org-element/dynamic-block-parser ()
  "Test `dynamic-block' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text
       "#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
     (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  ;; Ignore case.
  (should
   (org-test-with-temp-text
       "#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
     (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  ;; Ignore incomplete block.
  (should-not
   (org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
     (org-element-map (org-element-parse-buffer) 'dynamic-block
       'identity nil t)))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN: myblock :param val1\nC\n#+END:\n  "
     (= (org-element-property :end (org-element-at-point)) (point-max))))
  ;; Block name is mandatory.
  (should-not
   (org-test-with-temp-text "#+BEGIN:\n\n#+END:\n"
     (org-element-type-p (org-element-at-point) 'dynamic-block))))


;;;; Entity

(ert-deftest test-org-element/entity-parser ()
  "Test `entity' parser."
  ;; Without brackets.
  (should
   (org-test-with-temp-text "\\sin"
     (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  ;; Special case: space-based entities.
  (should
   (equal
    "_   "
    ;; Space after entity must be a part of its name.
    (org-test-with-temp-text "\\_   Foo"
      (org-element-property
       :name
       (car (org-element-map (org-element-parse-buffer) 'entity 'identity))))))
  (should-not
   ;; {} is not a part of whitespace entity name.
   (org-test-with-temp-text "\\_   {}Foo"
     (org-element-property
      :bracketsp
      (car (org-element-map (org-element-parse-buffer) 'entity 'identity)))))
  ;; With brackets.
  (should
   (org-element-property
    :use-brackets-p
    (org-test-with-temp-text "\\alpha{}text"
      (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
  ;; User-defined entity.
  (should
   (equal
    (org-element-property
     :name
     (let ((org-entities-user
	    '(("test" "test" nil "test" "test" "test" "test"))))
       (org-test-with-temp-text "\\test"
	 (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
    "test"))
  ;; Special case: entity at the end of a container.
  (should
   (eq 'entity
       (org-test-with-temp-text "*\\alpha \\beta*"
	 (search-forward "be")
	 (org-element-type (org-element-context))))))


;;;; Example Block

(ert-deftest test-org-element/example-block-parser ()
  "Test `example-block' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
     (org-element-map (org-element-parse-buffer) 'example-block 'identity)))
  ;; Ignore incomplete block.
  (should-not
   (eq 'example-block
       (org-test-with-temp-text "#+BEGIN_EXAMPLE"
	 (org-element-type (org-element-at-point)))))
  ;; Properly un-escape code.
  (should
   (equal "* Headline\n #+keyword:\nText\n"
	  (org-test-with-temp-text
	      "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword:\nText\n#+END_EXAMPLE"
	    (org-element-property :value (org-element-at-point)))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN_EXAMPLE\nC\n#+END_EXAMPLE\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))

(ert-deftest test-org-element/block-switches ()
  "Test `example-block' and `src-block' switches parsing."
  (let ((org-coderef-label-format "(ref:%s)"))
    ;; 1. Test "-i" switch.
    (should-not
     (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
       (org-element-property :preserve-indent (org-element-at-point))))
    (should
     (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
       (org-element-property :preserve-indent (org-element-at-point))))
    (should-not
     (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
       (org-element-property :preserve-indent (org-element-at-point))))
    (should
     (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
       (org-element-property :preserve-indent (org-element-at-point))))
    ;; 2. "-n -r -k" combination should number lines, retain labels but
    ;;    not use them in coderefs.
    (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
      (let ((element (org-element-at-point)))
	(should (org-element-property :number-lines element))
	(should (org-element-property :retain-labels element))
	(should-not (org-element-property :use-labels element))))
    (org-test-with-temp-text
	"#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
      (let ((element (org-element-at-point)))
	(should (org-element-property :number-lines element))
	(should (org-element-property :retain-labels element))
	(should-not (org-element-property :use-labels element))))
    ;; 3. "-n -r" combination should number-lines remove labels and not
    ;;    use them in coderefs.
    (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
      (let ((element (org-element-at-point)))
	(should (org-element-property :number-lines element))
	(should-not (org-element-property :retain-labels element))
	(should-not (org-element-property :use-labels element))))
    (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
      (let ((element (org-element-at-point)))
	(should (org-element-property :number-lines element))
	(should-not (org-element-property :retain-labels element))
	(should-not (org-element-property :use-labels element))))
    ;; 4. "-n" or "+n" should number lines, retain labels and use them
    ;;    in coderefs.
    (should
     (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
       (let ((element (org-element-at-point)))
	 (and (org-element-property :number-lines element)
	      (org-element-property :retain-labels element)
	      (org-element-property :use-labels element)))))
    (should
     (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
       (let ((element (org-element-at-point)))
	 (and (org-element-property :number-lines element)
	      (org-element-property :retain-labels element)
	      (org-element-property :use-labels element)))))
    (should
     (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
       (let ((element (org-element-at-point)))
	 (and (org-element-property :number-lines element)
	      (org-element-property :retain-labels element)
	      (org-element-property :use-labels element)))))
    (should
     (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
       (let ((element (org-element-at-point)))
	 (and (org-element-property :number-lines element)
	      (org-element-property :retain-labels element)
	      (org-element-property :use-labels element)))))
    ;; 5. No switch should not number lines, but retain labels and use
    ;;    them in coderefs.
    (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
      (let ((element (org-element-at-point)))
	(should (not (org-element-property :number-lines element)))
	(should (org-element-property :retain-labels element))
	(should (org-element-property :use-labels element))))
    (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
      (let ((element (org-element-at-point)))
	(should (not (org-element-property :number-lines element)))
	(should (org-element-property :retain-labels element))
	(should (org-element-property :use-labels element))))
    ;; 6. "-r" switch only: do not number lines, remove labels, and
    ;;    don't use labels in coderefs.
    (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
      (let ((element (org-element-at-point)))
	(should (not (org-element-property :number-lines element)))
	(should (not (org-element-property :retain-labels element)))
	(should (not (org-element-property :use-labels element)))))
    (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
      (let ((element (org-element-at-point)))
	(should (not (org-element-property :number-lines element)))
	(should (not (org-element-property :retain-labels element)))
	(should (not (org-element-property :use-labels element)))))
    ;; 7. Recognize coderefs with user-defined syntax.
    (should
     (equal
      "[ref:%s]"
      (org-test-with-temp-text
	  "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
	(org-element-property :label-fmt (org-element-at-point)))))
    (should
     (equal
      "[ref:%s]"
      (org-test-with-temp-text
	  "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
	(org-element-property :label-fmt (org-element-at-point)))))))


;;;; Export Block

(ert-deftest test-org-element/export-block-parser ()
  "Test `export-block' parser."
  ;; Standard test.
  (should
   (eq 'export-block
       (org-test-with-temp-text "#+BEGIN_EXPORT LATEX\nText\n#+END_EXPORT"
	 (org-element-type (org-element-at-point)))))
  (should
   (equal "LATEX"
	  (org-test-with-temp-text "#+BEGIN_EXPORT LATEX\nText\n#+END_EXPORT"
	    (org-element-property :type (org-element-at-point)))))
  ;; Ignore case.
  (should
   (eq 'export-block
       (org-test-with-temp-text "#+begin_export latex\nText\n#+end_export"
	 (org-element-type (org-element-at-point)))))
  ;; Ignore incomplete block.
  (should-not
   (eq 'export-block
       (org-test-with-temp-text "#+BEGIN_EXPORT"
	 (org-element-type (org-element-at-point)))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN_EXPORT latex\nC\n#+END_EXPORT\n "
     (= (org-element-property :end (org-element-at-point)) (point-max))))
  ;; Un-escape commas in `:value'.
  (should
   (equal "* H\n"
	  (org-test-with-temp-text "#+BEGIN_EXPORT org\n,* H\n#+END_EXPORT\n "
	    (org-element-property :value (org-element-at-point))))))


;;;; Export Snippet

(ert-deftest test-org-element/export-snippet-parser ()
  "Test `export-snippet' parser."
  (should
   (equal
    '("backend" . "contents")
    (org-test-with-temp-text "@@backend:contents@@"
      (org-element-map
       (org-element-parse-buffer) 'export-snippet
       (lambda (snippet) (cons (org-element-property :back-end snippet)
			  (org-element-property :value snippet)))
       nil t)))))


;;;; Fixed Width

(ert-deftest test-org-element/fixed-width-parser ()
  "Test fixed-width area parsing."
  ;; Preserve indentation.
  (should
   (equal "no blank\n one blank"
	  (org-test-with-temp-text ": no blank\n:  one blank"
	    (org-element-property :value (org-element-at-point)))))
  ;; Fixed-width with empty lines.
  (should
   (equal "first part\n\n\nsecond part"
	  (org-test-with-temp-text ": first part\n:\n: \n: second part"
	    (org-element-property :value (org-element-at-point)))))
  ;; Parse indented fixed-width markers.
  (should
   (eq 'fixed-width
       (org-test-with-temp-text "Text\n<point>  : no blank\n  :  one blank"
	 (org-element-type (org-element-at-point)))))
  ;; Distinguish fixed-width areas within a list and outside of it.
  (should
   (org-test-with-temp-text "
- Item
  : fixed-width inside<point>
: fixed-width outside"
     (= (org-element-property :end (org-element-at-point))
	(line-beginning-position 2))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text ": A\n "
     (= (org-element-property :end (org-element-at-point))
	(point-max)))))


;;;; Footnote Definition

(ert-deftest test-org-element/footnote-definition-parser ()
  "Test `footnote-definition' parser."
  (should
   (org-test-with-temp-text "[fn:1] Definition"
     (eq (org-element-type (org-element-at-point)) 'footnote-definition)))
  ;; Footnote with more contents.
  (should
   (= 29 (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
	   (org-element-property :end (org-element-at-point)))))
  ;; Test difference between :contents-end and :end property
  (should
   (< (org-test-with-temp-text "[fn:1] Definition\n\n\n"
	(org-element-property :contents-end (org-element-at-point)))
      (org-test-with-temp-text "[fn:1] Definition\n\n\n"
	(org-element-property :end (org-element-at-point)))))
  ;; Footnote starting with special syntax.
  (should-not
   (org-test-with-temp-text "[fn:1] <point>- no item"
     (eq (org-element-type (org-element-at-point)) 'item)))
  ;; Correctly handle footnote starting with an empty line.
  (should
   (= 9
      (org-test-with-temp-text "[fn:1]\n\n  Body"
	(org-element-property :contents-begin (org-element-at-point)))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "[fn:1] Definition\n "
     (= (org-element-property :end (org-element-at-point)) (point-max))))
  ;; Footnote with attributes.
  (should
   (= 1
      (org-test-with-temp-text "#+attr_latex: :offset 0in\n[fn:1] A footnote."
	(length
	 (org-element-map (org-element-parse-buffer) 'footnote-definition
	   #'identity)))))
  (should
   (org-test-with-temp-text "[fn:1] 1\n\n#+attr_latex: :offset 0in\n[fn:2] 2"
     (goto-char (org-element-property :end (org-element-at-point)))
     (looking-at "#")))
  ;; An empty footnote has no contents.
  (should-not
   (org-test-with-temp-text "[fn:1]\n\n"
     (let ((footnote (org-element-at-point)))
       (or (org-element-property :contents-begin footnote)
	   (org-element-property :contents-end footnote)))))
  ;; Parse `:pre-blank'.
  (should
   (= 0
      (org-test-with-temp-text "[fn:1] A"
	(org-element-property :pre-blank (org-element-at-point)))))
  (should
   (= 1
      (org-test-with-temp-text "[fn:1]\nA"
	(org-element-property :pre-blank (org-element-at-point)))))
  (should
   (= 2
      (org-test-with-temp-text "[fn:1]\n\nA"
	(org-element-property :pre-blank (org-element-at-point))))))


;;;; Footnotes Reference.

(ert-deftest test-org-element/footnote-reference-parser ()
  "Test `footnote-reference' parser."
  ;; Parse a standard reference.
  (should
   (org-test-with-temp-text "Text[fn:label]"
     (org-element-map
	 (org-element-parse-buffer) 'footnote-reference 'identity)))
  ;; Parse an inline reference.
  (should
   (org-test-with-temp-text "Text[fn:test:def]"
     (org-element-map
	 (org-element-parse-buffer) 'footnote-reference 'identity)))
  ;; Parse an anonymous reference.
  (should
   (org-test-with-temp-text "Text[fn::def]"
     (org-element-map
	 (org-element-parse-buffer) 'footnote-reference 'identity)))
  ;; Parse inline references with syntax loaded characters.
  (should
   (eq 'footnote-reference
       (org-test-with-temp-text "Text[fn<point>::(def]"
         (org-element-type (org-element-context)))))
  (should
   (eq 'footnote-reference
       (org-test-with-temp-text "Text[fn<point>::\"def]"
         (org-element-type (org-element-context)))))
  ;; Parse nested footnotes.
  (should
   (= 2
      (length
       (org-test-with-temp-text "Text[fn::def [fn:label]]"
	 (org-element-map
	     (org-element-parse-buffer) 'footnote-reference 'identity)))))
  ;; Parse adjacent footnotes.
  (should
   (org-test-with-temp-text "Text[fn:label1][fn:label2]"
     (= 2
	(length
	 (org-element-map
	     (org-element-parse-buffer) 'footnote-reference 'identity)))))
  ;; Only properly closed footnotes are recognized as such.
  (should-not
   (org-test-with-temp-text "Text[fn:label"
     (org-element-map
	 (org-element-parse-buffer) 'footnote-reference 'identity))))


;;;; Headline

(ert-deftest test-org-element/headline-todo-keyword ()
  "Test todo keyword recognition."
  ;; Reference test.
  (org-test-with-temp-text "* TODO Headline"
    (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
      (should (org-element-property :todo-keyword (org-element-at-point)))))
  ;; Todo keyword is prefix of headlines first word.
  (org-test-with-temp-text "* TODOHeadline"
    (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
      (should-not (org-element-property :todo-keyword (org-element-at-point)))))
  (org-test-with-temp-text "* TODO"
    (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
      (should (org-element-property :todo-keyword (org-element-at-point)))))
  (org-test-with-temp-text "* :tag:"
    (should (member "tag" (org-element-property :tags (org-element-at-point)))))
  (org-test-with-temp-text "* COMMENT"
    (should (org-element-property :commentedp (org-element-at-point))))
  (org-test-with-temp-text "* COMMENT title"
    (should (equal "title" (org-element-property :raw-value (org-element-at-point)))))
  (org-test-with-temp-text "* COMMENT:tag:"
    (should-not (org-element-property :commentedp (org-element-at-point)))))

(ert-deftest test-org-element/headline-comment-keyword ()
  "Test COMMENT keyword recognition."
  ;; Reference test.
  (org-test-with-temp-text "* Headline"
    (should-not (org-element-property :commentedp (org-element-at-point))))
  ;; Standard position.
  (org-test-with-temp-text "* COMMENT Headline"
    (let ((headline (org-element-at-point)))
      (should (org-element-property :commentedp headline))
      (should (equal (org-element-property :raw-value headline) "Headline"))))
  ;; Case sensitivity.
  (org-test-with-temp-text "* Comment Headline"
    (let ((headline (org-element-at-point)))
      (should-not (org-element-property :commentedp headline))
      (should (equal (org-element-property :raw-value headline)
		     "Comment Headline"))))
  ;; With another keyword.
  (org-test-with-temp-text "* TODO COMMENT Headline"
    (let* ((org-todo-keywords '((sequence "TODO" "DONE")))
	   (headline (org-element-at-point)))
      (should (org-element-property :commentedp headline))
      (should (equal (org-element-property :raw-value headline) "Headline"))))
  ;; With the keyword only.
  (org-test-with-temp-text "* COMMENT"
    (let* ((headline (org-element-at-point)))
      (should (org-element-property :commentedp headline))
      (should (equal (org-element-property :raw-value headline) "")))))

(ert-deftest test-org-element/headline-archive-tag ()
  "Test ARCHIVE tag recognition."
  ;; Reference test.
  (should-not
   (org-test-with-temp-text "* Headline"
     (org-element-property :archivedp (org-element-at-point))))
  ;; Single tag.
  (org-test-with-temp-text "* Headline :ARCHIVE:"
    (let ((headline (org-element-at-point)))
      (should (org-element-property :archivedp headline))))
  ;; Multiple tags.
  (org-test-with-temp-text "* Headline :test:ARCHIVE:"
    (let ((headline (org-element-at-point)))
      (should (org-element-property :archivedp headline))))
  ;; Tag is case-sensitive.
  (should-not
   (org-test-with-temp-text "* Headline :Archive:"
     (org-element-property :archivedp (org-element-at-point)))))

(ert-deftest test-org-element/headline-properties ()
  "Test properties from property drawer."
  ;; All properties from property drawer have their symbol upper
  ;; cased.
  (should
   (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
     (org-element-property :FOO (org-element-at-point))))
  (should-not
   (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
     (org-element-property :foo (org-element-at-point))))
  ;; Also parse properties associated in inlinetasks.
  (when (featurep 'org-inlinetask)
    (should
     (org-test-with-temp-text "*************** Inlinetask
:PROPERTIES:
:foo: bar
:END:
*************** END"
       (org-element-property :FOO (org-element-at-point)))))
  ;; Do not find property drawer in a verbatim area.
  (should-not
   (org-test-with-temp-text
       "* Headline
#+BEGIN_EXAMPLE
:PROPERTIES:
:foo: bar
:END:
#+END_EXAMPLE"
     (org-element-property :FOO (org-element-at-point))))
  ;; Do not use properties from a drawer associated to an inlinetask.
  (when (featurep 'org-inlinetask)
    (should-not
     (org-test-with-temp-text
	 "* Headline
*************** Inlinetask
:PROPERTIES:
:foo: bar
:END:
*************** END"
       (org-element-property
	:FOO (let ((org-inlinetask-min-level 15)) (org-element-at-point))))))
  ;; Do not find incomplete drawers.
  (should-not
   (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar"
     (org-element-property :FOO (org-element-at-point)))))


;;;; Horizontal Rule

(ert-deftest test-org-element/horizontal-rule-parser ()
  "Test `horizontal-rule' parser."
  ;; Standard.
  (should
   (org-test-with-temp-text "-----"
     (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  ;; Indented.
  (should
   (org-test-with-temp-text "  -----"
     (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  ;; Hyphen must be alone on the line.
  (should-not
   (org-test-with-temp-text "-----wrong"
     (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  ;; 4 hyphens is too small.
  (should-not
   (org-test-with-temp-text "----"
     (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "-----\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Inline Babel Call

(ert-deftest test-org-element/inline-babel-call-parser ()
  "Test `inline-babel-call' parser."
  ;; Standard test.
  (should
   (eq 'inline-babel-call
       (org-test-with-temp-text "call_test()"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'inline-babel-call
       (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
	 (org-element-type (org-element-context)))))
  ;; Parse call name.
  (should
   (equal
    "test"
    (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
      (org-element-property :call (org-element-context)))))
  ;; Parse inside header.
  (should
   (equal
    ":results output"
    (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
      (org-element-property :inside-header (org-element-context)))))
  ;; Parse arguments.
  (should
   (equal
    "x=2"
    (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
      (org-element-property :arguments (org-element-context)))))
  ;; Parse end header.
  (should
   (equal
    ":results html"
    (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
      (org-element-property :end-header (org-element-context)))))
  ;; Handle multi-line babel calls.
  (should
   (eq 'inline-babel-call
       (org-test-with-temp-text
	   "call_test[:results\noutput](x=2)[:results html]"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'inline-babel-call
       (org-test-with-temp-text
	   "call_test[:results output](x=2\ny=3)[:results html]"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'inline-babel-call
       (org-test-with-temp-text
	   "call_test[:results output](x=2)[:results\nhtml]"
	 (org-element-type (org-element-context)))))
  ;; Parse parameters containing round brackets.
  (should
   (eq 'inline-babel-call
       (org-test-with-temp-text "call_test[:var x='(1)](x=2)"
	 (org-element-type (org-element-context))))))


;;;; Inline Src Block

(ert-deftest test-org-element/inline-src-block-parser ()
  "Test `inline-src-block' parser."
  (should
   (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  ;; With switches.
  (should
   (org-test-with-temp-text "src_emacs-lisp[:foo bar]{(+ 1 1)}"
     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  (should
   (org-test-with-temp-text "src_emacs-lisp[ :foo bar]{(+ 1 1)}"
     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  ;; Empty switches.
  (should
   (org-test-with-temp-text "src_emacs-lisp[]{(+ 1 1)}"
     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  ;; Invalid syntax.
  (should-not
   (org-test-with-temp-text "src_emacs-lisp[]foo{(+ 1 1)}"
     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  (should-not
   (org-test-with-temp-text "foosrc_emacs-lisp[]{(+ 1 1)}"
     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  ;; Invalid language name
  (should-not
   (org-test-with-temp-text "src_emacs-\tlisp{(+ 1 1)}"
     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  ;; Test parsing at the beginning of an item.
  (should
   (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  ;; Test parsing multi-line source blocks.
  (should
   (eq 'inline-src-block
       (org-test-with-temp-text "src_emacs-lisp{(+ 1\n  1)}"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'inline-src-block
       (org-test-with-temp-text "src_emacs-lisp[\n:foo bar]{(+ 1 1)}"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'inline-src-block
       (org-test-with-temp-text "src_emacs-lisp[:foo\nbar]{(+ 1 1)}"
	 (org-element-type (org-element-context)))))
  ;; Besides curly brackets, ignore any other bracket type.
  (should
   (equal "[foo"
	  (org-test-with-temp-text "src_emacs-lisp{[foo}"
	    (org-element-property :value (org-element-context)))))
  (should
   (equal "foo]"
	  (org-test-with-temp-text "src_emacs-lisp{foo]}"
	    (org-element-property :value (org-element-context)))))
  (should
   (equal "(foo"
	  (org-test-with-temp-text "src_emacs-lisp{(foo}"
	    (org-element-property :value (org-element-context)))))
  (should
   (equal "foo)"
	  (org-test-with-temp-text "src_emacs-lisp{foo)}"
	    (org-element-property :value (org-element-context)))))
  ;; Parse parameters containing square brackets.
  (should
   (eq 'inline-src-block
       (org-test-with-temp-text "src_emacs-lisp[:var table=t[1,1]]{(+ 1 1)}"
	 (org-element-type (org-element-context))))))


;;;; Inlinetask

(ert-deftest test-org-element/inlinetask-parser ()
  "Test `inlinetask' parser."
  (when (featurep 'org-inlinetask)
    (let ((org-inlinetask-min-level 15))
      ;; Regular inlinetask.
      (should
       (eq 'inlinetask
	   (org-test-with-temp-text
	       "*************** Task\nTest\n*************** END"
	     (org-element-type (org-element-at-point)))))
      (should
       (eq 'inlinetask
	   (org-test-with-temp-text
	       "*************** Task\nTest\n***************   END"
	     (org-element-type (org-element-at-point)))))
      ;; Degenerate inlinetask.
      (should
       (eq 'inlinetask
	   (org-test-with-temp-text "*************** Task"
	     (org-element-type (org-element-at-point)))))
      ;; Mixed inlinetasks.
      (should-not
       (org-test-with-temp-text
	   "
*************** Task
*************** Task2
Contents
*************** END"
	 (forward-line)
	 (goto-char (org-element-property :end (org-element-at-point)))
	 (eobp)))
      ;; TODO keyword.
      (should
       (equal
	"TODO"
	(let ((org-todo-keywords '((sequence "TODO" "DONE"))))
	  (org-test-with-temp-text "*************** TODO Task"
	    (org-element-property :todo-keyword (org-element-at-point))))))
      ;; Planning info.
      (should
       (org-test-with-temp-text "
*************** Task<point>
DEADLINE: <2012-03-29 thu.>
*************** END"
	 (org-element-property :deadline (org-element-at-point))))
      (should
       (eq 'planning
	   (org-test-with-temp-text "
*************** Task
<point>DEADLINE: <2012-03-29 thu.>
*************** END"
	     (org-element-type (org-element-at-point)))))
      (should-not
       (org-test-with-temp-text "
*************** Task<point>
DEADLINE: <2012-03-29 thu.>"
	 (org-element-property :deadline (org-element-at-point))))
      (should-not
       (eq 'planning
	   (org-test-with-temp-text "
*************** Task
<point>DEADLINE: <2012-03-29 thu.>"
	     (org-element-type (org-element-at-point)))))
      ;; Priority.
      (should
       (eq
	?A
	(org-test-with-temp-text "
*************** [#A] Task"
	  (forward-line)
	  (org-element-property :priority (org-element-at-point)))))
      ;; Tags.
      (should
       (equal
	'("test")
	(org-test-with-temp-text "
*************** Task :test:"
	  (forward-line)
	  (org-element-property :tags (org-element-at-point)))))
      ;; Regular properties are accessed through upper case keywords.
      (should
       (org-test-with-temp-text "
*************** Task
:PROPERTIES:
:foo: bar
:END:
*************** END"
	 (forward-line)
	 (org-element-property :FOO (org-element-at-point))))
      (should-not
       (org-test-with-temp-text "
*************** Task
:PROPERTIES:
:foo: bar
:END:
*************** END"
	 (forward-line)
	 (org-element-property :foo (org-element-at-point))))
      ;; Handle non-empty blank line at the end of buffer.
      (should
       (org-test-with-temp-text "*************** Task\n*************** END\n "
	 (= (org-element-property :end (org-element-at-point)) (point-max)))))))


;;;; Italic

(ert-deftest test-org-element/italic-parser ()
  "Test `italic' parser."
  ;; Regular test.
  (should
   (org-test-with-temp-text "/italic/"
     (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
  ;; Multi-line markup.
  (should
   (equal
    (org-element-contents
     (org-test-with-temp-text "/first line\nsecond line/"
       (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
    '("first line\nsecond line"))))


;;;; Item

(ert-deftest test-org-element/item-parser ()
  "Test `item' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text "- item"
     (org-element-map (org-element-parse-buffer) 'item 'identity)))
  ;; Counter.
  (should
   (= 6
      (org-element-property
       :counter
       (org-test-with-temp-text "6. [@6] item"
	 (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  ;; Tag
  (should
   (equal
    '("tag")
    (org-element-property
     :tag
     (org-test-with-temp-text "- tag :: description"
       (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  ;; No tags in ordered lists.
  (should-not
   (org-element-property
    :tag
    (org-test-with-temp-text "1. tag :: description"
      (org-element-map (org-element-parse-buffer) 'item 'identity nil t))))
  ;; Check-boxes
  (should
   (equal
    '(trans on off)
    (org-test-with-temp-text "
- [-] item 1
  - [X] item 1.1
  - [ ] item 1.2"
      (org-element-map (org-element-parse-buffer) 'item
	(lambda (item) (org-element-property :checkbox item))))))
  ;; Item starting with special syntax.
  (should
   (equal '(("- item"))
	  (org-test-with-temp-text "- - item"
	    (org-element-map (org-element-parse-buffer) 'paragraph
	      'org-element-contents))))
  ;; Block in an item: ignore indentation within the block.
  (should
   (org-test-with-temp-text
       "-<point> item\n  #+begin_src emacs-lisp\n(+ 1 1)\n  #+end_src"
     (= (org-element-property :end (org-element-at-point)) (point-max))))
  ;; Parse `:pre-blank'.
  (should
   (= 0
      (org-test-with-temp-text "-<point> A"
	(org-element-property :pre-blank (org-element-at-point)))))
  (should
   (= 1
      (org-test-with-temp-text "-<point>\n  A"
	(org-element-property :pre-blank (org-element-at-point)))))
  (should
   (= 2
      (org-test-with-temp-text "-<point>\n\n  A"
	(org-element-property :pre-blank (org-element-at-point)))))
  ;; Last item in a list or sub-list has no `:post-blank' lines, since
  ;; those belong to the plain-list.
  (should
   (= 0
      (org-test-with-temp-text "- A\n\n- <point>B\n\nEnd list"
	(org-element-property :post-blank (org-element-at-point)))))
  (should
   (= 0
      (org-test-with-temp-text "- A\n\n  - B\n\n<point>  - C\n\n  End sub-list"
	(org-element-property :post-blank (org-element-at-point)))))
  (should
   (= 0
      (org-test-with-temp-text "1. foo\n   1. bar\n  2.<point>  baz\n\n2. lorem\nipsum"
	(org-element-property :post-blank (org-element-at-point))))))


;;;; Keyword

(ert-deftest test-org-element/keyword-parser ()
  "Test `keyword' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text "#+KEYWORD: value"
     (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  ;; Keywords are case-insensitive.
  (should
   (org-test-with-temp-text "#+keyword: value"
     (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  ;; Affiliated keywords are not keywords.
  (should-not
   (org-test-with-temp-text "#+NAME: value
Paragraph"
     (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  ;; Do not mix keywords with Babel calls and dynamic blocks.
  (should-not
   (org-test-with-temp-text "#+CALL: fun()"
     (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  (should-not
   (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
     (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+KEYWORD: value\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; LaTeX Environment

(ert-deftest test-org-element/latex-environment-parser ()
  "Test `latex-environment' parser."
  (should
   (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
     (org-element-map (org-element-parse-buffer) 'latex-environment 'identity)))
  ;; Allow nested environments.
  (should
   (equal
    "\\begin{outer}
\\begin{inner}
e^{i\\pi}+1=0
\\end{inner}
\\end{outer}"
    (org-test-with-temp-text "
\\begin{outer}
\\begin{inner}
e^{i\\pi}+1=0
\\end{inner}
\\end{outer}"
      (org-element-property
       :value
       (org-element-map
	   (org-element-parse-buffer) 'latex-environment 'identity nil t)))))
  ;; Allow environments with options and arguments.
  (should
   (eq 'latex-environment
       (org-test-with-temp-text
	   "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
	 (org-element-type (org-element-at-point)))))
  (should
   (eq 'latex-environment
       (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
	 (org-element-type (org-element-at-point)))))
  ;; Allow environments without newline after \begin{.}.
  (should
   (eq 'latex-environment
       (org-test-with-temp-text "\\begin{env}{arg}something\nvalue\n\\end{env}"
	 (org-element-type (org-element-at-point)))))
  ;; Allow one-line environments.
  (should
   (eq 'latex-environment
       (org-test-with-temp-text "\\begin{env}{arg}something\\end{env}"
	 (org-element-type (org-element-at-point)))))
  ;; Should not allow different tags.
  (should-not
   (eq 'latex-environment
       (org-test-with-temp-text "\\begin{env*}{arg}something\\end{env}"
				(org-element-type (org-element-at-point)))))
  ;; LaTeX environments must be on separate lines.
  (should-not
   (eq 'latex-environment
       (org-test-with-temp-text "\\begin{env} x \\end{env} y"
	 (org-element-type (org-element-at-point)))))
  (should-not
   (eq 'latex-environment
       (org-test-with-temp-text "y \\begin{env} x<point> \\end{env}"
	 (org-element-type (org-element-at-point)))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "\\begin{env}\n\\end{env}\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; LaTeX Fragment

(ert-deftest test-org-element/latex-fragment-parser ()
  "Test `latex-fragment' parser."
  ;; Basic $...$ test.
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "$a$"
	 (org-element-type (org-element-context)))))
  ;; Test valid characters after $...$ construct.
  (should-not
   (eq 'latex-fragment
       (org-test-with-temp-text "$a$a"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "$a$!"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "$a$,"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "$a$\""
	 (org-element-type (org-element-context)))))
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "$a$)"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "$a$ "
	 (org-element-type (org-element-context)))))
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "$a$'"
	 (org-element-type (org-element-context)))))
  ;; Test forbidden characters inside $...$.
  (should-not
   (eq 'latex-fragment
       (org-test-with-temp-text "$.a$"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'latex-fragment
       (org-test-with-temp-text "$,a$"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'latex-fragment
       (org-test-with-temp-text "$;a$"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'latex-fragment
       (org-test-with-temp-text "$ a$"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'latex-fragment
       (org-test-with-temp-text "$a.$"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'latex-fragment
       (org-test-with-temp-text "$a,$"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'latex-fragment
       (org-test-with-temp-text "$a $"
	 (org-element-type (org-element-context)))))
  ;; Test $$...$$.
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "$$a$$"
	 (org-element-type (org-element-context)))))
  ;; Test \(...\).
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "\\(a\\)"
	 (org-element-type (org-element-context)))))
  ;; Test \[...\].
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "\\[a\\]"
	 (org-element-type (org-element-context)))))
  ;; Test fragment at the beginning of an item.
  (should
   (eq 'latex-fragment
       (org-test-with-temp-text "- $<point>x$"
	 (org-element-type (org-element-context))))))


;;;; Line Break

(ert-deftest test-org-element/line-break-parser ()
  "Test `line-break' parser."
  ;; Regular test.
  (should
   (org-test-with-temp-text "Text \\\\"
     (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  ;; Line break with trailing white spaces.
  (should
   (org-test-with-temp-text "Text \\\\  "
     (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  ;; Three backslashes are too much.
  (should-not
   (org-test-with-temp-text "Text \\\\\\"
     (org-element-map (org-element-parse-buffer) 'line-break 'identity))))


;;;; Link

(ert-deftest test-org-element/link-parser ()
  "Test `link' parser."
  ;; Radio target.
  (should
   (equal
    "radio"
    (org-test-with-temp-text "<<<radio>>>A radio link"
      (org-update-radio-target-regexp)
      (org-element-property
       :type
       (org-element-map (org-element-parse-buffer) 'link #'identity nil t)))))
  (should
   (equal
    "radio"
    (org-test-with-temp-text "<<<radio>>><<<radio2>>><<<foo>>>A radio link"
      (org-update-radio-target-regexp)
      (org-element-property
       :type
       (org-element-map (org-element-parse-buffer) 'link #'identity nil t)))))
  (should
   (equal
    "radio"
    (let ((org-target-link-regexp-limit 9))
      (org-test-with-temp-text "<<<radio>>><<<radio2>>><<<foo>>>A radio link"
        (org-update-radio-target-regexp)
        (org-element-property
         :type
         (org-element-map (org-element-parse-buffer) 'link #'identity nil t))))))
  ;; Pathological case: radio target of length 1 at beginning of line
  ;; not followed by spaces.
  (should
   (org-test-with-temp-text "* <<<a>>>\n<point>a-bug"
     (org-update-radio-target-regexp)
     (org-element-parse-buffer)))
  ;; Pathological case: radio target in an emphasis environment.
  (should
   (eq 'bold
       (org-test-with-temp-text "* <<<radio>>>\n<point>*radio*"
	 (org-update-radio-target-regexp)
	 (org-element-type (org-element-context)))))
  (should
   (eq 'link
       (org-test-with-temp-text "* <<<radio>>>\n*<point>radio*"
	 (org-update-radio-target-regexp)
	 (org-element-type (org-element-context)))))
  ;; Standard link.
  ;;
  ;; ... with description.
  (should
   (equal
    '("Orgmode.org")
    (org-test-with-temp-text "[[https://orgmode.org][Orgmode.org]]"
      (org-element-contents
       (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  ;; ... without description.
  (should
   (equal
    "https"
    (org-test-with-temp-text "[[https://orgmode.org]]"
      (org-element-property
       :type
       (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  ;; ... with expansion.
  (should
   (equal
    "//orgmode.org/worg"
    (org-test-with-temp-text "[[Org:worg]]"
      (let ((org-link-abbrev-alist '(("Org" . "https://orgmode.org/"))))
	(org-element-property
	 :path
	 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))))))
  ;; ... with translation.
  (should
   (equal
    "127.0.0.1"
    (org-test-with-temp-text "[[https://orgmode.org]]"
      (let ((org-link-translation-function
	     (lambda (type _) (cons type "127.0.0.1"))))
	(org-element-property
	 :path
	 (org-element-map (org-element-parse-buffer) 'link
	   #'identity nil t))))))
  ;; ... custom-id link.
  (should
   (equal
    "custom-id"
    (org-test-with-temp-text "[[#some-id]]"
      (org-element-property
       :type
       (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  ;; ... coderef link.
  (should
   (equal
    "coderef"
    (org-test-with-temp-text "[[(reference)]]"
      (org-element-property
       :type
       (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  ;; ... fuzzy link.
  (should
   (equal
    "fuzzy"
    (org-test-with-temp-text "[[target-or-title]]"
      (org-element-property
       :type
       (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  ;; ... file-type link with search option.
  (should
   (equal
    '(("file" "projects.org" "*task title"))
    (org-test-with-temp-text "[[file:projects.org::*task title]]"
      (org-element-map (org-element-parse-buffer) 'link
	(lambda (l) (list (org-element-property :type l)
		     (org-element-property :path l)
		     (org-element-property :search-option l)))))))
  ;; ... file-type link with application...
  (should
   (equal
    '("file" "projects.org" "emacs")
    (org-test-with-temp-text "[[file+emacs:projects.org]]"
      (let ((l (org-element-context)))
	(list (org-element-property :type l)
	      (org-element-property :path l)
	      (org-element-property :application l))))))
  ;; ... `:path' in a file-type link must be compatible with "file"
  ;; scheme in URI syntax, even if Org syntax isn't...
  (should
   (org-test-with-temp-text-in-file ""
     (let ((file (expand-file-name (buffer-file-name))))
       (insert (format "[[file://%s]]" file))
       (equal (org-element-property :path (org-element-context)) file))))
  (should
   (org-test-with-temp-text-in-file ""
     (let ((file (expand-file-name (buffer-file-name))))
       (insert (format "[[file:%s]]" file))
       (equal (org-element-property :path (org-element-context)) file))))
  ;; ... multi-line link.
  (should
   (equal "ls *.org"
	  (org-test-with-temp-text "[[shell:ls\n*.org]]"
	    (org-element-property :path (org-element-context)))))
  ;; Plain link.
  (should
   (org-test-with-temp-text "A link: https://orgmode.org"
     (org-element-map (org-element-parse-buffer) 'link 'identity)))
  ;; Angular link.  Follow RFC 3986.
  (should
   (eq 'link
       (org-test-with-temp-text "A link: <point><https://orgmode.org>"
	 (org-element-type (org-element-context)))))
  (should
   (equal "//orgmode.org"
	  (org-test-with-temp-text "A link: <point><https://orgmode\n.org>"
	    (org-element-property :path (org-element-context)))))
  ;; Link abbreviation.
  (should
   (equal "https"
	  (org-test-with-temp-text
	      "#+LINK: orgmode https://www.orgmode.org/\n[[orgmode:#docs]]"
	    (progn (org-mode-restart)
		   (goto-char (1- (point-max)))
		   (org-element-property :type (org-element-context))))))
  ;; Link abbreviation with spaces.
  (should
   (equal "https"
          (org-test-with-temp-text
              "#+LINK: \"Nu Html Checker\" https://validator.w3.org/nu/?doc=%h
              [[Nu Html Checker:test]]"
	    (progn (org-mode-restart)
		   (goto-char (1- (point-max)))
		   (org-element-property :type (org-element-context))))))
  ;; Link abbreviation in a secondary string.
  (should
   (equal "https"
	  (org-test-with-temp-text
	      "#+LINK: orgmode https://www.orgmode.org/\n* H [[orgmode:#docs]]"
	    (progn (org-mode-restart)
		   (org-element-map (org-element-parse-buffer) 'link
		     (lambda (link) (org-element-property :type link))
		     nil t nil t))))))


;;;; Macro

(ert-deftest test-org-element/macro-parser ()
  "Test `macro' parser."
  ;; Without arguments.
  (should
   (org-test-with-temp-text "{{{macro}}}"
     (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  ;; With arguments.
  (should
   (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
     (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  ;; Properly handle protected commas in arguments...
  (should
   (= 2
      (length
       (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
	 (org-element-property :args (org-element-context))))))
  ;; ... even when last argument ends with a protected comma.
  (should
   (equal '("C-,")
	  (org-test-with-temp-text "{{{macro(C-\\,)}}}"
	    (org-element-property :args (org-element-context)))))
  ;; Allow to escape escaping character.
  (should
   (equal '("C-\\" "")
	  (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
	    (org-element-property :args (org-element-context)))))
  ;; No need to escape backslashes elsewhere.
  (should
   (equal '("\\")
	  (org-test-with-temp-text "{{{macro(\\)}}}"
	    (org-element-property :args (org-element-context))))))


;;;; Node Property

(ert-deftest test-org-element/node-property ()
  "Test `node-property' parser."
  ;; Standard test.
  (should
   (equal '("abc" "value")
	  (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:abc: value\n:END:"
	    (let ((element (org-element-at-point)))
	      (list (org-element-property :key element)
		    (org-element-property :value element))))))
  ;; The insides of property blocks on document level are parsed the
  ;; same way as headline property blocks.  I.e. the concept of
  ;; `node-property' apply also for properties in those blocks.
  (should
   (equal '("abc" "value")
	  (org-test-with-temp-text ":PROPERTIES:\n<point>:abc: value\n:END:"
	    (let ((element (org-element-at-point)))
	      (list (org-element-property :key element)
		    (org-element-property :value element))))))
  ;; Value should be trimmed.
  (should
   (equal "value"
	  (org-test-with-temp-text
	      "* H\n:PROPERTIES:\n<point>:abc: value  \n:END:"
	    (org-element-property :value (org-element-at-point)))))
  ;; A node property requires to be wrapped within a property drawer.
  (should-not
   (eq 'node-property
       (org-test-with-temp-text ":abc: value"
	 (org-element-type (org-element-at-point)))))
  ;; Accept empty properties.
  (should
   (equal '(("foo" "value") ("bar" ""))
	  (org-test-with-temp-text "* H\n:PROPERTIES:\n:foo: value\n:bar:\n:END:"
	    (org-element-map (org-element-parse-buffer) 'node-property
	      (lambda (p)
		(list (org-element-property :key p)
		      (org-element-property :value p))))))))


;;;; Paragraph

(ert-deftest test-org-element/paragraph-parser ()
  "Test `paragraph' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text "Paragraph"
     (org-element-map (org-element-parse-buffer) 'paragraph 'identity nil t)))
  ;; Property find end of a paragraph stuck to another element.
  (should
   (eq ?#
       (org-test-with-temp-text "Paragraph\n# Comment"
	 (org-element-map (org-element-parse-buffer) 'paragraph
	   (lambda (p) (char-after (org-element-property :end p)))
	   nil t))))
  ;; Include ill-formed Keywords.
  (should
   (org-test-with-temp-text "#+wrong_keyword something"
     (org-element-map (org-element-parse-buffer) 'paragraph 'identity)))
  ;; Include incomplete-drawers.
  (should
   (org-test-with-temp-text "<point>:TEST:\nParagraph"
     (let ((elem (org-element-at-point)))
       (and (eq (org-element-type elem) 'paragraph)
	    (= (point-max) (org-element-property :end elem))))))
  (should
   (org-test-with-temp-text "<point>foo\n:end:\nbar"
     (let ((elem (org-element-at-point)))
       (and (eq (org-element-type elem) 'paragraph)
	    (= (point-max) (org-element-property :end elem))))))
  ;; Include incomplete blocks.
  (should
   (org-test-with-temp-text "<point>#+BEGIN_CENTER\nParagraph"
     (let ((elem (org-element-at-point)))
       (and (eq (org-element-type elem) 'paragraph)
	    (= (point-max) (org-element-property :end elem))))))
  (should
   (org-test-with-temp-text "<point>foo\n#+END_CENTER\nbar"
     (let ((elem (org-element-at-point)))
       (and (eq (org-element-type elem) 'paragraph)
	    (= (point-max) (org-element-property :end elem))))))
  ;; Include incomplete latex environments.
  (should
   (org-test-with-temp-text "<point>\begin{equation}\nParagraph"
     (let ((elem (org-element-at-point)))
       (and (eq (org-element-type elem) 'paragraph)
	    (= (point-max) (org-element-property :end elem))))))
  (should
   (org-test-with-temp-text "<point>Paragraph\n\begin{equation}"
     (let ((elem (org-element-at-point)))
       (and (eq (org-element-type elem) 'paragraph)
	    (= (point-max) (org-element-property :end elem))))))
  ;; Stop at affiliated keywords.
  (should
   (org-test-with-temp-text "Paragraph\n#+NAME: test\n| table |"
     (let ((elem (org-element-at-point)))
       (and (eq (org-element-type elem) 'paragraph)
	    (not (org-element-property :name elem))
	    (= (org-element-property :end elem) (line-beginning-position 2))))))
  (should
   (org-test-with-temp-text
       "Paragraph\n#+CAPTION[with short caption]: test\n| table |"
     (let ((elem (org-element-at-point)))
       (and (eq (org-element-type elem) 'paragraph)
	    (not (org-element-property :name elem))
	    (= (org-element-property :end elem) (line-beginning-position 2))))))
  ;; Do not steal affiliated keywords from container.
  (should
   (org-test-with-temp-text "#+ATTR_LATEX: test\n- item<point> 1"
     (let ((elem (org-element-at-point)))
       (and (eq (org-element-type elem) 'paragraph)
	    (not (org-element-property :attr_latex elem))
	    (/= (org-element-property :begin elem) 1)))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n  "
     (= (org-element-property :end (org-element-at-point)) (point-max))))
  (should
   (org-test-with-temp-text "#+BEGIN_CENTER\n<point>C\n#+END_CENTER\n  "
     (= (org-element-property :end (org-element-at-point))
        (save-excursion
          (search-forward "END")
          (line-beginning-position))))))

;;;; Plain List

(ert-deftest test-org-element/plain-list-parser ()
  "Test `plain-list' parser."
  (org-test-with-temp-text "- item"
    (should (org-element-map (org-element-parse-buffer) 'plain-list 'identity)))
  ;; Blank lines after a list or sub-list belongs to that list.
  (should
   (= 1
      (org-test-with-temp-text "- A\n\n- B\n\nEnd list"
	(org-element-property :post-blank (org-element-at-point)))))
  (should
   (= 1
      (org-test-with-temp-text "- A\n\n<point>  - B\n\n  - C\n\n  End sub-list"
	(org-element-property :post-blank (org-element-at-point)))))
  ;; Blank lines after the list only belong to outer plain list,
  ;; however.
  (should
   (equal
    '(t t)
    (org-test-with-temp-text "
- outer
  - inner

Outside list"
      (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
		       (lambda (pl) (org-element-property :end pl)))))
	(list
	 ;; Move to ending of outer list.
	 (progn (goto-char (car endings)) (looking-at "Outside list"))
	 ;; Move to ending of inner list.
	 (progn (goto-char (nth 1 endings)) (looking-at "^$")))))))
  ;; Correctly compute end of list if it doesn't end at a line
  ;; beginning.
  (should
   (org-test-with-temp-text "- list\n   \n   "
     (= (org-element-property :end (org-element-at-point)) (point-max))))
  ;; Correctly compute list ending when list is before first headline.
  (dolist (org-element-use-cache '(t nil))
    (org-test-with-temp-text "- list\n* Headline\n"
      (should (= (org-element-property :end (org-element-at-point)) 8)))))


;;;; Planning

(ert-deftest test-org-element/planning-parser ()
  "Test `planning' parser."
  ;; Test various keywords.
  (should
   (org-element-property
    :closed
    (org-test-with-temp-text "* H\n<point>CLOSED: [2012-03-29 thu.]"
      (org-element-at-point))))
  (should
   (org-element-property
    :deadline
    (org-test-with-temp-text "* H\n<point>DEADLINE: <2012-03-29 thu.>"
      (org-element-at-point))))
  (should
   (org-element-property
    :scheduled
    (org-test-with-temp-text "* H\n<point>SCHEDULED: <2012-03-29 thu.>"
      (org-element-at-point))))
  ;; Planning line only exists right after a headline.
  (should-not
   (eq 'planning
       (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
	 (org-element-type (org-element-at-point)))))
  (should-not
   (eq 'planning
       (org-test-with-temp-text
	   "* H\n# Comment\n<point>DEADLINE: <2012-03-29 thu.>"
	 (org-element-type (org-element-at-point)))))
  (should-not
   (eq 'planning
       (org-test-with-temp-text
	   "* H\n\n<point>DEADLINE: <2012-03-29 thu.>"
	 (org-element-type (org-element-at-point))))))


;;;; Property Drawer

(ert-deftest test-org-element/property-drawer-parser ()
  "Test `property-drawer' parser."
  ;; Standard test.
  (should
   (eq 'property-drawer
       (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value\n:END:"
	 (org-element-type (org-element-at-point)))))
  (should
   (eq 'property-drawer
       (org-test-with-temp-text
	   "* H\nDEADLINE: <2014-03-04 tue.>\n<point>:PROPERTIES:\n:prop: value\n:END:"
	 (org-element-type (org-element-at-point)))))
  ;; Parse property drawer at the beginning of the document, possibly
  ;; after some initial comments.
  (should
   (eq 'property-drawer
       (org-test-with-temp-text "<point>:PROPERTIES:\n:prop: value\n:END:"
	 (org-element-type (org-element-at-point)))))
  (should
   (eq 'property-drawer
       (org-test-with-temp-text "# C\n# C\n<point>:PROPERTIES:\n:prop: value\n:END:"
	 (org-element-type (org-element-at-point)))))
  (should
   (eq 'property-drawer
       (org-test-with-temp-text "\n<point>:PROPERTIES:\n:prop: value\n:END:"
	 (org-element-type (org-element-at-point)))))
  ;; Allow properties without value and no property at all.
  (should
   (eq 'property-drawer
       (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop:\n:END:"
	 (org-element-type (org-element-at-point)))))
  (should
   (eq 'property-drawer
       (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:"
	 (org-element-type (org-element-at-point)))))
  ;; Ignore incomplete drawer, drawer at a wrong location or with
  ;; wrong contents.
  (should-not
   (eq 'property-drawer
       (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value"
	 (org-element-type (org-element-at-point)))))
  (should-not
   (eq 'property-drawer
       (org-test-with-temp-text
	   "* H\nParagraph\n<point>:PROPERTIES:\n:prop: value\n:END:"
	 (org-element-type (org-element-at-point)))))
  (should-not
   (eq 'property-drawer
       (org-test-with-temp-text
	   "* H\nParagraph\n<point>:PROPERTIES:\nparagraph\n:END:"
	 (org-element-type (org-element-at-point)))))
  (should-not
   (eq 'property-drawer
       (org-test-with-temp-text
	   "* H\n\n<point>:PROPERTIES:\n:prop: value\n:END:"
	 (org-element-type (org-element-at-point)))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Quote Block

(ert-deftest test-org-element/quote-block-parser ()
  "Test `quote-block' parser."
  ;; Regular test.
  (should
   (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
     (org-element-map (org-element-parse-buffer) 'quote-block 'identity)))
  ;; Ignore incomplete block.
  (should-not
   (org-test-with-temp-text "#+BEGIN_QUOTE"
     (org-element-map (org-element-parse-buffer) 'quote-block 'identity nil t)))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN_QUOTE\nC\n#+END_QUOTE\n  "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Radio Target

(ert-deftest test-org-element/radio-target-parser ()
  "Test `radio-target' parser."
  ;; Standard test.
  (should
   (eq 'radio-target
       (org-test-with-temp-text "<<<radio>>>"
	 (org-element-type (org-element-context)))))
  ;; Radio targets with objects.
  (should
   (eq 'radio-target
       (org-test-with-temp-text "<<<radio \\alpha>>>"
	 (org-element-type (org-element-context)))))
  ;; Radio targets starting with an object.
  (should
   (eq 'radio-target
       (org-test-with-temp-text "<<<\\alpha radio>>>"
	 (org-element-type (org-element-context)))))
  ;; Radio targets cannot begin or end with white space.
  (should-not
   (eq 'radio-target
       (org-test-with-temp-text "<<< radio>>>"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'radio-target
       (org-test-with-temp-text "<<<radio >>>"
	 (org-element-type (org-element-context))))))


;;;; Section

(ert-deftest test-org-element/section-parser ()
  "Test `section' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text "* Headline\nText"
     (org-element-map (org-element-parse-buffer) 'section 'identity)))
  ;; There's a section before the first headline.
  (should
   (org-test-with-temp-text "Text"
     (org-element-map (org-element-parse-buffer) 'section 'identity)))
  ;; A section cannot be empty.
  (should-not
   (org-test-with-temp-text "* Headline 1\n* Headline 2"
     (org-element-map (org-element-parse-buffer) 'section 'identity)))
  ;; A section doesn't contain sub-trees.
  (should-not
   (org-test-with-temp-text "* Head\nText\n** Sub-Head"
     (org-element-map
      (org-element-map (org-element-parse-buffer) 'section 'identity nil t)
      'headline 'identity))))


;;;; Special Block

(ert-deftest test-org-element/special-block-parser ()
  "Test `special-block' parser."
  ;; Standard test.
  (should
   (equal "SPECIAL"
	  (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
	    (org-element-property :type (org-element-at-point)))))
  ;; Special blocks are case sensitive.
  (should
   (equal "CaSe"
	  (org-test-with-temp-text "#+BEGIN_CaSe\nText\n#+END_CaSe"
	    (org-element-property :type (org-element-at-point)))))
  ;; Special blocks can contain paragraphs.
  (should
   (eq 'paragraph
       (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
	 (forward-line)
	 (org-element-type (org-element-at-point)))))
  ;; Ignore incomplete block.
  (should-not
   (eq 'special-block
       (org-test-with-temp-text "#+BEGIN_SPECIAL"
	 (org-element-type (org-element-at-point)))))
  ;; Allow special characters in type.
  (should
   (equal '(special-block "SPECIAL*")
	  (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
	    (let ((element (org-element-at-point)))
	      (list (org-element-type element)
		    (org-element-property :type element))))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n "
     (= (org-element-property :end (org-element-at-point)) (point-max))))
  ;; When contents is empty, the parsed contents is nil.
  (should
   (org-test-with-temp-text "#+BEGIN_SPECIAL\n#+END_SPECIAL"
     (eq nil (org-element-contents (org-element-at-point)))))
  ;; Parse parameters if any, trimming blanks.
  (should
   (org-test-with-temp-text "#+BEGIN_SPECIAL* s  p   :w 3   \nContent.\n#+END_SPECIAL*"
     (equal "s  p   :w 3"
	    (org-element-property :parameters (org-element-at-point)))))
  ;; When parameters is blank, `:parameters' is nil.
  (should
   (org-test-with-temp-text "#+BEGIN_SPECIAL*     \t   \nContent.\n#+END_SPECIAL*"
     (eq nil (org-element-property :parameters (org-element-at-point))))
   ))




;;;; Src Block

(ert-deftest test-org-element/src-block-parser ()
  "Test `src-block' parser."
  ;; Regular tests.
  (should
   (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
     (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  ;; Ignore incomplete block.
  (should-not
   (org-test-with-temp-text "#+BEGIN_SRC"
     (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  ;; Properly un-escape code.
  (should
   (equal "* Headline\n #+keyword\nText\n"
	  (org-test-with-temp-text
	      "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
	    (org-element-property :value (org-element-at-point)))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\nC\n#+END_SRC\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Statistics Cookie

(ert-deftest test-org-element/statistics-cookie ()
  "Test `statistics-cookie' parser."
  ;; With numbers.
  (should
   (org-test-with-temp-text "[1/2]"
     (org-element-map (org-element-parse-buffer) 'statistics-cookie 'identity)))
  ;; With percents.
  (should
   (org-test-with-temp-text "[33%]"
     (org-element-map
      (org-element-parse-buffer) 'statistics-cookie 'identity))))


;;;; Strike Through

(ert-deftest test-org-element/strike-through-parser ()
  "Test `strike-through' parser."
  ;; Regular test.
  (should
   (org-test-with-temp-text "+strike-through+"
     (org-element-map (org-element-parse-buffer) 'strike-through #'identity)))
  ;; Multi-line markup.
  (should
   (equal
    (org-element-contents
     (org-test-with-temp-text "+first line\nsecond line+"
       (org-element-map
	   (org-element-parse-buffer) 'strike-through #'identity nil t)))
    '("first line\nsecond line"))))


;;;; Subscript

(ert-deftest test-org-element/subscript-parser ()
  "Test `subscript' parser."
  ;; Without braces.
  (should
   (org-test-with-temp-text "a_b"
     (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  ;; With braces.
  (should
   (org-test-with-temp-text "a_{b}"
     (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  ;; Multiple subscripts in a paragraph.
  (should
   (= 2
      (org-test-with-temp-text "a_b and c_d"
	(length
	 (org-element-map (org-element-parse-buffer) 'subscript 'identity))))))


;;;; Superscript

(ert-deftest test-org-element/superscript-parser ()
  "Test `superscript' parser."
  ;; Without braces.
  (should
   (org-test-with-temp-text "a^b"
     (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  ;; With braces.
  (should
   (org-test-with-temp-text "a^{b}"
     (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  ;; Multiple superscript in a paragraph.
  (should
   (= 2
      (org-test-with-temp-text "a^b and c^d"
	(length
	 (org-element-map
	  (org-element-parse-buffer) 'superscript 'identity))))))


;;;; Table

(ert-deftest test-org-element/table-parser ()
  "Test `table' parser."
  (should
   (org-test-with-temp-text "| a |"
     (org-element-map (org-element-parse-buffer) 'table 'identity)))
  ;; TBLFM keyword is case insensitive.
  (should
   (org-test-with-temp-text "| a |\n#+tblfm: test"
     (org-element-property
      :tblfm
      (org-element-map (org-element-parse-buffer) 'table 'identity nil t))))
  ;; Handle multiple TBLFM lines.
  (should
   (= 2
      (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
	(length (org-element-property
		 :tblfm
		 (org-element-map
		     (org-element-parse-buffer) 'table 'identity nil t))))))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "| a |\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))


;;;; Table Cell

(ert-deftest test-org-element/table-cell-parser ()
  "Test `table-cell' parser."
  ;; Regular table cell.
  (should
   (org-test-with-temp-text "| a |"
     (org-element-map (org-element-parse-buffer) 'table-cell 'identity)))
  ;; Last vertical bar may be omitted.
  (should
   (org-test-with-temp-text "| a "
     (org-element-map (org-element-parse-buffer) 'table-cell 'identity))))


;;;; Table Row

(ert-deftest test-org-element/table-row-parser ()
  "Test `table-row' parser."
  (should
   (equal '(standard rule)
	  (org-test-with-temp-text "| a |\n|---|"
	    (org-element-map
	     (org-element-parse-buffer) 'table-row
	     (lambda (row) (org-element-property :type row)))))))


;;;; Target

(ert-deftest test-org-element/target-parser ()
  "Test `target' parser."
  (should
   (org-test-with-temp-text "<<target>>"
     (org-element-map (org-element-parse-buffer) 'target 'identity))))


;;;; Timestamp

(ert-deftest test-org-element/timestamp-parser ()
  "Test `timestamp' parser."
  ;; Active timestamp.
  (should
   (org-test-with-temp-text "<2012-03-29 16:40>"
     (eq (org-element-property :type (org-element-context)) 'active)))
  (should-not
   (org-test-with-temp-text "<2012-03-29 Thu>"
     (let ((timestamp (org-element-context)))
       (or (org-element-property :hour-start timestamp)
	   (org-element-property :minute-start timestamp)))))
  (should
   (equal '(2012 3 29 16 40)
	  (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
	    (let ((object (org-element-context)))
	      (list (org-element-property :year-start object)
		    (org-element-property :month-start object)
		    (org-element-property :day-start object)
		    (org-element-property :hour-start object)
		    (org-element-property :minute-start object))))))
  ;; Inactive timestamp.
  (should
   (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
     (eq (org-element-property :type (org-element-context)) 'inactive)))
  ;; Time range.
  (should
   (equal '(2012 3 29 16 40 7 30)
	  (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
	    (let ((object (org-element-context)))
	      (list (org-element-property :year-end object)
		    (org-element-property :month-end object)
		    (org-element-property :day-end object)
		    (org-element-property :hour-end object)
		    (org-element-property :minute-end object)
		    (org-element-property :hour-start object)
		    (org-element-property :minute-start object))))))
  (should
   (eq 'active-range
       (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
	 (org-element-property :type (org-element-context)))))
  ;; Date range.
  (should
   (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
     (eq (org-element-property :type (org-element-context)) 'inactive-range)))
  (should-not
   (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
     (let ((timestamp (org-element-context)))
       (or (org-element-property :hour-end timestamp)
	   (org-element-property :minute-end timestamp)))))
  ;; With repeater, repeater deadline, warning delay and combinations.
  (should
   (eq 'catch-up
       (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
	 (org-element-property :repeater-type (org-element-context)))))
  (should
   (equal '(catch-up 2 year)
       (org-test-with-temp-text "<2012-03-29 Thu ++1y/2y>"
         (let ((ts (org-element-context)))
           (list (org-element-property :repeater-type ts)
                 (org-element-property :repeater-deadline-value ts)
                 (org-element-property :repeater-deadline-unit ts))))))
  (should
   (eq 'first
       (org-test-with-temp-text "<2012-03-29 Thu --1y>"
	 (org-element-property :warning-type (org-element-context)))))
  (should
   (equal '(cumulate all)
	  (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
	    (let ((ts (org-element-context)))
	      (list (org-element-property :repeater-type ts)
		    (org-element-property :warning-type ts))))))
  (should
   (equal '(cumulate all 2 year)
          (org-test-with-temp-text "<2012-03-29 Thu +1y/2y -1y>"
            (let ((ts (org-element-context)))
              (list (org-element-property :repeater-type ts)
                    (org-element-property :warning-type ts)
                    (org-element-property :repeater-deadline-value ts)
                    (org-element-property :repeater-deadline-unit ts))))))
  ;; :range-type property
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    nil))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun 12:00>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    nil))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun 12:00-13:00>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'timerange))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun 12:00-12:00>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'timerange))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun>--<2023-07-02 Sun>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'daterange))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun>--<2023-07-03 Mon>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'daterange))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun 12:00>--<2023-07-02 Sun 12:00>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'daterange))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun 12:00>--<2023-07-03 Mon 13:00>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'daterange))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun 12:00>--<2023-07-02 Sun>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'daterange))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun 12:00>--<2023-07-03 Mon>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'daterange))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun 12:00>--<2023-07-02 Sun 13:00>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'daterange))
  (should
   (eq
    (org-test-with-temp-text "<2023-07-02 Sun 12:00 +5d>--<2023-07-02 Sun 13:00>"
      (org-element-property :range-type (org-element-timestamp-parser)))
    'daterange)))

;;;; Underline

(ert-deftest test-org-element/underline-parser ()
  "Test `underline' parser."
  ;; Regular test.
  (should
   (org-test-with-temp-text "_underline_"
     (org-element-map (org-element-parse-buffer) 'underline #'identity)))
  ;; Multi-line markup.
  (should
   (equal
    (org-element-contents
     (org-test-with-temp-text "_first line\nsecond line_"
       (org-element-map
	   (org-element-parse-buffer) 'underline #'identity nil t)))
    '("first line\nsecond line")))
  ;; Nested underlines.
  (should
   (= 2
      (org-test-with-temp-text "__test__"
	(length
	 (org-element-map (org-element-parse-buffer) 'underline 'identity)))))
  ;; Starting after non-blank
  (should
   (eq 'underline
       (org-test-with-temp-text "(_under<point>line_)"
         (org-element-type (org-element-context)))))
  (should-not
   (eq 'underline
       (org-test-with-temp-text "x_under<point>line_)"
         (org-element-type (org-element-context))))))


;;;; Verbatim

(ert-deftest test-org-element/verbatim-parser ()
  "Test `verbatim' parser."
  ;; Regular test.
  (should
   (org-test-with-temp-text "=verbatim="
     (org-element-map (org-element-parse-buffer) 'verbatim #'identity)))
  ;; Multi-line markup.
  (should
   (equal
    (org-element-property
     :value
     (org-test-with-temp-text "=first line\nsecond line="
       (org-element-map (org-element-parse-buffer) 'verbatim #'identity nil t)))
    "first line\nsecond line")))


;;;; Verse Block

(ert-deftest test-org-element/verse-block-parser ()
  "Test `verse-block' parser."
  ;; Standard test.
  (should
   (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
     (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  ;; Ignore case.
  (should
   (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
     (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  ;; Parse objects in verse blocks.
  (should
   (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
     (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  ;; Ignore incomplete verse block.
  (should-not
   (org-test-with-temp-text "#+BEGIN_VERSE"
     (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)))
  ;; Handle non-empty blank line at the end of buffer.
  (should
   (org-test-with-temp-text "#+BEGIN_VERSE\nC\n#+END_VERSE\n "
     (= (org-element-property :end (org-element-at-point)) (point-max)))))

;;; Org data.

(ert-deftest test-org-element/org-data-parser ()
  "Test `org-data' parser."
  ;; Standard test.
  (org-test-with-temp-text "This is test."
    (let ((data (org-element-lineage (org-element-at-point) 'org-data)))
      (should (equal 1 (org-element-begin data)))
      (should (equal (point-max) (org-element-end data)))))
  ;; Parse top-level property drawer.
  (should
   (equal
    "bar"
    (org-test-with-temp-text ":PROPERTIES:
:FOO: bar
:END:"
      (org-element-property-inherited :FOO (org-element-at-point)))))
  ;; With leading comment line.
  (org-test-with-temp-text "# comment
:PROPERTIES:
:FOO: bar
:END:"
    (should (equal "bar" (org-element-property-inherited :FOO (org-element-at-point)))))
  ;; Blank line on top.
  (should
   (equal
    "bar"
    (org-test-with-temp-text "
:PROPERTIES:
:FOO: bar<point>
:END:"
      (org-element-property-inherited :FOO (org-element-at-point))))))

\f
;;; Test Interpreters.

(ert-deftest test-org-element/interpret-data ()
  "Test `org-element-interpret-data' specifications."
  ;; Interpret simple affiliated keywords.
  (should
   (equal
    (org-element-interpret-data
     '(org-data nil (paragraph (:name "para") "Paragraph")))
    "#+name: para\nParagraph\n"))
  ;; Interpret multiple affiliated keywords.
  (should
   (equal
    (org-element-interpret-data
     '(org-data nil (paragraph (:attr_ascii ("line1" "line2")) "Paragraph")))
    "#+attr_ascii: line1\n#+attr_ascii: line2\nParagraph\n"))
  ;; Interpret parsed affiliated keywords.
  (should
   (equal
    (org-element-interpret-data
     '(org-data nil (paragraph (:caption (("caption"))) "Paragraph")))
    "#+caption: caption\nParagraph\n"))
  ;; Interpret dual affiliated keywords.
  (should
   (equal
    (org-element-interpret-data
     '(org-data nil (paragraph (:caption ((("long") "short"))) "Paragraph")))
    "#+caption[short]: long\nParagraph\n"))
  ;; Interpret multiple parsed dual keywords.
  (should
   (equal
    (org-element-interpret-data
     '(org-data nil (paragraph
		     (:caption ((("l1") "s1") (("l2") "s2"))) "Paragraph")))
    "#+caption[s1]: l1\n#+caption[s2]: l2\nParagraph\n"))
  ;; Pseudo objects and elements are transparent.
  (should
   (equal "A B"
	  (org-trim
	   (org-element-interpret-data
	    '(paragraph nil (pseudo-object (:post-blank 1) "A") "B")))))
  (should
   (equal "A\n\nB\n"
	  (org-element-interpret-data
	   '(center nil
		    (pseudo-element (:post-blank 1) (paragraph nil "A"))
		    (paragraph nil "B")))))
  ;; Obey post-blank property in strings.
  (should
   (equal "A "
	  (org-element-interpret-data
           (org-element-put-property "A" :post-blank 1)))))

(ert-deftest test-org-element/center-block-interpreter ()
  "Test center block interpreter."
  (should
   (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
	  "#+begin_center\nTest\n#+end_center\n")))

(ert-deftest test-org-element/drawer-interpreter ()
  "Test drawer interpreter."
  (should
   (equal (org-test-parse-and-interpret ":TEST:\nTest\n:END:")
	  ":TEST:\nTest\n:END:\n")))

(ert-deftest test-org-element/dynamic-block-interpreter ()
  "Test dynamic block interpreter."
  (should
   (equal (org-test-parse-and-interpret
	   "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
	  "#+begin: myblock :parameter value1\nTest\n#+end:\n")))

(ert-deftest test-org-element/footnote-definition-interpreter ()
  "Test footnote definition interpreter."
  (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n"))
  ;; Handle `:pre-blank' in definitions.
  (should
   (equal (org-test-parse-and-interpret "[fn:1]\nparagraph")
	  "[fn:1]\nparagraph\n"))
  (should
   (equal (org-test-parse-and-interpret "[fn:1]\n\nparagraph")
	  "[fn:1]\n\nparagraph\n")))

(ert-deftest test-org-element/headline-interpreter ()
  "Test headline and section interpreters."
  ;; 1. Standard test.
  (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
  ;; 2. With TODO keywords.
  (should
   (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
	    (org-test-parse-and-interpret "* TODO Headline"))
	  "* TODO Headline\n"))
  ;; 3. With tags...
  ;;
  ;; 3.1. ... and a positive `org-tags-column' value.
  (should
   (equal (let ((org-tags-column 20))
	    (org-test-parse-and-interpret "* Headline :tag:"))
	  "* Headline          :tag:\n"))
  ;; 3.2. ... and a negative `org-tags-column' value.
  (should
   (equal (let ((org-tags-column -20))
	    (org-test-parse-and-interpret "* Headline :tag:"))
	  "* Headline     :tag:\n"))
  ;; 3.3. ... and a null `org-tags-column' value.
  (should
   (equal (let ((org-tags-column 0))
	    (org-test-parse-and-interpret "* Headline     :tag:"))
	  "* Headline :tag:\n"))
  ;; 4. With priority cookie.
  (should
   (equal (org-test-parse-and-interpret "* [#B] Headline")
	  "* [#B] Headline\n"))
  ;; 5. With comment keyword.
  (should
   (equal (let ((org-comment-string "COMMENT"))
	    (org-test-parse-and-interpret "* COMMENT Headline"))
	  "* COMMENT Headline\n"))
  ;; 6. Keep same number of blank lines before body.
  (should
   (equal (org-test-parse-and-interpret
	   "* Headline\n\n\nText after two blank lines.")
	  "* Headline\n\n\nText after two blank lines.\n"))
  ;; 8. Preserve `org-odd-levels-only' state.
  (should
   (equal "* H\n*** H2\n"
	  (let ((org-odd-levels-only t))
	    (org-test-parse-and-interpret "* H\n*** H2")))))

(ert-deftest test-org-element/inlinetask-interpreter ()
  "Test inlinetask interpretation."
  (when (featurep 'org-inlinetask)
    (let ((org-inlinetask-min-level 15))
      ;; 1. Regular inlinetask.
     (should (equal (org-test-parse-and-interpret
		     "*************** Task\nTest\n*************** END")
		    "*************** Task\nTest\n*************** end\n"))
     ;; 2. Degenerate inlinetask.
     (should (equal (org-test-parse-and-interpret "*************** Task")
		    "*************** Task\n"))
     ;; 3. Prefer degenerate form when there are no contents.
     (should (equal (org-test-parse-and-interpret
		     "*************** Task\n*************** end")
		    "*************** Task\n"))
     ;; 4. With TODO keywords.
     (should
      (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
	       (org-test-parse-and-interpret "*************** TODO Task"))
	     "*************** TODO Task\n"))
     ;; 5. With tags...
     ;;
     ;; 5.1. ... and a positive `org-tags-column' value.
     (should
      (equal (let ((org-tags-column 30))
	       (org-test-parse-and-interpret "*************** Task :tag:"))
	     "*************** Task          :tag:\n"))
     ;; 5.2. ... and a negative `org-tags-column' value.
     (should
      (equal (let ((org-tags-column -30))
	       (org-test-parse-and-interpret "*************** Task :tag:"))
	     "*************** Task     :tag:\n"))
     ;; 5.3. ... and a null `org-tags-column' value.
     (should
      (equal (let ((org-tags-column 0))
	       (org-test-parse-and-interpret "*************** Task     :tag:"))
	     "*************** Task :tag:\n"))
     ;; 6. With priority cookie.
     (should
      (equal (org-test-parse-and-interpret "*************** [#B] Task")
	     "*************** [#B] Task\n")))))

(ert-deftest test-org-element/plain-list-interpreter ()
  "Test plain-list and item interpreters."
  (let ((org-list-two-spaces-after-bullet-regexp nil))
    ;; Unordered list.
    (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
    ;; Description list.
    (should
     (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
    ;; Ordered list.
    (should
     (equal (let ((org-plain-list-ordered-item-terminator t))
	      (org-test-parse-and-interpret "1. Item"))
	    "1. Item\n"))
    (should
     (equal (let ((org-plain-list-ordered-item-terminator ?\)))
	      (org-test-parse-and-interpret "1) Item"))
	    "1) Item\n"))
    ;; Ordered list with counter.
    (should
     (equal (let ((org-plain-list-ordered-item-terminator t))
	      (org-test-parse-and-interpret "1. [@5] Item"))
	    "5. [@5] Item\n"))
    ;; List with check-boxes.
    (should
     (equal (org-test-parse-and-interpret
	     "- [-] Item 1\n  - [X] Item 2\n  - [ ] Item 3")
	    "- [-] Item 1\n  - [X] Item 2\n  - [ ] Item 3\n"))
    ;; Item not starting with a paragraph.
    (should
     (equal (org-test-parse-and-interpret "-\n  | a | b |")
	    "- \n  | a | b |\n"))
    ;; Handle `:pre-blank' in items.
    (should
     (equal (org-test-parse-and-interpret "-\n  paragraph")
	    "- \n  paragraph\n"))
    (should
     (equal (org-test-parse-and-interpret "-\n\n  paragraph")
	    "- \n\n  paragraph\n"))
    ;; Special case: correctly handle "*" bullets.
    (should (org-test-parse-and-interpret " * item"))
    ;; Special case: correctly handle empty items.
    (should (org-test-parse-and-interpret "-"))))

(ert-deftest test-org-element/quote-block-interpreter ()
  "Test quote block interpreter."
  (should (equal (org-test-parse-and-interpret
		  "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
		 "#+begin_quote\nTest\n#+end_quote\n")))

(ert-deftest test-org-element/special-block-interpreter ()
  "Test special block interpreter."
  ;; No parameters
  (should (equal (org-test-parse-and-interpret
		  "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
		 "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n"))
  ;; No content
  (should (equal (org-test-parse-and-interpret
		  "#+BEGIN_SPECIAL\n#+END_SPECIAL")
		 "#+begin_SPECIAL\n#+end_SPECIAL\n"))
  ;; Some parameters
  (should
   (equal (org-test-parse-and-interpret
           "#+BEGIN_special some parameters until EOL\nA very special content\n#+END_special")
          "#+begin_special some parameters until EOL\nA very special content\n#+end_special\n"))
  ;; No parameters (blanks only)
  (should
   (equal (org-test-parse-and-interpret
           "#+BEGIN_special   \t \nA very special content\n#+END_special")
          "#+begin_special\nA very special content\n#+end_special\n"))
  ;; Some parameters with leading and trailing blanks, no content, and
  ;; a /special/ name.
  (should
   (equal (org-test-parse-and-interpret
           "#+BEGIN_spe<c>ial  :a  :b \t  :c  \t \n#+END_spe<c>ial")
          "#+begin_spe<c>ial :a  :b \t  :c\n#+end_spe<c>ial\n")))

(ert-deftest test-org-element/babel-call-interpreter ()
  "Test Babel call interpreter."
  ;; Without argument.
  (should (equal (org-test-parse-and-interpret "#+CALL: test()")
		 "#+call: test()\n"))
  ;; With argument.
  (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
		 "#+call: test(x=2)\n"))
  ;; With header arguments.
  (should (equal (org-test-parse-and-interpret
		  "#+CALL: test[:results output]() :results html")
		 "#+call: test[:results output]() :results html\n")))

(ert-deftest test-org-element/clock-interpreter ()
  "Test clock interpreter."
  ;; Running clock.
  (should
   (string-match
    "CLOCK: \\[2012-01-01 .* 00:01\\]"
    (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]")))
  ;; Closed clock.
  (should
   (string-match
    "CLOCK: \\[2012-01-01 .* 00:01\\]--\\[2012-01-01 .* 00:02\\] =>  0:01"
    (org-test-parse-and-interpret "
CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] =>  0:01")))
  ;; Closed clock without timestamp.
  (should
   (string-match
    "CLOCK:  =>  0:01"
    (org-test-parse-and-interpret "CLOCK: => 0:01"))))

(ert-deftest test-org-element/comment-interpreter ()
  "Test comment interpreter."
  ;; Regular comment.
  (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
  ;; Inline comment.
  (should (equal (org-test-parse-and-interpret "  # Comment")
		 "# Comment\n"))
  ;; Preserve indentation.
  (should (equal (org-test-parse-and-interpret "  # No blank\n#  One blank")
		 "# No blank\n#  One blank\n")))

(ert-deftest test-org-element/comment-block-interpreter ()
  "Test comment block interpreter."
  (should (equal (org-test-parse-and-interpret
		  "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
		 "#+begin_comment\nTest\n#+end_comment\n"))
  ;; Accept missing final newline in value.
  (should
   (equal "#+begin_comment\nTest\n#+end_comment\n"
	  (org-element-interpret-data '(comment-block (:value "Test"))))))

(ert-deftest test-org-element/diary-sexp ()
  "Test diary-sexp interpreter."
  (should
   (equal
    (org-test-parse-and-interpret
     "%%(org-anniversary 1956  5 14)(2) Arthur Dent is %d years old")
    "%%(org-anniversary 1956  5 14)(2) Arthur Dent is %d years old\n")))

(ert-deftest test-org-element/example-block-interpreter ()
  "Test example block interpreter."
  ;; Without switches.
  (should (equal "#+begin_example\nTest\n#+end_example\n"
		 (let ((org-src-preserve-indentation t))
		   (org-test-parse-and-interpret
		    "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
  ;; With switches.
  (should
   (equal "#+begin_example -n -k\n(+ 1 1)\n#+end_example\n"
	  (let ((org-src-preserve-indentation t))
	    (org-test-parse-and-interpret
	     "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE"))))
  ;; Preserve code escaping.
  (should
   (equal
    (let ((org-src-preserve-indentation t))
      (org-test-parse-and-interpret
       "#+BEGIN_EXAMPLE\n,* Headline\n,#+KEYWORD: value\nText\n#+END_EXAMPLE"))
    "#+begin_example\n,* Headline\n,#+KEYWORD: value\nText\n#+end_example\n"))
  ;; Accept missing final newline in value.
  (should
   (equal
    "#+begin_example\nTest\n#+end_example\n"
    (let ((org-src-preserve-indentation t))
      (org-element-interpret-data '(example-block (:value "Test"))))))
  ;; Handle indentation.
  (should (equal "#+begin_example\n  Test\n#+end_example\n"
		 (let ((org-src-preserve-indentation nil)
		       (org-edit-src-content-indentation 2))
		   (org-test-parse-and-interpret
		    "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
  (should (equal "#+begin_example\n Test\n#+end_example\n"
		 (let ((org-src-preserve-indentation t)
		       (org-edit-src-content-indentation 2))
		   (org-test-parse-and-interpret
		    "#+BEGIN_EXAMPLE\n Test\n#+END_EXAMPLE")))))

(ert-deftest test-org-element/export-block-interpreter ()
  "Test export block interpreter."
  (should (equal (org-test-parse-and-interpret
		  "#+begin_export HTML\nTest\n#+end_export")
		 "#+begin_export HTML\nTest\n#+end_export\n")))

(ert-deftest test-org-element/fixed-width-interpreter ()
  "Test fixed width interpreter."
  ;; Standard test.
  (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
  ;; Preserve indentation.
  (should (equal (org-test-parse-and-interpret ":  2 blanks\n: 1 blank")
		 ":  2 blanks\n: 1 blank\n"))
  ;; Handle empty string.
  (should
   (equal (org-element-fixed-width-interpreter
	   '(fixed-width (:value "")) nil)
	  ":\n"))
  ;; Handle nil value.
  (should-not
   (org-element-fixed-width-interpreter
    '(fixed-width (:value nil)) nil)))

(ert-deftest test-org-element/horizontal-rule-interpreter ()
  "Test horizontal rule interpreter."
  (should (equal (org-test-parse-and-interpret "-------") "-----\n")))

(ert-deftest test-org-element/keyword-interpreter ()
  "Test keyword interpreter."
  (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
		 "#+keyword: value\n")))

(ert-deftest test-org-element/latex-environment-interpreter ()
  "Test latex environment interpreter."
  (should (equal (org-test-parse-and-interpret
		  "\\begin{equation}\n1+1=2\n\\end{equation}")
		 "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
  (should (equal (org-test-parse-and-interpret
		  "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
		 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))

(ert-deftest test-org-element/planning-interpreter ()
  "Test planning interpreter."
  (should
   (string-match
    "\\* Headline
DEADLINE: <2012-03-29 .*?> SCHEDULED: <2012-03-29 .*?> CLOSED: \\[2012-03-29 .*?\\]"
    (org-test-parse-and-interpret
     "* Headline
DEADLINE: <2012-03-29 thu.> SCHEDULED: <2012-03-29 thu.> CLOSED: [2012-03-29 thu.]"))))

(ert-deftest test-org-element/property-drawer-interpreter ()
  "Test property drawer interpreter."
  (should (equal (let ((org-property-format "%-10s %s"))
		   (org-test-parse-and-interpret
		    "* H\n:PROPERTIES:\n:prop: value\n:END:"))
		 "* H\n:PROPERTIES:\n:prop:     value\n:END:\n")))

(ert-deftest test-org-element/src-block-interpreter ()
  "Test src block interpreter."
  ;; With arguments.
  (should
   (equal (let ((org-edit-src-content-indentation 2)
		(org-src-preserve-indentation nil))
	    (org-test-parse-and-interpret
	     "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
	  "#+begin_src emacs-lisp :results silent\n  (+ 1 1)\n#+end_src\n"))
  ;; With switches.
  (should
   (equal (let ((org-edit-src-content-indentation 2)
		(org-src-preserve-indentation nil))
	    (org-test-parse-and-interpret
	     "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
	  "#+begin_src emacs-lisp -n -k\n  (+ 1 1)\n#+end_src\n"))
  ;; Preserve code escaping.
  (should
   (equal
    (let ((org-edit-src-content-indentation 2)
	  (org-src-preserve-indentation nil))
      (org-test-parse-and-interpret
       "#+BEGIN_SRC org\n,* Headline\n,#+KEYWORD: value\nText\n#+END_SRC"))
    "#+begin_src org\n  ,* Headline\n  ,#+KEYWORD: value\n  Text\n#+end_src\n"))
  ;; Do not apply `org-edit-src-content-indentation' when preserving
  ;; indentation.
  (should
   (equal (let ((org-edit-src-content-indentation 2)
		(org-src-preserve-indentation t))
	    (org-test-parse-and-interpret
	     "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
	  "#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src\n"))
  (should
   (equal (let ((org-edit-src-content-indentation 2)
		(org-src-preserve-indentation nil))
	    (org-test-parse-and-interpret
	     "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
	  "#+begin_src emacs-lisp -i\n(+ 1 1)\n#+end_src\n"))
  ;; Accept missing final newline in value.
  (should
   (equal
    "#+begin_src emacs-lisp\n  Test\n#+end_src\n"
    (let ((org-edit-src-content-indentation 2)
	  (org-src-preserve-indentation nil))
      (org-element-interpret-data
       '(src-block (:language "emacs-lisp" :value "Test")))))))

(ert-deftest test-org-element/table-interpreter ()
  "Test table, table-row and table-cell interpreters."
  ;; 1. Simple table.
  (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
		 "| a | b |\n| c | d |\n"))
  ;; 2. With horizontal rules.
  (should (equal (org-test-parse-and-interpret
		  "| a | b |\n|---+---|\n| c | d |")
		 "| a | b |\n|---+---|\n| c | d |\n"))
  ;; 3. With meta-data.
  (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
		 "| / | < | > |\n| * | 1 | 2 |\n"))
  ;; 4. With a formula.
  (should
   (equal (org-test-parse-and-interpret
	   "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
	  "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
  ;; 5. With multiple formulas.
  (should
   (equal (org-test-parse-and-interpret
	   "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
	  "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))

(ert-deftest test-org-element/timestamp-interpreter ()
  "Test timestamp interpreter."
  ;; Active.
  (should
   (string-match "<2012-03-29 .* 16:40>"
		 (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")))
  (should
   (string-match "<2012-03-29 .* 16:40>"
		 (org-element-timestamp-interpreter
		  '(timestamp
		    (:type active :year-start 2012 :month-start 3 :day-start 29
			   :hour-start 16 :minute-start 40)) nil)))
  (should
   (string-match
    "<2012-03-29 .* 16:40>"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active :year-start 2012 :month-start 3 :day-start 29
	      :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
	      :day-end 29 :hour-end 16 :minute-end 40)) nil)))
  ;; Inactive.
  (should
   (string-match "\\[2012-03-29 .* 16:40\\]"
		 (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")))
  (should
   (string-match
    "\\[2012-03-29 .* 16:40\\]"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type inactive :year-start 2012 :month-start 3 :day-start 29
	      :hour-start 16 :minute-start 40)) nil)))
  ;; Active daterange.
  (should
   (string-match "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
		 (org-test-parse-and-interpret
		  "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")))
  ;;; No end time, dates are not equal
  (should
   ;; Expected result: "<2012-03-29 Thu 16:40>--<2012-03-30 Fri>"
   (string=
    (format
     "<%s>--<%s>"
     (format-time-string (cdr org-time-stamp-formats) (org-encode-time 0 40 16 29 03 2012))
     (format-time-string (car org-time-stamp-formats) (org-encode-time 0 0 0 30 03 2012)))
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active-range :year-start 2012 :month-start 3 :day-start 29
	      :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
	      :day-end 30)) nil)))
  ;;; No start time, dates are not equal
  (should
   ;; Expected result: "<2012-03-29 Thu>--<2012-03-30 Fri 16:40>"
   (string=
    (format
     "<%s>--<%s>"
     (format-time-string (car org-time-stamp-formats) (org-encode-time 0 0 0 29 03 2012))
     (format-time-string (cdr org-time-stamp-formats) (org-encode-time 0 40 16 30 03 2012)))
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active-range :year-start 2012 :month-start 3 :day-start 29
	      :hour-end 16 :minute-end 40 :year-end 2012 :month-end 3
	      :day-end 30)) nil)))
  (should
   (string-match
    "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:40>"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active-range :year-start 2012 :month-start 3 :day-start 29
	      :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
	      :day-end 29 :hour-end 16 :minute-end 40)) nil)))
  (should
   (string-match
    "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active-range :year-start 2012 :month-start 3 :day-start 29
	      :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
	      :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  ;; Inactive daterange.
  (should
   (string-match "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
		 (org-test-parse-and-interpret
		  "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")))
  (should
   (string-match
    "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type inactive-range :year-start 2012 :month-start 3 :day-start 29
	      :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
	      :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  ;; Active timerange
  (should
   (string-match "<2012-03-29 .* 16:40-16:41>"
		 (org-test-parse-and-interpret
		  "<2012-03-29 thu. 16:40-16:41>")))
  ;; Diary.
  (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
		 "<%%diary-float t 4 2>\n"))
  ;; Timestamp with repeater interval, repeater deadline, with delay, with combinations.
  (should
   (string-match "<2012-03-29 .* \\+1y>"
		 (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")))
  (should
   (string-match
    "<2012-03-29 .* \\+1y>"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active :year-start 2012 :month-start 3 :day-start 29
	      :repeater-type cumulate :repeater-value 1 :repeater-unit year))
     nil)))
  (should
   (string-match
    "<2012-03-29 .* \\+1y/2y>"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active :year-start 2012 :month-start 3 :day-start 29
              :repeater-type cumulate :repeater-value 1 :repeater-unit year
              :repeater-deadline-value 2 :repeater-deadline-unit year))
     nil)))
  (should
   (string-match
    "<2012-03-29 .* -1y>"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active :year-start 2012 :month-start 3 :day-start 29
	      :warning-type all :warning-value 1 :warning-unit year))
     nil)))
  (should
   (string-match
    "<2012-03-29 .* \\+1y -1y>"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active :year-start 2012 :month-start 3 :day-start 29
	      :warning-type all :warning-value 1 :warning-unit year
	      :repeater-type cumulate :repeater-value 1 :repeater-unit year))
     nil)))
  (should
   (string-match
    "<2012-03-29 .* \\+1y/2y -1y>"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active :year-start 2012 :month-start 3 :day-start 29
              :warning-type all :warning-value 1 :warning-unit year
              :repeater-type cumulate :repeater-value 1 :repeater-unit year
              :repeater-deadline-value 2 :repeater-deadline-unit year))
     nil)))
  ;; Timestamp range with repeater interval
  (should
   (string-match "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
		 (org-test-parse-and-interpret
		  "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")))
  (should
   (string-match
    "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
    (org-element-timestamp-interpreter
     '(timestamp
       (:type active-range :year-start 2012 :month-start 3 :day-start 29
	      :year-end 2012 :month-end 3 :day-end 30 :repeater-type cumulate
	      :repeater-value 1 :repeater-unit year))
     nil)))
  ;; Tests for :range-type property
  ;;; Errors
  (should-error
   (org-element-timestamp-interpreter
    '(timestamp
      (:range-type timerange
                   :type active
                   :year-start 2023 :month-start 7 :day-start 10
                   :year-end 2023 :month-end 7 :day-end 10
                   :hour-start 17 :minute-start 30
                   :hour-end 17 :minute-end 30))
    nil))
  (should-error
   (org-element-timestamp-interpreter
    '(timestamp
      (:range-type daterange
                   :type active :year-start 2023 :month-start 7 :day-start 10
                   :hour-start 17 :minute-start 30)) nil))
  (should-error
   (org-element-timestamp-interpreter
    '(timestamp
      (:range-type timerange
                   :type inactive
                   :year-start 2023 :month-start 7 :day-start 10
                   :year-end 2023 :month-end 7 :day-end 10
                   :hour-start 17 :minute-start 30
                   :hour-end 17 :minute-end 30))
    nil))
  (should-error
   (org-element-timestamp-interpreter
    '(timestamp
      (:range-type daterange
                   :type inactive :year-start 2023 :month-start 7 :day-start 10
                   :hour-start 17 :minute-start 30)) nil))
  
  ;;; End part is nil
  (should
   ;; Expected result: "<2023-07-10 Mon>--<2023-07-10 Mon>"
   (string=
    (format
     "<%s>--<%s>"
     (format-time-string (car org-time-stamp-formats) (org-encode-time 0 0 0 10 7 2023))
     (format-time-string (car org-time-stamp-formats) (org-encode-time 0 0 0 10 7 2023)))
    (org-element-timestamp-interpreter
     '(timestamp
       (:range-type daterange
                    :type active-range :year-start 2023 :month-start 7 :day-start 10)) nil)))
  (should
   (string-match "<2023-07-10 .* 17:30-17:30>"
		 (org-element-timestamp-interpreter
		  '(timestamp
		    (:range-type timerange
                                 :type active-range :year-start 2023 :month-start 7 :day-start 10
		                 :hour-start 17 :minute-start 30)) nil)))
  (should
   ;; Expected result: "<2023-07-10 Mon 17:30>--<2023-07-10 Mon>"
   (string=
    (format
     "<%s>--<%s>"
     (format-time-string (cdr org-time-stamp-formats) (org-encode-time 0 30 17 10 7 2023))
     (format-time-string (car org-time-stamp-formats) (org-encode-time 0 0 0 10 7 2023)))
    (org-element-timestamp-interpreter
     '(timestamp
       (:range-type daterange
                    :type active-range :year-start 2023 :month-start 7 :day-start 10
		    :hour-start 17 :minute-start 30)) nil)))
  ;;; End is equal to start
  (should
   (string-match "<2023-07-10 .* 17:30-17:30>"
		 (org-element-timestamp-interpreter
		  '(timestamp
		    (:range-type timerange
                                 :type active-range
                                 :year-start 2023 :month-start 7 :day-start 10
                                 :year-end 2023 :month-end 7 :day-end 10
		                 :hour-start 17 :minute-start 30
                                 :hour-end 17 :minute-end 30)) nil)))
  (should
   (string-match "<2023-07-10 .* 17:30>--<2023-07-10 .* 17:30>"
		 (org-element-timestamp-interpreter
		  '(timestamp
		    (:range-type daterange
                                 :type active-range
                                 :year-start 2023 :month-start 7 :day-start 10
                                 :year-end 2023 :month-end 7 :day-end 10
		                 :hour-start 17 :minute-start 30
                                 :hour-end 17 :minute-end 30)) nil)))
  ;;;; End date is not equal to start date, but interpret the object as a timerange (:range-type 'timerange)
  (should
   (string-match "<2023-07-10 .* 17:30-18:30>"
		 (org-element-timestamp-interpreter
		  '(timestamp
		    (:range-type timerange
                                 :type active-range
                                 :year-start 2023 :month-start 7 :day-start 10
                                 :year-end 2023 :month-end 8 :day-end 10
                                 :hour-start 17 :minute-start 30
                                 :hour-end 18 :minute-end 30)) nil)))
  ;;;; End date is not equal to start date, interpret the object as a daterange (:range-type 'daterange)
  (should
   (string-match "<2023-07-10 .* 17:30>--<2023-08-10 .* 18:30>"
		 (org-element-timestamp-interpreter
		  '(timestamp
		    (:range-type daterange
                                 :type active-range
                                 :year-start 2023 :month-start 7 :day-start 10
		                 :year-end 2023 :month-end 8 :day-end 10
                                 :hour-start 17 :minute-start 30
                                 :hour-end 18 :minute-end 30)) nil))))

(ert-deftest test-org-element/verse-block-interpreter ()
  "Test verse block interpretation."
  (should
   (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
	  "#+begin_verse\nTest\n#+end_verse\n")))

(ert-deftest test-org-element/bold-interpreter ()
  "Test bold interpreter."
  (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))

(ert-deftest test-org-element/citation-interpreter ()
  "Test citation interpreter."
  (should
   (equal "[cite:@key]\n"
	  (org-test-parse-and-interpret "[cite:@key]")))
  (should
   (equal "[cite:-@key]\n"
	  (org-test-parse-and-interpret "[cite:-@key]")))
  (should
   (equal "[cite/style:@key]\n"
	  (org-test-parse-and-interpret "[cite/style:@key]")))
  (should
   (equal "[cite:pre @key]\n"
	  (org-test-parse-and-interpret "[cite:pre @key]")))
  (should
   (equal "[cite:@key post]\n"
	  (org-test-parse-and-interpret "[cite:@key post]")))
  (should
   (equal "[cite:@a ;b]\n"
	  (org-test-parse-and-interpret "[cite: @a ;b]")))
  (should
   (equal "[cite:@a;@b;@c]\n"
	  (org-test-parse-and-interpret "[cite:@a;@b;@c]")))
  (should
   (equal "[cite:common-pre ; @a]\n"
	  (org-test-parse-and-interpret "[cite:common-pre ; @a]")))
  (should
   (equal "[cite:@a ; common-post]\n"
	  (org-test-parse-and-interpret "[cite:@a ; common-post]"))))

(ert-deftest test-org-element/code-interpreter ()
  "Test code interpreter."
  (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))

(ert-deftest test-org-element/entity-interpreter ()
  "Test entity interpreter."
  ;; 1. Without brackets.
  (should
   (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
  ;; 2. With brackets.
  (should
   (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))

(ert-deftest test-org-element/export-snippet-interpreter ()
  "Test export snippet interpreter."
  (should (equal (org-test-parse-and-interpret "@@backend:contents@@")
		 "@@backend:contents@@\n")))

(ert-deftest test-org-element/footnote-reference-interpreter ()
  "Test footnote reference interpreter."
  ;; Regular reference.
  (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
  ;; Named reference.
  (should (equal (org-test-parse-and-interpret "Text[fn:label]")
		 "Text[fn:label]\n"))
  ;; Inline reference.
  (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
		 "Text[fn:label:def]\n"))
  ;; Anonymous reference.
  (should (equal (org-test-parse-and-interpret "Text[fn::def]")
		 "Text[fn::def]\n")))

(ert-deftest test-org-element/inline-babel-call-interpreter ()
  "Test inline babel call interpreter."
  ;; Without arguments.
  (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
  ;; With arguments.
  (should (equal (org-test-parse-and-interpret "call_test(x=2)")
		 "call_test(x=2)\n"))
  ;; With header arguments.
  (should (equal (org-test-parse-and-interpret
		  "call_test[:results output]()[:results html]")
		 "call_test[:results output]()[:results html]\n")))

(ert-deftest test-org-element/inline-src-block-interpreter ()
  "Test inline src block interpreter."
  ;; 1. Without header argument.
  (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
		 "src_emacs-lisp{(+ 1 1)}\n"))
  ;; 2. With header arguments.
  (should (equal (org-test-parse-and-interpret
		  "src_emacs-lisp[:results silent]{(+ 1 1)}")
		 "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))

(ert-deftest test-org-element/italic-interpreter ()
  "Test italic interpreter."
  (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))

(ert-deftest test-org-element/latex-fragment-interpreter ()
  "Test latex fragment interpreter."
  (should (equal (org-test-parse-and-interpret "\\command{}") "\\command{}\n"))
  (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
  (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
  (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
  (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
  (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n")))

(ert-deftest test-org-element/line-break-interpreter ()
  "Test line break interpreter."
  (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
		 "First line \\\\\nSecond line\n")))

(ert-deftest test-org-element/link-interpreter ()
  "Test link interpreter."
  ;; Links targeted from a radio target.
  (should (equal (let ((org-target-link-regexp "radio-target"))
		   (org-test-parse-and-interpret "a radio-target"))
		 "a radio-target\n"))
  ;; Links without description.
  (should (equal (org-test-parse-and-interpret "[[https://orgmode.org]]")
		 "[[https://orgmode.org]]\n"))
  ;; Links with a description, even one containing a link.
  (should (equal (org-test-parse-and-interpret
		  "[[https://orgmode.org][Org mode]]")
		 "[[https://orgmode.org][Org mode]]\n"))
  (should (equal (org-test-parse-and-interpret
		  "[[https://orgmode.org][https://orgmode.org]]")
		 "[[https://orgmode.org][https://orgmode.org]]\n"))
  ;; File links.
  (should
   (equal (org-test-parse-and-interpret "[[file+emacs:todo.org]]")
	  "[[file+emacs:todo.org]]\n"))
  (should
   (equal (org-test-parse-and-interpret "[[file:todo.org::*task]]")
	  "[[file:todo.org::*task]]\n"))
  (should
   (equal (org-test-parse-and-interpret "[[/tmp/todo.org::*task]]")
	  "[[/tmp/todo.org::*task]]\n"))
  ;; Id links.
  (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
  ;; Custom-id links.
  (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
  ;; Code-ref links.
  (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
  ;; Plain links.
  (should (equal (org-test-parse-and-interpret "https://orgmode.org")
		 "https://orgmode.org\n"))
  ;; Angular links.
  (should (equal (org-test-parse-and-interpret "<https://orgmode.org>")
		 "<https://orgmode.org>\n"))
  ;; Pathological case: link with a %-sign in description.
  (should (equal (org-test-parse-and-interpret "[[file://path][%s]]")
		 "[[file://path][%s]]\n")))

(ert-deftest test-org-element/macro-interpreter ()
  "Test macro interpreter."
  ;; 1. Without argument.
  (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
  ;; 2. With arguments.
  (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
		 "{{{test(arg1,arg2)}}}\n")))

(ert-deftest test-org-element/radio-target-interpreter ()
  "Test radio target interpreter."
  (should (equal (org-test-parse-and-interpret "<<<some text>>>")
		 "<<<some text>>>\n")))

(ert-deftest test-org-element/statistics-cookie-interpreter ()
  "Test statistics cookie interpreter."
  ;; 1. Without percent
  (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
  ;; 2. With percent.
  (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))

(ert-deftest test-org-element/strike-through-interpreter ()
  "Test strike through interpreter."
  (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))

(ert-deftest test-org-element/subscript-interpreter ()
  "Test subscript interpreter."
  ;; 1. Without brackets.
  (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
  ;; 2. With brackets.
  (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))

(ert-deftest test-org-element/superscript-interpreter ()
  "Test superscript interpreter."
  ;; 1. Without brackets.
  (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
  ;; 2. With brackets.
  (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))

(ert-deftest test-org-element/target-interpreter ()
  "Test target interpreter."
  (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))

(ert-deftest test-org-element/underline-interpreter ()
  "Test underline interpreter."
  (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))

(ert-deftest test-org-element/verbatim-interpreter ()
  "Test verbatim interpreter."
  (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))


\f
;;; Test Granularity

(ert-deftest test-org-element/granularity ()
  "Test granularity impact on buffer parsing."
  (org-test-with-temp-text
      "* Head 1
** Head 2
#+BEGIN_CENTER
Centered paragraph.
#+END_CENTER
Paragraph \\alpha."
    ;; 1.1. Granularity set to `headline' should parse every headline
    ;;      in buffer, and only them.
    (let ((tree (org-element-parse-buffer 'headline)))
      (should (= 2 (length (org-element-map tree 'headline 'identity))))
      (should-not (org-element-map tree 'paragraph 'identity)))
    ;; 1.2. Granularity set to `greater-element' should not enter
    ;;      greater elements excepted headlines and sections.
    (let ((tree (org-element-parse-buffer 'greater-element)))
      (should (= 1 (length (org-element-map tree 'center-block 'identity))))
      (should (= 1 (length (org-element-map tree 'paragraph 'identity))))
      (should-not (org-element-map tree 'entity 'identity)))
    ;; 1.3. Granularity set to `element' should enter every
    ;;      greater-element.
    (let ((tree (org-element-parse-buffer 'element)))
      (should (= 2 (length (org-element-map tree 'paragraph 'identity))))
      (should-not (org-element-map tree 'entity 'identity)))
    ;; 1.4. Granularity set to `object' can see everything.
    (let ((tree (org-element-parse-buffer 'object)))
      (should (= 1 (length (org-element-map tree 'entity 'identity)))))))

(ert-deftest test-org-element/secondary-string-parsing ()
  "Test if granularity correctly toggles secondary strings parsing."
  ;; With a granularity bigger than `object', no secondary string
  ;; should be parsed.
  (should
   (stringp
    (org-test-with-temp-text "* Headline"
      (let ((headline
	     (org-element-map (org-element-parse-buffer 'headline) 'headline
	       #'identity nil 'first-match)))
	(org-element-property :title headline)))))
  (should
   (stringp
    (org-test-with-temp-text "* Headline\n- tag :: item"
      (let ((item (org-element-map (org-element-parse-buffer 'element) 'item
		    #'identity nil 'first-match)))
	(org-element-property :tag item)))))
  (when (featurep 'org-inlinetask)
    (should
     (stringp
      (let ((org-inlinetask-min-level 15))
	(org-test-with-temp-text "*************** Inlinetask"
	  (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
				'inlinetask
			      #'identity nil 'first-match)))
	    (org-element-property :title inlinetask)))))))
  ;; With a default granularity, secondary strings should be parsed.
  (should
   (listp
    (org-test-with-temp-text "* Headline"
      (let ((headline
	     (org-element-map (org-element-parse-buffer) 'headline
	       #'identity nil 'first-match)))
	(org-element-property :title headline)))))
  ;; `org-element-at-point' should never parse a secondary string.
  (should-not
   (listp
    (org-test-with-temp-text "* Headline"
      (org-element-property :title (org-element-at-point)))))
  ;; Preserve current local variables when parsing a secondary string.
  (should
   (let ((org-entities nil)
	 (org-entities-user nil))
     (org-test-with-temp-text "
#+CAPTION: \\foo
Text
# Local Variables:
# org-entities-user: ((\"foo\"))
# End:"
       (let ((safe-local-variable-values '((org-entities-user . (("foo"))))))
	 (hack-local-variables))
       (org-element-map (org-element-parse-buffer) 'entity
	 #'identity nil nil nil t)))))


\f
;;; Test Visible Only Parsing

(ert-deftest test-org-element/parse-buffer-visible ()
  "Test `org-element-parse-buffer' with visible only argument."
  (should
   (equal '("H1" "H3" "H5")
	  (org-test-with-temp-text
	      "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
	    (org-occur ":visible:")
	    (org-element-map (org-element-parse-buffer nil t) 'headline
	      (lambda (hl) (org-element-property :raw-value hl))))))
  (should
   (equal "Test"
	  (let ((contents "Test"))
	    (org-test-with-temp-text contents
	      (add-text-properties 0 1 '(invisible t) contents)
	      (org-element-map (org-element-parse-buffer nil t) 'plain-text
		#'org-no-properties nil t)))))
  (should
   (equal "Test"
	  (let ((contents "Test"))
	    (org-test-with-temp-text (concat "- " contents)
	      (add-text-properties 0 1 '(invisible t) contents)
	      (org-element-map (org-element-parse-buffer nil t) 'plain-text
		#'org-no-properties nil t))))))


\f
;;; Test `:parent' Property

(ert-deftest test-org-element/parent-property ()
  "Test `:parent' property."
  ;; Elements.
  (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
    (let* ((tree (org-element-parse-buffer))
	   (parent (org-element-property
		    :parent
		    (org-element-map tree 'paragraph 'identity nil t))))
      (should parent)
      (should (eq (org-element-map tree 'center-block 'identity nil t)
		  parent))))
  ;; Objects.
  (org-test-with-temp-text "a_{/b/}"
    (let* ((tree (org-element-parse-buffer))
	   (parent (org-element-property
		    :parent
		    (org-element-map tree 'italic 'identity nil t))))
      (should parent)
      (should (eq parent
		  (org-element-map tree 'subscript 'identity nil t)))))
  ;; Secondary strings
  (org-test-with-temp-text "* /italic/"
    (let* ((tree (org-element-parse-buffer))
	   (parent (org-element-property
		    :parent (org-element-map tree 'italic 'identity nil t))))
      (should parent)
      (should (equal parent
		     (org-element-map tree 'headline 'identity nil t))))))


\f
;;; Test Normalize Contents

(ert-deftest test-org-element/normalize-contents ()
  "Test `org-element-normalize-contents' specifications."
  ;; Remove maximum common indentation from element's contents.
  (should
   (equal
    (org-element-normalize-contents
     '(paragraph nil "  Two spaces\n   Three spaces"))
    '(paragraph nil "Two spaces\n Three spaces")))
  (should
   (equal
    (org-element-normalize-contents
     '(paragraph nil "  Two spaces\nNo space"))
    '(paragraph nil "  Two spaces\nNo space")))
  ;; Ignore objects within contents when computing maximum common
  ;; indentation.  However, if contents start with an object, common
  ;; indentation is 0.
  (should
   (equal
    (org-element-normalize-contents
     '(paragraph nil " One " (emphasis nil "space") "\n  Two spaces"))
    '(paragraph nil "One " (emphasis nil "space") "\n Two spaces")))
  (should
   (equal
    (org-element-normalize-contents
     '(paragraph nil (verbatim nil "V") "No space\n  Two\n   Three"))
    '(paragraph nil (verbatim nil "V") "No space\n  Two\n   Three")))
  ;; Ignore blank lines.
  (should
   (equal
    (org-element-normalize-contents
     '(paragraph nil "  Two spaces\n\n \n  Two spaces"))
    '(paragraph nil "Two spaces\n\n\nTwo spaces")))
  (should
   (equal
    '(paragraph nil " Two spaces\n" (verbatim nil "V") "\n Two spaces")
    (org-element-normalize-contents
     '(paragraph nil "  Two spaces\n " (verbatim nil "V") "\n  Two spaces"))))
  (should
   (equal
    '(verse-block nil "line 1\n\nline 2")
    (org-element-normalize-contents
     '(verse-block nil "  line 1\n\n  line 2"))))
  ;; Recursively enter objects in order to compute common indentation.
  (should
   (equal
    (org-element-normalize-contents
     '(paragraph nil "  Two spaces " (bold nil " and\n One space")))
    '(paragraph nil " Two spaces " (bold nil " and\nOne space"))))
  ;; When optional argument is provided, ignore first line
  ;; indentation.
  (should
   (equal
    (org-element-normalize-contents
     '(paragraph nil "No space\n  Two spaces\n   Three spaces") t)
    '(paragraph nil "No space\nTwo spaces\n Three spaces")))
  (should
   (equal
    (org-element-normalize-contents
     '(paragraph nil (verbatim nil "V") "No space\n  Two\n   Three") t)
    '(paragraph nil (verbatim nil "V") "No space\nTwo\n Three")))
  ;; Corner case: do not ignore indentation of string right after
  ;; a line break.
  (should
   (equal
    (org-element-normalize-contents
     '(paragraph nil " 1 space" (line-break) "  2 spaces"))
    '(paragraph nil "1 space" (line-break) " 2 spaces"))))


\f
;;; Test Navigation Tools.

(ert-deftest test-org-element/at-point ()
  "Test `org-element-at-point' specifications."
  ;; Return closest element containing point.
  (should
   (eq 'paragraph
       (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
	 (progn (search-forward "A")
		(org-element-type (org-element-at-point))))))
  ;; Point in other buffer.
  (should
   (eq 'paragraph
       (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
	 (progn (search-forward "A")
		(org-element-type
                 (let ((mk (point-marker)))
                   (with-temp-buffer
                     (org-element-at-point mk))))))))
  ;; Correctly set `:parent' property.
  (should
   (eq 'center-block
       (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
	 (progn (search-forward "A")
		(org-element-type
		 (org-element-property :parent (org-element-at-point)))))))
  ;; Special case: at a blank line just below a headline, return that
  ;; headline.
  (should
   (equal "H1" (org-test-with-temp-text "* H1\n  \n* H2\n"
		 (forward-line)
		 (org-element-property :title (org-element-at-point)))))
  ;; Special case: at the very beginning of a table, return `table'
  ;; object instead of `table-row'.
  (should
   (eq 'table
       (org-test-with-temp-text "| a | b |"
	 (org-element-type (org-element-at-point)))))
  ;; Special case: at the very beginning of a list or sub-list, return
  ;; `plain-list' object instead of `item'.
  (should
   (eq 'plain-list
       (org-test-with-temp-text "- item"
	 (org-element-type (org-element-at-point)))))
  ;; Special case: at the closing line of a greater element, be sure
  ;; to return it instead of the last element in its contents.
  (should
   (eq 'center-block
       (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph\n#+END_CENTER"
	 (progn (forward-line 2)
		(org-element-type (org-element-at-point))))))
  ;; Special case: at a blank line between two items, be sure to
  ;; return item above instead of the last element of its contents.
  (should
   (eq 'item
       (org-test-with-temp-text "- Para1\n\n- Para2"
	 (forward-line)
	 (org-element-type (org-element-at-point)))))
  ;; Special case: at the last blank line in a plain list, return it
  ;; instead of the last item.
  (should
   (eq 'plain-list
       (org-test-with-temp-text "- Para1\n- Para2\n\nPara3"
	 (progn (forward-line 2)
		(org-element-type (org-element-at-point))))))
  ;; Special case: at the last blank line in a plain list at the end of
  ;; a headline, return the plain list, not the last item, and not the
  ;; headline.
  (should
   (eq 'plain-list
       (org-test-with-temp-text "* Headline\n- Para1\n- Para2\n\nPara3\n* Another headline"
	 (progn (forward-line 3)
		(org-element-type (org-element-at-point))))))
  ;; Special case: when a list ends at the end of buffer and there's
  ;; no final newline, return last element in last item.
  (should
   (eq 'paragraph
       (org-test-with-temp-text "- a"
	 (end-of-line)
	 (org-element-type (org-element-at-point)))))
  ;; Parse a list within a block itself contained in a list.
  (should
   (eq 'plain-list
       (org-test-with-temp-text
	   "- outer\n  #+begin_center\n  - inner\n  #+end_center"
	 (search-forward "inner")
	 (beginning-of-line)
	 (org-element-type (org-element-at-point)))))
  ;; Do not error at eob on an empty line.
  (should
   (org-test-with-temp-text "* H\n"
     (forward-line)
     (or (org-element-at-point) t)))
  ;; Return greater element when outside contents.
  (should
   (eq 'drawer
       (org-test-with-temp-text
        ":DRAWER:\ntest\n:EN<point>D:\n"
        (org-element-type (org-element-at-point)))))
  (should
   (eq 'drawer
       (org-test-with-temp-text
        ":DRA<point>WER:\ntest\n:END:\n"
        (org-element-type (org-element-at-point)))))
  ;; Return greater element when at :contents-end.
  (should
   (eq 'drawer
       (org-test-with-temp-text
        ":DRAWER:\ntest\n<point>:END:\n"
        (org-element-type (org-element-at-point))))))

(ert-deftest test-org-element/context ()
  "Test `org-element-context' specifications."
  ;; Return closest object containing point.
  (should
   (eq 'underline
       (org-test-with-temp-text "Some *text with _under<point>line_ text*"
	 (org-element-type (org-element-context)))))
  ;; Find objects in secondary strings.
  (should
   (eq 'underline
       (org-test-with-temp-text "* Headline _<point>with_ underlining"
	 (org-element-type (org-element-context)))))
  ;; Find objects in objects.
  (should
   (eq 'macro
       (org-test-with-temp-text "| a | {<point>{{macro}}} |"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'table-cell
       (org-test-with-temp-text "| a | b<point> {{{macro}}} |"
	 (org-element-type (org-element-context)))))
  ;; Find objects in item tags.
  (should
   (eq 'bold
       (org-test-with-temp-text "- *bo<point>ld* ::"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'bold
       (org-test-with-temp-text "- *bold* ::<point>"
	 (org-element-type (org-element-context)))))
  (should-not
   (eq 'bold
       (org-test-with-temp-text "- *bold* ::\n<point>"
	 (org-element-type (org-element-context)))))
  ;; Do not find objects in table rules.
  (should
   (eq 'table-row
       (org-test-with-temp-text "| a | b |\n|-<point>--|---|\n| c | d |"
	 (org-element-type (org-element-context)))))
  ;; Find objects in parsed affiliated keywords.
  (should
   (eq 'macro
       (org-test-with-temp-text "#+CAPTION: {<point>{{macro}}}\n| a | b |"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'bold
       (org-test-with-temp-text "#+caption: *<point>bold*\nParagraph"
	 (org-element-type (org-element-context)))))
  ;; Find objects at the end of buffer.
  (should
   (eq 'bold
       (org-test-with-temp-text "*bold*"
	 (goto-char (point-max))
	 (org-element-type (org-element-context)))))
  ;; Correctly set `:parent' property.
  (should
   (eq 'paragraph
       (org-test-with-temp-text "Some *bold<point>* text"
	 (org-element-type
	  (org-element-property :parent (org-element-context))))))
  ;; Between two objects, return the second one.
  (should
   (eq 'macro
       (org-test-with-temp-text "<<target>><point>{{{test}}}"
	 (org-element-type (org-element-context)))))
  ;; Test optional argument.
  (should
   (eq 'underline
       (org-test-with-temp-text "Some *text with _under<point>line_ text*"
	 (org-element-type (org-element-context (org-element-at-point))))))
  ;; Special case: bold object at the beginning of a headline.
  (should
   (eq 'bold
       (org-test-with-temp-text "* *bo<point>ld*"
	 (org-element-type (org-element-context)))))
  ;; Special case: incomplete cell at the end of a table row.
  (should
   (eq 'table-cell
       (org-test-with-temp-text "|a|b|c<point>"
	 (org-element-type (org-element-context)))))
  ;; Special case: objects in inline footnotes.
  (should
   (eq 'link
       (org-test-with-temp-text "[fn::[[<point>https://orgmode.org]]]"
	 (org-element-type (org-element-context)))))
  ;; Special case: tags looking like a link.
  (should-not
   (eq 'link
       (org-test-with-temp-text "* Headline :file<point>:tags:"
	 (org-element-type (org-element-context)))))
  (should
   (eq 'link
       (org-test-with-temp-text "* Headline :file<point>:tags: :real:tag:"
	 (org-element-type (org-element-context)))))
  ;; Do not parse partial export snippets.
  (should-not
   (eq 'export-snippet
       (org-test-with-temp-text
           "<point>@@latex:\n\nparagraph\n\n@@"
         (org-element-type (org-element-context))))))


\f
;;; Test Tools

(ert-deftest test-org-element/lineage ()
  "Test `org-element-lineage' specifications."
  ;; Regular tests.  When applied to an element or object returned by
  ;; `org-element-at-point' or `org-element-context', the list is
  ;; limited to the current section.
  (should
   (equal '(paragraph center-block section headline headline org-data)
	  (org-test-with-temp-text
	      "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
	    (mapcar #'car (org-element-lineage (org-element-context))))))
  (should
   (equal '(paragraph center-block section headline headline org-data)
	  (org-test-with-temp-text
	      "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
	    (mapcar #'car
		    (org-element-lineage
		     (org-element-map (org-element-parse-buffer) 'bold
		       #'identity nil t))))))
  ;; Test TYPES optional argument.
  (should
   (eq 'center-block
       (org-test-with-temp-text
	   "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
	 (org-element-type
	  (org-element-lineage (org-element-context) 'center-block)))))
  (should-not
   (org-test-with-temp-text
       "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
     (org-element-lineage (org-element-context) '(example-block))))
  ;; Test WITH-SELF optional argument.
  (should
   (equal '(bold paragraph center-block section headline headline org-data)
	  (org-test-with-temp-text
	      "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
	    (mapcar #'car (org-element-lineage (org-element-context) nil t)))))
  ;; When TYPES and WITH-SELF are provided, the latter is also checked
  ;; against the former.
  (should
   (org-test-with-temp-text
       "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
     (org-element-lineage (org-element-context) '(bold) t))))

(ert-deftest test-org-element/lineage-map ()
  "Test `org-element-lineage-map' specifications."
  (should
   (equal '(paragraph center-block section headline headline org-data)
	  (org-test-with-temp-text
	      "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
	    (org-element-lineage-map (org-element-context) #'org-element-type))))
  ;; WITH-SELF.
  (should
   (equal '(bold paragraph center-block section headline headline org-data)
	  (org-test-with-temp-text
	      "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
	    (org-element-lineage-map (org-element-context) #'org-element-type nil t))))
  ;; FUN as a Lisp form.
  (should
   (equal '("H2" "H1")
	  (org-test-with-temp-text
	      "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
	    (org-element-lineage-map
                (org-element-context)
                '(org-element-property :raw-value node)))))
  ;; FIRST-MATCH
  (should
   (equal "H2"
	  (org-test-with-temp-text
	      "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
	    (org-element-lineage-map
                (org-element-context)
                '(org-element-property :raw-value node)
              nil nil t)))))

(ert-deftest test-org-element/property-inherited ()
  "Test `org-element-property-inherited' specifications."
  ;; Property without self.
  (should
   (equal
    'bar
    (org-element-property-inherited
     :foo
     (car (org-element-contents
           (org-element-create
            'parent '(:foo bar)
            (org-element-create 'child '(:foo baz))))))))
  ;; With self
  (should
   (equal
    'baz
    (org-element-property-inherited
     :foo
     (car (org-element-contents
           (org-element-create
            'parent '(:foo bar)
            (org-element-create 'child '(:foo baz)))))
     'with-self)))
  ;; ACCUMULATE non-nil.
  (should
   (equal
    '(bar baz)
    (org-element-property-inherited
     :foo
     (car (org-element-contents
           (org-element-create
            'parent '(:foo bar)
            (org-element-create 'child '(:foo baz)))))
     'with-self 'accumulate)))
  ;; LITERAL-NIL.
  (should-not
   (org-element-property-inherited
    :foo
    (org-element-create 'child '(:foo "nil"))
    'with-self nil t))
  (should
   (org-element-property-inherited
    :foo
    (org-element-create 'child '(:foo "nil"))
    'with-self))
  ;; INCLUDE-NIL
  (should-not
   (org-element-property-inherited
    :foo
    (org-element-map
        (thread-last
          (org-element-create 'grandchild '(:foo baz))
          (org-element-create 'child '(:foo nil))
          (org-element-create 'parent '(:foo bar)))
        'grandchild #'identity nil t)
    nil nil nil t))
  (should
   (eq
    'bar
    (org-element-property-inherited
     :foo
     (org-element-map
         (thread-last
           (org-element-create 'grandchild '(:foo baz))
           (org-element-create 'child '(:foo nil))
           (org-element-create 'parent '(:foo bar)))
         'grandchild #'identity nil t))))
  ;; INCLUDE-NIL in accumulated.
  (should
   (equal
    '(bar nil baz)
    (org-element-property-inherited
     :foo
     (org-element-map
         (thread-last
           (org-element-create 'grandchild '(:foo baz))
           (org-element-create 'child '(:foo nil))
           (org-element-create 'parent '(:foo bar)))
         'grandchild #'identity nil t)
     'with-self 'accumulate nil t)))
  ;; PROPERTY as a list.
  (should
   (equal
    '(bar value)
    (org-element-property-inherited
     '(:foo :extra)
     (car (org-element-contents
           (org-element-create
            'parent '(:foo bar :extra value)
            (org-element-create 'child '(:foo baz)))))
     nil 'accumulate)))
  ;; Append list values
  (should
   (equal
    '(bar value value2)
    (org-element-property-inherited
     '(:foo :extra)
     (car (org-element-contents
           (org-element-create
            'parent '(:foo bar :extra (value value2))
            (org-element-create 'child '(:foo baz)))))
     nil 'accumulate))))

\f
;;; Test Cache.

(ert-deftest test-org-element/cache ()
  "Test basic expectations and common pitfalls for cache."
  ;; Shift positions.
  (should
   (equal '(18 . 23)
	  (org-test-with-temp-text "para1\n\npara2\n\npara3"
	    (let ((org-element-use-cache t))
	      (save-excursion (goto-char (point-max)) (org-element-at-point))
	      (insert "add")
	      (forward-line 4)
	      (let ((element (org-element-at-point)))
		(cons (org-element-property :begin element)
		      (org-element-property :end element)))))))
  ;; Partial shifting: when the contents of a greater element are
  ;; modified, only shift ending positions.
  (should
   (org-test-with-temp-text
       "#+BEGIN_CENTER\nPara1\n\nPara2\n\nPara3\n#+END_CENTER"
     (let ((org-element-use-cache t))
       (save-excursion (search-forward "3") (org-element-at-point))
       (search-forward "Para2")
       (insert " ")
       (let ((element (org-element-property :parent (org-element-at-point))))
	 (equal (cons (org-element-property :begin element)
		      (org-element-property :end element))
		(cons (point-min) (point-max)))))))
  ;; Re-parent shifted elements.
  (should
   (eq 'item
       (org-test-with-temp-text "- item\n\n\n  para1\n  para2"
	 (let ((org-element-use-cache t))
	   (end-of-line)
	   (org-element-at-point)
	   (save-excursion (goto-char (point-max)) (org-element-at-point))
	   (forward-line)
	   (delete-char 1)
	   (goto-char (point-max))
	   (org-element-type
	    (org-element-property :parent (org-element-at-point)))))))
  ;; Preserve local structures when re-parenting.
  (should
   (eq 'table
       (let ((org-element-use-cache t))
	 (org-test-with-temp-text
	     "#+begin_center\nP0\n\n<point>\n\n  P1\n  | a | b |\n  | c | d |\n#+end_center"
	   (save-excursion (search-forward "| c |") (org-element-at-point))
	   (insert "- item")
	   (search-forward "| c |")
	   (beginning-of-line)
	   (org-element-type
	    (org-element-property :parent (org-element-at-point)))))))
  (should-not
   (eq 'center-block
       (org-test-with-temp-text
	   "#+begin_center\nP0\n\n<point>\n\n  P1\n  | a | b |\n#+end_center"
	 (let ((org-element-use-cache t))
	   (save-excursion (search-forward "| a |") (org-element-at-point))
	   (insert "- item")
	   (search-forward "| a |")
	   (beginning-of-line)
	   (org-element-type
	    (org-element-property :parent (org-element-at-point)))))))
  ;; When re-parenting, also propagate changes to list structures.
  (should
   (= 2
      (org-test-with-temp-text "\n  Para\n  - item<point>"
	(let ((org-element-use-cache t))
	  (org-element-at-point)
	  (goto-char (point-min))
	  (insert "- Top\n")
	  (search-forward "- item")
	  (beginning-of-line)
	  (length (org-element-property :structure (org-element-at-point)))))))
  ;; Modifying the last line of an element alters the element below.
  (should
   (org-test-with-temp-text "para1\n\npara2"
     (let ((org-element-use-cache t))
       (goto-char (point-max))
       (org-element-at-point)
       (forward-line -1)
       (insert "merge")
       (let ((element (org-element-at-point)))
	 (equal (cons (org-element-property :begin element)
		      (org-element-property :end element))
		(cons (point-min) (point-max)))))))
  ;; Modifying the first line of an element alters the element above.
  (should
   (org-test-with-temp-text ": fixed-width\n:not-fixed-width"
     (let ((org-element-use-cache t))
       (goto-char (point-max))
       (org-element-at-point)
       (search-backward ":")
       (forward-char)
       (insert " ")
       (let ((element (org-element-at-point)))
	 (equal (cons (org-element-property :begin element)
		      (org-element-property :end element))
		(cons (point-min) (point-max)))))))
  (org-test-with-temp-text ":DRAWER:\ntest\n:END:\n <point>#\nParagraph"
    (let ((org-element-use-cache t))
      (org-element-cache-map #'ignore :granularity 'element)
      (should (eq 'comment (org-element-type (org-element-at-point))))
      (should (eq 0 (org-element-property :post-blank (org-element-at-point (point-min)))))
      (insert " ") (delete-char -1)
      (org-element-cache-map #'ignore :granularity 'element)
      (delete-char 1)
      (should (eq 1 (org-element-property :post-blank (org-element-at-point (point-min)))))))
  ;; Sensitive change: adding a line alters document structure both
  ;; above and below.
  (should
   (eq 'example-block
       (org-test-with-temp-text "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n"
	 (let ((org-element-use-cache t))
	   (goto-char (point-max))
	   (org-element-at-point)
	   (insert "#+END_EXAMPLE")
	   (search-backward "Para1")
	   (org-element-type (org-element-at-point))))))
  (should
   (eq 'example-block
       (org-test-with-temp-text "Para1\n\nPara2\n#+END_EXAMPLE"
	 (let ((org-element-use-cache t))
	   (save-excursion (goto-char (point-max)) (org-element-at-point))
	   (insert "#+BEGIN_EXAMPLE\n")
	   (search-forward "Para2")
	   (org-element-type (org-element-at-point))))))
  ;; Sensitive change: removing a line alters document structure both
  ;; above and below.
  (should
   (eq 'example-block
       (org-test-with-temp-text
	   "# +BEGIN_EXAMPLE\nPara1\n\nPara2\n#+END_EXAMPLE"
	 (let ((org-element-use-cache t))
	   (save-excursion (goto-char (point-max)) (org-element-at-point))
	   (forward-char)
	   (delete-char 1)
	   (search-forward "Para2")
	   (org-element-type (org-element-at-point))))))
  (should
   (eq 'example-block
       (org-test-with-temp-text
	   "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n# +END_EXAMPLE"
	 (let ((org-element-use-cache t))
	   (save-excursion (goto-char (point-max)) (org-element-at-point))
	   (search-forward "# ")
	   (delete-char -1)
	   (search-backward "Para1")
	   (org-element-type (org-element-at-point))))))
  ;; Make sure that we do not generate intersecting elements.
  (should (eq 'paragraph
              (org-test-with-temp-text ":DRAWER:\nP1\n<point>\n:END:\n#+END_EXAMPLE"
                (let ((org-element-use-cache t))
                  (org-element-at-point (point-max))
                  (org-element-at-point)
                  (insert "#+BEGIN_EXAMPLE")
                  (org-element-type (org-element-at-point))))))
  ;; But yet correctly slurp obsolete elements inside a new element.
  (should (eq 'example-block
              (org-test-with-temp-text ":DRAWER:\nP1\n<point>\nP2\n#+END_EXAMPLE\n:END:"
                (let ((org-element-use-cache t))
                  (org-element-at-point (point-max))
                  (save-excursion
                    (re-search-forward "P2")
                    (should (eq 'paragraph (org-element-type (org-element-at-point))))
                    (re-search-forward "END_")
                    (should (eq 'paragraph (org-element-type (org-element-at-point)))))
                  (insert "#+BEGIN_EXAMPLE")
                  (re-search-forward "P2")
                  (should (eq 'example-block (org-element-type (org-element-at-point))))
                  (re-search-forward "END_")
                  (org-element-type (org-element-at-point))))))
  ;; Test edits near :end of element
  (should-not (eq 'headline
                  (org-test-with-temp-text "* H1\nP1\n<point>*H2\n"
                    (let ((org-element-use-cache t))
                      (org-element-cache-map #'ignore :granularity 'element)
                      (insert "Blah")
                      (org-element-type (org-element-at-point))))))
  (should-not (eq 'headline
                  (org-test-with-temp-text "* H1\nP1\n<point>*H2\n"
                    (let ((org-element-use-cache t))
                      (org-element-cache-map #'ignore :granularity 'element)
                      (backward-delete-char 1)
                      (org-element-type (org-element-at-point))))))
  (org-test-with-temp-text "Paragraph.\n #<point> comment"
    (let ((org-element-use-cache t))
      (org-element-cache-map #'ignore :granularity 'element)
      (should (eq 'comment (org-element-type (org-element-at-point))))
      (insert "not comment anymore")
      (org-element-cache-map #'ignore :granularity 'element)
      (should-not (eq 'comment (org-element-type (org-element-at-point))))
      (should (eq (org-element-at-point) (org-element-at-point 1)))))
  (should (eq 'headline
              (org-test-with-temp-text "* H1\nP1\n<point>* H2\n"
                (let ((org-element-use-cache t))
                  (org-element-cache-map #'ignore :granularity 'element)
                  (insert "Blah\n")
                  (org-element-type (org-element-at-point))))))
  ;; Corner case: watch out drawers named "PROPERTIES" as they are
  ;; fragile, unlike to other drawers.
  (should
   (eq 'node-property
       (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A<point>\n:END:"
	 (let ((org-element-use-cache t))
	   (org-element-at-point)
	   (insert "+:")
	   (org-element-type (org-element-at-point))))))
  ;; Properly handle elements not altered by modifications but whose
  ;; parents were removed from cache.
  (should
   (org-test-with-temp-text
       "Paragraph\n\n\n\n#+begin_center\n<point>contents\n#+end_center"
     (let ((org-element-use-cache t)
	   (parent-end (point-max)))
       (org-element-at-point)
       (save-excursion (search-backward "Paragraph")
		       (forward-line 2)
		       (insert "\n  "))
       (eq (org-element-property
	    :end (org-element-property :parent (org-element-at-point)))
	   (+ parent-end 3))))))

(ert-deftest test-org-element/cache-affiliated ()
  "Test updating affiliated keywords."
  ;; Inserting a line right after other keywords.
  (let ((org-element-use-cache t))
    (org-test-with-temp-text "
#+caption: test
#+name: test
<point>
line"
      (org-element-cache-map #'ignore :granularity 'element)
      (should (eq 'keyword (org-element-type (org-element-at-point))))
      (insert "1")
      (should (eq 2 (org-element-property :begin (org-element-at-point)))))))

(ert-deftest test-org-element/cache-table ()
  "Test handling edits in tables."
  ;; Unindented second row of the table should not be re-parented by
  ;; inserted item.
  (should
   (eq 'table
       (let ((org-element-use-cache t))
	 (org-test-with-temp-text
	  "#+begin_center
P0

<point>

  P1
  | a | b |
| c | d |
#+end_center"
	  (save-excursion (search-forward "| c |") (org-element-at-point))
	  (insert "- item")
	  (search-forward "| c |")
	  (beginning-of-line)
	  (org-element-type (org-element-at-point))))))
  (should
   (eq 'table
       (let ((org-element-use-cache t))
	 (org-test-with-temp-text
	  "
- item 1

<point>
  | a | b |
| c | d |
#+end_center"
	  (save-excursion (search-forward "| c |") (org-element-at-point))
          (delete-char 1)
	  (search-forward "| c |")
	  (beginning-of-line)
	  (org-element-type (org-element-at-point))))))
  (should
   (eq 'table-row
       (let ((org-element-use-cache t))
	 (org-test-with-temp-text
	  "
- item 1
<point>
  | a | b |
| c | d |
#+end_center"
	  (save-excursion (search-forward "| c |") (org-element-at-point))
          (insert "\n")
	  (search-forward "| c |")
	  (beginning-of-line)
	  (org-element-type (org-element-at-point)))))))

(ert-deftest test-org-element/cache-headline ()
  "Test basic expectations and common pitfalls for cached headings."
  ;; Appending to final headline in a subtree.
  (org-test-with-temp-text
      "
* Heading
Aliquam erat volutpat.

*** Subheading
** Another
** Final
:PROPERTIES:
:ID: some
<point>:END:
* Heading 2
** End
"
    (let ((org-element-use-cache t))
      (org-element-at-point)
      (save-excursion
        (goto-char (point-max))
        (org-element-at-point))
      (insert ":CATEOGORY: cat\n")
      (search-backward "* Heading")
      (should
       (eq (org-element-property :end (org-element-at-point))
           (save-excursion
             (search-forward "* Heading 2")
             (line-beginning-position))))
      (search-forward "* Heading 2")
      (beginning-of-line)
      (insert "\n\n")
      (search-backward "* Heading")
      (should
       (eq (org-element-property :end (org-element-at-point))
           (save-excursion
             (search-forward "* Heading 2")
             (line-beginning-position))))))
  ;; Appending at eob.
  (org-test-with-temp-text
      "
* Heading
*** Sub-heading
** Another
*** 1
***** 2
** 3
 Aenean in sem ac leo mollis blandit.


<point>"
    (let ((org-element-use-cache t))
      (org-element-at-point (point-max))
      (insert "\n\nTest\n")
      (search-backward "* Heading")
      (should
       (eq (point-max)
           (org-element-property :end (org-element-at-point))))))
  ;; Breaking headline at eob.
  (org-test-with-temp-text
      "
* Heading
*** Sub-heading
<point>"
    (let ((org-element-use-cache t))
      (org-element-at-point (point-max))
      (insert "* heading 2")
      (beginning-of-line)
      (should
       (eq (point-max)
           (org-element-property :end (org-element-at-point))))
      (delete-char 1)
      (search-backward "* Heading")
      (should
       (eq (point-max)
           (org-element-property :end (org-element-at-point))))))
  ;; Inserting low-level headline in-between.
  (org-test-with-temp-text
      "
* Heading
*** Sub-heading
<point>
*** Sub-heading 2
*** Sub-heading 3
"
    (let ((org-element-use-cache t))
      (org-element-at-point (point-max))
      (insert "** heading 2")
      (search-forward "*** Sub-heading 2")
      (should
       (equal (org-element-property :parent (org-element-at-point))
              (progn
                (search-backward "** heading 2")
                (org-element-at-point))))))
  ;; Test when `org-element--cache-for-removal' modifies common parent
  ;; (`org-data' in this case) around changed region.
  (org-test-with-temp-text
      "blah
:DRAWER:
<point>test
:END:
paragraph
* headline"
    (let ((org-element-use-cache t))
      (org-element-at-point (point-max))
      (delete-region (point) (point-max))
      (should (eq 'paragraph (org-element-type (org-element-at-point))))))
  ;; Remove/re-introduce heading.
  (org-test-with-temp-text
      "
* 1
** 1-1
a
<point>** 1-2
a
"
    (let ((org-element-use-cache t))
      (org-element-at-point (point-max))
      (insert "FOO")
      (should
       (equal
        "1-1"
        (org-element-property
         :title
         (org-element-lineage (org-element-at-point) '(headline)))))
      (insert "\n")
      (should
       (equal
        "1"
        (org-element-property
         :title
         (org-element-lineage (org-element-at-point) '(headline))))))))

(ert-deftest test-org-element/cache-ignored-locals ()
  "Test `org-element-ignored-local-variables' value.
Anything holding element cache state must not be copied around
buffers, as in `org-element-copy-buffer' or
`org-export-copy-buffer'.  Otherwise, we may encounter
hard-to-debug errors when cache state is either not up-to-date or
modified by side effect, influencing the original values."
  (mapatoms
   (lambda (var)
     (when (and (boundp var)
                (symbol-value var)
                (string-match-p "^org-element--cache" (symbol-name var))
                (not (memq var '(org-element--cache-interrupt-C-g-max-count
                               org-element--cache-map-statistics-threshold
                               org-element--cache-variables
                               org-element--cache-interrupt-C-g-count
                               org-element--cache-interrupt-C-g
                               org-element--cache-element-properties
                               org-element--cache-sensitive-re
                               org-element--cache-hash-size
                               org-element--cache-non-modifying-commands
                               org-element--cache-self-verify-frequency
                               org-element--cache-diagnostics-level))))
       (should (memq var org-element-ignored-local-variables))))))

(ert-deftest test-org-element/cache-get-key ()
  "Test `org-element-cache-get-key' and `org-element-cache-store-key'."
  (org-test-with-temp-text
      "* Heading
Paragraph
with text <point>

Another paragraph."
    (org-element-cache-store-key
     (org-element-lineage (org-element-at-point) '(headline))
     :robust-key 'val-robust 'robust)
    (org-element-cache-store-key
     (org-element-lineage (org-element-at-point) '(headline))
     :fragile-key 'val-fragile)
    (insert "and more text.")
    (should (eq 'val-robust
                (org-element-cache-get-key
                 (org-element-lineage (org-element-at-point) '(headline))
                 :robust-key)))
    (should (eq 'not-found
                (org-element-cache-get-key
                 (org-element-lineage (org-element-at-point) '(headline))
                 :fragile-key 'not-found))))
  ;; No length change in the altered.
  (org-test-with-temp-text
      "* Heading
Paragraph
<point>with text

Another paragraph."
    (org-element-cache-store-key
     (org-element-lineage (org-element-at-point) '(headline))
     :robust-key 'val-robust 'robust)
    (org-element-cache-store-key
     (org-element-lineage (org-element-at-point) '(headline))
     :fragile-key 'val-fragile)
    (search-forward "with")
    (org-combine-change-calls (match-beginning 0) (match-end 0)
      (replace-match "asdf"))
    (should (eq 'val-robust
                (org-element-cache-get-key
                 (org-element-lineage (org-element-at-point) '(headline))
                 :robust-key)))
    (should (eq 'not-found
                (org-element-cache-get-key
                 (org-element-lineage (org-element-at-point) '(headline))
                 :fragile-key 'not-found)))))

(provide 'test-org-element)

;;; test-org-element.el ends here

debug log:

solving 8b2cf1642 ...
found 8b2cf1642 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).