emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 896870303eca54a220ade05dd1b209085e941c7e 79077 bytes (raw)
name: testing/lisp/test-oc.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
 
;;; test-oc.el --- Tests for Org Cite library        -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Nicolas Goaziou

;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'oc)
(require 'ox)
;; We need `org-test-with-parsed-data' macro.
(require 'test-ox "../testing/lisp/test-ox.el")

(ert-deftest test-org-cite/register-processor ()
  "Test `org-cite-register-processor'."
  ;; Default test.
  (should
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name)))
  ;; Handle duplicate processor.
  (should
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name)
     (org-cite-register-processor 'name)))
  ;; Invalid name type.
  (should-error (org-cite-register-processor "name"))
  ;; Unknown property.
  (should-error
   (let ((org-cite--processors nil))
     (org-cite-register-processor :foo 'bar))))

(ert-deftest test-org-cite/unregister-processor ()
  "Test `org-cite-unregister-processor'."
  ;; Default test.
  (should-not
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name)
     (org-cite-unregister-processor 'name)
     org-cite--processors))
  ;; Error out with an unknown processor.
  (should-error
   (let ((org-cite--processors nil))
     (org-cite-unregister-processor 'name))))

(ert-deftest test-org-cite/inside-footnote-p ()
  "Test `org-cite-inside-footnote-p'."
  ;; Regular tests.
  (should
   (org-test-with-parsed-data "[fn:1] <point>[cite:@key]"
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t))))
  (should
   (org-test-with-parsed-data "[fn::<point>[cite:@key]]"
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t))))
  (should-not
   (org-test-with-parsed-data "[cite:@key]"
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t))))
  (should
   (org-test-with-parsed-data "[fn:1] Text.<point>[cite:@key]"
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t))))
  (should
   (org-test-with-parsed-data "[fn:1] <point>[cite:@key]\n: fixed width"
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t))))
  (should
   (org-test-with-parsed-data "[fn:1] <point>[cite:@key]  "
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t))))
  ;; Test optional argument.
  (should
   (org-test-with-parsed-data "[fn:1] <point>[cite:@key]"
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t)
      t)))
  (should-not
   (org-test-with-parsed-data "[fn:1] <point>See [cite:@key]."
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t)
      t)))
  (should
   (org-test-with-parsed-data "[fn::<point>[cite:@key]]"
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t)
      t)))
  (should-not
   (org-test-with-parsed-data "[fn::<point>See [cite:@key].]"
     (org-cite-inside-footnote-p
      (org-element-map tree 'citation #'identity info t)
      t))))

(ert-deftest test-org-cite/processor-has-capability-p ()
  "Test `org-cite-processor-has-capability-p'."
  ;; Unknown capability error.
  (should-error
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name :activate #'ignore)
     (org-cite-processor-has-capability-p 'name 'unknown)))
  ;; Test `activate' capability.
  (should
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name :activate #'ignore)
     (org-cite-processor-has-capability-p 'name 'activate)))
  (should-not
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name :follow #'ignore)
     (org-cite-processor-has-capability-p 'name 'activate)))
  ;; Test `export' capability.
  (should
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name
       :export-bibliography #'ignore
       :export-citation #'ignore)
     (org-cite-processor-has-capability-p 'name 'export)))
  (should
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name :export-citation #'ignore)
     (org-cite-processor-has-capability-p 'name 'export)))
  (should-not
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name :export-bibliography #'ignore)
     (org-cite-processor-has-capability-p 'name 'export)))
  ;; Test `follow' capability.
  (should
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name :follow #'ignore)
     (org-cite-processor-has-capability-p 'name 'follow)))
  (should-not
   (let ((org-cite--processors nil))
     (org-cite-register-processor 'name :activate #'ignore)
     (org-cite-processor-has-capability-p 'name 'follow)))
  ;; Unknown processors have no capabilities.
  (should-not (org-cite-processor-has-capability-p 'foo 'activate))
  (should-not (org-cite-processor-has-capability-p 'foo 'export))
  (should-not (org-cite-processor-has-capability-p 'foo 'follow)))

(ert-deftest test-org-cite/get-references ()
  "Test `org-cite-get-references'."
  ;; Return a list of citation reference objects.
  (should
   (equal '(citation-reference)
          (org-test-with-temp-text "[cite:@a]"
            (mapcar #'org-element-type
                    (org-cite-get-references (org-element-context))))))
  (should
   (equal '("a")
          (org-test-with-temp-text "[cite:@a]"
            (mapcar (lambda (r) (org-element-property :key r))
                    (org-cite-get-references (org-element-context))))))
  ;; Preserve order of references.
  (should
   (equal '("a" "b")
          (org-test-with-temp-text "[cite:@a;@b]"
            (mapcar (lambda (r) (org-element-property :key r))
                    (org-cite-get-references (org-element-context))))))
  ;; Parse prefix and suffix.
  (should
   (equal '("a" "b")
          (org-test-with-temp-text "[cite:prefix @a suffix;@b]"
            (mapcar (lambda (r) (org-element-property :key r))
                    (org-cite-get-references (org-element-context))))))
  (should
   (equal '(("prefix ") nil)
          (org-test-with-temp-text "[cite:prefix @a suffix;@b]"
            (mapcar (lambda (r) (org-element-property :prefix r))
                    (org-cite-get-references (org-element-context))))))
  (should
   (equal '((" suffix") nil)
          (org-test-with-temp-text "[cite:prefix @a suffix;@b]"
            (mapcar (lambda (r) (org-element-property :suffix r))
                    (org-cite-get-references (org-element-context))))))
  ;; Ignore common prefix and suffix.
  (should
   (equal '("a")
          (org-test-with-temp-text "[cite:common prefix; @a ; common suffix]"
            (mapcar (lambda (r) (org-element-property :key r))
                    (org-cite-get-references (org-element-context))))))
  ;; Preserve buffer positions.
  (should
   (org-test-with-temp-text "[cite:@a] [cite<point>:@b]"
     (= (1+ (point))
        (org-element-property :begin
                              (car (org-cite-get-references (org-element-context)))))))
  ;; Handle citation from a full parse tree.
  (should
   (equal '(1 2)
          (org-test-with-temp-text "[cite:@a] [cite:@a;@b]"
            (org-element-map (org-element-parse-buffer) 'citation
              (lambda (c) (length (org-cite-get-references c)))))))
  ;; Test optional argument.
  (should
   (equal '("a" "b")
          (org-test-with-temp-text "[cite:@a;@b]"
            (org-cite-get-references (org-element-context) t))))
  (should
   (equal '("a" "b")
          (org-test-with-temp-text "[cite:@a;@b]"
            (org-element-map (org-element-parse-buffer) 'citation
              (lambda (c) (org-cite-get-references c t)) nil t)))))

(ert-deftest test-org-cite/key-boundaries ()
  "Test `org-cite-key-boundaries'."
  (should
   (equal "@key"
          (org-test-with-temp-text "[cite:<point>@key]"
            (let ((boundaries (org-cite-key-boundaries (org-element-context))))
              (buffer-substring-no-properties
               (car boundaries)
               (cdr boundaries))))))
  (should
   (equal "@key"
          (org-test-with-temp-text "[cite:<point>prefix @key]"
            (let ((boundaries (org-cite-key-boundaries (org-element-context))))
              (buffer-substring-no-properties
               (car boundaries)
               (cdr boundaries))))))
  (should
   (equal "@key"
          (org-test-with-temp-text "[cite:<point>@key suffix]"
            (let ((boundaries (org-cite-key-boundaries (org-element-context))))
              (buffer-substring-no-properties
               (car boundaries)
               (cdr boundaries))))))
  (should
   (equal "@key"
          (org-test-with-temp-text "[cite:global ;<point>@key]"
            (let ((boundaries (org-cite-key-boundaries (org-element-context))))
              (buffer-substring-no-properties
               (car boundaries)
               (cdr boundaries))))))
  (should
   (equal "@key"
          (org-test-with-temp-text "[cite:<point>@key; global]"
            (let ((boundaries (org-cite-key-boundaries (org-element-context))))
              (buffer-substring-no-properties
               (car boundaries)
               (cdr boundaries)))))))

(ert-deftest test-org-cite/main-affixes ()
  "Test `org-cite-main-affixes'."
  (should
   (equal '(nil . nil)
          (org-test-with-temp-text "[cite:@key]"
            (org-cite-main-affixes (org-element-context)))))
  (should
   (equal '(nil . nil)
          (org-test-with-temp-text "[cite:@key1;@key2]"
            (org-cite-main-affixes (org-element-context)))))
  (should
   (equal '(("pre ") . nil)
          (org-test-with-temp-text "[cite:pre @key]"
            (org-cite-main-affixes (org-element-context)))))
  (should
   (equal '(("pre ") . (" post"))
          (org-test-with-temp-text "[cite:pre @key post]"
            (org-cite-main-affixes (org-element-context)))))
  (should
   (equal '(("pre ") . nil)
          (org-test-with-temp-text "[cite:global pre;pre @key]"
            (org-cite-main-affixes (org-element-context)))))
  (should
   (equal '(nil . (" post"))
          (org-test-with-temp-text "[cite:@key post;global post]"
            (org-cite-main-affixes (org-element-context)))))
  (should
   (equal '(("global pre") . ("global post"))
          (org-test-with-temp-text "[cite:global pre;@key1;@key2;global post]"
            (org-cite-main-affixes (org-element-context)))))
  (should
   (equal '(("global pre") . nil)
          (org-test-with-temp-text "[cite:global pre;pre1 @key1;pre2 @key2]"
            (org-cite-main-affixes (org-element-context)))))
  (should
   (equal '(nil . (" global post"))
          (org-test-with-temp-text "[cite:@key1 post1;@key2 post2; global post]"
            (org-cite-main-affixes (org-element-context))))))

(ert-deftest test-org-cite/supported-styles ()
  "Test `org-cite-supported-styles'."
  ;; Default behavior is to use export processors.
  (should
   (equal '((("")))
          (let ((org-cite--processors nil)
                (org-cite-export-processors '((t test))))
            (org-cite-register-processor 'test :cite-styles '(((""))))
            (org-cite-supported-styles))))
  (should
   (equal '((("foo" "f")) (("")))
          (let ((org-cite--processors nil)
                (org-cite-export-processors '((t test))))
            (org-cite-register-processor 'test
              :cite-styles '((("foo" "f")) ((""))))
            (org-cite-supported-styles))))
  ;; Explicitly provide a processor.
  (should
   (equal '((("")))
          (let ((org-cite--processors nil))
            (org-cite-register-processor 'test :cite-styles '(((""))))
            (org-cite-supported-styles '(test)))))
  ;; Merge style shortcuts.
  (should
   (equal '((("foo" "f" "g")))
          (let ((org-cite--processors nil))
            (org-cite-register-processor 'test :cite-styles '((("foo" "f"))))
            (org-cite-register-processor 'test2 :cite-styles '((("foo" "g"))))
            (org-cite-supported-styles '(test test2)))))
  ;; Merge style variants.
  (should
   (equal '((("foo") ("bar") ("baz")))
          (let ((org-cite--processors nil))
            (org-cite-register-processor 'test
              :cite-styles '((("foo") ("bar"))))
            (org-cite-register-processor 'test2
              :cite-styles '((("foo") ("baz"))))
            (org-cite-supported-styles '(test test2)))))
  ;; Merge variant shortcuts.
  (should
   (equal '((("foo") ("bar" "b" "c")))
          (let ((org-cite--processors nil))
            (org-cite-register-processor 'test
              :cite-styles '((("foo") ("bar" "b"))))
            (org-cite-register-processor 'test2
              :cite-styles '((("foo") ("bar" "c"))))
            (org-cite-supported-styles '(test test2)))))
  ;; Ignore duplicates.
  (should
   (equal '((("foo") ("bar")))
          (let ((org-cite--processors nil))
            (org-cite-register-processor 'test
              :cite-styles '((("foo") ("bar"))))
            (org-cite-register-processor 'test2
              :cite-styles '((("foo") ("bar"))))
            (org-cite-supported-styles '(test test2)))))
  (should
   (equal '((("foo") ("bar" "b")))
          (let ((org-cite--processors nil))
            (org-cite-register-processor 'test
              :cite-styles '((("foo") ("bar" "b"))))
            (org-cite-register-processor 'test2
              :cite-styles '((("foo") ("bar" "b"))))
            (org-cite-supported-styles '(test test2))))))

(ert-deftest test-org-cite/delete-citation ()
  "Test `org-cite-delete-citation'."
  ;; Error when not on a citation or citation reference.
  (should-error
   (org-test-with-temp-text "Text"
     (org-cite-delete-citation (org-element-context))))
  ;; When argument is a citation, delete it completely.  Manage
  ;; properly blanks around it.
  (should
   (equal ""
          (org-test-with-temp-text "[cite:@key]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal "Before After"
          (org-test-with-temp-text "Before [<point>cite:@key] After"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal "Before After"
          (org-test-with-temp-text "Before [<point>cite:@key]After"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal "Before After"
          (org-test-with-temp-text "Before[<point>cite:@key] After"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  ;; Ensure there is at least a blank to separate consecutive objects.
  (should
   (equal "Before After"
          (org-test-with-temp-text "Before[<point>cite:@key]After"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  ;; Remove trailing blanks when removing the citation.
  (should
   (equal "Before"
          (org-test-with-temp-text "Before[<point>cite:@key]  "
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  ;; Preserve indentation if citation is at the beginning of the line.
  (should
   (equal "  After"
          (org-test-with-temp-text "  [<point>cite:@key] After"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  ;; When the citation is alone on a line, remove the whole line.
  (should
   (equal "Line 1\nLine 3"
          (org-test-with-temp-text "Line 1\n[<point>cite:@key]\nLine 3"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  ;; When there is only one citation reference object, remove the full
  ;; citation.
  (should
   (equal ""
          (org-test-with-temp-text "[cite:@<point>key]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal ""
          (org-test-with-temp-text "[cite:pre @<point>key post]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal ""
          (org-test-with-temp-text "[cite:pre; @<point>key ;post]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  ;; Otherwise, remove the reference, including any affix.
  (should
   (equal "[cite:@before;@after]"
          (org-test-with-temp-text "[cite:@before;@<point>key;@after]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal "[cite:@before;@after]"
          (org-test-with-temp-text "[cite:@before;pre @<point>key post;@after]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal "[cite:@before]"
          (org-test-with-temp-text "[cite:@before;@<point>key]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal "[cite:@before;post]"
          (org-test-with-temp-text "[cite:@before;@<point>key;post]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal "[cite:@after]"
          (org-test-with-temp-text "[cite:@<point>key;@after]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string))))
  (should
   (equal "[cite:pre;@after]"
          (org-test-with-temp-text "[cite:pre;@<point>key;@after]"
            (org-cite-delete-citation (org-element-context))
            (buffer-string)))))

(ert-deftest test-org-cite/list-bibliography-files ()
  "Test `org-cite-list-bibliography-files'."
  (should
   (equal '("/bibliography")
          (org-test-with-temp-text "#+bibliography: /bibliography"
            (let ((org-cite-global-bibliography nil))
              (org-cite-list-bibliography-files)))))
  (should
   (equal '("/bibliography")
          (org-test-with-temp-text "#+bibliography: \"/bibliography\""
            (let ((org-cite-global-bibliography nil))
              (org-cite-list-bibliography-files)))))
  (should
   (equal '("/bibliography" "/other-bibliography")
          (org-test-with-temp-text "#+bibliography: /bibliography"
            (let ((org-cite-global-bibliography '("/other-bibliography")))
              (org-cite-list-bibliography-files)))))
  (should
   (equal '(t)
          (org-test-with-temp-text "#+bibliography: ./bibliography"
            (let ((org-cite-global-bibliography nil))
              (mapcar #'file-name-absolute-p (org-cite-list-bibliography-files))))))
  (should
   (equal '("/bibliographyA" "/bibliographyB")
          (org-test-with-temp-text
              "#+bibliography: /bibliographyA\n#+bibliography: /bibliographyB"
            (let ((org-cite-global-bibliography nil))
              (org-cite-list-bibliography-files)))))
  (should
   (equal '("/bibliographyA")
          (org-test-with-temp-text
              "#+bibliography: /bibliographyA\n#+bibliography: /bibliographyA"
            (let ((org-cite-global-bibliography nil))
              (org-cite-list-bibliography-files))))))

(ert-deftest test-org-cite/bibliography-style ()
  "Test `org-cite-bibliography-style'."
  ;; Extract style from global processor definition.
  (should
   (equal "a"
          (catch :exit
            (org-test-with-temp-text "#+print_bibliography:"
              (let ((org-cite-export-processors '((t . (foo "a" "b"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
                  :export-citation #'ignore)
                (org-export-as (org-export-create-backend)))))))
  ;; Extract style from local processor definition.
  (should
   (equal "a"
          (catch :exit
            (org-test-with-temp-text
                "#+cite_export: foo a b\n#+print_bibliography:"
              (let ((org-cite-export-processors nil)
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
                  :export-citation #'ignore)
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal "a b"
          (catch :exit
            (org-test-with-temp-text
                "#+cite_export: foo \"a b\" c\n#+print_bibliography:"
              (let ((org-cite-export-processors nil)
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
                  :export-citation #'ignore)
                (org-export-as (org-export-create-backend)))))))
  ;; Test priority: first keyword, then local.
  (should
   (equal "local"
          (catch :exit
            (org-test-with-temp-text
                "#+print_bibliography:\n#+cite_export: foo local a\n[cite:@a]"
              (let ((org-cite-export-processors '((t . (foo "global" "b"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
                  :export-citation #'ignore)
                (org-export-as (org-export-create-backend)))))))
  ;; Explicit "nil" styles forces default style.
  (should-not
   (catch :exit
     (org-test-with-temp-text
         "#+print_bibliography:\n#+cite_export: foo nil a\n[cite:@a]"
       (let ((org-cite-export-processors '((t . (foo "global" "b"))))
             (org-cite--processors nil))
         (org-cite-register-processor 'foo
           :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
           :export-citation #'ignore)
         (org-export-as (org-export-create-backend)))))))

(ert-deftest test-org-cite/bibliography-properties ()
  "Test `org-cite-bibliography-properties'."
  ;; Return nil without properties.
  (should-not
   (org-test-with-parsed-data "#+print_bibliography:"
     (org-element-map tree 'keyword
       #'org-cite-bibliography-properties info t)))
  ;; Regular tests.
  (should
   (equal
    '(:key "value")
    (org-test-with-parsed-data "#+print_bibliography: :key value"
      (org-element-map tree 'keyword
        #'org-cite-bibliography-properties info t))))
  (should
   (equal
    '(:key "value" :key2 "value2")
    (org-test-with-parsed-data "#+print_bibliography: :key value :key2 value2"
      (org-element-map tree 'keyword
        #'org-cite-bibliography-properties info t))))
  ;; Allow empty values.
  (should
   (equal
    '(:key)
    (org-test-with-parsed-data "#+print_bibliography: :key"
      (org-element-map tree 'keyword
        #'org-cite-bibliography-properties info t))))
  (should
   (equal
    '(:key "")
    (org-test-with-parsed-data "#+print_bibliography: :key \"\""
      (org-element-map tree 'keyword
        #'org-cite-bibliography-properties info t))))
  ;; Allow space with double quotes.
  (should
   (equal
    '(:key "space space")
    (org-test-with-parsed-data "#+print_bibliography: :key \"space space\""
      (org-element-map tree 'keyword
        #'org-cite-bibliography-properties info t))))
  ;; Ignore spurious values.
  (should
   (equal
    '(:key "space")
    (org-test-with-parsed-data "#+print_bibliography: :key space space"
      (org-element-map tree 'keyword
        #'org-cite-bibliography-properties info t))))
  ;; Gracefully handle incomplete quotations.
  (should
   (equal
    '(:key "\"space" :key2 "value2")
    (org-test-with-parsed-data "#+print_bibliography: :key \"space :key2 value2"
      (org-element-map tree 'keyword
        #'org-cite-bibliography-properties info t)))))

(ert-deftest test-org-cite/citation-style ()
  "Test `org-cite-citation-style'."
  ;; Extract style from global processor definition.
  (should
   (equal '("b")
          (catch :exit
            (org-test-with-temp-text "[cite:@a]"
              (let ((org-cite-export-processors '((t . (foo "a" "b"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '("b" . "variant")
          (catch :exit
            (org-test-with-temp-text "[cite:@a]"
              (let ((org-cite-export-processors '((t . (foo "a" "b/variant"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  ;; Extract style from local processor definition.
  (should
   (equal '("b")
          (catch :exit
            (org-test-with-temp-text "#+cite_export: foo a b\n[cite:@a]"
              (let ((org-cite-export-processors nil)
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '("b c")
          (catch :exit
            (org-test-with-temp-text "#+cite_export: foo a \"b c\"\n[cite:@a]"
              (let ((org-cite-export-processors nil)
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '("b" . "variant")
          (catch :exit
            (org-test-with-temp-text "#+cite_export: foo a b/variant\n[cite:@a]"
              (let ((org-cite-export-processors nil)
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '("b c" . "variant")
          (catch :exit
            (org-test-with-temp-text
                "#+cite_export: foo a \"b c/variant\"\n[cite:@a]"
              (let ((org-cite-export-processors nil)
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  ;; Extract style from citation itself.
  (should
   (equal '("b")
          (catch :exit
            (org-test-with-temp-text "[cite/b:@a]"
              (let ((org-cite-export-processors '((t . (foo nil nil))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '("b" . "variant")
          (catch :exit
            (org-test-with-temp-text "[cite/b/variant:@a]"
              (let ((org-cite-export-processors '((t . (foo nil nil))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  ;; Test priority: first object, then local.
  (should
   (equal '("object")
          (catch :exit
            (org-test-with-temp-text
                "#+cite_export: foo nil local\n[cite/object:@a]"
              (let ((org-cite-export-processors '((t . (foo nil "global"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '("local")
          (catch :exit
            (org-test-with-temp-text
                "#+cite_export: foo nil local\n[cite:@a]"
              (let ((org-cite-export-processors '((t . (foo nil "global"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  ;; Force default style with "nil".
  (should
   (equal '(nil)
          (catch :exit
            (org-test-with-temp-text
                "#+cite_export: foo nil nil\n[cite:@a]"
              (let ((org-cite-export-processors '((t . (foo nil "global"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '(nil)
          (catch :exit
            (org-test-with-temp-text "[cite/nil:@a]"
              (let ((org-cite-export-processors '((t . (foo nil "global"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  ;; Test variant inheritance.
  (should
   (equal '("local" . "v2")
          (catch :exit
            (org-test-with-temp-text "[cite/local/v2:@a]"
              (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '("global" . "v2")
          (catch :exit
            (org-test-with-temp-text "[cite//v2:@a]"
              (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '(nil . "v2")
          (catch :exit
            (org-test-with-temp-text "[cite/nil/v2:@a]"
              (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend)))))))
  (should
   (equal '("local" . nil)
          (catch :exit
            (org-test-with-temp-text "[cite/local:@a]"
              (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
                    (org-cite--processors nil))
                (org-cite-register-processor 'foo
                  :export-citation (lambda (_ s _ _) (throw :exit s)))
                (org-export-as (org-export-create-backend))))))))

(ert-deftest test-org-cite/list-citations ()
  "Test `org-cite-list-citations'."
  (should
   (equal '("a")
          (org-test-with-parsed-data "Test [cite:@a]"
            (cl-mapcan (lambda (c)
                         (mapcar (lambda (ref)
                                   (org-element-property :key ref))
                                 (org-element-contents c)))
                       (org-cite-list-citations info)))))
  (should
   (equal '("a" "b")
          (org-test-with-parsed-data "Test [cite:@a] [cite:@b]"
            (cl-mapcan (lambda (c)
                         (mapcar (lambda (ref)
                                   (org-element-property :key ref))
                                 (org-element-contents c)))
                       (org-cite-list-citations info)))))
  (should
   (equal '("a")
          (org-test-with-parsed-data "Test[fn:1]\n[fn:1] [cite:@a]"
            (cl-mapcan (lambda (c)
                         (mapcar (lambda (ref)
                                   (org-element-property :key ref))
                                 (org-element-contents c)))
                       (org-cite-list-citations info)))))
  (should
   (equal '("a" "b")
          (org-test-with-parsed-data "First[cite:@a] Second[fn:1]\n[fn:1] [cite:@b]"
            (cl-mapcan (lambda (c)
                         (mapcar (lambda (ref)
                                   (org-element-property :key ref))
                                 (org-element-contents c)))
                       (org-cite-list-citations info)))))
  (should
   (equal '("b" "a")
          (org-test-with-parsed-data "First[fn:1] Second[cite:@a]\n[fn:1] [cite:@b]"
            (cl-mapcan (lambda (c)
                         (mapcar (lambda (ref)
                                   (org-element-property :key ref))
                                 (org-element-contents c)))
                       (org-cite-list-citations info)))))
  (should
   (equal '("a" "b")
          (org-test-with-parsed-data
              "Text[fn:1][fn:2]\n[fn:1] [cite:@a]\n\n[fn:2] [cite:@b]"
            (cl-mapcan (lambda (c)
                         (mapcar (lambda (ref)
                                   (org-element-property :key ref))
                                 (org-element-contents c)))
                       (org-cite-list-citations info)))))
  (should
   (equal '("b" "a")
          (org-test-with-parsed-data
              "Text[fn:1]\n[fn:1] [fn:2][cite:@a]\n\n[fn:2] [cite:@b]"
            (cl-mapcan (lambda (c)
                         (mapcar (lambda (ref)
                                   (org-element-property :key ref))
                                 (org-element-contents c)))
                       (org-cite-list-citations info)))))
  (should
   (equal '("a" "b")
          (org-test-with-parsed-data
              "Text[fn:1]\n[fn:1] [cite:@a][fn:2]\n\n[fn:2] [cite:@b]"
            (cl-mapcan (lambda (c)
                         (mapcar (lambda (ref)
                                   (org-element-property :key ref))
                                 (org-element-contents c)))
                       (org-cite-list-citations info)))))
  (should
   (equal '("a")
          (org-test-with-parsed-data "Text[fn::[cite:@a]]"
            (cl-mapcan (lambda (c)
                         (mapcar (lambda (ref)
                                   (org-element-property :key ref))
                                 (org-element-contents c)))
                       (org-cite-list-citations info))))))

(ert-deftest test-org-cite/list-keys ()
  "Test `org-cite-list-keys'."
  (should
   (equal '("a")
          (org-test-with-parsed-data "Test [cite:@a]"
            (org-cite-list-keys info))))
  (should
   (equal '("a" "b")
          (org-test-with-parsed-data "Test [cite:@a] [cite:@b]"
            (org-cite-list-keys info))))
  ;; Remove duplicates.
  (should
   (equal '("a")
          (org-test-with-parsed-data "Test [cite:@a] [cite:@a]"
            (org-cite-list-keys info))))
  ;; Keys are ordered by first appearance in the document.
  (should
   (equal '("a" "b")
          (org-test-with-parsed-data "Test [cite:@a] [cite:@b] [cite:@a]"
            (org-cite-list-keys info))))
  (should
   (equal '("a" "b" "c")
          (org-test-with-parsed-data
              "Test [cite:@a][fn:1] [cite:@c] [cite:@a]\n[fn:1] [cite:@b]"
            (org-cite-list-keys info)))))

(ert-deftest test-org-cite/key-number ()
  "Test `org-cite-key-number'."
  (should
   (= 1 (org-test-with-parsed-data "[cite:@key]"
          (org-cite-key-number "key" info))))
  (should
   (equal '(1 2)
          (org-test-with-parsed-data "[cite:@key] [cite:@key2] [cite:@key]"
            (list (org-cite-key-number "key" info)
                  (org-cite-key-number "key2" info)))))
  ;; When "predicate" is nil, keys are sorted by appearance order in
  ;; the buffer.
  (should
   (equal '((1 . "a") (2 . "c") (3 . "b"))
          (org-test-with-parsed-data
              "[cite:@a][fn:1] [cite:@b]\n[fn:1] [cite:@c]"
            (sort (mapcar (lambda (key)
                            (cons (org-cite-key-number key info) key))
                          '("a" "b" "c"))
                  #'car-less-than-car))))
  (should
   (equal '((1 . "a") (2 . "b") (3 . "c"))
          (org-test-with-parsed-data
              "[cite:@a][fn:1] [cite:@b]\n[fn:1] [cite:@c]"
            (sort (mapcar (lambda (key)
                            (cons (org-cite-key-number key info #'string<) key))
                          '("a" "b" "c"))
                  #'car-less-than-car)))))

(ert-deftest test-org-cite/wrap-citation ()
  "Test `org-cite-wrap-citation'."
  ;; Reference test.
  (should
   (org-test-with-parsed-data "[cite:@key]"
     (org-element-map tree 'citation
       (lambda (c)
         (org-cite-wrap-citation c info)
         (org-cite-inside-footnote-p c))
       info)))
  ;; Created footnote is anonymous.
  (should-not
   (org-test-with-parsed-data "[cite:@key]  "
     (org-element-map tree 'citation
       (lambda (c)
         (org-cite-wrap-citation c info)
         (org-element-property :label (org-cite-inside-footnote-p c)))
       info)))
  ;; Created footnote is inline.
  (should
   (equal '(inline)
          (org-test-with-parsed-data "[cite:@key]"
            (org-element-map tree 'citation
              (lambda (c)
                (org-cite-wrap-citation c info)
                (org-element-property :type
                                      (org-cite-inside-footnote-p c)))
              info))))
  ;; Preserve `:post-blank' property.
  (should
   (equal '(2)
          (org-test-with-parsed-data "[cite:@key]  "
            (org-element-map tree 'citation
              (lambda (c)
                (org-cite-wrap-citation c info)
                (org-element-property :post-blank
                                      (org-cite-inside-footnote-p c)))
              info))))
  ;; Set `:post-blank' to 0 in the element before new footnote.
  (should-not
   (org-test-with-parsed-data "Text [cite:@key]"
     (org-element-map tree 'citation
       (lambda (c)
         (org-cite-wrap-citation c info)
         (let ((previous
                (org-export-get-previous-element
                 (org-cite-inside-footnote-p c) info)))
           (string-match (rx blank string-end) previous)))
       info)))
  (should
   (equal '(0)
          (org-test-with-parsed-data "*Text* [cite:@key]"
            (org-element-map tree 'citation
              (lambda (c)
                (org-cite-wrap-citation c info)
                (let ((previous
                       (org-export-get-previous-element
                        (org-cite-inside-footnote-p c) info)))
                  (org-element-property :post-blank previous)))
              info))))
  (should
   (equal '("Text")
          (org-test-with-parsed-data "Text [cite:@key]"
            (org-element-map tree 'citation
              (lambda (c)
                (org-cite-wrap-citation c info)
                (org-export-get-previous-element
                 (org-cite-inside-footnote-p c) info))
              info)))))

(defun test-org-cite--export-with-rule (text &optional rule punct)
  "Export TEXT string using RULE for punctuation positioning.
Call `org-cite-adjust-note' on each citation object with RULE and, PUNCT
arguments.  Replace citation with \"@\" character in the output."
  (org-test-with-temp-text text
    (let ((org-cite--processors nil))
      (org-cite-register-processor 'test
        :export-citation
        (lambda (citation _s _b info)
          (org-cite-adjust-note citation info rule punct)
          "@"))
      (let ((org-cite-export-processors '((t . (test nil nil)))))
        (org-trim
         (org-export-as
	  (org-export-create-backend
	   :transcoders
	   '((section . (lambda (_s c _i) (replace-regexp-in-string " @" "@" c)))
             (paragraph . (lambda (_s c _i) c))))))))))

(ert-deftest test-org-cite/adjust-note ()
  "Test `org-cite-adjust-note' function."
  ;; Basic tests for all rules.  In the output, @ replaces citation.
  (let ((cases '("\"[cite:@k]!"
                 ".\"[cite:@k]!"
                 "\"[cite:@k]"
                 ".\"[cite:@k]"
                 ".[cite:@k]"
                 "[cite:@k]!")))
    (should                             ;test (inside inside after)
     (equal
      '(iia "!@\"" ".@\"!" "@\"" ".@\"" ".@" "!@")
      (cons 'iia
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(inside inside after)))
                    cases))))
    (should                             ;test (inside inside before)
     (equal
      '(iib "@!\"" "@.\"!" "@\"" "@.\"" "@." "@!")
      (cons 'iib
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(inside inside before)))
                    cases))))
    (should                             ;test (inside outside after)
     (equal
      '(ioa "!\"@" ".\"!@" "\"@" ".\"@" ".@" "!@")
      (cons 'ioa
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(inside outside after)))
                    cases))))
    (should                             ;test (inside outside before)
     (equal
      '(iob "!\"@" ".\"@!" "\"@" ".\"@" "@." "@!")
      (cons 'iob
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(inside outside before)))
                    cases))))
    (should                             ;test (inside same after)
     (equal
      '(isa "!@\"" ".\"!@" "\"@" ".@\"" ".@" "!@")
      (cons 'isa
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(inside same after)))
                    cases))))
    (should                             ;test (inside same before)
     (equal
      '(isb "@!\"" ".\"@!" "\"@" "@.\"" "@." "@!")
      (cons 'isb
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(inside same before)))
                    cases))))
    (should                             ;test (outside inside after)
     (equal
      '(oia "@\"!" ".@\"!" "@\"" "@\"." ".@" "!@")
      (cons 'oia
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(outside inside after)))
                    cases))))
    (should                             ;test (outside inside before)
     (equal
      '(oib "@\"!" "@.\"!" "@\"" "@\"." "@." "@!")
      (cons 'oib
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(outside inside before)))
                    cases))))
    (should                             ;test (outside outside after)
     (equal
      '(ooa "\"!@" ".\"!@" "\"@" "\".@" ".@" "!@")
      (cons 'ooa
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(outside outside after)))
                    cases))))
    (should                             ;test (outside outside before)
     (equal
      '(oob "\"@!" ".\"@!" "\"@" "\"@." "@." "@!")
      (cons 'oob
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(outside outside before)))
                    cases))))
    (should                             ;test (outside same after)
     (equal
      '(osa "\"!@" ".\"!@" "\"@" "\".@" ".@" "!@")
      (cons 'osa
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(outside same after)))
                    cases))))
    (should                             ;test (outside same before)
     (equal
      '(osb "\"@!" ".\"@!" "\"@" "\"@." "@." "@!")
      (cons 'osb
            (mapcar (lambda (c)
                      (test-org-cite--export-with-rule
                       c '(outside same before)))
                    cases)))))
  ;; Test `adaptive' behaviour.
  (should
   (equal "@\"."
          (test-org-cite--export-with-rule ".\" [cite:@k]"
                                           '(adaptive inside after))))
  (should
   (equal "@\"!"
          (test-org-cite--export-with-rule "\" [cite:@k]!"
                                           '(adaptive inside after))))
  (should
   (equal ".@\""
          (test-org-cite--export-with-rule ".\"[cite:@k]"
                                           '(adaptive inside after))))
  (should
   (equal "!@\""
          (test-org-cite--export-with-rule "\"[cite:@k]!"
                                           '(adaptive inside after))))
  ;; Handle white space when inserting citation before quotation mark
  ;; or punctuation.
  (should
   (equal ",@\" next"
          (test-org-cite--export-with-rule ",\" [cite:@k] next"
                                           '(inside inside after))))
  (should
   (equal "@,\" next"
          (test-org-cite--export-with-rule ",\" [cite:@k] next"
                                           '(inside inside before))))
  (should
   (equal "@\"."
          (test-org-cite--export-with-rule "\" [cite:@k]."
                                           '(outside inside before))))
  (should
   (equal "@\" !"
          (test-org-cite--export-with-rule "\" [cite:@k] !"
                                           '(outside inside before))))
  (should
   (equal "text@ !"
          (test-org-cite--export-with-rule "text ![cite:@k]"
                                           '(inside outside before))))
  ;; Preserve white space between citation and final punctuation when
  ;; moving citation past final punctuation.
  (should
   (equal "text !@"
          (test-org-cite--export-with-rule "text [cite:@k] !"
                                           '(inside inside after))))
  (should
   (equal "text\n !@"
          (test-org-cite--export-with-rule "text [cite:@k]\n !"
                                           '(inside inside after))))
  ;; Choose punctuation with optional argument.
  (should-not
   (equal "!@"
          (test-org-cite--export-with-rule "[cite:@k]!"
                                           '(inside outside after)
                                           '("."))))
  (should
   (equal ".@"
          (test-org-cite--export-with-rule "[cite:@k]."
                                           '(inside outside after)
                                           '(".")))))

(ert-deftest test-org-cite/parse-elements ()
  "Test `org-cite-parse-elements' function."
  (should-error (org-cite-parse-elements "* H"))
  (should-error (org-cite-parse-elements "Paragraph\n* H"))
  (should
   (equal '(paragraph)
          (mapcar #'org-element-type (org-cite-parse-elements "s"))))
  (should
   (equal '(paragraph paragraph)
          (mapcar #'org-element-type (org-cite-parse-elements "Text\n\nText")))))

(ert-deftest test-org-cite/parse-objects ()
  "Test `org-cite-parse-objects' function."
  (should
   (equal '(plain-text)
          (mapcar #'org-element-type (org-cite-parse-objects "s"))))
  (should
   (equal '(plain-text bold)
          (mapcar #'org-element-type (org-cite-parse-objects "s *b*"))))
  (should
   (equal '(link)
          (mapcar #'org-element-type (org-cite-parse-objects "[[link]]"))))
  ;; When optional argument is non-nil, only recognize types allowed
  ;; in as a citation reference affix.
  (should-not
   (equal '(link)
          (mapcar #'org-element-type (org-cite-parse-objects "[[link]]" t))))
  (should
   (equal '(bold)
          (mapcar #'org-element-type (org-cite-parse-objects "*b*" t)))))

(ert-deftest test-org-cite/make-paragraph ()
  "Test `org-cite-make-paragraph' function."
  ;; Check string as argument.
  (should
   (eq 'paragraph
       (org-element-type (org-cite-make-paragraph "a"))))
  (should
   (equal '("a")
          (org-element-contents (org-cite-make-paragraph "a"))))
  ;; Check object as argument.
  (should
   (eq 'paragraph
       (org-element-type
        (org-cite-make-paragraph (org-element-create 'bold nil "b")))))
  (should
   (equal '(bold)
          (mapcar #'org-element-type
                  (org-element-contents
                   (org-cite-make-paragraph (org-element-create 'bold nil "b"))))))
  ;; Check secondary string as argument.
  (should
   (eq 'paragraph
       (org-element-type (org-cite-make-paragraph '("a")))))
  (should
   (equal '("a")
          (org-element-contents (org-cite-make-paragraph '("a")))))
  ;; Mix all types of arguments.
  (should
   (equal '(plain-text bold plain-text)
          (mapcar #'org-element-type
                  (org-element-contents
                   (org-cite-make-paragraph
                    "a" (org-element-create 'bold nil "b") '("c"))))))
  ;; Check `:parent' property.
  (should
   (eq 'paragraph
       (org-element-type
        (org-element-property
         :parent
         (car (org-element-contents (org-cite-make-paragraph "a"))))))))

(ert-deftest test-org-cite/emphasize ()
  "Test `org-cite-emphasize' function."
  ;; Raise an error if first argument has wrong type.
  (should-error (org-cite-emphasize 'code "a"))
  ;; Check string argument.
  (should (eq 'bold (org-element-type (org-cite-emphasize 'bold "a"))))
  (should (equal '("a") (org-element-contents (org-cite-emphasize 'bold "a"))))
  ;; Check object argument.
  (should
   (eq 'bold
       (org-element-type
        (org-cite-emphasize 'bold (org-element-create 'bold nil "a")))))
  (should
   (equal '(italic)
          (mapcar #'org-element-type
                  (org-element-contents
                   (org-cite-emphasize 'bold
                     (org-element-create 'italic nil "a"))))))
  ;; Check secondary string argument.
  (should (eq 'bold (org-element-type (org-cite-emphasize 'bold '("a")))))
  (should (equal '("a") (org-element-contents (org-cite-emphasize 'bold '("a")))))
  ;; Mix all types of arguments.
  (should
   (equal '(plain-text italic plain-text)
          (mapcar #'org-element-type
                  (org-element-contents
                   (org-cite-emphasize 'bold
                     "a" (org-element-create 'italic nil "b") '("c"))))))
  ;; Check `:parent' property.
  (should
   (eq 'bold
       (org-element-type
        (org-element-property
         :parent
         (car (org-element-contents (org-cite-emphasize 'bold "a"))))))))

(ert-deftest test-org-cite/concat ()
  "Test `org-cite-concat' function."
  ;; Return nil when there is no data.
  (should
   (equal "" (org-element-interpret-data (org-cite-concat))))
  ;; Concatenate strings, objects and secondary strings.
  (should
   (equal "ab"
          (org-element-interpret-data (org-cite-concat "a" "b"))))
  (should
   (equal "*a* b"
          (org-element-interpret-data
           (org-cite-concat (org-element-create 'bold nil "a") " b"))))
  (should
   (equal "*a* b"
          (org-element-interpret-data
           (org-cite-concat
            (list (org-element-create 'bold nil "a")) " b"))))
  ;; Return an error for any other object type.
  (should-error (org-cite-concat 2)))

(ert-deftest test-org-cite/mapconcat ()
  "Test `org-cite-mapconcat' function."
  (should
   (equal ""
          (org-element-interpret-data
           (org-cite-mapconcat #'identity nil ""))))
  (should
   (equal "ab"
          (org-element-interpret-data
           (org-cite-mapconcat #'identity '("a" "b") ""))))
  (should
   (equal "*a* b *c*"
          (org-element-interpret-data
           (org-cite-mapconcat
            #'identity
            (list (org-element-create 'bold nil "a")
                  (list " b " (org-element-create 'bold nil "c"))) ""))))
  (should
   (equal "*a* *b*"
          (org-element-interpret-data
           (org-cite-mapconcat
            (lambda (s) (org-element-create 'bold nil s))
            '("a" "b") " "))))
  (should
   (equal "*a* b*c*"
          (org-element-interpret-data
           (org-cite-mapconcat
            #'identity
            (list (org-element-create 'bold nil "a")
                  (list "b" (org-element-create 'bold nil "c"))) " ")))))

\f
;;; Test capabilities.
(ert-deftest test-org-cite/activate-capability ()
  "Test \"activate\" capability."
  ;; Standard test.
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "[cite:@key]"
           (let ((org-cite--processors nil)
                 (org-cite-activate-processor 'foo))
             (org-cite-register-processor 'foo
               :activate (lambda (_) (throw :exit 'success)))
             (font-lock-ensure))))))
  ;; If there is no "follow" processor, or if processor does not
  ;; handle this capability, fall back to fontifying whole citation
  ;; with `org-cite' face and each key with `org-cite-key' face.
  (should
   (eq 'org-cite
       (org-test-with-temp-text "[cite:@key]"
         (let ((org-cite-activate-processor nil))
           (font-lock-ensure)
           (face-at-point)))))
  (should
   (eq 'org-cite-key
       (org-test-with-temp-text "[cite:@<point>key]"
         (let ((org-cite-activate-processor nil))
           (font-lock-ensure)
           (face-at-point)))))
  (should
   (eq 'org-cite
       (org-test-with-temp-text "[cite:@key]"
         (let ((org-cite--processors nil)
               (org-cite-activate-processor 'foo))
           (org-cite-register-processor 'foo)
           (font-lock-ensure)
           (face-at-point))))))

(ert-deftest test-org-cite/export-capability ()
  "Test \"export\" capability."
  ;; Regular citations export.
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "[cite:@key]"
           (let ((org-cite--processors nil)
                 (org-cite-export-processors '((t . (foo nil nil)))))
             (org-cite-register-processor 'foo
               :export-citation (lambda (&rest _) (throw :exit 'success)))
             (org-export-as (org-export-create-backend)))))))
  ;; Export citation as string.
  (should
   (equal "citation\n"
          (org-test-with-temp-text "[cite:@key]"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-citation (lambda (&rest _) "citation"))
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c)))))))))
  ;; Export citation as parsed object.
  (should
   (equal "success\n"
          (org-test-with-temp-text "[cite:@key]"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-citation (lambda (&rest _)
                                   (org-element-create 'bold nil "cite")))
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c))
                                (bold . (lambda (&rest _) "success")))))))))
  ;; Export citation as a secondary string.
  (should
   (equal "boldtwo\n"
          (org-test-with-temp-text "[cite:@key]"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-citation (lambda (&rest _)
                                   (list (org-element-create 'bold nil "one")
                                         "two")))
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c))
                                (bold . (lambda (&rest _) "bold")))))))))
  ;; When exporting citation as a secondary string, last object
  ;; inherits post-blank from initial citation.
  (should
   (equal "twobold one-space\n"
          (org-test-with-temp-text "[cite:@key] one-space"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-citation (lambda (&rest _)
                                   (list "two"
                                         (org-element-create 'bold nil "one"))))
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c))
                                (bold . (lambda (&rest _) "bold")))))))))
  (should
   (equal "boldtwo one-space\n"
          (org-test-with-temp-text "[cite:@key] one-space"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-citation (lambda (&rest _)
                                   (list (org-element-create 'bold nil "one")
                                         "two")))
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c))
                                (bold . (lambda (&rest _) "bold")))))))))
  ;; Make sure to have a space between a quote and a citation.
  (should
   (equal "\"quotation\" citation\n"
          (org-test-with-temp-text "\"quotation\"[cite:@key]"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-citation (lambda (&rest _) "citation"))
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c)))))))))
  (should
   (equal "\"quotation\"  citation\n"
          (org-test-with-temp-text "\"quotation\"  [cite:@key]"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-citation (lambda (&rest _) "citation"))
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c)))))))))
  ;; Regular bibliography export.
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "#+print_bibliography:"
           (let ((org-cite--processors nil)
                 (org-cite-export-processors '((t . (foo nil nil)))))
             (org-cite-register-processor 'foo
               :export-bibliography (lambda (&rest _) (throw :exit 'success))
               :export-citation #'ignore)
             (org-export-as (org-export-create-backend)))))))
  (should
   (equal ""
          (org-test-with-temp-text "#+print_bibliography:"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-citation #'ignore)
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c)))))))))
  ;; Export bibliography as string.
  (should
   (equal "bibliography\n"
          (org-test-with-temp-text "#+print_bibliography:"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-bibliography (lambda (&rest _) "bibliography")
                :export-citation #'ignore)
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c)))))))))
  ;; Export bibliography as a parsed element.
  (should
   (equal "success\n"
          (org-test-with-temp-text "#+print_bibliography:"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-bibliography
                (lambda (&rest _)
                  (org-element-create 'example-block '(:value "foo")))
                :export-citation #'ignore)
              (org-export-as
               (org-export-create-backend
                :transcoders
                '((section . (lambda (_ c _) c))
                  (example-block . (lambda (&rest _) "success")))))))))
  ;; Export bibliography as a list of parsed elements.
  (should
   (equal "success\nsuccess\n"
          (org-test-with-temp-text "#+print_bibliography:"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-bibliography
                (lambda (&rest _)
                  (list (org-element-create 'example-block '(:value "foo"))
                        (org-element-create 'example-block '(:value "bar"))))
                :export-citation #'ignore)
              (org-export-as
               (org-export-create-backend
                :transcoders
                '((section . (lambda (_ c _) c))
                  (example-block . (lambda (&rest _) "success")))))))))
  ;; When exporting bibliography as a list of parsed elements, the
  ;; last element inherits post-blank from initial keyword.
  (should
   (equal "success\nsuccess\n\nText\n"
          (org-test-with-temp-text "#+print_bibliography:\n\nText"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-bibliography
                (lambda (&rest _)
                  (list (org-element-create 'example-block '(:value "foo"))
                        (org-element-create 'example-block '(:value "bar"))))
                :export-citation #'ignore)
              (org-export-as
               (org-export-create-backend
                :transcoders
                '((section . (lambda (_ c _) c))
                  (example-block . (lambda (&rest _) "success"))
                  (paragraph . (lambda (_ c _) c)))))))))
  ;; Use more appropriate citation processor.
  (should
   (equal
    '(p1 p1 p1 p3)
    (org-test-with-temp-text "[cite:@a]"
      (let ((org-export-registered-backends nil)
            (org-cite--procesors nil)
            (org-cite-export-processors
             '((b1 . (p1))
               (t  . (p3)))))
        (org-cite-register-processor 'p1
          :export-citation (lambda (&rest _) (throw :exit 'p1)))
        (org-cite-register-processor 'p2
          :export-citation (lambda (&rest _) (throw :exit 'p2)))
        (org-cite-register-processor 'p3
          :export-citation (lambda (&rest _) (throw :exit 'p3)))
        (org-export-define-backend 'b1 nil)
        (org-export-define-derived-backend 'b2 'b1)
        (org-export-define-derived-backend 'b3 'b2)
        (list (catch :exit (org-export-as 'b1))
              (catch :exit (org-export-as 'b2))
              (catch :exit (org-export-as 'b3))
              (catch :exit (org-export-as (org-export-create-backend))))))))
  (should
   (eq 'p2
       (org-test-with-temp-text "#+cite_export: p2\n[cite:@a]"
         (let ((org-export-registered-backends nil)
               (org-cite--procesors nil)
               (org-cite-export-processors '((t  . (p1)))))
           (org-cite-register-processor 'p1
             :export-citation (lambda (&rest _) (throw :exit 'p1)))
           (org-cite-register-processor 'p2
             :export-citation (lambda (&rest _) (throw :exit 'p2)))
           (catch :exit (org-export-as (org-export-create-backend)))))))
  ;; Test finalizer.
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "[cite:@key]"
           (let ((org-cite--processors nil)
                 (org-cite-export-processors '((t . (foo nil nil)))))
             (org-cite-register-processor 'foo
               :export-citation (lambda (&rest _) "")
               :export-finalizer (lambda (&rest _) (throw :exit 'success)))
             (org-export-as (org-export-create-backend)))))))
  (should
   (equal "finalized!"
          (org-test-with-temp-text "[cite:@key]"
            (let ((org-cite--processors nil)
                  (org-cite-export-processors '((t . (foo nil nil)))))
              (org-cite-register-processor 'foo
                :export-citation #'ignore
                :export-finalizer (lambda (&rest _) "finalized!"))
              (org-export-as (org-export-create-backend))))))
  ;; Ignore citations when there is no selected "export" processor.
  ;; In that case, white space is removed before the citation, not
  ;; after.
  (should
   (equal ""
          (org-test-with-temp-text "[cite:@key]"
            (let ((org-cite-export-processors nil))
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c)))))))))
  (should
   (equal "Text.\n"
          (org-test-with-temp-text "Text [cite:@key]."
            (let ((org-cite-export-processors nil))
              (org-export-as (org-export-create-backend
                              :transcoders
                              '((section . (lambda (_ c _) c))
                                (paragraph . (lambda (_ c _) c)))))))))
  ;; Throw an error if selected processor does not handle "export"
  ;; capability.
  (should-error
   (org-test-with-temp-text "[cite:@key]"
     (let ((org-cite--processors nil)
           (org-cite-export-processors '((t . (foo nil nil)))))
       (org-cite-register-processor 'foo)
       (org-export-as (org-export-create-backend))))))

(ert-deftest test-org-cite/follow-capability ()
  "Test \"follow\" capability."
  ;; Standard test.
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "[cite:@key]"
           (let ((org-cite--processors nil)
                 (org-cite-follow-processor 'foo))
             (org-cite-register-processor 'foo
               :follow (lambda (_ _) (throw :exit 'success)))
             (org-open-at-point))))))
  ;; Throw an error if there is no "follow" processor, or if it is
  ;; unable to follow a citation.
  (should-error
   (org-test-with-temp-text "[cite:@key]"
     (let ((org-cite-follow-processor nil))
       (org-open-at-point))))
  (should-error
   (org-test-with-temp-text "[cite:@key]"
     (let ((org-cite--processors nil)
           (org-cite-follow-processor 'foo))
       (org-cite-register-processor 'foo)
       (org-open-at-point)))))

(ert-deftest test-org-cite/make-insert-processor ()
  "Test `org-cite-make-insert-processor'."
  (should-error (org-cite-make-insert-processor 1 2))
  (should-error
   (org-test-with-temp-text "[cite:@<point>a]"
     (let ((org-cite--processors nil)
           (org-cite-insert-processor 'foo))
       (org-cite-register-processor 'foo
         :insert (org-cite-make-insert-processor
                  #'ignore (lambda (&rest _) "s")))
       (org-cite-insert nil))))
  (should
   (equal "[cite:@k]"
          (org-test-with-temp-text "[cite:@<point>a]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal "[cite:@k;@a]"
          (org-test-with-temp-text "[cite:<point>@a]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal "[cite:@k;pre @a]"
          (org-test-with-temp-text "[cite:<point>pre @a]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal "[cite:pre;@k;@a]"
          (org-test-with-temp-text "[cite:<point>pre;@a]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal "[cite:@a;@k]"
          (org-test-with-temp-text "[cite:@a<point>]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal "[cite:@a post;@k]"
          (org-test-with-temp-text "[cite:@a post<point>]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal "[cite:@a;@k;post]"
          (org-test-with-temp-text "[cite:@a;post<point>]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal ""
          (org-test-with-temp-text "[cite:@<point>a]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "s")))
              (org-cite-insert t)
              (buffer-string)))))
  (should
   (equal "[cite/s:@a]"
          (org-test-with-temp-text "[cite<point>:@a]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal "[cite:@a]"
          (org-test-with-temp-text "[cite<point>/style:@a]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) "k") (lambda (&rest _) "")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should-error
   (org-test-with-temp-text "[cite<point>/style:@a]"
     (let ((org-cite--processors nil)
           (org-cite-insert-processor 'foo))
       (org-cite-register-processor 'foo
         :insert (org-cite-make-insert-processor
                  (lambda (&rest _) "k") #'ignore))
       (org-cite-insert nil))))
  (should
   (equal "[cite:@a][cite:@k]"
          (org-test-with-temp-text "[cite:@a]<point>"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal "[cite:@k][cite:@a]"
          (org-test-with-temp-text "<point>[cite:@a]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
              (org-cite-insert nil)
              (buffer-string)))))
  (should
   (equal "[cite/s:@k][cite:@a]"
          (org-test-with-temp-text "<point>[cite:@a]"
            (let ((org-cite--processors nil)
                  (org-cite-insert-processor 'foo))
              (org-cite-register-processor 'foo
                :insert (org-cite-make-insert-processor
                         (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
              (org-cite-insert t)
              (buffer-string))))))

(ert-deftest test-org-cite/insert-capability ()
  "Test \"insert\" capability."
  ;; Standard test.
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text ""
           (let ((org-cite--processors nil)
                 (org-cite-insert-processor 'foo))
             (org-cite-register-processor 'foo
               :insert (lambda (_ _) (throw :exit 'success)))
             (call-interactively #'org-cite-insert))))))
  ;; Throw an error if there is no "insert" processor, or if it is
  ;; unable to insert a citation.
  (should-error
   (org-test-with-temp-text ""
     (let ((org-cite-insert-processor nil))
       (call-interactively #'org-cite-insert))))
  (should-error
   (org-test-with-temp-text ""
     (let ((org-cite--processors nil)
           (org-cite-insert-processor 'foo))
       (org-cite-register-processor 'foo)
       (call-interactively #'org-cite-insert))))
  ;; Throw an error if the location is inappropriate for a citation.
  (should-error
   (org-test-with-temp-text "=verbatim<point> text="
     (let ((org-cite--processors nil)
           (org-cite-insert-processor 'foo))
       (org-cite-register-processor 'foo
         :insert (lambda (_ _) (throw :exit 'success)))
       (call-interactively #'org-cite-insert))))
  ;; Allow inserting citations at the beginning of a footnote
  ;; definition, right after the label.
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "[fn:1]<point>"
           (let ((org-cite--processors nil)
                 (org-cite-insert-processor 'foo))
             (org-cite-register-processor 'foo
               :insert (lambda (_ _) (throw :exit 'success)))
             (call-interactively #'org-cite-insert))))))
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "[fn:1] <point>"
           (let ((org-cite--processors nil)
                 (org-cite-insert-processor 'foo))
             (org-cite-register-processor 'foo
               :insert (lambda (_ _) (throw :exit 'success)))
             (call-interactively #'org-cite-insert))))))
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "[fn:1]<point>\nParagraph"
           (let ((org-cite--processors nil)
                 (org-cite-insert-processor 'foo))
             (org-cite-register-processor 'foo
               :insert (lambda (_ _) (throw :exit 'success)))
             (call-interactively #'org-cite-insert))))))
  (should-error
   (org-test-with-temp-text "[fn:1<point>]"
     (let ((org-cite--processors nil)
           (org-cite-insert-processor 'foo))
       (org-cite-register-processor 'foo
         :insert (lambda (_ _) (throw :exit 'success)))
       (call-interactively #'org-cite-insert))))
  (should-error
   (org-test-with-temp-text "<point>[fn:1]"
     (let ((org-cite--processors nil)
           (org-cite-insert-processor 'foo))
       (org-cite-register-processor 'foo
         :insert (lambda (_ _) (throw :exit 'success)))
       (call-interactively #'org-cite-insert))))
  ;; Allow inserting citations in captions.
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "#+caption: <point>\n| table |"
           (let ((org-cite--processors nil)
                 (org-cite-insert-processor 'foo))
             (org-cite-register-processor 'foo
               :insert (lambda (_ _) (throw :exit 'success)))
             (call-interactively #'org-cite-insert))))))
  ;; Allow inserting citations in table cells.
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "| <point>table |"
           (let ((org-cite--processors nil)
                 (org-cite-insert-processor 'foo))
             (org-cite-register-processor 'foo
               :insert (lambda (_ _) (throw :exit 'success)))
             (call-interactively #'org-cite-insert))))))
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "| table<point> |"
           (let ((org-cite--processors nil)
                 (org-cite-insert-processor 'foo))
             (org-cite-register-processor 'foo
               :insert (lambda (_ _) (throw :exit 'success)))
             (call-interactively #'org-cite-insert))))))
  (should
   (eq 'success
       (catch :exit
         (org-test-with-temp-text "| table  <point> |"
           (let ((org-cite--processors nil)
                 (org-cite-insert-processor 'foo))
             (org-cite-register-processor 'foo
               :insert (lambda (_ _) (throw :exit 'success)))
             (call-interactively #'org-cite-insert)))))))

(provide 'test-oc)
;;; test-oc.el ends here

debug log:

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