File: markdown-test.el

package info (click to toggle)
markdown-mode 2.3%2B154-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,052 kB
  • sloc: lisp: 13,081; makefile: 69; sh: 16
file content (5746 lines) | stat: -rw-r--r-- 261,725 bytes parent folder | download
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
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
;;;; markdown-test.el --- Tests for markdown-mode

;; Copyright (C) 2013-2017 Jason R. Blevins <jblevins@xbeta.org>
;; Copyright (C) 2013 Makoto Motohashi <mkt.motohashi@gmail.com>
;; Copyright (C) 2015 Google, Inc. (Contributor: Samuel Freilich <sfreilich@google.com>)

;; This file is not part of GNU Emacs.

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

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

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

;;; Commentary:

;; This file contains the `markdown-mode' test suite.  To run the tests:
;;
;;     M-x load-file RET markdown-test.el RET
;;     M-x markdown-test RET

;;; Code:

(require 'markdown-mode)
(require 'ert)
(require 'cl-lib)

(defconst markdown-test-dir
  (expand-file-name (file-name-directory
                     (or load-file-name buffer-file-name))))

(defconst markdown-test-font-lock-function
  (if (and noninteractive (fboundp 'font-lock-ensure))
      #'font-lock-ensure #'font-lock-fontify-buffer))

(defmacro markdown-test-string-mode (mode string &rest body)
  "Run BODY in a temporary buffer containing STRING in MODE."
  (declare (indent 2))
  `(let ((win (selected-window)))
     (unwind-protect
         (with-temp-buffer
           (set-window-buffer win (current-buffer) t)
           (erase-buffer)
           (funcall ,mode)
           (setq-default indent-tabs-mode nil)
           (insert ,string)
           (goto-char (point-min))
           (funcall markdown-test-font-lock-function)
           (prog1 ,@body (kill-buffer))))))

(defmacro markdown-test-file-mode (mode file &rest body)
  "Open FILE from `markdown-test-dir' in MODE and execute BODY."
  (declare (indent 2))
  `(let ((fn (concat markdown-test-dir ,file)))
     (save-window-excursion
       (with-temp-buffer
         (insert-file-contents fn)
         (funcall ,mode)
         (goto-char (point-min))
         (funcall markdown-test-font-lock-function)
         ,@body))))

(defmacro markdown-test-string (string &rest body)
  "Run BODY in a temporary buffer containing STRING in `markdown-mode'."
  (declare (indent 1))
  `(markdown-test-string-mode 'markdown-mode ,string ,@body))
(def-edebug-spec markdown-test-string (form body))

(defmacro markdown-test-file (file &rest body)
  "Open FILE from `markdown-test-dir' in `markdown-mode' and execute BODY."
  (declare (indent 1))
  `(markdown-test-file-mode 'markdown-mode ,file ,@body))
(def-edebug-spec markdown-test-file (form body))

(defmacro markdown-test-string-gfm (string &rest body)
  "Run BODY in a temporary buffer containing STRING in `gfm-mode'."
  (declare (indent 1))
  `(markdown-test-string-mode 'gfm-mode ,string ,@body))
(def-edebug-spec markdown-test-string-gfm (form body))

(defmacro markdown-test-file-gfm (file &rest body)
  "Open FILE from `markdown-test-dir' in `gfm-mode' and execute BODY."
  (declare (indent 1))
  `(markdown-test-file-mode 'gfm-mode ,file ,@body))
(def-edebug-spec markdown-test-file-gfm (form body))

(defmacro markdown-test-temp-file (file &rest body)
  "Open FILE from `markdown-test-dir' visiting temp file and execute BODY.
This file is not saved."
  (declare (indent 1))
  `(let ((fn (concat markdown-test-dir ,file))
         (tmp (make-temp-file "markdown-test" nil ".text"))
         buf)
     (save-window-excursion
       (unwind-protect
           (progn
             (setq buf (find-file tmp))
             (insert-file-contents fn)
             (markdown-mode)
             (goto-char (point-min))
             (funcall markdown-test-font-lock-function)
             ,@body
             (set-buffer-modified-p nil))
         (when (buffer-live-p buf) (kill-buffer buf))
         (delete-file tmp)))))
(def-edebug-spec markdown-test-temp-file (form body))

(defun markdown-test-report-property-range (begin end prop)
  "Report buffer substring and property PROP from BEGIN to END."
  (message "Buffer substring: %s" (buffer-substring begin (1+ end)))
  (message "Properties in range are as follows:")
  (dolist (loc (number-sequence begin end))
    (message "%d: %s" loc (get-char-property loc prop))))

(defun markdown-test-range-has-property (begin end prop value)
  "Verify that range BEGIN to END has PROP equal to or containing VALUE."
  (let (vals fail-loc)
    (setq fail-loc
          (catch 'fail
            (dolist (loc (number-sequence begin end))
              (setq vals (get-char-property loc prop))
              (if (and vals (listp vals))
                  (unless (memq value vals)
                    (throw 'fail loc))
                (unless (eq vals value)
                  (throw 'fail loc))))))
    (when fail-loc
      (message "Testing range (%d,%d) for property %s equal to %s."
               begin end prop value)
      (message "Expected value (%s) not found in property (%s) at location %d" value prop fail-loc)
      (markdown-test-report-property-range begin end prop))
    (should-not fail-loc)))

(defun markdown-test-range-property-equals (begin end prop value)
  "Verify that range BEGIN to END has property PROP equal to VALUE."
  (let ((fail-loc
         (catch 'fail
           (dolist (loc (number-sequence begin end))
             (unless (eq (get-char-property loc prop) value)
               (throw 'fail loc))))))
    (when fail-loc
      (message "Testing range (%d,%d) for property %s equal to %s."
               begin end prop value)
      (message "Expected value (%s) not found in property (%s) at location %d" value prop fail-loc)
      (markdown-test-report-property-range begin end prop))
    (should-not fail-loc)))

(defun markdown-test-range-has-face (begin end face)
  "Verify that the range from BEGIN to END has face FACE."
  (markdown-test-range-has-property begin end 'face face))

(defun markdown-test-range-face-equals (begin end face)
  "Verify that the range from BEGIN to END has face equal to FACE."
  (markdown-test-range-property-equals begin end 'face face))

(defun markdown-test-goto-heading (title)
  "Move the point to section with TITLE."
  (let ((regexp (format "\\(^#+ %s\\( #+\\)?\\|^%s\n[=-]+\n\\)" title title)))
    (if (re-search-forward regexp nil t)
        (goto-char (match-end 0)))))

(defun markdown-test ()
  "Run all defined test cases for `markdown-mode'."
  (interactive)
  (ert "markdown"))

;;; Example tests:

(ert-deftest test-markdown-example/string ()
  "An example string test using the `ert' framework."
  (markdown-test-string "foo *bar* baz"
                        (goto-char 5)
                        (delete-char 1)
                        (should (looking-at "bar"))))

(ert-deftest test-markdown-example/file ()
  "An example file test using the `ert' framework."
  (markdown-test-file "inline.text"
                      (goto-char 9)
                      (should (looking-at "\*"))))

;;; Basic mode tests:

(ert-deftest test-markdown-mode/variables ()
  "Test `markdown-mode' variables."
  (markdown-test-file "inline.text"
                      (should (= tab-width 4))
                      (should (eq font-lock-multiline t))
                      (should (eq major-mode 'markdown-mode))))

;;; Element insertion tests:

(ert-deftest test-markdown-insertion/blank-line-before-1 ()
  "Test function `markdown-ensure-blank-line-before' at beginning of line."
  (markdown-test-file "syntax.text"
                      (search-forward "as plain text")
                      (should (= (point) 1556))
                      (beginning-of-line)
                      (should (= (point) 1505))
                      (should (looking-back "A Markdown-formatted\n" nil))
                      (should (not (markdown-prev-line-blank-p)))
                      (markdown-ensure-blank-line-before)
                      (should (looking-back "A Markdown-formatted\n\n" nil))
                      (should (markdown-prev-line-blank-p))))

(ert-deftest test-markdown-insertion/blank-line-before-2 ()
  "Test function `markdown-ensure-blank-line-before' in middle of line."
  (markdown-test-file "syntax.text"
                      (search-forward "as plain text")
                      (should (= (point) 1556))
                      (should (looking-back "as plain text" nil))
                      (should (not (markdown-prev-line-blank-p)))
                      (markdown-ensure-blank-line-before)
                      (should (looking-back "as plain text\n\n" nil))
                      (should (markdown-prev-line-blank-p))))

(ert-deftest test-markdown-insertion/blank-line-before-3 ()
  "Test function `markdown-ensure-blank-line-before' with blank line before."
  (markdown-test-file "syntax.text"
                      (search-forward "web.\n\nMarkdown is not a replacement for HTML")
                      (beginning-of-line)
                      (should (= (point) 2704))
                      (should (looking-back "web.\n\n" nil))
                      (should (markdown-prev-line-blank-p))
                      (markdown-ensure-blank-line-before)
                      (should (= (point) 2704))
                      (should (looking-back "web.\n\n" nil))
                      (should (markdown-prev-line-blank-p))))

(ert-deftest test-markdown-insertion/blank-line-before-4 ()
  "Test function `markdown-ensure-blank-line-before' at beginning of buffer."
  (markdown-test-string "first line"
                        (beginning-of-line)
                        (should (bobp))
                        (should (= (point-max) 11))
                        (markdown-ensure-blank-line-before)
                        (should (= (point-max) 11))
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "first line"))
                        (forward-word)
                        (markdown-ensure-blank-line-before)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "first\n\n line"))))

(ert-deftest test-markdown-insertion/blank-line-after-1 ()
  "Test function `markdown-ensure-blank-line-after' at end of line."
  (markdown-test-file "syntax.text"
                      (search-forward "as plain text")
                      (should (= (point) 1556))
                      (end-of-line)
                      (should (= (point) 1573))
                      (should (looking-at "\nlike it's been"))
                      (should (not (markdown-next-line-blank-p)))
                      (markdown-ensure-blank-line-after)
                      (should (looking-at "\n\nlike it's been"))
                      (should (markdown-next-line-blank-p))))

(ert-deftest test-markdown-insertion/blank-line-after-2 ()
  "Test function `markdown-ensure-blank-line-after' in middle of line."
  (markdown-test-file "syntax.text"
                      (search-forward "as plain text")
                      (should (= (point) 1556))
                      (should (looking-at ", without looking"))
                      (should (not (markdown-next-line-blank-p)))
                      (markdown-ensure-blank-line-after)
                      (should (looking-at "\n\n, without looking"))
                      (should (markdown-next-line-blank-p))))

(ert-deftest test-markdown-insertion/blank-line-after-3 ()
  "Test function `markdown-ensure-blank-line-after' with blank line after."
  (markdown-test-file "syntax.text"
                      (search-forward "*writing* for the web.")
                      (should (= (point) 2702))
                      (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
                      (should (markdown-next-line-blank-p))
                      (markdown-ensure-blank-line-after)
                      (should (= (point) 2702))
                      (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
                      (should (markdown-next-line-blank-p))))

(ert-deftest test-markdown-insertion/blank-line-after-4 ()
  "Test function `markdown-ensure-blank-line-after' at end of buffer."
  (markdown-test-string "last line"
                        (end-of-line)
                        (should (eobp))
                        (should (= (point-max) 10))
                        (markdown-ensure-blank-line-after)
                        (should (= (point-max) 10))
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "last line"))
                        (backward-word)
                        (markdown-ensure-blank-line-after)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "last \n\nline"))))

(ert-deftest test-markdown-insertion/point-after-unwrap ()
  "Test new point position calculations after unwrap operations."
  (markdown-test-string "line **one**\n"
                        (let ((prefix (cons 6 8)) (suffix (cons 11 13)))
                          ;; Prefix
                          (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
                          (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
                          ;; Word
                          (should (eq (markdown-point-after-unwrap 8 prefix suffix) 6))
                          (should (eq (markdown-point-after-unwrap 9 prefix suffix) 7))
                          (should (eq (markdown-point-after-unwrap 10 prefix suffix) 8))
                          ;; Suffix
                          (should (eq (markdown-point-after-unwrap 11 prefix suffix) 9))
                          (should (eq (markdown-point-after-unwrap 12 prefix suffix) 9))
                          ;; Immediately after
                          (should (eq (markdown-point-after-unwrap 13 prefix suffix) 9))))
  (markdown-test-string "line _one_\n"
                        (let ((prefix (cons 6 7)) (suffix (cons 10 11)))
                          ;; Prefix
                          (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
                          ;; Word
                          (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
                          (should (eq (markdown-point-after-unwrap 8 prefix suffix) 7))
                          (should (eq (markdown-point-after-unwrap 9 prefix suffix) 8))
                          ;; Suffix
                          (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9))
                          ;; Immediately after
                          (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9)))))

(ert-deftest test-markdown-insertion/unwrap-thing-at-point-italic ()
  "Test function `markdown-unwrap-thing-at-point' on italics."
  (markdown-test-file "syntax.text"
                      ;; Unwrap *not*
                      (goto-char 2859)
                      (should (thing-at-point-looking-at markdown-regex-italic))
                      (should (equal (markdown-unwrap-thing-at-point
                                      markdown-regex-italic 1 3)
                                     (cons 2859 2862)))
                      (should (= (point) 2859))
                      ;; Unwrap *publishing*
                      (goto-char 3064)
                      (should (thing-at-point-looking-at markdown-regex-italic))
                      (should (equal (markdown-unwrap-thing-at-point
                                      markdown-regex-italic 1 3)
                                     (cons 3060 3070)))
                      (should (= (point) 3063))
                      ;; Unwrap *writing*
                      (goto-char 3101)
                      (should (thing-at-point-looking-at markdown-regex-italic))
                      (should (equal (markdown-unwrap-thing-at-point
                                      markdown-regex-italic 1 3)
                                     (cons 3093 3100)))
                      (should (= (point) 3100))))

(ert-deftest test-markdown-insertion/unwrap-things-in-region-italic ()
  "Test function `markdown-unwrap-things-in-region' on italics."
  (markdown-test-file "syntax.text"
                      (should (equal (markdown-unwrap-things-in-region
                                      2704 3207 markdown-regex-italic 1 3)
                                     (cons 2704 3201)))))

(ert-deftest test-markdown-insertion/unwrap-things-in-region-bound ()
  "Ensure that `markdown-unwrap-things-in-region' respects end bound"
  (markdown-test-string "**a** **b** **c** **d** **e** **f**"
                        ;; Set region to unrwap a, b, c, and d only.  If endpoint is not
                        ;; respected (i.e, not adjusted for character removal), the
                        ;; function will unwrap e and f also.
                        (should (equal (markdown-unwrap-things-in-region
                                        1 24 markdown-regex-bold 2 4)
                                       (cons 1 8)))
                        (should (string-equal (buffer-string) "a b c d **e** **f**"))))

(ert-deftest test-markdown-insertion/unwrap-things-in-region-links ()
  "Test function `markdown-unwrap-things-in-region' on inline links."
  (markdown-test-string "a [link](http://jblevins.org/) or [two](/).\n"
                        (should (equal (markdown-unwrap-things-in-region
                                        (point-min) (point-max) markdown-regex-link-inline 0 3)
                                       (cons 1 16)))
                        (should (string-equal (buffer-string) "a link or two.\n"))))

(ert-deftest test-markdown-insertion/toggle-bold ()
  "Test toggling functionality of `markdown-insert-bold'."
  (markdown-test-string "one **two** three"
                        (forward-word 2)
                        (markdown-insert-bold)
                        (should (string-equal (buffer-string) "one two three"))
                        (should (= (point) 8))
                        (forward-word)
                        (markdown-insert-bold)
                        (should (= (point) 16))
                        (should (string-equal (buffer-string) "one two **three**"))))

(ert-deftest test-markdown-insertion/toggle-italic ()
  "Test toggling functionality of `markdown-insert-italic'."
  (markdown-test-string "one *two* three"
                        (forward-word 2)
                        (markdown-insert-italic)
                        (should (string-equal (buffer-string) "one two three"))
                        (should (= (point) 8))
                        (forward-word)
                        (markdown-insert-italic)
                        (should (string-equal (buffer-string) "one two *three*"))
                        (should (= (point) 15))))

(ert-deftest test-markdown-insertion/toggle-code ()
  "Test toggling functionality of `markdown-insert-code'."
  (markdown-test-string "one `two` three"
                        (forward-word 2)
                        (markdown-insert-code)
                        (should (string-equal (buffer-string) "one two three"))
                        (should (= (point) 8))
                        (forward-word)
                        (markdown-insert-code)
                        (should (string-equal (buffer-string) "one two `three`"))
                        (should (= (point) 15))))

(ert-deftest test-markdown-insertion/toggle-kbd ()
  "Test toggling functionality of `markdown-insert-code'."
  (markdown-test-string "test <kbd>C-c C-s k</kbd> toggle"
                        (forward-word 2)
                        (markdown-insert-kbd)
                        (should (string-equal (buffer-string) "test C-c C-s k toggle"))
                        (should (= (point) 6))
                        (backward-word)
                        (markdown-insert-kbd)
                        (should (string-equal (buffer-string) "<kbd>test</kbd> C-c C-s k toggle"))
                        (should (= (point) 6))))

(ert-deftest test-markdown-insertion/toggle-wiki-link-alias-first ()
  "Test toggling of `markdown-insert-wiki-link' with alias first.
Test point position upon removal and insertion."
  (let ((markdown-wiki-link-alias-first t))
    (markdown-test-string "[[text|page]]"
                          (goto-char 5) ; point in interior of alias text, at 'x'
                          (call-interactively 'markdown-insert-wiki-link)
                          (should (= (point) 3)) ; leave point at, at 'x'
                          (should (string-equal (buffer-string) "text"))
                          (call-interactively 'markdown-insert-wiki-link)
                          (should (= (point) 5)) ; leave point at, at 'x'
                          (should (string-equal (buffer-string) "[[text]]")))
    (markdown-test-string "[[text|page]]"
                          (goto-char 10) ; point in interior of link text, at 'g'
                          (call-interactively 'markdown-insert-wiki-link)
                          (should (= (point) 5)) ; leave point at end of alias text
                          (should (string-equal (buffer-string) "text"))
                          (call-interactively 'markdown-insert-wiki-link)
                          (should (= (point) 7)) ; leave point at end of alias text
                          (should (string-equal (buffer-string) "[[text]]")))))

(ert-deftest test-markdown-insertion/toggle-wiki-link-alias-last ()
  "Test toggling of `markdown-insert-wiki-link' with alias last.
Test point position upon removal and insertion."
  (let ((markdown-wiki-link-alias-first nil))
    (markdown-test-string "[[page|text]]"
                          (goto-char 10) ; point in interior of alias text, at 'x'
                          (call-interactively 'markdown-insert-wiki-link)
                          (goto-char 3) ; leave point at, at 'x'
                          (should (string-equal (buffer-string) "text"))
                          (call-interactively 'markdown-insert-wiki-link)
                          (should (= (point) 5)) ; leave point at, at 'x'
                          (should (string-equal (buffer-string) "[[text]]")))
    (markdown-test-string "[[page|text]]"
                          (goto-char 3) ; point in interior of link text, at 'g'
                          (call-interactively 'markdown-insert-wiki-link)
                          (should (= (point) 1)) ; leave point at beginning of alias text
                          (should (string-equal (buffer-string) "text"))
                          (call-interactively 'markdown-insert-wiki-link)
                          (should (= (point) 3)) ; leave point at beginning of alias text
                          (should (string-equal (buffer-string) "[[text]]")))))

(ert-deftest test-markdown-insertion/bold-region ()
  "Test region functionality of `markdown-insert-bold'."
  (markdown-test-string "one two three"
                        (push-mark (point) t t)
                        (forward-word 2)
                        (markdown-insert-bold)
                        (should (string-equal (buffer-string) "**one two** three"))
                        (should (= (point) 10))))

(ert-deftest test-markdown-insertion/italic-region ()
  "Test region functionality of `markdown-insert-italic'."
  (markdown-test-string "one two three"
                        (transient-mark-mode)
                        (push-mark (point) t t)
                        (forward-word 2)
                        (markdown-insert-italic)
                        (should (string-equal (buffer-string) "*one two* three"))
                        (should (= (point) 9))))

(ert-deftest test-markdown-insertion/code-region ()
  "Test region functionality of `markdown-insert-code'."
  (markdown-test-string "one two three"
                        (transient-mark-mode)
                        (push-mark (point) t t)
                        (forward-word 2)
                        (markdown-insert-code)
                        (should (string-equal (buffer-string) "`one two` three"))
                        (should (= (point) 9))))

(ert-deftest test-markdown-insertion/kbd-region ()
  "Test region functionality of `markdown-insert-kbd'."
  (markdown-test-string "one two three"
                        (transient-mark-mode)
                        (push-mark (point) t t)
                        (forward-word 2)
                        (markdown-insert-kbd)
                        (should (string-equal (buffer-string) "<kbd>one two</kbd> three"))
                        (should (= (point) 13))))

(ert-deftest test-markdown-insertion/atx-line ()
  "Test ATX header insertion without region."
  (markdown-test-string "line one\nline two\n"
                        (forward-word)
                        (markdown-insert-header-atx-1)
                        (should (= (point) 11))
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "# line one #\n\nline two\n"))
                        (forward-line 2)
                        (markdown-insert-header-atx-2)
                        (should (= (point) 26))
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "# line one #\n\n## line two ##\n\n"))))

(ert-deftest test-markdown-insertion/atx-region ()
  "Test ATX header insertion with region."
  (markdown-test-string "line one\nline two\n"
                        (transient-mark-mode)
                        (forward-char 5)
                        (push-mark (point) t t)
                        (forward-word)
                        (should (string-equal (buffer-substring (region-beginning) (region-end))
                                              "one"))
                        (markdown-insert-header-atx-4)
                        (should (= (point) 16))
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "line \n\n#### one ####\n\nline two\n"))))

(ert-deftest test-markdown-insertion/atx-blank ()
  "Test ATX header insertion on blank line."
  (markdown-test-string "line one\n\nline two\n"
                        (forward-line)
                        (markdown-insert-header-atx-3)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "line one\n\n###  ###\n\nline two\n"))
                        (should (= (point) 15))
                        (should (looking-at " ###\n"))))

(ert-deftest test-markdown-insertion/atx-region-whitespace ()
  "Test ATX header insertion using a region with whitespace."
  (markdown-test-string "  line one\n\nline two\n  \n"
                        (transient-mark-mode)
                        (push-mark (point) t t)
                        (goto-char (point-max))
                        (markdown-insert-header-atx-2)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "## line one line two ##"))
                        (should (= (point) 21))
                        (should (looking-at " ##"))))

(ert-deftest test-markdown-insertion/atx-line-whitespace ()
  "Test ATX header insertion using current line with whitespace."
  (markdown-test-string "  line one  \n\nline two\n"
                        (goto-char (line-end-position))
                        (markdown-insert-header-atx-3)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "### line one ###\n\nline two\n"))
                        (should (= (point) 13))
                        (should (looking-at " ###\n"))))

(ert-deftest test-markdown-insertion/atx-replace-atx ()
  "Test ATX header insertion when replacing an existing ATX header."
  (markdown-test-string "## replace ##\n"
                        (markdown-insert-header-atx-4)
                        (should (string-equal (buffer-string) "#### replace ####\n\n"))
                        (should (looking-at " ####\n"))))

(ert-deftest test-markdown-insertion/atx-replace-setext-1 ()
  "Test ATX header insertion when replacing an existing setext header."
  (markdown-test-string "replace\n=======\n"
                        (markdown-insert-header-atx-2)
                        (should (string-equal (buffer-string) "## replace ##\n\n"))
                        (should (looking-at " ##\n"))))

(ert-deftest test-markdown-insertion/atx-replace-setext-2 ()
  "Test ATX header insertion when replacing an existing setext header."
  (markdown-test-string "replace\n-------\n"
                        (markdown-insert-header-atx-5)
                        (should (string-equal (buffer-string) "##### replace #####\n\n"))
                        (should (looking-at " #####\n"))))

(ert-deftest test-markdown-insertion/atx-asymmetric-point ()
  "Test point after ATX header insertion with `markdown-asymmetric-header'."
  (markdown-test-string
   "Test"
   (let ((markdown-asymmetric-header t))
     (markdown-insert-header-atx-5)
     (should (= (point) 11))
     (should (string-equal (buffer-string) "##### Test")))))

(ert-deftest test-markdown-insertion/setext-line ()
  "Test setext header insertion without region."
  (markdown-test-string "line one\nline two\n"
                        (forward-word)
                        (markdown-insert-header-setext-1)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "line one\n========\n\nline two\n"))
                        (forward-line 3)
                        (markdown-insert-header-setext-2)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "line one\n========\n\nline two\n--------\n\n"))))

(ert-deftest test-markdown-insertion/setext-region ()
  "Test setext header insertion with region."
  (markdown-test-string "line one\nline two\n"
                        (transient-mark-mode)
                        (forward-char 5)
                        (push-mark (point) t t)
                        (forward-word)
                        (should (string-equal (buffer-substring (region-beginning) (region-end))
                                              "one"))
                        (markdown-insert-header-setext-1)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "line \n\none\n===\n\nline two\n"))))

(ert-deftest test-markdown-insertion/setext-blank ()
  "Test setext header insertion on blank line."
  (markdown-test-string "line one\n\nline two\n"
                        (forward-line)
                        (markdown-insert-header 2 "foo" t)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "line one\n\nfoo\n---\n\nline two\n"))
                        (should (= (point) 14))
                        (should (looking-at "\n---"))))

(ert-deftest test-markdown-insertion/setext-region-whitespace ()
  "Test setext header insertion using a region with whitespace."
  (markdown-test-string "  line one\n\nline two\n  \n"
                        (transient-mark-mode)
                        (push-mark (point) t t)
                        (goto-char (point-max))
                        (markdown-insert-header-setext-1)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "line one line two\n================="))
                        (should (= (point) 18))
                        (should (looking-at "\n===="))))

(ert-deftest test-markdown-insertion/setext-line-whitespace ()
  "Test setext header insertion using current line with whitespace."
  (markdown-test-string "  line one  \n\nline two\n"
                        (goto-char (line-end-position))
                        (markdown-insert-header-setext-2)
                        (should (string-equal (buffer-substring (point-min) (point-max))
                                              "line one\n--------\n\nline two\n"))
                        (should (= (point) 9))
                        (should (looking-at "\n---"))))

(ert-deftest test-markdown-insertion/setext-replace-atx ()
  "Test setext header insertion when replacing an existing ATX header."
  (markdown-test-string "## replace ##\n"
                        (markdown-insert-header-setext-1)
                        (should (string-equal (buffer-string) "replace\n=======\n\n"))
                        (should (looking-at "\n==="))))

(ert-deftest test-markdown-insertion/setext-replace-setext-1 ()
  "Test setext header insertion when replacing an existing setext title."
  (markdown-test-string "replace\n=======\n"
                        (markdown-insert-header-setext-2)
                        (should (string-equal (buffer-string) "replace\n-------\n\n"))
                        (should (looking-at "\n---"))))

(ert-deftest test-markdown-insertion/setext-replace-setext-2 ()
  "Test setext header insertion when replacing an existing setext section."
  (markdown-test-string "replace\n-------\n"
                        (markdown-insert-header-setext-1)
                        (should (string-equal (buffer-string) "replace\n=======\n\n"))
                        (should (looking-at "\n==="))))

(ert-deftest test-markdown-insertion/header-dwim ()
  "Test 'do what I mean' header insertion."
  (markdown-test-file "outline.text"
                      (call-interactively 'markdown-insert-header-dwim)
                      (should (looking-at " #$"))
                      (end-of-defun 2)
                      (call-interactively 'markdown-insert-header-dwim)
                      (beginning-of-line)
                      (should (looking-at "^#  #$"))
                      (end-of-defun 3)
                      (call-interactively 'markdown-insert-header-dwim)
                      (beginning-of-line)
                      (should (looking-at "^###  ###$"))))

(ert-deftest test-markdown-insertion/header-dwim-prefix ()
  "Test 'do what I mean' header insertion with prefix arguments."
  (let ((tests (list '(nil . "## abc ##")
                     '(1 . "# abc #")
                     '(2 . "## abc ##")
                     '(3 . "### abc ###")
                     '(4 . "#### abc ####")
                     '(5 . "##### abc #####")
                     '(6 . "###### abc ######")
                     '((4) . "# abc #")
                     '((16) . "### abc ###"))))
    (dolist (test tests)
      (markdown-test-string "## atx\n\nabc"
                            (goto-char (point-max))
                            (let ((current-prefix-arg (car test)))
                              (call-interactively 'markdown-insert-header-dwim)
                              (should (string-equal
                                       (buffer-substring (line-beginning-position) (line-end-position))
                                       (cdr test))))))))

(ert-deftest test-markdown-insertion/header-setext-dwim-prefix ()
  "Test 'do what I mean' header insertion with prefix arguments."
  (let ((tests (list '(nil . "abc\n---")
                     '(1 . "abc\n===")
                     '(2 . "abc\n---")
                     '(3 . "### abc ###")
                     '(4 . "#### abc ####")
                     '(5 . "##### abc #####")
                     '(6 . "###### abc ######")
                     '((4) . "abc\n===")
                     '((16) . "### abc ###"))))
    (dolist (test tests)
      (markdown-test-string "atx\n---\n\nabc"
                            (goto-char (point-max))
                            (let ((current-prefix-arg (car test)))
                              (call-interactively 'markdown-insert-header-setext-dwim)
                              (should (string-equal
                                       (buffer-substring (line-beginning-position) (line-end-position 2))
                                       (cdr test))))))))

(ert-deftest test-markdown-insertion/header-setext-dwim ()
  "Test 'do what I mean' header insertion with setext headers."
  (markdown-test-string
   "asdfasfasfdsadfasdfasdf\n======="
   (goto-char 12)
   (call-interactively 'markdown-insert-header-dwim)
   (should (string-equal
            (buffer-string)
            "asdfasfasfdsadfasdfasdf\n======================="))))

(ert-deftest test-markdown-insertion/remove-header ()
  "Test ATX and setext header."
  (markdown-test-string
   "# atx1\n\n## atx2 ##\n\nsetext1\n=======\n\nsetext2\n-------\n"
   (should (equal (markdown-remove-header) (cons 1 5)))
   (forward-line)
   (should (not (markdown-remove-header)))
   (forward-line)
   (should (equal (markdown-remove-header) (cons 7 11)))
   (forward-line)
   (should (not (markdown-remove-header)))
   (forward-line)
   (should (equal (markdown-remove-header) (cons 13 20)))
   (forward-line)
   (should (not (markdown-remove-header)))
   (forward-line)
   (should (equal (markdown-remove-header) (cons 22 29)))
   (should (string-equal (buffer-string)
                         "atx1\n\natx2\n\nsetext1\n\nsetext2\n"))))

(ert-deftest test-markdown-insertion/italic-unwrap-region ()
  "A test of inserting italics with italic text in the region."
  (markdown-test-string "*foo* bar *baz*"
                        (transient-mark-mode)
                        (push-mark (point) t t)
                        (end-of-line)
                        (markdown-insert-italic)
                        (should (string-equal (buffer-string) "*foo bar baz*"))))

(ert-deftest test-markdown-insertion/bold-unwrap-region ()
  "A test of inserting bold with italic text in the region."
  (markdown-test-string "*foo* **bar** *baz*"
                        (transient-mark-mode)
                        (push-mark (point) t t)
                        (end-of-line)
                        (markdown-insert-bold)
                        (should (string-equal (buffer-string) "***foo* bar *baz***"))))

(ert-deftest test-markdown-insertion/code-unwrap-region ()
  "A test of inserting code with code already in the region."
  (markdown-test-string "`foo` *bar* `baz`"
                        (transient-mark-mode)
                        (push-mark (point) t t)
                        (end-of-line)
                        (markdown-insert-code)
                        (should (string-equal (buffer-string) "`foo *bar* baz`"))))

(ert-deftest test-markdown-insertion/hr-order ()
  "Test inserting horizontal rules."
  (dotimes (n (length markdown-hr-strings))
    (markdown-test-string ""
                          (let ((current-prefix-arg n))
                            (call-interactively 'markdown-insert-hr))
                          (should (string-equal (buffer-string) (nth (1- n) markdown-hr-strings))))))

(ert-deftest test-markdown-insertion/hr-prefix ()
  "Test inserting horizontal rule with C-u prefix."
  (markdown-test-string ""
                        (let ((current-prefix-arg '(4)))
                          (call-interactively 'markdown-insert-hr))
                        (should (string-equal (buffer-string) (car (last markdown-hr-strings))))))

(ert-deftest test-markdown-insertion/hr-bob ()
  "Test inserting horizontal rule at beginning of buffer."
  (markdown-test-string "one line\n"
                        (call-interactively 'markdown-insert-hr)
                        (should (string-equal (buffer-string)
                                              (concat (car markdown-hr-strings)
                                                      "\n\none line\n")))))

(ert-deftest test-markdown-insertion/hr-eob ()
  "Test inserting horizontal rule at end of buffer."
  (markdown-test-string "one line\n"
                        (forward-line)
                        (call-interactively 'markdown-insert-hr)
                        (should (string-equal (buffer-string)
                                              (concat "one line\n\n" (car markdown-hr-strings))))))

(ert-deftest test-markdown-insertion/hr-mob ()
  "Test inserting horizontal rule in middle of buffer."
  (markdown-test-string "one line\n"
                        (forward-word)
                        (let ((markdown-hr-strings '("----------")))
                          (call-interactively 'markdown-insert-hr)
                          (should (string-equal (buffer-string)
                                                (concat "one\n\n" (car markdown-hr-strings)
                                                        "\n\n line\n"))))))

(ert-deftest test-markdown-insertion/pre-region-1 ()
  "Test `markdown-pre-region'."
  ;; Simple test as non-interactive command
  (markdown-test-string "line one\nline two\n"
                        (markdown-pre-region (line-beginning-position) (line-end-position))
                        (should (string-equal (buffer-string) "    line one\n\nline two\n")))
  ;; Test removal of whitespace before and after region
  (markdown-test-string "line one abc\nline two\n"
                        (markdown-pre-region 6 9)
                        (should (string-equal (buffer-string) "line\n\n    one\n\nabc\nline two\n")))
  ;; Simple test as interactive command
  (markdown-test-string "line one\nline two\n"
                        (push-mark (point) t t)
                        (forward-line 2)
                        (call-interactively 'markdown-pre-region)
                        (should (string-equal (buffer-string) "    line one\n    line two\n\n"))))

(ert-deftest test-markdown-insertion/blockquote-region-1 ()
  "Test `markdown-blockquote-region'."
  ;; Simple test as non-interactive command
  (markdown-test-string "line one\nline two\n"
                        (markdown-blockquote-region (line-beginning-position) (line-end-position))
                        (should (string-equal (buffer-string) "> line one\n\nline two\n")))
  ;; Test removal of whitespace before and after region
  (markdown-test-string "line one abc\nline two\n"
                        (markdown-blockquote-region 6 9)
                        (should (string-equal (buffer-string) "line\n\n> one\n\nabc\nline two\n")))
  ;; Simple test as interactive command
  (markdown-test-string "line one\nline two\n"
                        (push-mark (point) t t)
                        (forward-line 2)
                        (call-interactively 'markdown-blockquote-region)
                        (should (string-equal (buffer-string) "> line one\n> line two\n\n"))))

(ert-deftest test-markdown-insertion/pre-nested-lists ()
  "Test `markdown-pre-indentation' and `markdown-insert-pre' with nested list."
  (markdown-test-string "* item\n    * item\n"
                        ;; before the first item
                        (should (string-equal (markdown-pre-indentation (point)) "    "))
                        (markdown-insert-pre)
                        (beginning-of-line)
                        (should (markdown-prev-line-blank-p))
                        (should (looking-at "^    $"))
                        (should (markdown-next-line-blank-p))
                        ;; before the second item
                        (forward-line 3)
                        (should (string-equal (markdown-pre-indentation (point)) "        "))
                        (markdown-insert-pre)
                        (beginning-of-line)
                        (should (markdown-prev-line-blank-p))
                        (should (looking-at "^        $"))
                        (should (markdown-next-line-blank-p))
                        ;; after the second item
                        (forward-line 3)
                        (should (string-equal (markdown-pre-indentation (point)) "            "))
                        (markdown-insert-pre)
                        (beginning-of-line)
                        (should (markdown-prev-line-blank-p))
                        (should (looking-at "^            $"))
                        (should (markdown-next-line-blank-p))))

(ert-deftest test-markdown-insertion/pre-faux-list ()
  "Test `markdown-pre-indentation' following a list-marker in a pre block."
  (markdown-test-string "    * pre block, not a list item\n"
                        (should (string-equal (markdown-pre-indentation (point-max)) "    "))))

(ert-deftest test-markdown-insertion/blockquote-nested-lists ()
  "Test blockquote insertion in a nested list context."
  (markdown-test-string "* item\n    * item\n"
                        ;; before the first item
                        (should (string-equal (markdown-blockquote-indentation (point)) ""))
                        (markdown-insert-blockquote)
                        (beginning-of-line)
                        (should (markdown-prev-line-blank-p))
                        (should (looking-at "^> $"))
                        (should (markdown-next-line-blank-p))
                        ;; before the second item
                        (forward-line 3)
                        (should (string-equal (markdown-blockquote-indentation (point)) "    "))
                        (markdown-insert-blockquote)
                        (beginning-of-line)
                        (should (markdown-prev-line-blank-p))
                        (should (looking-at "^    > $"))
                        (should (markdown-next-line-blank-p))
                        ;; after the second item
                        (forward-line 3)
                        (should (string-equal (markdown-blockquote-indentation (point)) "        "))
                        (markdown-insert-blockquote)
                        (beginning-of-line)
                        (should (markdown-prev-line-blank-p))
                        (should (looking-at "^        > $"))
                        (should (markdown-next-line-blank-p))))

(ert-deftest test-markdown-insertion/blockquote-region-with-newline ()
  (markdown-test-string "a\n\nb\n"
    (markdown-blockquote-region 1 (point-max))
    (should (equal (buffer-string) "> a\n>\n> b\n\n"))))

(ert-deftest test-markdown-insertion/empty-italic ()
  "Test `markdown-insert-italic' with no word at point and no region."
  (markdown-test-string ""
                        (call-interactively 'markdown-insert-italic)
                        (should (string-equal (buffer-string) "**"))
                        (should (= (point) 2))))

(ert-deftest test-markdown-insertion/empty-bold ()
  "Test `markdown-insert-bold' with no word at point and no region."
  (markdown-test-string ""
                        (call-interactively 'markdown-insert-bold)
                        (should (string-equal (buffer-string) "****"))
                        (should (= (point) 3))))

(ert-deftest test-markdown-insertion/uri ()
  "Test `markdown-insert-uri'."
  (markdown-test-string "http://jblevins.org/projects/markdown-mode/"
                        (call-interactively 'markdown-insert-uri)
                        (should (string-equal (buffer-string) "<http://jblevins.org/projects/markdown-mode/>"))
                        (should (= (point) 2))
                        (call-interactively 'markdown-insert-uri)
                        (should (string-equal (buffer-string) "http://jblevins.org/projects/markdown-mode/"))
                        (should (= (point) 1))
                        (erase-buffer)
                        (call-interactively 'markdown-insert-uri)
                        (should (string-equal (buffer-string) "<>"))
                        (should (= (point) 2))))

(ert-deftest test-markdown-insertion/list-item ()
  "Test `markdown-insert-list-item' on several lists."
  ;; No existing list
  (markdown-test-string "abc"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "abc\n  * "))
                        (should (= (point) 9)))
  ;; Following a list item, on the same line
  (markdown-test-string "  * foo"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "  * foo\n  * ")))
  ;; Following a list item, on the next line
  (markdown-test-string "- foo\n"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "- foo\n- ")))
  ;; Following a list item, after a blank line
  (markdown-test-string "- foo\n\n"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "- foo\n\n- ")))
  ;; Preceding a list item
  (markdown-test-string "- foo\n"
                        (goto-char (point-min))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "- \n- foo\n")))
  ;; Preceding a list item and a blank line
  (markdown-test-string "\n\n- foo\n"
                        (goto-char (point-min))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "- \n\n- foo\n")))
  ;; In the middle of a list item
  (markdown-test-string "- foo bar\n"
                        (forward-word)
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "- foo\n-  bar\n")))
  ;; Before a list marker, but not at beginning of line
  (markdown-test-string "   - foo\n"
                        (forward-char 2)
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "   - \n   - foo\n")))
  ;; Following an ordered list item
  (markdown-test-string "6. foo"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "6. foo\n7. ")))
  ;; Following a fancy list item, on the next line
  (markdown-test-string "#. foo"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "#. foo\n#. ")))
  ;; Following a nested ordered list item
  (markdown-test-string "6. foo\n    1. bar"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "6. foo\n    1. bar\n    2. ")))
  ;; Preceding an ordered list item
  (markdown-test-string "\n1. foo\n2. bar"
                        (goto-char (point-min))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string) "1. \n1. foo\n2. bar")))
  ;; Preserve previous spacing in ordered list
  (markdown-test-string "1.        foo"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string)  "1.        foo\n2.        ")))
  ;; Adjust spacing for number width changes (e.g., 9 -> 10)
  (markdown-test-string "9.  foo"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string)  "9.  foo\n10. ")))
  ;; Don't adjust spacing for number width changes if no extra whitespace
  (markdown-test-string "99. foo"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string)  "99. foo\n100. ")))
  ;; Don't adjust spacing if tabs are used as whitespace
  (markdown-test-string "9.\tfoo"
                        (goto-char (point-max))
                        (call-interactively 'markdown-insert-list-item)
                        (should (string-equal (buffer-string)  "9.\tfoo\n10.\t"))))

(ert-deftest test-markdown-insertion/nested-list-marker ()
  "Test marker detection for `markdown-insert-list-item'."
  (markdown-test-string
   "1. A\n    * AA\n        1. AAA"
   (goto-char (point-max))
   (let ((current-prefix-arg '(4)))
     (call-interactively 'markdown-insert-list-item))
   (should (eq (point) 36))
   (should (looking-back "\* "))
   (should (string-equal
            (buffer-string)
            "1. A\n    * AA\n        1. AAA\n    * "))
   (let ((current-prefix-arg '(4)))
     (call-interactively 'markdown-insert-list-item))
   (should (eq (point) 40))
   (should (looking-back "2\. "))
   (should (string-equal
            (buffer-string)
            "1. A\n    * AA\n        1. AAA\n    * \n2. "))
   (let ((current-prefix-arg '(4)))
     (call-interactively 'markdown-insert-list-item))
   (should (eq (point) 44))
   (should (looking-back "3\. "))
   (should (string-equal
            (buffer-string)
            "1. A\n    * AA\n        1. AAA\n    * \n2. \n3. "))))

(ert-deftest test-markdown-insertion/reference-link ()
  "Basic tests for `markdown-insert-reference-link'."
  ;; Test optional parameters (leave point after link)
  (markdown-test-string ""
                        (markdown-insert-reference-link "abc" "1")
                        (should (string-equal (buffer-string) "[abc][1]"))
                        (should (= (point) 9)))
  ;; Full link without title (leave point after link)
  (markdown-test-string ""
                        (markdown-insert-reference-link "link" "label" "http://jblevins.org/")
                        (should (string-equal (buffer-string) "[link][label]\n\n[label]: http://jblevins.org/\n"))
                        (should (= (point) 14)))
  ;; Full link without label or title (leave point after link)
  (markdown-test-string ""
                        (markdown-insert-reference-link "link" "" "http://jblevins.org/")
                        (should (string-equal (buffer-string) "[link][]\n\n[link]: http://jblevins.org/\n"))
                        (should (= (point) 9)))
  ;; Link only with no label, URL, or title (leave point after link)
  (markdown-test-string ""
                        (markdown-insert-reference-link "link" "")
                        (should (string-equal (buffer-string) "[link][]"))
                        (should (= (point) 9))))

(ert-deftest test-markdown-insertion/reference-link-end ()
  "Basic reference link insertion test for 'end location."
  (let ((markdown-reference-location 'end))
    (markdown-test-string "first para\n\nsecond para\n"
                          (end-of-line)
                          (markdown-insert-reference-link "link" "" "http://jblevins.org/")
                          (should (= (point) 19))
                          (goto-char (point-min))
                          (forward-line 4)
                          (should (looking-at "\\[link\\]: http://jblevins.org/")))))

(ert-deftest test-markdown-insertion/reference-link-immediately ()
  "Basic reference link insertion test for 'immediately location."
  (let ((markdown-reference-location 'immediately))
    (markdown-test-string "first para\n\nsecond para\n"
                          (end-of-line)
                          (markdown-insert-reference-link "link" "" "http://jblevins.org/")
                          (should (= (point) 19))
                          (goto-char (point-min))
                          (forward-line 2)
                          (should (looking-at "\\[link\\]: http://jblevins.org/")))))

(ert-deftest test-markdown-insertion/reference-link-header ()
  "Basic reference link and definition insertion test for 'header location."
  (let ((markdown-reference-location 'header))
    (markdown-test-string "par one\n\npar two\n\n### header\n"
                          (end-of-line)
                          (markdown-insert-reference-link "link" "")
                          (markdown-insert-reference-definition "link")
                          (should (= (point) 35))
                          (should (looking-back "\\[link\\]: " nil)))))

(ert-deftest test-markdown-insertion/reference-definition-block ()
  "Test whitespace when inserting a reference definition among others"
  (let ((markdown-reference-location 'header))
    (markdown-test-string "text

[1]: https://www.gnu.org/

### header
"
      (markdown-insert-reference-definition "2")
      (should (= (point) 38))
      (should (looking-back "https://www.gnu.org/\n\\[2\\]: " nil)))))

(ert-deftest test-markdown-insertion/reference-link-before-file-locals ()
  "Test inserting a reference link before file-local variables."
  (markdown-test-string "

<!-- Local Variables: -->
<!-- mode: markdown -->
<!-- End: -->
"
    (markdown-insert-reference-link "link" "" "http://jblevins.org/")
    (should (equal (buffer-substring-no-properties 1 (point-max))
                   "[link][]

\[link]: http://jblevins.org/

<!-- Local Variables: -->
<!-- mode: markdown -->
<!-- End: -->
"))
    (should (equal (point) 9))))

(ert-deftest test-markdown-insertion/inline-to-reference-link ()
  "Inline link to reference link conversion with tab completion."
  (markdown-test-string "[text](http://jblevins.org/ \"title\")"
                        (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET M-DEL M-DEL M-DEL [1] RET RET h TAB RET RET"))
                        (should (string-equal (buffer-string) "[text][1]\n\n[1]: http://jblevins.org/ \"title\"\n"))))

(ert-deftest test-markdown-insertion/inline-to-reference-link-2 ()
  "Inline link to reference link conversion with existing reference links."
  (markdown-test-string "[text](http://jblevins.org/ \"title\")\n\n[1]: https://www.gnu.org"
                        (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET M-DEL M-DEL M-DEL [1] RET RET"))
                        (should (string-equal (buffer-string) "[text][1]\n\n[1]: https://www.gnu.org"))))

(ert-deftest test-markdown-insertion/inline-link-angle-url-at-point ()
  "Test `markdown-insert-link' with angle URL at point."
  (markdown-test-string "<https://www.gnu.org/>"
    (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET RET GNU RET RET"))
    (should (string-equal (buffer-string) "[GNU](https://www.gnu.org/)"))))

(ert-deftest test-markdown-insertion/inline-link-plain-url-at-point ()
  "Test `markdown-insert-link' with plain URL at point."
  (markdown-test-string "https://www.gnu.org/"
    (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET RET GNU RET RET"))
    (should (string-equal (buffer-string) "[GNU](https://www.gnu.org/)"))))

(ert-deftest test-markdown-insertion/inline-link-reference-link-at-point ()
  "Test `markdown-insert-link' with reference link at point."
  (markdown-test-string ""
    (markdown-insert-reference-link "link" "label" "http://jblevins.org/")
    (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET DEL DEL DEL DEL DEL DEL DEL http://example.com/ RET RET RET"))
    (should (string-equal (buffer-substring 1 28) "[link](http://example.com/)"))
    (should (= (point) 28))))

(ert-deftest test-markdown-insertion/inline-link-active-region ()
  "Test `markdown-insert-link' with active region."
  (markdown-test-string "abc def ghi"
    (let ((tmm-orig transient-mark-mode))
      (transient-mark-mode 1)
      (push-mark (point) t t)
      (forward-word 2)
      (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET http://example.com/ RET RET RET"))
      (should (string-equal (buffer-string) "[abc def](http://example.com/) ghi"))
      (should (= (point) 31))
      (transient-mark-mode tmm-orig))))

;;; Footnote tests:

(ert-deftest test-markdown-footnote/basic-end ()
  "Basic footnote insertion and deletion tests for 'end location."
  (let ((markdown-footnote-location 'end))
    (markdown-test-string "first line\nsecond line\n"
                          ;; new buffer with no footnotes
                          (should (= markdown-footnote-counter 0))
                          ;; footnote insertion
                          (end-of-line)
                          (markdown-insert-footnote)
                          (should (= (point) 35))
                          (should (= markdown-footnote-counter 1))
                          (should (looking-back "\\[^1\\]: " nil))
                          ;; kill with point in footnote definition
                          (insert "footnote text")
                          (let (kill-ring)
                            (markdown-footnote-kill))
                          (should (= (point) 24))
                          (should (bolp))
                          (should (string-equal (buffer-string) "first line\nsecond line\n"))
                          ;; insertion, counter should increment
                          (goto-char (point-min))
                          (end-of-line)
                          (markdown-insert-footnote)
                          (should (= (point) 35))
                          (should (= markdown-footnote-counter 2))
                          (should (looking-back "\\[^2\\]: " nil))
                          (insert "footnote text")
                          ;; return to marker
                          (markdown-footnote-return)
                          (should (= (point) 15))
                          (should (looking-back "\\[^2\\]" nil))
                          ;; kill with point at marker
                          (let (kill-ring)
                            (markdown-footnote-kill))
                          (should (= (point) 11))
                          (should (eolp))
                          (should (string-equal (buffer-string) "first line\nsecond line\n")))))

(ert-deftest test-markdown-footnote/basic-immediately ()
  "Basic footnote insertion and deletion tests for 'immediately location."
  (let ((markdown-footnote-location 'immediately))
    (markdown-test-string "first paragraph\n\nsecond paragraph\n"
                          ;; new buffer with no footnotes
                          (should (= markdown-footnote-counter 0))
                          ;; footnote insertion
                          (end-of-line)
                          (markdown-insert-footnote)
                          (should (= (point) 28))
                          (should (= markdown-footnote-counter 1))
                          (should (looking-back "\\[^1\\]: " nil))
                          ;; kill with point in footnote definition
                          (insert "footnote text")
                          (let (kill-ring)
                            (markdown-footnote-kill))
                          (should (= (point) 18))
                          (should (bolp))
                          (should (string-equal (buffer-string)
                                                "first paragraph\n\nsecond paragraph\n")))))

(ert-deftest test-markdown-footnote/basic-header ()
  "Basic footnote insertion and deletion tests for 'header location."
  (let ((markdown-footnote-location 'header))
    (markdown-test-string "par one\n\npar two\n\n### header\n"
                          ;; new buffer with no footnotes
                          (should (= markdown-footnote-counter 0))
                          ;; footnote insertion
                          (end-of-line)
                          (markdown-insert-footnote)
                          (should (= (point) 29))
                          (should (= markdown-footnote-counter 1))
                          (should (looking-back "\\[^1\\]: " nil))
                          ;; kill with point in footnote definition
                          (insert "footnote text")
                          (let (kill-ring)
                            (markdown-footnote-kill))
                          (should (= (point) 19))
                          (should (bolp))
                          (should (string-equal (buffer-string)
                                                "par one\n\npar two\n\n### header\n"))
                          ;; insertion, counter should increment
                          (goto-char (point-min))
                          (end-of-line)
                          (markdown-insert-footnote)
                          (should (= (point) 29))
                          (should (= markdown-footnote-counter 2))
                          (should (looking-back "\\[^2\\]: " nil))
                          (insert "footnote text")
                          ;; return to marker
                          (markdown-footnote-return)
                          (should (= (point) 12))
                          (should (looking-back "\\[^2\\]" nil))
                          ;; kill with point at marker
                          (let (kill-ring)
                            (markdown-footnote-kill))
                          (should (= (point) 8))
                          (should (eolp))
                          (should (string-equal (buffer-string)
                                                "par one\n\npar two\n\n### header\n")))))

(ert-deftest test-markdown-footnote/basic-subtree ()
  "Basic footnote insertion and deletion tests for 'subtree location."
  (let ((markdown-footnote-location 'subtree))
    (markdown-test-string "# h1\n\nfoo\n\n## h2\n\nbar\n"
      ;; new buffer with no footnotes
      (should (= markdown-footnote-counter 0))
      ;; footnote insertion
      (forward-line 2)
      (end-of-line)
      (markdown-insert-footnote)
      (should (= (point) 34))
      (should (= markdown-footnote-counter 1))
      (should (looking-back "\\[^1\\]: " nil)))))

(ert-deftest test-markdown-footnote/kill-empty-text ()
  "Test killing a footnote with marker but no text."
  (markdown-test-string "no text[^1]\n\n[^1]: \n"
                        (end-of-line)
                        (markdown-footnote-goto-text)
                        (should (looking-back "\\[^1\\]: " nil))
                        (let (kill-ring)
                          (markdown-footnote-kill))
                        (should (string-equal (buffer-string) "no text\n"))))

(ert-deftest test-markdown-footnote/kill-empty-after ()
  "Test killing an empty footnote after one with text (previously killed the
footnote with text above)."
  (markdown-test-string "[^with-text][^no-text]\n\n[^with-text]: Text\n[^no-text]:"
                        (let (kill-ring)
                          (forward-line 3)
                          (should (looking-at "\\[\\^no-text\\]:$"))
                          (markdown-footnote-kill)
                          (should (string-equal (current-kill 0) "")))))

(ert-deftest test-markdown-footnote/kill-hanging-paras ()
  "Test killing a footnote where block text starts after the label (previously
killed the footnote above)."
  (markdown-test-string "[^1][^2]\n\n[^1]: Foo\n\n[^2]:\n    Text\n\n    More text\n\n\nNot indented"
                        (let (kill-ring)
                          (forward-line 4)
                          (should (looking-at "\\[\\^2\\]:$"))
                          (markdown-footnote-kill)
                          ;; We want to include the leading space on hanging footnote paragraphs,
                          ;; even if a hanging paragraph is the first item in the footnote.
                          (should (string-equal (current-kill 0) "Text\n\n    More text\n")))))

(ert-deftest test-markdown-footnote/text-positions-buffer-top ()
  "Test markdown-footnote-text-positions on footnote adjacent to buffer top
(was infinite loop)."
  (markdown-test-string "[^label]: text\n    more text"
   (should (equal (markdown-footnote-text-positions) (list "^label" 1 29)))))

(ert-deftest test-markdown-footnote/text-positions-buffer-top-one-line ()
  "Test markdown-footnote-text-positions on one-line footnote adjacent to
buffer top (failed to find positions)."
  (markdown-test-string "[^label]: text\n"
                        (should (equal (markdown-footnote-text-positions) (list "^label" 1 16)))))

(ert-deftest test-markdown-footnote/text-positions-buffer-top-not-footnote ()
  "Test markdown-footnote-text-positions on plain paragraph adjacent to buffer
top (was infinite loop)."
  (markdown-test-string "text\n    more text\n"
                        (should (eq (markdown-footnote-text-positions) nil))))

(ert-deftest test-markdown-footnote/text-positions-buffer-bottom ()
  "Test markdown-footnote-text-positions on footnote adjacent to buffer bottom
(was infinite loop)."
  (markdown-test-string "\n[^label]: text\n    more text"
   (forward-line 1)
   (should (equal (markdown-footnote-text-positions) (list "^label" 2 30)))))

(ert-deftest test-markdown-footnote/kill-adjacent-footnote ()
  "Test killing a footnote adjacent to other one-line footnotes (previously
killed the wrong one)."
  (markdown-test-string "Text[^1] with[^2] footnotes[^3]\n\n[^1]: foo\n[^2]: bar\n[^3]: baz"
                        (let (kill-ring)
                          (forward-line 3)
                          (should (looking-at "\\[\\^2\\]: bar"))
                          (markdown-footnote-kill)
                          (should (string-equal (current-kill 0) "bar\n")))))

(ert-deftest test-markdown-footnote/kill-adjacent-markers ()
  "Test killing a footnote where the labels are adjacent (previously, the wrong
footnote would be killed because the attempt to jump to the marker would jump to
the opening bracket of [^2], and then subsequent functions would kill [^2])."
  (markdown-test-string "Text with footnotes[^1][^2]\n\n[^1]: foo\n\n[^2]: bar\n"
                        (let (kill-ring)
                          (forward-line 2)
                          (should (looking-at "\\[\\^1\\]: foo"))
                          (markdown-footnote-kill)
                          (should (string-equal (current-kill 0) "foo\n")))))

(ert-deftest test-markdown-footnote-reference/jump ()
  "Test `markdown-do' for footnotes and reference links."
  (markdown-test-string
      "body[^1], [link 1][ref],
[link 2][ref]

[^1]: footnote

[ref]: https://duckduckgo.com/"
   (goto-char 5) ; start of [^1]
   (markdown-do) ; markdown-footnote-goto-text
   (should (looking-at "footnote"))
   (markdown-do) ; markdown-footnote-return
   (should (= (point) 9)) ; just after [^1]
   (markdown-next-link) ; beginning of [link 1][]
   (markdown-do)
   (should (looking-at "https://duckduckgo.com/"))
   (should (equal (markdown-reference-find-links "ref")
                  (list (list "link 2" 26 2) (list "link 1" 11 1))))
   (markdown-do) ; opens a reference link buffer
   (should (string= (buffer-string) "Links using reference ref:\n\nlink 1 (line 1)\nlink 2 (line 2)\n"))
   (should (looking-at "link 1")) ; in reference link popop buffer
   (execute-kbd-macro (read-kbd-macro "RET")) ; jump to "link 1"
   (should (looking-at "\\[link 1\\]")) ; back in main buffer
   (should (= (point) 11))))

;;; Element removal tests:

(ert-deftest test-markdown-kill/simple ()
  "Simple tests for `markdown-kill-thing-at-point'."
  (let ((kill-ring nil)
        (tests (list '("`foo`" . "foo")
                     '("## foo ##" . "foo")
                     '("## foo" . "foo")
                     '("foo\n---" . "foo")
                     '("foo\n===" . "foo")
                     '("* * * * *" . "* * * * *")
                     '("[foo](http://bar.com/)" . "foo")
                     '("![foo](http://bar.com/)" . "foo")
                     '("[foo][bar]" . "foo")
                     '("![foo][bar]" . "foo")
                     '("<http://foo.com/>" . "http://foo.com/")
                     '("<foo@bar.com>" . "foo@bar.com")
                     '("**foo**" . "foo")
                     '("__foo__" . "foo")
                     '("*foo*" . "foo")
                     '("_foo_" . "foo")
                     '("  [foo]: http://bar.com/" . "http://bar.com/")
                     '("  [foo]: http://bar.com/ \"title\"" . "http://bar.com/")
                     '("foo[^bar]\n\n[^bar]: baz" . "baz")
                     '("[^bar]: baz" . "baz")
                     '("  * foo\n  bar" . "  * foo\n  bar"))))
    (dolist (test tests)
      ;; Load test string (the car), move to end of first line, kill
      ;; thing at point, and then verify that the kill ring contains cdr.
      (markdown-test-string (car test)
                            (end-of-line)
                            (call-interactively 'markdown-kill-thing-at-point)
                            (should (string-equal (current-kill 0) (cdr test)))))))

(ert-deftest test-markdown-kill/footnote-text ()
  "Test killing a footnote with point at footnote text."
  (markdown-test-string "some text[^1]\n\n[^1]: footnote\n"
                        (end-of-line)
                        (markdown-footnote-goto-text)
                        (let (kill-ring)
                          (markdown-footnote-kill))
                        (should (string-equal (buffer-string) "some text\n"))))

(ert-deftest test-markdown-kill/code ()
  "Test killing with code regex.."
  (let ((kill-ring nil))
    (markdown-test-string "Lorem `ipsum` dolor `sit` `amet`."
                          (goto-char 22) ; position point at s in `sit`
                          (call-interactively 'markdown-kill-thing-at-point)
                          (should (string-equal (current-kill 0) "sit")))))

;;; Completion:

(ert-deftest test-markdown-complete/atx-header-incomplete ()
  "Test `markdown-incomplete-atx-p'."
  (markdown-test-string "###  ###"
                        (should (looking-at markdown-regex-header-atx))
                        (should-not (markdown-incomplete-atx-p)))
  (markdown-test-string "###abc###"
                        (should-not (looking-at markdown-regex-header-atx)))
  (markdown-test-string "###   ###"
                        (should (looking-at markdown-regex-header-atx))
                        (should (markdown-incomplete-atx-p))))

(ert-deftest test-markdown-complete/atx-header ()
  "Test `markdown-complete' for atx headers."
  (markdown-test-string "##### test"
                        (call-interactively 'markdown-complete)
                        (should (string-equal (buffer-string) "##### test #####"))))

(ert-deftest test-markdown-complete/setext-header-incomplete ()
  "Test `markdown-incomplete-setext-p'."
  (markdown-test-string "abc\n===\n"
                        (should (looking-at markdown-regex-header-setext))
                        (should-not (markdown-incomplete-setext-p)))
  (markdown-test-string "abc\n==\n"
                        (should (looking-at markdown-regex-header-setext))
                        (should (markdown-incomplete-setext-p)))
  (markdown-test-string "abc\n====\n"
                        (should (looking-at markdown-regex-header-setext))
                        (should (markdown-incomplete-setext-p))))

(ert-deftest test-markdown-complete/setext-header ()
  "Test `markdown-complete' for setext headers."
  (markdown-test-string "test  \n=="
                        (call-interactively 'markdown-complete)
                        (should (string-equal (buffer-string) "test\n===="))))

(ert-deftest test-markdown-complete/hr-incomplete ()
  "Test `markdown-incomplete-hr-p'."
  (dolist (i (number-sequence 0 (1- (length markdown-hr-strings))))
    (markdown-test-string (nth i markdown-hr-strings)
                          (should (looking-at markdown-regex-hr))
                          (should-not (markdown-incomplete-hr-p))
                          (should-error (call-interactively 'markdown-complete)))))

(ert-deftest test-markdown-complete/hr ()
  "Test completion via `markdown-complete' for horizontal rules."
  (markdown-test-string "- - - - -"
                        (call-interactively 'markdown-complete)
                        (should (string-equal (buffer-string) (car markdown-hr-strings)))))

(ert-deftest test-markdown-complete/buffer-setext-2 ()
  "Test `markdown-complete-buffer' for level two setext heading."
  ;; Ensure markdown-complete-buffer doesn't mistake this for a horizontal rule
  (markdown-test-string "Subheading\n--\n"
                        (call-interactively 'markdown-complete-buffer)
                        (should (string-equal (buffer-string) "Subheading\n----------\n\n")))
  (markdown-test-string "Abc\n--\n\nDef\n--\n"
                        (call-interactively 'markdown-complete-buffer)
                        (should (string-equal (buffer-string) "Abc\n---\n\nDef\n---\n\n"))))

;;; Promotion and demotion tests:

(ert-deftest test-markdown-promote/atx-header ()
  "Test `markdown-promote' for atx headers."
  (markdown-test-string "###### test ######"
                        (markdown-promote)
                        (should (string-equal (buffer-string) "##### test #####"))
                        (markdown-promote)
                        (should (string-equal (buffer-string) "#### test ####"))
                        (markdown-promote)
                        (should (string-equal (buffer-string) "### test ###"))
                        (markdown-promote)
                        (should (string-equal (buffer-string) "## test ##"))
                        (markdown-promote)
                        (should (string-equal (buffer-string) "# test #"))))

(ert-deftest test-markdown-demote/atx-header ()
  "Test `markdown-demote' for atx headers."
  (markdown-test-string "# test #"
                        (markdown-demote)
                        (should (string-equal (buffer-string) "## test ##"))
                        (markdown-demote)
                        (should (string-equal (buffer-string) "### test ###"))
                        (markdown-demote)
                        (should (string-equal (buffer-string) "#### test ####"))
                        (markdown-demote)
                        (should (string-equal (buffer-string) "##### test #####"))
                        (markdown-demote)
                        (should (string-equal (buffer-string) "###### test ######"))))

(ert-deftest test-markdown-promote/setext-header ()
  "Test `markdown-promote' for setext headers."
  (markdown-test-string "test\n----"
                        (markdown-promote)
                        (should (string-equal (buffer-string) "test\n===="))))

(ert-deftest test-markdown-demote/setext-header ()
  "Test `markdown-demote' for setext headers."
  (markdown-test-string "test\n===="
                        (markdown-demote)
                        (should (string-equal (buffer-string) "test\n----"))
                        (markdown-demote)
                        (should (string-equal (buffer-string) "### test ###"))
                        (markdown-demote)
                        (should (string-equal (buffer-string) "#### test ####"))
                        (markdown-demote)
                        (should (string-equal (buffer-string) "##### test #####"))
                        (markdown-demote)
                        (should (string-equal (buffer-string) "###### test ######"))))

(ert-deftest test-markdown-promote/hr ()
  "Test `markdown-promote' for horizontal rules."
  (markdown-test-string (car (reverse markdown-hr-strings))
                        (dolist (n (number-sequence 4 0 -1))
                          (markdown-promote)
                          (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))

(ert-deftest test-markdown-demote/hr ()
  "Test `markdown-demote' for horizontal rules."
  (markdown-test-string (car markdown-hr-strings)
                        (dolist (n (number-sequence 1 5))
                          (markdown-demote)
                          (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))

(ert-deftest test-markdown-promote/bold ()
  "Test `markdown-promote' for bold markup."
  (markdown-test-string "__bold__"
                        (call-interactively 'markdown-promote)
                        (should (string-equal (buffer-string) "**bold**"))))

(ert-deftest test-markdown-demote/bold ()
  "Test `markdown-demote' for bold markup."
  (markdown-test-string "**bold**"
                        (call-interactively 'markdown-promote)
                        (should (string-equal (buffer-string) "__bold__"))))

(ert-deftest test-markdown-promote/italic ()
  "Test `markdown-promote' for italic markup."
  (markdown-test-string "_italic_"
                        (call-interactively 'markdown-promote)
                        (should (string-equal (buffer-string) "*italic*"))))

(ert-deftest test-markdown-demote/italic ()
  "Test `markdown-demote' for italic markup."
  (markdown-test-string "*italic*"
                        (call-interactively 'markdown-promote)
                        (should (string-equal (buffer-string) "_italic_"))))

;;; Subtree editing tests:

(ert-deftest test-markdown-subtree/promote ()
  "Test `markdown-promote-subtree'."
  (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
                        ;; The first h1 should get promoted away.
                        ;; The second h1 should not be promoted.
                        (markdown-promote-subtree)
                        (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
                        ;; Second call should do nothing since point is no longer at a heading.
                        (markdown-promote-subtree)
                        (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
                        ;; Move to h2 and promote again.
                        (forward-line 2)
                        (markdown-promote-subtree)
                        (should (string-equal (buffer-string) "h1\n\nh2\n\n# h3 #\n\n# h2 #\n\n# h1 #\n"))))

(ert-deftest test-markdown-subtree/promote-single-section ()
  "Test `markdown-promote-subtree' on a single or last section.
Should not cause an infinite loop."
  (markdown-test-string "foo\n\n## h2 ##\n\nbar\n"
    ;; The h2 should get promoted to h1 away.
    (markdown-test-goto-heading "h2")
    (markdown-promote-subtree)
    (should (string-equal (buffer-string) "foo\n\n# h2 #\n\nbar\n"))))

(ert-deftest test-markdown-subtree/demote ()
  "Test `markdown-demote-subtree'."
  (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
                        ;; The second h1 should not be demoted
                        (markdown-demote-subtree)
                        (should (string-equal (buffer-string) "## h1 ##\n\n### h2 ###\n\n#### h3 ####\n\n### h2 ###\n\n# h1 #\n"))
                        (markdown-demote-subtree)
                        (should (string-equal (buffer-string) "### h1 ###\n\n#### h2 ####\n\n##### h3 #####\n\n#### h2 ####\n\n# h1 #\n"))
                        (markdown-demote-subtree)
                        (should (string-equal (buffer-string) "#### h1 ####\n\n##### h2 #####\n\n###### h3 ######\n\n##### h2 #####\n\n# h1 #\n"))
                        ;; Stop demoting at level six
                        (markdown-demote-subtree)
                        (should (string-equal (buffer-string) "##### h1 #####\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))
                        (markdown-demote-subtree)
                        (should (string-equal (buffer-string) "###### h1 ######\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))))

(ert-deftest test-markdown-subtree/move-up ()
  "Test `markdown-move-subtree-up'."
  ;; Note that prior to Emacs 24.5, this does not work for the last subtree in
  ;; the buffer due to Emacs bug #19102:
  ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19102
  ;; https://github.com/emacs-mirror/emacs/commit/b3910f
  ;; That also corrects the type of the "Cannot move pase superior level" error
  ;; from 'error to 'user-error.
  (markdown-test-string "# 1 #\n\n## 1.1 ##\n\n### 1.1.1 ###\n\n## 1.2 ##\n\n### 1.2.1 ###\n\n# 2 #\n# Extra\n"
                        (re-search-forward "^# 2")
                        (markdown-move-subtree-up)
                        (should (string-equal (buffer-string) "# 2 #\n# 1 #\n\n## 1.1 ##\n\n### 1.1.1 ###\n\n## 1.2 ##\n\n### 1.2.1 ###\n\n# Extra\n"))
                        ;; Second attempt should fail, leaving buffer unchanged.
                        ;; (This way of asserting the contents of the error
                        ;; message is a bit convoluted and more fragile than
                        ;; ideal. But prior to Emacs 24.5, the type of this
                        ;; error is just 'error, and a bare "should-error" is
                        ;; really overly broad.)
                        (should (string-equal
                                 "Cannot move past superior level"
                                 (cl-second (should-error (markdown-move-subtree-up)))))))

(ert-deftest test-markdown-subtree/move-down ()
  "Test `markdown-move-subtree-down'."
  (markdown-test-string "# 1 #\n\n## 1.1 ##\n\n### 1.1.1 ###\n\n## 1.2 ##\n\n### 1.2.1 ###\n\n# 2 #\n"
                        (re-search-forward "^## 1\.1")
                        (markdown-move-subtree-down)
                        (should (string-equal (buffer-string) "# 1 #\n\n## 1.2 ##\n\n### 1.2.1 ###\n\n## 1.1 ##\n\n### 1.1.1 ###\n\n# 2 #\n"))))

(ert-deftest test-markdown-subtree/mark ()
  "Test `markdown-mark-subtree'."
  (markdown-test-file "outline.text"
    (markdown-next-visible-heading 1)
    (should-not mark-active)
    (markdown-mark-subtree)
    (should (= (point) 19))
    (should (= (mark) 349))
    (should mark-active)
    (deactivate-mark)
    (should-not mark-active)
    (markdown-forward-same-level 1)
    (markdown-mark-subtree)
    (should (= (point) 351))
    (should (= (mark) 515))
    (should mark-active)))

(ert-deftest test-markdown-subtree/narrow ()
  "Test `markdown-narrow-to-subtree'."
  (markdown-test-file "outline.text"
    (markdown-next-visible-heading 1)
    (markdown-forward-same-level 1)
    (widen)
    (should (= (point-min) 1))
    (should (= (point-max) 553))
    (markdown-narrow-to-subtree)
    (should (= (point-min) 351))
    (should (= (point-max) 515))))

;;; Cycling:

(ert-deftest test-markdown-cycle/atx-header ()
  "Test `markdown-demote' cycling for atx headers."
  (markdown-test-string "# test"
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "## test ##"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "### test ###"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "#### test ####"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "##### test #####"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "###### test ######"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "###### test ######"))))

(ert-deftest test-markdown-cycle/setext-header ()
  "Test `markdown-demote' cycling for setext headers."
  (markdown-test-string "test\n===="
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "test\n----"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "### test ###"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "#### test ####"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "##### test #####"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "###### test ######"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "###### test ######"))))

(ert-deftest test-markdown-cycle/hr ()
  "Test cycling of horizontal rules."
  ;; Cycle using markdown-demote
  (markdown-test-string (car markdown-hr-strings)
                        (dolist (n (number-sequence 1 5))
                          (call-interactively 'markdown-demote)
                          (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) (car markdown-hr-strings))))
  ;; Cycle using markdown-promote
  (markdown-test-string (car (reverse markdown-hr-strings))
                        (dolist (n (number-sequence 4 0 -1))
                          (call-interactively 'markdown-promote)
                          (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
                        (call-interactively 'markdown-promote)
                        (should (string-equal (buffer-string) (car (reverse markdown-hr-strings))))))

(ert-deftest test-markdown-cycle/bold ()
  "Test cycling of bold markup."
  (markdown-test-string "**bold**"
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "__bold__"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "**bold**"))))

(ert-deftest test-markdown-cycle/italic ()
  "Test cycling of italic markup."
  (markdown-test-string "*italic*"
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "_italic_"))
                        (call-interactively 'markdown-demote)
                        (should (string-equal (buffer-string) "*italic*"))))

;;; Indentation tests:

(ert-deftest test-markdown-indentation/calc-indents ()
  "Test `markdown-calc-indents' a nested list context."
  (markdown-test-file "nested-list.text"
                      (goto-char (point-max))
                      (let ((indents (markdown-calc-indents)))
                        (should (= (car indents) 17)) ; indentation of previous line first
                        (should (equal (sort indents '<)
                                       (list
                                        0 ; beginning of line
                                        3 ; first-level list marker
                                        7 ; second-level list marker
                                        11 ; third-level list marker
                                        13 ; previous list item text
                                        16 ; pre-block indentation
                                        17 ; indentation of previous line
                                        21 ; previous line plus tab-width
                                        ))))))

(ert-deftest test-markdown-indentation/indent-region ()
  "Test `markdown-indent-region'."
  ;; Basic test with multiple lines
  (markdown-test-string "abc\ndef\nghi\n"
                        (markdown-indent-region (point-min) (point-max) nil)
                        (should (string-equal (buffer-string) "    abc\n    def\n    ghi\n")))
  ;; Following a list item
  (markdown-test-string "  * abc\ndef\n"
                        (forward-line)
                        (markdown-indent-region (line-beginning-position) (line-end-position) nil)
                        (should (string-equal (buffer-string) "  * abc\n  def\n"))
                        (markdown-indent-region (line-beginning-position) (line-end-position) nil)
                        (should (string-equal (buffer-string) "  * abc\n    def\n"))))

(ert-deftest test-markdown-indentation/indent-list-hanging ()
  "Test `markdown-indent-line' with hanging list item."
  (markdown-test-string
   "- list
  - nested list with long lines which need to be
    hard wrapped"
   (goto-char (point-max))
   (markdown-enter-key)
   (should (eq (point) 78))))

(ert-deftest test-markdown-indentation/indent-list-single ()
  "Test `markdown-indent-line' with single list item."
  (let ((markdown-indent-on-enter 'indent-and-new-item))
    (markdown-test-string "  * item 1"
      (end-of-line)
      (call-interactively #'markdown-enter-key)
      (should (string-equal (buffer-string) "  * item 1\n  * "))
      (should (eq (point) 16))
      (call-interactively #'markdown-enter-key)
      (should (string-equal (buffer-string) "  * item 1\n\n"))
      (should (eq (point) 13)))))

(ert-deftest test-markdown-indentation/indent-nested-list ()
  "Test `markdown-enter-key' with a nested list item."
  (let ((markdown-indent-on-enter 'indent-and-new-item))
    (markdown-test-string "* foo\n* bar\n  * baz"
      (goto-char (point-max))
      (call-interactively #'markdown-enter-key)
      (should (string-equal (buffer-string) "* foo\n* bar\n  * baz\n  * "))
      (should (eq (point) 25))
      (call-interactively #'markdown-enter-key)
      (should (string-equal (buffer-string) "* foo\n* bar\n  * baz\n\n"))
      (should (eq (point) 22)))))

(ert-deftest test-markdown-indentation/indent-pre ()
  "Test `markdown-indent-line' with a pre block."
  (markdown-test-string
   "I'm gonna write a code block:

    my first line of code"
   (goto-char (point-max))
   (markdown-enter-key)
   (should (eq (point) 62))
   (should (looking-back "^    "))))

(ert-deftest test-markdown-indentation/indent-hanging-line ()
  "Test `markdown-indent-line' with hanging indentation.
See GH-245."
  (markdown-test-string "Stuff
  More"
    (forward-line)
    (should (looking-at "^  More"))
    (should (= (current-column) 0))
    (should (= (current-indentation) 2))
    (let ((last-command this-command)
          (this-command 'markdown-cycle))
      (call-interactively #'markdown-cycle))
    (should (= (current-column) 0))
    (should (= (current-indentation) 0))))

(ert-deftest test-markdown-indentation/continue-gfm-task-lists ()
  (markdown-test-string "   -   [X] item"
    (end-of-line)
    (let ((markdown-indent-on-enter 'indent-and-new-item))
      (call-interactively #'markdown-enter-key))
    (should (string-equal (buffer-string) "   -   [X] item\n   -   [ ] "))
    (should (= (point) 28))))

;;; Markup hiding tests:

(ert-deftest test-markdown-markup-hiding/italics-1 ()
  "Test hiding markup for italics."
  (markdown-test-file "inline.text"
    (goto-char 9)
    (should (looking-at "\*italic\*"))
    (markdown-test-range-has-property (point) (point) 'invisible 'markdown-markup)
    (should-not (invisible-p (point)))
    (should-not (invisible-p (+ 1 (point))))
    (markdown-toggle-markup-hiding t)
    (should (invisible-p (point)))
    (should-not (invisible-p (+ 1 (point))))))

(ert-deftest test-markdown-markup-hiding/bold-1 ()
  "Test hiding markup for bold."
  (markdown-test-file "inline.text"
    (goto-char 27)
    (should (looking-at "\*\*bold\*\*"))
    (markdown-test-range-has-property (point) (1+ (point)) 'invisible 'markdown-markup)
    (should-not (invisible-p (point)))
    (should-not (invisible-p (+ 1 (point))))
    (should-not (invisible-p (+ 2 (point))))
    (markdown-toggle-markup-hiding t)
    (should (invisible-p (point)))
    (should (invisible-p (+ 1 (point))))
    (should-not (invisible-p (+ 2 (point))))))

(ert-deftest test-markdown-markup-hiding/code-1 ()
  "Test hiding markup for inline code."
  (markdown-test-file "inline.text"
    (goto-char 45)
    (should (looking-at "`code`"))
    (markdown-test-range-has-property (point) (point) 'invisible 'markdown-markup)
    (should-not (invisible-p (point)))
    (should-not (invisible-p (1+ (point))))
    (markdown-toggle-markup-hiding t)
    (should (invisible-p (point)))
    (should-not (invisible-p (1+ (point))))))

(ert-deftest test-markdown-markup-hiding/kbd-1 ()
  "Test hiding markup for <kbd> tags."
  (markdown-test-string "<kbd>C-c C-x C-m</kbd>"
    (markdown-test-range-has-property (point) (+ 4 (point)) 'invisible 'markdown-markup)
    (should-not (invisible-p (point))) ;; part of <kbd>
    (should-not (invisible-p (+ 4 (point)))) ;; part of <kbd>
    (should-not (invisible-p (+ 5 (point)))) ;; inside <kbd>
    (markdown-toggle-markup-hiding t)
    (should (invisible-p (point))) ;; part of <kbd>
    (should (invisible-p (+ 4 (point)))) ;; part of <kbd>
    (should-not (invisible-p (+ 5 (point)))))) ;; inside <kbd>

(ert-deftest test-markdown-markup-hiding/inline-links ()
  "Test hiding markup for inline links."
  (markdown-test-file "inline.text"
    (goto-char 925)
    (should (looking-at "\\[text\\](http://www.w3.org/ \"title\")"))
    (markdown-test-range-has-property 925 925 'invisible 'markdown-markup) ; [
    (markdown-test-range-has-property 930 958 'invisible 'markdown-markup) ; ](...)
    (should-not (invisible-p 925))
    (should-not (invisible-p 958))
    (markdown-toggle-markup-hiding t)
    (should (invisible-p 925))
    (should-not (invisible-p 926))
    (should (invisible-p 958))))

(ert-deftest test-markdown-markup-hiding/reference-links ()
  "Test hiding markup for reference links."
  (markdown-test-string "[text][ref]"
    (markdown-test-range-has-property 1 1 'invisible 'markdown-markup) ; [
    (markdown-test-range-has-property 6 11 'invisible 'markdown-markup) ; ][ref]
    (should-not (invisible-p 1))
    (should-not (invisible-p 6))
    (markdown-toggle-markup-hiding t)
    (should (invisible-p 1))
    (should-not (invisible-p 2))
    (should (invisible-p 6))))

(ert-deftest test-markdown-markup-hiding/angle-urls ()
  "Test hiding markup for angle urls."
  (markdown-test-string "<http://jblevins.org/projects/markdown-mode/>"
    (markdown-test-range-has-property 1 1 'invisible 'markdown-markup) ; <
    (markdown-test-range-has-property 45 45 'invisible 'markdown-markup) ; >
    (should-not (invisible-p 1))
    (should-not (invisible-p 2))
    (should-not (invisible-p 45))
    (markdown-toggle-markup-hiding t)
    (should (invisible-p 1))
    (should-not (invisible-p 2))
    (should (invisible-p 45))))

(ert-deftest test-markdown-markup-hiding/list-items ()
  "Test hiding markup for list items."
  (let ((markdown-hide-markup t))
    (markdown-test-file "nested-list.text"
      (markdown-test-range-has-property 4 4 'display (nth 0 markdown-list-item-bullets))
      (markdown-test-range-has-property 194 194 'display (nth 0 markdown-list-item-bullets))
      (markdown-test-range-has-property 224 224 'display (nth 1 markdown-list-item-bullets))
      (markdown-test-range-has-property 525 525 'display (nth 2 markdown-list-item-bullets)))))

(ert-deftest test-markdown-markup-hiding/gfm-code-blocks ()
  "Test hiding markup for GFM code blocks."
  (let ((markdown-hide-markup t))
    (markdown-test-file "GFM.md"
      (markdown-test-range-has-property 1548 1552 'invisible 'markdown-markup)
      (should (invisible-p 1548))
      (should (invisible-p 1552))
      (markdown-test-range-has-property 1607 1609 'invisible 'markdown-markup)
      (should (invisible-p 1607))
      (should (invisible-p 1609)))))

(ert-deftest test-markdown-markup-hiding/fenced-code-blocks ()
  "Test hiding markup for tilde fenced code blocks."
  (let ((markdown-hide-markup t))
    (markdown-test-file "outline-code.text"
      (markdown-test-range-has-property 83 93 'invisible 'markdown-markup)
      (should (invisible-p 83))
      (should (invisible-p 93))
      (markdown-test-range-has-property 154 156 'invisible 'markdown-markup)
      (should (invisible-p 154))
      (should (invisible-p 156)))))

;;; Font lock tests:

(ert-deftest test-markdown-font-lock/italics-1 ()
  "A simple italics test."
  (markdown-test-file "inline.text"
                      (goto-char 9)
                      (should (looking-at "\*"))
                      ;; Check face of char before leading asterisk
                      (markdown-test-range-has-face 8 8 nil)
                      ;; Check face of italic range
                      (markdown-test-range-has-face 9 9 'markdown-markup-face)
                      (markdown-test-range-has-face 10 16 'markdown-italic-face)
                      (markdown-test-range-has-face 17 17 'markdown-markup-face)
                      ;; Check face of point past leading asterisk
                      (markdown-test-range-has-face 18 18 nil)))

(ert-deftest test-markdown-font-lock/italics-2 ()
  "Test space after leading asterisk or underscore."
  (markdown-test-string
   "This is * not italic*, nor _ is this_."
   (markdown-test-range-has-face (point-min) (point-max) nil)))

(ert-deftest test-markdown-font-lock/italics-3 ()
  "Test that slash inside asterisks is not italic."
  (markdown-test-string
   "not italic *\\*"
   (markdown-test-range-has-face (point-min) (point-max) nil)))

(ert-deftest test-markdown-font-lock/italics-4 ()
  "Test escaped asterisk inside italics."
  (markdown-test-string
   "italic *\\**"
   (markdown-test-range-has-face 1 7 nil)
   (markdown-test-range-has-face 8 8 'markdown-markup-face)
   (markdown-test-range-has-face 9 10 'markdown-italic-face)
   (markdown-test-range-has-face 11 11 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/italics-5 ()
  "Test italic single letter."
  (markdown-test-string
   "*a*"
   (markdown-test-range-has-face 1 1 'markdown-markup-face)
   (markdown-test-range-has-face 2 2 'markdown-italic-face)
   (markdown-test-range-has-face 3 3 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/italics-6 ()
  "Test multiline italics across list items."
  (markdown-test-string
   "* something about function foo_bar
* something else about foo_bar"
   (markdown-test-range-has-face 31 34 nil)
   (markdown-test-range-has-face 38 62 nil)))

(ert-deftest test-markdown-font-lock/italics-8 ()
  "Test multiline italics across list items."
  (markdown-test-string
   "* something about function
  foo_bar
* something else about
  foo_bar"
   (markdown-test-range-has-face 30 36 nil)
   (markdown-test-range-has-face 63 69 nil)))

(ert-deftest test-markdown-font-lock/italics-9 ()
  "Test multiline italics across list items."
  (markdown-test-string
   "foo_bar
* foo_bar"
   (markdown-test-range-has-face 4 7 nil)
   (markdown-test-range-has-face 11 14 nil)))

(ert-deftest test-markdown-font-lock/italics-10 ()
  "Underscores in URLs should not trigger italics."
  (markdown-test-string
   "<http://jblevins.org/research/centroid/cd_z_path.m>"
   (markdown-test-range-face-equals 2 50 'markdown-plain-url-face)
   (should-not (markdown-range-property-any 43 43 'face '(markdown-italic-face)))))

(ert-deftest test-markdown-font-lock/italics-11 ()
  "Underscores in URLs should not trigger italics."
  (markdown-test-string
   "[1]: http://jblevins.org/research/centroid/cd_z_path.m"
   (markdown-test-range-face-equals 6 54 'markdown-url-face)))

(ert-deftest test-markdown-font-lock/italics-12 ()
  "Underscores in URLs should not trigger italics."
  (markdown-test-string
   "[cd\\_z\\_path.m](http://jblevins.org/research/centroid/cd_z_path.m)"
   (markdown-test-range-face-equals 17 65 'markdown-url-face)))

(ert-deftest test-markdown-font-lock/italics-after-hr ()
  "Test italics after a horizontal rule with asterisks."
  (markdown-test-string "* * *\n\n*italic*\n"
                        (markdown-test-range-has-face 1 5 'markdown-hr-face)
                        (markdown-test-range-has-face 8 8 'markdown-markup-face)
                        (markdown-test-range-has-face 9 14 'markdown-italic-face)
                        (markdown-test-range-has-face 15 15 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/italics-in-heading ()
  "Test italic overlay in a heading."
  (markdown-test-string
   "# *Italics* in a Heading"
   (markdown-test-range-has-face 3 3 'markdown-markup-face)
   (markdown-test-range-has-face 4 10 'markdown-italic-face)
   (markdown-test-range-has-face 11 11 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/italics-link ()
  "Test italic overlay in an inline link."
  (markdown-test-string
   "*[italic link](http://www.link.com/)*"
   (markdown-test-range-has-face 1 1 'markdown-markup-face)
   (markdown-test-range-has-face 2 36 'markdown-italic-face)
   (markdown-test-range-has-face 37 37 'markdown-markup-face))
  (markdown-test-string
   "[*italic link*](http://www.link.com/)"
   (markdown-test-range-has-face 2 2 'markdown-markup-face)
   (markdown-test-range-has-face 3 13 'markdown-italic-face)
   (markdown-test-range-has-face 14 14 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/italics-in-blockquote ()
  "Test italics overlay in a blockquote."
  (markdown-test-string
   "> *italics* inside a blockquote"
   (markdown-test-range-has-face 3 3 'markdown-markup-face)
   (markdown-test-range-has-face 4 10 'markdown-italic-face)
   (markdown-test-range-has-face 11 11 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/italics-in-pre ()
  "Test italics overlay in a blockquote."
  (markdown-test-string
   "    *italics* inside a pre block"
   (markdown-test-range-has-face (point-min) (1- (point-max))
                                 'markdown-pre-face)))

(ert-deftest test-markdown-font-lock/italics-and-code ()
  "Test seeming italics mixed with code."
  (markdown-test-string
   "define `var_1` and `var_2` inline code"
   (markdown-test-range-has-face 9 13 'markdown-inline-code-face)
   (markdown-test-range-has-face 21 25 'markdown-inline-code-face))
  (markdown-test-string
   "`var_1` and var_2"
   (markdown-test-range-has-face 2 6 'markdown-inline-code-face)
   (markdown-test-range-has-face 8 17 nil))
  (markdown-test-string
   "var_1 and `var_2`"
   (markdown-test-range-has-face 1 10 nil)
   (markdown-test-range-has-face 12 16 'markdown-inline-code-face)))

(ert-deftest test-markdown-font-lock/code-in-italics ()
  "Test inline code inside italics.
See GH-275."
  (markdown-test-string
   "*text `code` text*"
   (markdown-test-range-has-face 1 1 'markdown-markup-face)
   (markdown-test-range-has-face 2 17 'markdown-italic-face)
   (markdown-test-range-has-face 7 7 'markdown-markup-face)
   (markdown-test-range-has-face 8 11 'markdown-inline-code-face)
   (markdown-test-range-has-face 12 12 'markdown-markup-face)
   (markdown-test-range-has-face 18 18 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/italics-in-reference-definitions ()
  "Test not matching italics in reference definitions across lines."
  (markdown-test-string
   "[lg]: twilight_sm.png\n[sm]: twilight_lg.png"
   (markdown-test-range-has-face 7 21 'markdown-url-face)
   (markdown-test-range-has-face 22 22 nil)
   (markdown-test-range-has-face 29 43 'markdown-url-face)
   (markdown-test-range-has-face 28 28 nil)))

(ert-deftest test-markdown-font-lock/italics-in-comment ()
  "Test not matching italics in comments."
  (markdown-test-string
   "<!-- -*- coding: utf-8 -*- -->"
   (markdown-test-range-has-face 1 30 'markdown-comment-face)
   (should-not (markdown-range-property-any 1 30 'face '(markdown-italic-face)))))

(ert-deftest test-markdown-font-lock/italics-after-bold ()
  "Test bold and italics on the same line.
See GH-223."
  (markdown-test-string
   "**foo** is a *bar*"
   (markdown-test-range-has-face 1 2 'markdown-markup-face)
   (markdown-test-range-has-face 3 5 'markdown-bold-face)
   (markdown-test-range-has-face 6 7 'markdown-markup-face)
   (should-not
    (markdown-range-property-any 8 13 'face '(markdown-italic-face)))
   (markdown-test-range-has-face 14 14 'markdown-markup-face)
   (markdown-test-range-has-face 15 17 'markdown-italic-face)
   (markdown-test-range-has-face 18 18 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/bold-1 ()
  "A simple bold test."
  (markdown-test-file "inline.text"
                      (goto-char 27)
                      (should (looking-at "\*\*"))
                      ;; Check face of char before leading asterisk
                      (markdown-test-range-has-face 26 26 nil)
                      ;; Check face of opening asterisks
                      (markdown-test-range-has-face 27 28 'markdown-markup-face)
                      ;; Check face of bold range
                      (markdown-test-range-has-face 29 33 'markdown-bold-face)
                      ;; Check face of closing asterisks
                      (markdown-test-range-has-face 34 35 'markdown-markup-face)
                      ;; Check face of point past leading asterisk
                      (markdown-test-range-has-face 36 36 nil)))

(ert-deftest test-markdown-font-lock/bold-2 ()
  "Test space after leading asterisks or underscores."
  (markdown-test-string
   "This is ** not bold**, nor __ is this__."
   (should-not
    (markdown-range-property-any
     (point-min) (point-max) 'face '(markdown-bold-face)))))

(ert-deftest test-markdown-font-lock/bold-3 ()
  "Test escaped asterisk inside bold."
  (markdown-test-string
   "bold **\\***"
   (markdown-test-range-has-face 1 5 nil)
   (markdown-test-range-has-face 6 7 'markdown-markup-face)
   (markdown-test-range-has-face 8 9 'markdown-bold-face)
   (markdown-test-range-has-face 10 11 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/bold-4 ()
  "Test bold single letter."
  (markdown-test-string
   "**a**"
   (markdown-test-range-has-face 1 2 'markdown-markup-face)
   (markdown-test-range-has-face 3 3 'markdown-bold-face)
   (markdown-test-range-has-face 4 5 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/bold-after-hr ()
  "Test bold after a horizontal rule with asterisks."
  (markdown-test-string "* * *\n\n**bold**\n"
   (markdown-test-range-has-face 1 5 'markdown-hr-face)
   (markdown-test-range-has-face 8 9 'markdown-markup-face)
   (markdown-test-range-has-face 10 13 'markdown-bold-face)
   (markdown-test-range-has-face 14 15 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/bold-link ()
  "Test bold overlay in an inline link."
  (markdown-test-string
   "**[bold link](http://www.link.com/)**"
   (markdown-test-range-has-face 1 2 'markdown-markup-face)
   (markdown-test-range-has-face 3 35 'markdown-bold-face)
   (markdown-test-range-has-face 36 37 'markdown-markup-face))
  (markdown-test-string
   "[**bold link**](http://www.link.com/)"
   (markdown-test-range-has-face 2 3 'markdown-markup-face)
   (markdown-test-range-has-face 4 12 'markdown-bold-face)
   (markdown-test-range-has-face 13 14 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/bold-in-blockquote ()
  "Test bold overlay in a blockquote."
  (markdown-test-string
   "> **bold** inside a blockquote"
   (markdown-test-range-has-face 3 4 'markdown-markup-face)
   (markdown-test-range-has-face 5 8 'markdown-bold-face)
   (markdown-test-range-has-face 9 10 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/bold-in-pre ()
  "Test bold overlay in a blockquote."
  (markdown-test-string
   "    **bold** inside a pre block"
   (markdown-test-range-has-face (point-min) (1- (point-max))
                                 'markdown-pre-face)))

(ert-deftest test-markdown-font-lock/no-bold-in-code ()
  "Bold markers in inline code should not trigger bold."
  (markdown-test-string
   "`def __init__(self):`"
   (markdown-test-range-has-face 8 11 'markdown-inline-code-face)
   (should-not (markdown-range-property-any
                (point-min) (point-max) 'face '(markdown-bold-face))))
  (markdown-test-string
   "`**foo` bar `baz**`"
   (markdown-test-range-has-face 2 6 'markdown-inline-code-face)
   (markdown-test-range-face-equals 9 11 nil)
   (markdown-test-range-has-face 14 18 'markdown-inline-code-face)
   (should-not (markdown-range-property-any
                (point-min) (point-max) 'face '(markdown-bold-face)))))

(ert-deftest test-markdown-font-lock/code-in-bold ()
  "Test inline code inside bold."
  (markdown-test-string
   "**text `code` text**"
   (markdown-test-range-has-face 1 2 'markdown-markup-face)
   (markdown-test-range-has-face 3 18 'markdown-bold-face)
   (markdown-test-range-has-face 8 8 'markdown-markup-face)
   (markdown-test-range-has-face 9 12 'markdown-inline-code-face)
   (markdown-test-range-has-face 13 13 'markdown-markup-face)
   (markdown-test-range-has-face 19 20 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/bold-in-comment ()
  "Test not matching bold in comments."
  (markdown-test-string
   "<!-- **not bold** -->"
   (markdown-test-range-has-face 1 21 'markdown-comment-face)
   (should-not
    (markdown-range-property-any 1 21 'face '(markdown-bold-face)))))

(ert-deftest test-markdown-font-lock/no-bold-in-url ()
  "Test not matching bold in plain URL links."
  (markdown-test-string
   "<https://example.com/__not-bold__>"
   (should-not (markdown-range-property-any 23 30 'face '(markdown-bold-face)))))

(ert-deftest test-markdown-font-lock/code-1 ()
  "A simple inline code test."
  (markdown-test-file "inline.text"
   (goto-char 45)
   (should (looking-at "`"))
   ;; Regular code span
   (markdown-test-range-has-face 45 45 'markdown-markup-face)
   (markdown-test-range-has-face 46 49 'markdown-inline-code-face)
   (markdown-test-range-has-face 50 50 'markdown-markup-face)
   ;; Code containing backticks
   (markdown-test-range-has-face 61 62 'markdown-markup-face)
   (markdown-test-range-has-face 63 87 'markdown-inline-code-face)
   (markdown-test-range-has-face 88 89 'markdown-markup-face)
   ;; Seven backquotes in a row
   (markdown-test-range-has-face 119 125 nil)
   ;; Backquotes at beginning or end
   (markdown-test-range-has-face 228 229 'markdown-markup-face)
   (markdown-test-range-has-face 230 237 'markdown-inline-code-face)
   (markdown-test-range-has-face 238 239 'markdown-markup-face)
   (markdown-test-range-has-face 341 342 'markdown-markup-face)
   (markdown-test-range-has-face 343 349 'markdown-inline-code-face)
   (markdown-test-range-has-face 350 351 'markdown-markup-face)
   ;; Backslash as final character
   (markdown-test-range-has-face 460 460 'markdown-markup-face)
   (markdown-test-range-has-face 461 467 'markdown-inline-code-face)
   (markdown-test-range-has-face 468 468 'markdown-markup-face)
   ;; Escaping of leading backquotes
   (markdown-test-range-has-face 586 592 nil)
   (markdown-test-range-has-face 597 603 nil)
   ;; A code span crossing lines
   (markdown-test-range-has-face 652 656 nil)
   (markdown-test-range-has-face 657 657 'markdown-markup-face)
   (markdown-test-range-has-face 658 665 'markdown-inline-code-face)
   (markdown-test-range-has-face 666 666 'markdown-markup-face)
   ;; Three backquotes: same line, across lines, not across blocks
   (markdown-test-range-has-face 695 748 nil)
   (markdown-test-range-has-face 749 750 'markdown-markup-face)
   (markdown-test-range-has-face 751 755 'markdown-inline-code-face)
   (markdown-test-range-has-face 756 757 'markdown-markup-face)
   (markdown-test-range-has-face 758 805 nil)
   (markdown-test-range-has-face 806 807 'markdown-markup-face)
   (markdown-test-range-has-face 808 812 'markdown-inline-code-face)
   (markdown-test-range-has-face 813 814 'markdown-markup-face)
   (markdown-test-range-has-face 815 891 nil)
   ))

(ert-deftest test-markdown-font-lock/code-2 ()
  "Multiple code spans in a row and on different lines."
  (markdown-test-string "`foo` `bar` `baz`"
   (markdown-test-range-has-face 1 1 'markdown-markup-face)
   (markdown-test-range-has-face 2 4 'markdown-inline-code-face)
   (markdown-test-range-has-face 5 5 'markdown-markup-face)
   (markdown-test-range-has-face 6 6 nil)
   (markdown-test-range-has-face 7 7 'markdown-markup-face)
   (markdown-test-range-has-face 8 10 'markdown-inline-code-face)
   (markdown-test-range-has-face 11 11 'markdown-markup-face)
   (markdown-test-range-has-face 12 12 nil)
   (markdown-test-range-has-face 13 13 'markdown-markup-face)
   (markdown-test-range-has-face 14 16 'markdown-inline-code-face)
   (markdown-test-range-has-face 17 17 'markdown-markup-face))
  (markdown-test-string "`a`\n`b`\n`c`\n"
   (markdown-test-range-has-face 1 1 'markdown-markup-face)
   (markdown-test-range-has-face 2 2 'markdown-inline-code-face)
   (markdown-test-range-has-face 3 3 'markdown-markup-face)
   (markdown-test-range-has-face 4 4 nil)
   (markdown-test-range-has-face 5 5 'markdown-markup-face)
   (markdown-test-range-has-face 6 6 'markdown-inline-code-face)
   (markdown-test-range-has-face 7 7 'markdown-markup-face)
   (markdown-test-range-has-face 8 8 nil)
   (markdown-test-range-has-face 9 9 'markdown-markup-face)
   (markdown-test-range-has-face 10 10 'markdown-inline-code-face)
   (markdown-test-range-has-face 11 11 'markdown-markup-face)
   (markdown-test-range-has-face 12 12 nil))
  (markdown-test-string "a`foo`b`bar`c`baz`d"
   (markdown-test-range-has-face 1 1 nil)
   (markdown-test-range-has-face 2 2 'markdown-markup-face)
   (markdown-test-range-has-face 3 5 'markdown-inline-code-face)
   (markdown-test-range-has-face 6 6 'markdown-markup-face)
   (markdown-test-range-has-face 7 7 nil)
   (markdown-test-range-has-face 8 8 'markdown-markup-face)
   (markdown-test-range-has-face 9 11 'markdown-inline-code-face)
   (markdown-test-range-has-face 12 12 'markdown-markup-face)
   (markdown-test-range-has-face 13 13 nil)
   (markdown-test-range-has-face 14 14 'markdown-markup-face)
   (markdown-test-range-has-face 15 17 'markdown-inline-code-face)
   (markdown-test-range-has-face 18 18 'markdown-markup-face)
   (markdown-test-range-has-face 19 19 nil)))

(ert-deftest test-markdown-font-lock/code-3 ()
  "Backslashes don't escape backticks inside of inline code strings."
  (markdown-test-string
   "`foo\\`bar`"
   (markdown-test-range-has-face 1 1 'markdown-markup-face)
   (markdown-test-range-has-face 2 5 'markdown-inline-code-face)
   (markdown-test-range-has-face 6 6 'markdown-markup-face)
   (markdown-test-range-has-face 7 10 nil)))

(ert-deftest test-markdown-font-lock/code-link-precedence ()
  "Test that inline code takes precedence over inline links."
  (markdown-test-string
   "[not a `link](/foo`)"
   (markdown-test-range-has-face 1 7 nil)
   (markdown-test-range-has-face 8 8 'markdown-markup-face)
   (markdown-test-range-has-face 9 18 'markdown-inline-code-face)
   (markdown-test-range-has-face 19 19 'markdown-markup-face)
   (markdown-test-range-has-face 20 20 nil)))

(ert-deftest test-markdown-font-lock/code-in-link-text ()
  "Test that inline code in link text is fontified properly"
  (markdown-test-string
   "[`this is a` link](/foo)"
   (markdown-test-range-has-face 1 2 'markdown-markup-face)
   (markdown-test-range-has-face 3 11 'markdown-inline-code-face)
   (markdown-test-range-has-face 12 12 'markdown-markup-face)
   (markdown-test-range-has-face 3 17 'markdown-link-face)
   (markdown-test-range-has-face 18 19 'markdown-markup-face)
   (markdown-test-range-has-face 20 23 'markdown-url-face)
   (markdown-test-range-has-face 24 24 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/code-italics-precedence ()
  "Test that inline code takes precedence over italics.
Test currently fails because this case isn't handled properly."
  (markdown-test-string
   "*text `code* text`"
   (markdown-test-range-has-face 1 6 nil)
   (markdown-test-range-has-face 7 7 'markdown-markup-face)
   (markdown-test-range-has-face 8 17 'markdown-inline-code-face)
   (markdown-test-range-has-face 18 18 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/code-in-comment ()
  "Test that inline code is not matched inside a comment."
  (markdown-test-string
   "<!-- `not code` -->"
   (markdown-test-range-has-face 1 19 'markdown-comment-face)
   (should-not (markdown-range-property-any 1 19 'face '(markdown-inline-code-face)))))

(ert-deftest test-markdown-font-lock/kbd ()
  "Test font lock for <kbd> tags."
  (markdown-test-string "<kbd>C-c <</kbd>"
   (markdown-test-range-has-face 1 5 'markdown-markup-face)
   (markdown-test-range-has-face 6 10 'markdown-inline-code-face)
   (markdown-test-range-has-face 11 16 'markdown-markup-face))
  (markdown-test-string "To quit Emacs, press <kbd>C-x C-c</kbd>."
   (markdown-test-range-has-face 1 21 nil)
   (markdown-test-range-has-face 22 26 'markdown-markup-face)
   (markdown-test-range-has-face 27 33 'markdown-inline-code-face)
   (markdown-test-range-has-face 34 39 'markdown-markup-face)
   (markdown-test-range-has-face 40 40 nil)))

(ert-deftest test-markdown-font-lock/lists-1 ()
  "A simple list marker font lock test."
  (markdown-test-file "lists.text"
   (dolist (loc (list 1063 1283 1659 1830 1919 2150 2393 2484
                      2762 2853 3097 3188 3700 3903 4009))
     (goto-char loc)
     (should (looking-at "[*+-]"))
     (markdown-test-range-has-face loc loc 'markdown-list-face))))

(ert-deftest test-markdown-font-lock/definition-list ()
  "A simple definition list marker font lock test."
  (markdown-test-file "definition-list.text"
    (markdown-test-range-has-face 7 7 'markdown-list-face)
    (markdown-test-range-has-face 29 52 'markdown-pre-face)
    (markdown-test-range-has-face 55 55 'markdown-list-face)))

(ert-deftest test-markdown-font-lock/pre-1 ()
  "Nested list and pre block font lock test."
  (markdown-test-file "nested-list.text"
    (dolist (loc (list 4 29 194 224 491 525))
      (markdown-test-range-has-face loc loc 'markdown-list-face))
    (markdown-test-range-has-face 6 25 nil)
    (markdown-test-range-has-face 31 83 nil)
    (markdown-test-range-has-face 85 154 'markdown-pre-face)
    (markdown-test-range-has-face 157 189 nil)
    (markdown-test-range-has-face 196 215 nil)
    (markdown-test-range-has-face 226 403 nil)
    (markdown-test-range-has-face 405 481 'markdown-pre-face)
    (markdown-test-range-has-face 493 512 nil)
    (markdown-test-range-has-face 527 546 nil)
    (markdown-test-range-has-face 548 580 'markdown-pre-face)))

(ert-deftest test-markdown-font-lock/pre-2 ()
  (markdown-test-string "* item\n\nreset baseline\n\n    pre block\n"
   (markdown-test-range-has-face 1 1 'markdown-list-face)
   (markdown-test-range-has-face 2 23 nil)
   (markdown-test-range-has-face 29 37 'markdown-pre-face)))

(ert-deftest test-markdown-font-lock/pre-3 ()
  (markdown-test-string "It is interesting to see what happens when one queries
`social upheaval` and `protopalatial era`.

* `social upheaval`: the follwing queries have been tried:

    social upheaval subClassOf"
   (markdown-test-range-has-face 160 190 nil)))

(ert-deftest test-markdown-font-lock/pre-4 ()
  "Pre blocks must be preceded by a blank line"
  (markdown-test-string "Paragraph
    for (var i = 0; i < 10; i++) {
        console.log(i);
    }"
    (markdown-test-range-has-face (point-min) (point-max) nil)))

(ert-deftest test-markdown-font-lock/fenced-1 ()
  "Test fenced code blocks containing four-space indents."
  (markdown-test-string "Fenced code block

~~~
if (x)
    foo();

if (y)
    bar();
~~~
"
   (markdown-test-range-has-face 1 19 nil)
   (markdown-test-range-has-face 20 22 'markdown-markup-face)
   (markdown-test-range-has-face 24 59 'markdown-pre-face)
   (markdown-test-range-has-face 61 63 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/gfm-fenced-1 ()
  "Test GFM-style fenced code blocks (1)."
  (let ((markdown-fontify-code-blocks-natively t))
    (markdown-test-string "```ruby
require 'redcarpet'
markdown = Redcarpet.new('Hello World!')
puts markdown.to_html
```"
      (markdown-test-range-has-face 1 3 'markdown-markup-face) ; ```
      (markdown-test-range-has-face 4 7 'markdown-language-keyword-face) ; ruby
      (markdown-test-range-has-face 9 90 'markdown-code-face) ; entire code block
      (markdown-test-range-has-face 9 15 'font-lock-builtin-face) ; require
      (markdown-test-range-has-face 17 27 'font-lock-string-face) ; 'redcarpet'
      (markdown-test-range-has-face 40 48 'font-lock-type-face) ; Redcarpet
      (markdown-test-range-has-face 70 72 'font-lock-builtin-face) ; puts
      (markdown-test-range-has-face 92 94 'markdown-markup-face)))) ; ```

(ert-deftest test-markdown-font-lock/gfm-fenced-2 ()
  "Test GFM-style fenced code blocks (2)."
  (markdown-test-string "```{r sum}\n2+2\n```"
   (markdown-test-range-has-face 1 3 'markdown-markup-face) ; ```
   (markdown-test-range-has-face 4 4 'markdown-markup-face) ; {
   (markdown-test-range-has-face 5 5 'markdown-language-keyword-face) ; r
   (markdown-test-range-has-face 7 9 'markdown-language-info-face) ; sum
   (markdown-test-range-has-face 10 10 'markdown-markup-face) ; }
   (markdown-test-range-has-face 12 14 'markdown-pre-face) ; 2+2
   (markdown-test-range-has-face 16 18 'markdown-markup-face))) ; ```

(ert-deftest test-markdown-font-lock/gfm-fenced-3 ()
  "GFM-style code blocks need not be preceded by a blank line."
  (markdown-test-string "Paragraph
```js
for (var i = 0; i < 10; i++) {
    console.log(i);
}
```"
    (markdown-test-range-has-face 1 10 nil) ; Paragraph
    (markdown-test-range-has-face 11 13 'markdown-markup-face) ; ```
    (markdown-test-range-has-face 14 15 'markdown-language-keyword-face) ; js
    (markdown-test-range-has-face 17 68 'markdown-pre-face)
    (markdown-test-range-has-face 70 72 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/gfm-fenced-4 ()
  "Test GFM-style fenced code blocks (2)."
  (markdown-test-string "```scalaFiddle libraries=\"Java8 Time-0.1.0\"\nimport java.time._\n\nval hour = LocalTime.now().getHour()\n\nprintln(hour)\n```"
   (markdown-test-range-has-face 1 3 'markdown-markup-face) ; ```
   (markdown-test-range-has-face 4 14 'markdown-language-keyword-face) ; scalaFiddle
   (markdown-test-range-has-face 16 43 'markdown-language-info-face) ; libraries="Java8 Time-0.1.0"
   (markdown-test-range-has-face 45 115 'markdown-pre-face) ; [code]
   (markdown-test-range-has-face 117 119 'markdown-markup-face))) ; ```

(ert-deftest test-markdown-font-lock/tilde-fenced-1 ()
  "Test native fontification of tilde fenced code blocks."
  (let ((markdown-fontify-code-blocks-natively t))
    (markdown-test-string "~~~ruby
require 'redcarpet'
markdown = Redcarpet.new('Hello World!')
puts markdown.to_html
~~~"
      (markdown-test-range-has-face 1 3 'markdown-markup-face) ; ```
      (markdown-test-range-has-face 4 7 'markdown-language-keyword-face) ; ruby
      (markdown-test-range-has-face 9 90 'markdown-code-face) ; entire code block
      (markdown-test-range-has-face 9 15 'font-lock-builtin-face) ; require
      (markdown-test-range-has-face 17 27 'font-lock-string-face) ; 'redcarpet'
      (markdown-test-range-has-face 40 48 'font-lock-type-face) ; Redcarpet
      (markdown-test-range-has-face 70 72 'font-lock-builtin-face) ; puts
      (markdown-test-range-has-face 92 94 'markdown-markup-face)))) ; ```

(ert-deftest test-markdown-font-lock/atx-no-spaces ()
  "Test font-lock for atx headers with no spaces."
  (markdown-test-string "##abc##"
   (markdown-test-range-has-face 1 7 nil))
  (markdown-test-string "##"
   (markdown-test-range-has-face 1 2 nil))
  (markdown-test-string "###"
   (markdown-test-range-has-face 1 3 nil)))

(ert-deftest test-markdown-font-lock/setext-1-letter ()
  "An edge case for level-one setext headers."
  (markdown-test-string "a\n=\n"
   (markdown-test-range-has-face 1 1 'markdown-header-face-1)
   (markdown-test-range-has-face 3 3 'markdown-header-rule-face)))

(ert-deftest test-markdown-font-lock/setext-2-letter ()
  "An edge case for level-two setext headers."
  (markdown-test-string "b\n-\n"
   (markdown-test-range-has-face 1 1 'markdown-header-face-2)
   (markdown-test-range-has-face 3 3 'markdown-header-rule-face)))

(ert-deftest test-markdown-font-lock/inline-links ()
  "Test font lock for inline links."
  (let ((markdown-hide-urls nil))
    (markdown-test-file "inline.text"
      (markdown-test-range-has-face 925 925 'markdown-markup-face)
      (markdown-test-range-has-face 926 929 'markdown-link-face)
      (markdown-test-range-has-face 930 931 'markdown-markup-face)
      (markdown-test-range-has-face 932 949 'markdown-url-face)
      (markdown-test-range-has-face 951 957 'markdown-link-title-face)
      (markdown-test-range-has-face 958 958 'markdown-markup-face))))

(ert-deftest test-markdown-font-lock/inline-links-with-parentheses ()
  "Test font lock for inline links with nested parentheses.
See <https://github.com/jrblevin/markdown-mode/issues/170>."
  (let ((markdown-hide-urls nil))
    (markdown-test-string "[foo](bar(baz)qux)"
      (markdown-test-range-has-face 1 1 'markdown-markup-face)
      (markdown-test-range-has-face 2 4 'markdown-link-face)
      (markdown-test-range-has-face 5 6 'markdown-markup-face)
      (markdown-test-range-has-face 7 17 'markdown-url-face)
      (markdown-test-range-has-face 18 18 'markdown-markup-face))))

(ert-deftest test-markdown-font-lock/pre-comment ()
  "Test comments inside of a pre block."
  (markdown-test-string "    <!-- pre, not comment -->"
   (markdown-test-range-has-face (point-min) (1- (point-max)) 'markdown-pre-face)))

(ert-deftest test-markdown-font-lock/inline-code-comment ()
  "Test comments inside of inline code."
  (markdown-test-string "`<h1> <!-- HTML comment inside inline code -->`"
   (markdown-test-range-has-face (1+ (point-min)) (- (point-max) 2) 'markdown-inline-code-face)))

(ert-deftest test-markdown-font-lock/inline-code-link ()
  "Test links inside of inline code."
  (markdown-test-string "`[text](url)`"
   (markdown-test-range-has-face (1+ (point-min)) (- (point-max) 2) 'markdown-inline-code-face)
   (should-not (markdown-range-property-any
                (1+ (point-min)) (- (point-max) 2) 'face
                '(markdown-markup-face markdown-link-face markdown-url-face)))))

(ert-deftest test-markdown-font-lock/comment-hanging-indent ()
  "Test comments with hanging indentation."
  (markdown-test-string "<!-- This comment has\n    hanging indentation -->"
   (markdown-test-range-has-face (point-min) (1- (point-max)) 'markdown-comment-face)))

(ert-deftest test-markdown-font-lock/comment-multiple ()
  "Test multiple single-line comments in arow."
  (markdown-test-string "<!-- This is a comment -->\n<!-- And so is this -->"
   (markdown-test-range-has-face
    (point-at-bol) (1- (point-at-eol)) 'markdown-comment-face)
   (forward-line)
   (markdown-test-range-has-face
    (point-at-bol) (1- (point-at-eol)) 'markdown-comment-face)))

(ert-deftest test-markdown-font-lock/comment-list-items ()
  "Test comment with list inside."
  (markdown-test-string
   "<!--
  - note 1;
  - note 2.
-->"
   (markdown-test-range-face-equals (point-min) (1- (point-max))
                                    'markdown-comment-face)))

(ert-deftest test-markdown-font-lock/comment-angle-bracket ()
  "Regression test for GH-117."
  (markdown-test-string "<!-- > test -->"
   (markdown-test-range-face-equals (point-min) (1- (point-max))
                                    'markdown-comment-face)))

(ert-deftest test-markdown-font-lock/comment-link ()
  "Test links inside of comments."
  (markdown-test-string "<!-- [text](url) -->"
   (markdown-test-range-has-face (+ (point-min) 5) (- (point-max) 4) 'markdown-comment-face)
   (should-not (markdown-range-property-any
                (+ (point-min) 5) (- (point-max) 4) 'face
                '(markdown-markup-face markdown-link-face markdown-url-face)))))

(ert-deftest test-markdown-font-lock/footnote-markers-links ()
  "Test an edge case involving footnote markers and inline reference links."
  (markdown-test-string "Harvard[^1] [tuition][]"
   (markdown-test-range-has-face 1 7 nil)
   (markdown-test-range-has-face 8 8 'markdown-markup-face)
   (markdown-test-range-has-face 10 10 'markdown-footnote-marker-face)
   (markdown-test-range-has-face 11 11 'markdown-markup-face)
   (markdown-test-range-has-face 12 12 nil)
   (markdown-test-range-has-face 13 13 'markdown-markup-face)
   (markdown-test-range-has-face 14 20 'markdown-link-face)
   (markdown-test-range-has-face 21 21 'markdown-markup-face)
   (markdown-test-range-has-face 22 23 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/mmd-metadata ()
  "Basic MultMarkdown metadata tests."
  (markdown-test-string "Title: peg-multimarkdown User's Guide
Author: Fletcher T. Penney
Base Header Level: 2"
   (markdown-test-range-has-face 1 5 'markdown-metadata-key-face)
   (markdown-test-range-has-face 6 6 'markdown-markup-face)
   (markdown-test-range-has-face 8 37 'markdown-metadata-value-face)
   (markdown-test-range-has-face 39 44 'markdown-metadata-key-face)
   (markdown-test-range-has-face 46 46 'markdown-markup-face)
   (markdown-test-range-has-face 47 64 'markdown-metadata-value-face)
   (markdown-test-range-has-face 66 82 'markdown-metadata-key-face)
   (markdown-test-range-has-face 83 83 'markdown-markup-face)
   (markdown-test-range-has-face 85 85 'markdown-metadata-value-face))
  ;; Avoid triggering when a title contains a colon (e.g., Markdown: Syntax)
  (markdown-test-file "syntax.text"
   (markdown-test-range-has-face 1 16 'markdown-header-face-1)))

(ert-deftest test-markdown-font-lock/mmd-metadata-after-header ()
  "Ensure that similar lines are not matched after the header."
  (markdown-test-string "Title: peg-multimarkdown User's Guide

Author: Fletcher T. Penney
Base Header Level: 2"
   (markdown-test-range-has-face 1 5 'markdown-metadata-key-face)
   (markdown-test-range-has-face 6 6 'markdown-markup-face)
   (markdown-test-range-has-face 8 37 'markdown-metadata-value-face)
   (markdown-test-range-has-face 40 65 nil)
   (markdown-test-range-has-face 67 86 nil)))

(ert-deftest test-markdown-font-lock/mmd-metadata-after-header-with-whitespace ()
  "Ensure that similar lines are not matched after the header.
The blank line here has two spaces, which should not change how
it is parsed."
  (markdown-test-string
   "Title: peg-multimarkdown User's Guide\n  \nAuthor: Fletcher T. Penney\nBase Header Level: 2\n"
   (markdown-test-range-has-face 1 5 'markdown-metadata-key-face)
   (markdown-test-range-has-face 6 6 'markdown-markup-face)
   (markdown-test-range-has-face 8 37 'markdown-metadata-value-face)
   (markdown-test-range-has-face 42 67 nil)
   (markdown-test-range-has-face 69 88 nil)))

(ert-deftest test-markdown-font-lock/pandoc-metadata ()
  "Basic Pandoc metadata tests."
  (markdown-test-string "% title
  two-line title
% first author;
  second author
% date

body"
   (markdown-test-range-has-face 1 1 'markdown-markup-face)
   (markdown-test-range-has-face 3 24 'markdown-metadata-value-face)
   (markdown-test-range-has-face 26 26 'markdown-markup-face)
   (markdown-test-range-has-face 28 56 'markdown-metadata-value-face)
   (markdown-test-range-has-face 58 58 'markdown-markup-face)
   (markdown-test-range-has-face 60 63 'markdown-metadata-value-face)
   (markdown-test-range-has-face 64 69 nil)))

(ert-deftest test-markdown-font-lock/yaml-metadata ()
  "Basic YAML metadata tests."
  (markdown-test-string
   "---
layout: post
date: 2015-08-13 11:35:25 EST
---
"
   (markdown-test-range-has-face 1 3 'markdown-markup-face)
   (markdown-test-range-has-face 5 10 'markdown-metadata-key-face)
   (markdown-test-range-has-face 11 11 'markdown-markup-face)
   (markdown-test-range-has-face 13 16 'markdown-metadata-value-face)
   (markdown-test-range-has-face 18 21 'markdown-metadata-key-face)
   (markdown-test-range-has-face 22 22 'markdown-markup-face)
   (markdown-test-range-has-face 24 46 'markdown-metadata-value-face)
   (markdown-test-range-has-face 48 50 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/toml-metadata ()
  "Basic TOML metadata tests."
  (markdown-test-string
   "---
layout = post
date = 2015-08-13 11:35:25 EST
---
"
   (markdown-test-range-has-face 1 3 'markdown-markup-face)
   (markdown-test-range-has-face 5 10 'markdown-metadata-key-face)
   (markdown-test-range-has-face 12 12 'markdown-markup-face)
   (markdown-test-range-has-face 14 17 'markdown-metadata-value-face)
   (markdown-test-range-has-face 19 22 'markdown-metadata-key-face)
   (markdown-test-range-has-face 24 24 'markdown-markup-face)
   (markdown-test-range-has-face 26 48 'markdown-metadata-value-face)
   (markdown-test-range-has-face 50 52 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/pandoc-yaml-metadata ()
  "Basic yaml metadata tests, with pandoc syntax."
  (let ((markdown-use-pandoc-style-yaml-metadata t))
    (markdown-test-string
     "some text

---
layout: post
date: 2015-08-13 11:35:25 EST
...

more text

---
layout: post
date: 2015-08-13 11:35:25 EST
---

But this is merely a code block

```
---
layout: post
date: 2015-08-13 11:35:25 EST
---
```
"
     ;; first section
     (markdown-test-range-has-face 12 14 'markdown-markup-face)
     (markdown-test-range-has-face 16 21 'markdown-metadata-key-face)
     (markdown-test-range-has-face 22 22 'markdown-markup-face)
     (markdown-test-range-has-face 24 27 'markdown-metadata-value-face)
     (markdown-test-range-has-face 29 32 'markdown-metadata-key-face)
     (markdown-test-range-has-face 33 33 'markdown-markup-face)
     (markdown-test-range-has-face 35 57 'markdown-metadata-value-face)
     (markdown-test-range-has-face 59 61 'markdown-markup-face)
     ;; second section
     (markdown-test-range-has-face 75 77 'markdown-markup-face)
     (markdown-test-range-has-face 79 84 'markdown-metadata-key-face)
     (markdown-test-range-has-face 85 85 'markdown-markup-face)
     (markdown-test-range-has-face 87 90 'markdown-metadata-value-face)
     (markdown-test-range-has-face 92 95 'markdown-metadata-key-face)
     (markdown-test-range-has-face 96 96 'markdown-markup-face)
     (markdown-test-range-has-face 98 120 'markdown-metadata-value-face)
     (markdown-test-range-has-face 122 124 'markdown-markup-face)
     ;; third section
     (markdown-test-range-has-face 160 162 'markdown-markup-face)
     (markdown-test-range-has-face 164 213 'markdown-pre-face)
     (markdown-test-range-has-face 215 217 'markdown-markup-face))))

(ert-deftest test-markdown-font-lock/line-break ()
  "Basic line break tests."
  (markdown-test-string "    \nasdf  \n"
   (markdown-test-range-has-face 1 9 nil)
   (markdown-test-range-has-face 10 11 'markdown-line-break-face)))

(ert-deftest test-markdown-font-lock/blockquote-bold ()
  "Test font lock for bold inside of a blockquote."
  (markdown-test-string
   "> **bold**"
   (markdown-test-range-has-face 1 10 'markdown-blockquote-face)
   (markdown-test-range-has-face 5 8 'markdown-bold-face)))

(ert-deftest test-markdown-font-lock/blockquote-italic ()
  "Test font lock for italic inside of a blockquote."
  (markdown-test-string
   "> *italic*"
   (markdown-test-range-has-face 1 10 'markdown-blockquote-face)
   (markdown-test-range-has-face 4 9 'markdown-italic-face)))

(ert-deftest test-markdown-font-lock/blockquote-code ()
  "Test font lock for inline code inside of a blockquote."
  (markdown-test-string
   "> `code`"
   (markdown-test-range-has-face 1 8 'markdown-blockquote-face)
   (markdown-test-range-has-face 3 3 'markdown-markup-face)
   (markdown-test-range-has-face 4 7 'markdown-inline-code-face)
   (markdown-test-range-has-face 8 8 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/blockquote-link ()
  "Test font lock for links inside of a blockquote.
This test will fail until font lock for inline links inside
blockquotes is implemented (at present, the blockquote face
takes precedence)."
  (markdown-test-string
   "> [link](url)"
   (markdown-test-range-has-face 1 1 'markdown-markup-face)
   (markdown-test-range-has-face 2 13 'markdown-blockquote-face)
   (markdown-test-range-has-face 3 3 'markdown-markup-face)
   (markdown-test-range-has-face 4 7 'markdown-link-face)
   (markdown-test-range-has-face 8 9 'markdown-markup-face)
   (markdown-test-range-has-face 10 12 'markdown-url-face)
   (markdown-test-range-has-face 13 13 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/blockquote-comment ()
  "Test font lock for comments inside of a blockquote."
  (markdown-test-string
   "> <!-- comment -->"
   (markdown-test-range-has-face 1 1 'markdown-markup-face)
   (markdown-test-range-has-face 3 18 'markdown-comment-face)))

(ert-deftest test-markdown-font-lock/pre-override ()
  "Test that font lock for pre blocks overrides everything else."
  (markdown-test-string
   "    **bold**
    _italic_
    <!-- comment -->
    [link](url)
    * list"
   (markdown-test-range-has-face 1 73 'markdown-pre-face)))

(ert-deftest test-markdown-font-lock/gfm-code-block-font-lock ()
  "GFM code block font lock test. Now in base markdown-mode as well!"
  (markdown-test-file "gfm.text"
    (markdown-test-range-has-face 2639 2641 'markdown-markup-face) ; ```
    (markdown-test-range-has-face 2642 2645 'markdown-language-keyword-face) ; lang
    (markdown-test-range-has-face 2647 2728 'markdown-pre-face) ; code
    (markdown-test-range-has-face 2730 2732 'markdown-markup-face))) ; ```

(ert-deftest test-markdown-font-lock/reference-definition ()
  "Reference definitions should not include ]."
  (let ((markdown-hide-urls nil))
    (markdown-test-string "[1]: http://daringfireball.net/ \"title\""
      (markdown-test-range-has-face 2 2 'markdown-reference-face) ; 1
      (markdown-test-range-has-face 6 31 'markdown-url-face) ; URL
      (markdown-test-range-has-face 34 38 'markdown-link-title-face)) ; title
    (markdown-test-string "[foo][1] and [bar][2]: not a reference definition"
      (markdown-test-range-has-face 2 4 'markdown-link-face) ; foo
      (markdown-test-range-has-face 7 7 'markdown-reference-face) ; 1
      (markdown-test-range-has-face 9 13 nil) ; [ ]and[ ]
      (markdown-test-range-has-face 15 17 'markdown-link-face) ; bar
      (markdown-test-range-has-face 20 20 'markdown-reference-face) ; 2
      (markdown-test-range-has-face 22 49 nil)))) ; [ ]and[ ]

(ert-deftest test-markdown-font-lock/subscripts ()
  "Test font lock for subscripts."
  (markdown-test-string "H~2~0"
    (markdown-test-range-has-face 2 2 'markdown-markup-face) ; First ~
    (markdown-test-range-has-face 3 3 nil) ; 2
    (markdown-test-range-has-face 4 4 'markdown-markup-face))) ; Second ~

(ert-deftest test-markdown-font-lock/superscripts ()
  "Test font lock for subscripts."
  (markdown-test-string "334^10^"
    (markdown-test-range-has-face 1 3 nil) ; 334
    (markdown-test-range-has-face 4 4 'markdown-markup-face) ; First ^
    (markdown-test-range-has-face 5 6 nil) ; 10
    (markdown-test-range-has-face 7 7 'markdown-markup-face))) ; Second ^

(ert-deftest test-markdown-font-lock/hidden-urls-inline ()
  "Test URL hiding and toggling."
  (let ((markdown-hide-urls t))
    (markdown-test-file "inline.text"
      (markdown-test-range-has-face 925 925 'markdown-markup-face)
      (markdown-test-range-has-face 926 929 'markdown-link-face)
      (markdown-test-range-has-face 930 931 'markdown-markup-face)
      (markdown-test-range-has-face 932 949 'markdown-url-face)
      (markdown-test-range-has-face 951 957 'markdown-link-title-face)
      (markdown-test-range-has-face 958 958 'markdown-markup-face)
      (should (get-text-property 932 'composition)))))

(ert-deftest test-markdown-font-lock/hidden-urls-reference ()
  "Test URL hiding and toggling."
  (let ((markdown-hide-urls t))
    (markdown-test-string "[link][15]"
      ;; Two-character reference labels shouldn't get composed.
      (markdown-test-range-has-face 1 1 'markdown-markup-face)
      (markdown-test-range-has-face 2 5 'markdown-link-face)
      (markdown-test-range-has-face 6 7 'markdown-markup-face)
      (markdown-test-range-has-face 8 9 'markdown-reference-face)
      (markdown-test-range-has-face 10 10 'markdown-markup-face)
      (should-not (get-text-property 8 'composition)))
    (markdown-test-string "[link][long-reference-label]"
      ;; Longer reference labels should be composed
      (markdown-test-range-has-face 1 1 'markdown-markup-face)
      (markdown-test-range-has-face 2 5 'markdown-link-face)
      (markdown-test-range-has-face 6 7 'markdown-markup-face)
      (markdown-test-range-has-face 8 27 'markdown-reference-face)
      (markdown-test-range-has-face 28 28 'markdown-markup-face)
      (should (get-text-property 8 'composition)))))

(ert-deftest test-markdown-font-lock/snake-case-code-in-heading ()
  "Test underscores in inline code in headings."
  (markdown-test-string "# Title with `snake_case_code`"
    (should-not (markdown-range-property-any 21 24 'face '(markdown-italic-face)))
    (markdown-test-range-has-face 15 29 'markdown-inline-code-face)))

(ert-deftest test-markdown-font-lock/stars-in-code-in-heading ()
  "Test asterisks in inline code in headings."
  (markdown-test-string "# Title with `char** foo, int* bar`"
    (should-not (markdown-range-property-any 20 29 'face '(markdown-italic-face)))
    (markdown-test-range-has-face 15 34 'markdown-inline-code-face)))

(ert-deftest test-markdown-font-lock/stars-in-code-in-blockquote ()
  "Test asterisks in inline code in blockquote."
  (markdown-test-string "> Quote with `**stars**`"
    (should-not (markdown-range-property-any
                 17 21 'face '(markdown-italic-face markdown-bold-face)))
    (markdown-test-range-has-face 15 23 'markdown-inline-code-face)))

(ert-deftest test-markdown-font-lock/two-bold-words-after-list ()
  "Test two bold words after a list marker."
  (markdown-test-string "- **foo** **bar**"
    (should-not (markdown-range-property-any
                 (point-min) (point-max) 'face '(markdown-italic-face)))))

(ert-deftest test-markdown-font-lock/heading-with-italics-and-bold ()
  "Test two bold words after a list marker."
  (markdown-test-string "# Title with *italics* and **bold**"
    (markdown-test-range-has-face 15 21 'markdown-italic-face)
    (markdown-test-range-has-face 30 33 'markdown-bold-face)
    (should-not (markdown-range-property-any 30 33 'face '(markdown-italic-face)))))

(ert-deftest test-markdown-font-lock/heading-with-italics-and-bold ()
  "Test that HRs are distinguished from setext H2 markup."
  (markdown-test-file "outline.text"
    (goto-char 485)
    (should (markdown-on-heading-p))
    (beginning-of-line)
    (should (markdown-on-heading-p))
    (should-not (markdown-range-property-any 453 484 'face '(markdown-hr-face)))))

(ert-deftest test-markdown-font-lock/heading-code-block-no-whitespace ()
  "Headings immediately before code blocks should be identified correctly.
See GH-234."
  (markdown-test-string
   "#### code snippet
```javascript
const styles = require('gadgets/dist/styles.css');
```"
   (goto-char (point-min))
   (forward-word)
   (should (markdown-on-heading-p))
   (should (markdown-match-propertized-text 'markdown-heading (point-at-eol)))
   (goto-char (match-beginning 0))
   (should (markdown-outline-level))
   (should (= (markdown-outline-level) 4))
   (markdown-test-range-has-face 6 17 'markdown-header-face-4)
   (end-of-line)
   (should-not (markdown-code-block-at-point-p))))

(ert-deftest test-markdown-font-lock/hr-underscore-with-spaces ()
  "Test font-lock for HR with spaced underscores."
  (markdown-test-string "_ _ _ _ _ _ _\n"
    (markdown-test-range-face-equals (point-min) (- (point-max) 2) 'markdown-hr-face)))

(ert-deftest test-markdown-font-lock/hr-underscore-no-spaces ()
  "Test font-lock for HR with underscores and no spaces."
  (markdown-test-string "_____________\n"
    (markdown-test-range-face-equals (point-min) (- (point-max) 2) 'markdown-hr-face)))

(ert-deftest test-markdown-font-lock/inline-attributes ()
  "Test inline attributes before a fenced code block."
  (markdown-test-file "Leanpub.md"
    ;; Inline attributes for a heading
    (markdown-test-range-has-face 38 42 'markdown-markup-face)
    ;; Inline attributes inside an aside block
    (markdown-test-range-has-face 123 141 'markdown-markup-face)
    ;; Inline attributes before a fenced code block
    (markdown-test-range-has-face 632 696 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/leanpub-sections ()
  "Test Leanpub section markers."
  (markdown-test-file "Leanpub.md"
    ;; {frontmatter}
    (markdown-test-range-has-face 12 24 'markdown-markup-face)
    ;; {mainmatter}
    (markdown-test-range-has-face 69 80 'markdown-markup-face)
    ;; {pagebreak}
    (markdown-test-range-has-face 427 437 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/leanpub-include ()
  "Test Leanpub include syntax."
  (markdown-test-file "Leanpub.md"
    ;; no title
    (markdown-test-range-has-face 561 563 'markdown-markup-face)
    (markdown-test-range-has-face 564 577 'markdown-url-face)
    (markdown-test-range-has-face 578 578 'markdown-markup-face)
    ;; title
    (markdown-test-range-has-face 581 583 'markdown-markup-face)
    (markdown-test-range-has-face 584 611 'markdown-link-title-face)
    (markdown-test-range-has-face 612 613 'markdown-markup-face)
    (markdown-test-range-has-face 614 628 'markdown-url-face)
    (markdown-test-range-has-face 629 629 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/curly-brace-include ()
  "Test curly brace include syntax."
  (markdown-test-string "<<{file}"
    (markdown-test-range-has-face 1 3 'markdown-markup-face)
    (markdown-test-range-has-face 4 7 'markdown-url-face)
    (markdown-test-range-has-face 8 8 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/square-bracket-include ()
  "Test square bracket include syntax."
  (markdown-test-string "<<[file]"
    (markdown-test-range-has-face 1 3 'markdown-markup-face)
    (markdown-test-range-has-face 4 7 'markdown-url-face)
    (markdown-test-range-has-face 8 8 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/pandoc-inline-footnote ()
  "Test font lock for Pandoc inline footnotes."
  (markdown-test-string "Here is an inline note.^[Inline notes are easier to write, since
you don't have to pick an identifier and move down to type the
note.]  And then you can close it and continue writing."
    (markdown-test-range-has-face 1 23 nil)
    (markdown-test-range-has-face 24 25 'markdown-markup-face)
    (markdown-test-range-has-face 26 133 'markdown-footnote-text-face)
    (markdown-test-range-has-face 134 134 'markdown-markup-face)))

(ert-deftest test-markdown-font-lock/pandoc-inline-footnote-across-block ()
  "Test font lock for Pandoc inline footnotes."
  (markdown-test-string "Inline notes should not^[match

across blocks]"
    (markdown-test-range-has-face (point-min) (point-max) nil)))

(ert-deftest test-markdown-font-lock/html-entity-named ()
  "Test basic font-lock support for named HTML entities."
  (markdown-test-string "&nbsp;"
    (markdown-test-range-has-face 1 6 'markdown-html-entity-face)))

(ert-deftest test-markdown-font-lock/html-entity-hex ()
  "Test basic font-lock support for hexadecimal HTML entities."
  (markdown-test-string "&#x272a;"
    (markdown-test-range-has-face 1 8 'markdown-html-entity-face)))

(ert-deftest test-markdown-font-lock/html-entity-decimal ()
  "Test basic font-lock support for decimal HTML entities."
  (markdown-test-string "&#9;"
    (markdown-test-range-has-face 1 4 'markdown-html-entity-face)))

(ert-deftest test-markdown-font-lock/html-entity-in-inline-code ()
  "Test that HTML entities are not matched inside inline code."
  (markdown-test-string "`&#9;`"
    (markdown-test-range-has-face 1 1 'markdown-markup-face)
    (markdown-test-range-has-face 2 5 'markdown-inline-code-face)
    (markdown-test-range-has-face 6 6 'markdown-markup-face)
    (should-not (markdown-range-property-any 1 6 'face '(markdown-html-entity-face)))))

(ert-deftest test-markdown-font-lock/html-entity-in-gfm-code-block ()
  "Test that HTML entities are not matched inside GFM code blocks."
  (markdown-test-string "```\n&nbsp;\n&#x272a;\n&#9;\n```"
    (should-not
     (markdown-range-property-any
      (point-min) (point-max) 'face '(markdown-html-entity-face)))))

(ert-deftest test-markdown-font-lock/html-tags-in-syntax-file ()
  "Test matching HTML tags in syntax.text."
  (markdown-test-file "syntax.text"
    ;; <ul id="ProjectSubmenu">
    (markdown-test-range-has-face 36 36 'markdown-html-tag-delimiter-face)
    (markdown-test-range-has-face 37 38 'markdown-html-tag-name-face)
    (markdown-test-range-has-face 40 41 'markdown-html-attr-name-face)
    (markdown-test-range-has-face 42 42 'markdown-html-tag-delimiter-face)
    (markdown-test-range-has-face 43 58 'markdown-html-attr-value-face)
    (markdown-test-range-has-face 59 59 'markdown-html-tag-delimiter-face)
    ;; <li>
    (markdown-test-range-has-face 65 65 'markdown-html-tag-delimiter-face)
    (markdown-test-range-has-face 66 67 'markdown-html-tag-name-face)
    (markdown-test-range-has-face 68 68 'markdown-html-tag-delimiter-face)
    ;; <a href="/projects/markdown/" title="Markdown Project Page">
    (markdown-test-range-has-face 69 69 'markdown-html-tag-delimiter-face)
    (markdown-test-range-has-face 70 70 'markdown-html-tag-name-face)
    (markdown-test-range-has-face 72 75 'markdown-html-attr-name-face)
    (markdown-test-range-has-face 76 76 'markdown-html-tag-delimiter-face)
    (markdown-test-range-has-face 77 97 'markdown-html-attr-value-face)
    (markdown-test-range-has-face 99 103 'markdown-html-attr-name-face)
    (markdown-test-range-has-face 104 104 'markdown-html-tag-delimiter-face)
    (markdown-test-range-has-face 105 127 'markdown-html-attr-value-face)
    (markdown-test-range-has-face 128 128 'markdown-html-tag-delimiter-face)))

(ert-deftest test-markdown-font-lock/html-tag-in-gfm-code-block ()
  "Test that HTML tags are not matched inside GFM code blocks."
  (markdown-test-string "```\n<ul id=\"ProjectSubmenu\">\n```"
    (should-not
     (markdown-range-property-any
      (point-min) (point-max) 'face
      '(markdown-html-tag-name-face
        markdown-html-tag-delimiter-face
        markdown-html-attr-name-face
        markdown-html-attr-value-face)))))

(ert-deftest test-markdown-font-lock/html-tag-in-code-block ()
  "Test that HTML tags are not matched inside code blocks."
  (markdown-test-string "    <ul id=\"ProjectSubmenu\">"
    (should-not
     (markdown-range-property-any
      (point-min) (point-max) 'face
      '(markdown-html-tag-name-face
        markdown-html-tag-delimiter-face
        markdown-html-attr-name-face
        markdown-html-attr-value-face)))))

(ert-deftest test-markdown-font-lock/html-tag-in-inline-code ()
  "Test that HTML tags are not matched inside inline code spans."
  (markdown-test-string "`<ul id=\"ProjectSubmenu\">`"
    (should-not
     (markdown-range-property-any
      (point-min) (point-max) 'face
      '(markdown-html-tag-name-face
        markdown-html-tag-delimiter-face
        markdown-html-attr-name-face
        markdown-html-attr-value-face)))))

(ert-deftest test-markdown-font-lock/html-disabled ()
  "Test disabling font-lock for HTML tags"
  (let ((markdown-enable-html nil))
    (markdown-test-file "syntax.text"
      (should-not
       (markdown-range-property-any
        (point-min) (point-max) 'face
        '(markdown-html-tag-name-face
          markdown-html-tag-delimiter-face
          markdown-html-attr-name-face
          markdown-html-attr-value-face))))))

;;; Markdown Parsing Functions:

(ert-deftest test-markdown-parsing/extend-region-function ()
  "Test `markdown-syntax-propertize-extend-region'.
Should return a cons (NEW-START . NEW-END) or nil if no
adjustment should be made. Function is called repeatedly until it
returns nil."
  (markdown-test-file
   "inline.text"
   (should (equal (markdown-syntax-propertize-extend-region 1 17)
                  (cons 1 91)))
   (should (equal (markdown-syntax-propertize-extend-region 2 17)
                  (cons 1 91)))
   (should (equal (markdown-syntax-propertize-extend-region 1 91)
                  nil))
   (should (equal (markdown-syntax-propertize-extend-region 93 157)
                  nil))
   (should (equal (markdown-syntax-propertize-extend-region 496 502)
                  (cons 486 510)))
   (should (equal (markdown-syntax-propertize-extend-region 486 510)
                  nil))
   ;; Region that begins and ends with \n\n should not be extended
   (should (equal (markdown-syntax-propertize-extend-region 157 355)
                  nil))))

(defun markdown-test-check-match-limits (prop num begin end &optional pos)
  (let* ((posn (or pos (point)))
         (props (get-text-property posn prop)))
    (save-match-data
      (set-match-data props)
      (should (match-beginning num))
      (should (match-end num))
      (should (= (match-beginning num) begin))
      (should (= (match-end num) end)))))

(ert-deftest test-markdown-parsing/syntax-with-adjacent-code-blocks ()
  "Test `markdown-syntax-propertize-fenced-code-blocks' with adjacent blocks."
  (markdown-test-string
   "~~~ shell
#!/bin/sh

echo \"Hello, world!\"
~~~

~~~ shell
#!/bin/sh

echo \"Hello, world v2!\"
~~~
"
   (let ((start-top-1 (make-marker)) (end-top-1 (make-marker))
         (start-lang-1 (make-marker)) (end-lang-1 (make-marker))
         (start-mid-1 (make-marker)) (end-mid-1 (make-marker))
         (start-bottom-1 (make-marker)) (end-bottom-1 (make-marker))
         (between (make-marker))
         (start-top-2 (make-marker)) (end-top-2 (make-marker))
         (start-lang-2 (make-marker)) (end-lang-2 (make-marker))
         (start-mid-2 (make-marker)) (end-mid-2 (make-marker))
         (start-bottom-2 (make-marker)) (end-bottom-2 (make-marker)))
     ;; First code block
     (set-marker start-top-1 1)
     (set-marker end-top-1 4)
     (set-marker start-lang-1 5)
     (set-marker end-lang-1 10)
     (set-marker start-mid-1 11)
     (set-marker end-mid-1 43)
     (set-marker start-bottom-1 43)
     (set-marker end-bottom-1 46)
     ;; check top tildes
     (markdown-test-check-match-limits
      'markdown-tilde-fence-begin 1 (marker-position start-top-1)
      (marker-position end-top-1) (marker-position start-top-1))
     ;; check top language specifier
     (markdown-test-check-match-limits
      'markdown-tilde-fence-begin 3 (marker-position start-lang-1)
      (marker-position end-lang-1) (marker-position start-lang-1))
     ;; check text in between
     (markdown-test-check-match-limits
      'markdown-fenced-code 0 (marker-position start-mid-1)
      (marker-position end-mid-1) (marker-position start-mid-1))
     ;; check bottom tildes
     (markdown-test-check-match-limits
      'markdown-tilde-fence-end 1 (marker-position start-bottom-1)
      (marker-position end-bottom-1) (marker-position start-bottom-1))
     ;; Point between code blocks
     (set-marker between 47)
     (should (equal (get-text-property between 'markdown-fenced-code)
                    nil))
     ;; Second code block
     (set-marker start-top-2 48)
     (set-marker end-top-2 51)
     (set-marker start-lang-2 52)
     (set-marker end-lang-2 57)
     (set-marker start-mid-2 58)
     (set-marker end-mid-2 93)
     (set-marker start-bottom-2 93)
     (set-marker end-bottom-2 96)
     (markdown-test-check-match-limits
      'markdown-tilde-fence-begin 1 (marker-position start-top-2)
      (marker-position end-top-2) (marker-position start-top-2))
     (markdown-test-check-match-limits
      'markdown-tilde-fence-begin 3 (marker-position start-lang-2)
      (marker-position end-lang-2) (marker-position start-lang-2))
     (markdown-test-check-match-limits
      'markdown-fenced-code 0 (marker-position start-mid-2)
      (marker-position end-mid-2) (marker-position start-mid-2))
     (markdown-test-check-match-limits
      'markdown-tilde-fence-end 1 (marker-position start-bottom-2)
      (marker-position end-bottom-2) (marker-position start-bottom-2))
     ;; ;; Move point between code blocks and insert a character
     (goto-char between)
     (insert "x")
     ;; Re-propertize region after change
     (let ((range (markdown-syntax-propertize-extend-region (1- between) (point-max))))
       (markdown-syntax-propertize (car range) (cdr range)))
     ;; Re-check first code block
     (markdown-test-check-match-limits
      'markdown-tilde-fence-begin 1 (marker-position start-top-1)
      (marker-position end-top-1) (marker-position start-top-1))
     (markdown-test-check-match-limits
      'markdown-tilde-fence-begin 3 (marker-position start-lang-1)
      (marker-position end-lang-1) (marker-position start-lang-1))
     (markdown-test-check-match-limits
      'markdown-fenced-code 0 (marker-position start-mid-1)
      (marker-position end-mid-1) (marker-position start-mid-1))
     (markdown-test-check-match-limits
      'markdown-tilde-fence-end 1 (marker-position start-bottom-1)
      (marker-position end-bottom-1) (marker-position start-bottom-1))
     ;; Re-check point between code blocks
     (should (equal (get-text-property between 'markdown-fenced-code)
                    nil))
     ;; Re-check second code block
     (markdown-test-check-match-limits
      'markdown-tilde-fence-begin 1 (marker-position start-top-2)
      (marker-position end-top-2) (marker-position start-top-2))
     (markdown-test-check-match-limits
      'markdown-tilde-fence-begin 3 (marker-position start-lang-2)
      (marker-position end-lang-2) (marker-position start-lang-2))
     (markdown-test-check-match-limits
      'markdown-fenced-code 0 (marker-position start-mid-2)
      (marker-position end-mid-2) (marker-position start-mid-2))
     (markdown-test-check-match-limits
      'markdown-tilde-fence-end 1 (marker-position start-bottom-2)
      (marker-position end-bottom-2)
      (marker-position start-bottom-2)))))

(ert-deftest test-markdown-parsing/propertize-fenced-in-between ()
  "Test whether `markdown-syntax-propertize-fenced-block-constructs' handles the
case when it can't propertize both the start and end of a fenced block within a
single pass (the end of the block is past the END argument)."
  (markdown-test-string
      "~~~ shell
#!/bin/sh

echo \"Hello, world!\"
~~~
"
    (set-text-properties (point-min) (point-max) nil)
    ;; syntax-propertize up to right after hashbang
    (markdown-syntax-propertize-fenced-block-constructs (point-min) 21)
    ;; ~~~ shell should be propertized, but nothing else
    ;; check tildes
    (markdown-test-check-match-limits 'markdown-tilde-fence-begin 1 1 4 1)
    ;; check language
    (markdown-test-check-match-limits 'markdown-tilde-fence-begin 3 5 10 5)
    ;; middle should not be propertized
    (should-not (get-text-property 11 'markdown-fenced-code))
    ;; neither should end
    (should-not (get-text-property 43 'markdown-tilde-fence-end))
    (markdown-syntax-propertize-fenced-block-constructs 21 (point-max))
    ;; everything should be propertized now
    ;; re-check top
    (markdown-test-check-match-limits 'markdown-tilde-fence-begin 1 1 4 1)
    (markdown-test-check-match-limits 'markdown-tilde-fence-begin 3 5 10 5)
    ;; check middle
    (markdown-test-check-match-limits 'markdown-fenced-code 0 10 43 10)
    ;; check ending tildes
    (markdown-test-check-match-limits 'markdown-tilde-fence-end 1 43 46 43)))

(ert-deftest test-markdown-parsing/get-code-block-at-pos ()
  "Test whether `markdown-code-block-at-pos' works in all situations. All
  situations are:
1. pre block
2. tilde block
3. gfm block
4. yaml metadata block"
  (let ((markdown-use-pandoc-style-yaml-metadata t))
    (markdown-test-string
        "
~~~ ruby
some_ruby_fun()
~~~

---
a: b
---

``` {.bash}
#!/bin/sh
echo hey
```

    pre code
    random stuff
    more preformatted code

---
data: pandoc
...
"
      ;; start/mid/end at tilde block
      (should (equal (markdown-code-block-at-pos 2) (list 2 30)))
      (should (equal (markdown-code-block-at-pos 11) (list 2 30)))
      (should (equal (markdown-code-block-at-pos 27) (list 2 30)))
      ;; yaml metadata block
      (should (equal (markdown-code-block-at-pos 32) (list 32 44)))
      (should (equal (markdown-code-block-at-pos 36) (list 32 44)))
      (should (equal (markdown-code-block-at-pos 41) (list 32 44)))
      ;; gfm block
      (should (equal (markdown-code-block-at-pos 46) (list 46 80)))
      (should (equal (markdown-code-block-at-pos 58) (list 46 80)))
      (should (equal (markdown-code-block-at-pos 77) (list 46 80)))
      ;; pre block
      (should (equal (markdown-code-block-at-pos 82) (list 82 138)))
      (should (equal (markdown-code-block-at-pos 99) (list 82 138)))
      (should (equal (markdown-code-block-at-pos 137) (list 82 138)))
      ;; pandoc yaml metadata block (should work if yaml above works)
      (should (equal (markdown-code-block-at-pos 140) (list 140 160)))
      (should (equal (markdown-code-block-at-pos 142) (list 140 160)))
      (should (equal (markdown-code-block-at-pos 144) (list 140 160)))
      (should (equal (markdown-code-block-at-pos 157) (list 140 160)))
      (should (equal (markdown-code-block-at-pos 159) (list 140 160))))))

(ert-deftest test-markdown-parsing/syntax-get-fenced-blocks ()
  "Test whether *-get-fenced-block-* functions work in the case where a block is
only partially propertized."
  (save-match-data
    (markdown-test-string
     "~~~
"
     (should (equal (markdown-syntax-propertize-extend-region
                     (point-min) (point-max))
                    nil))
     (goto-char 1)
     (set-match-data (markdown-text-property-at-point
                      'markdown-tilde-fence-begin))
     (should (equal (markdown-get-fenced-block-from-start
                     'markdown-tilde-fence-begin)
                    nil)))
    (markdown-test-string
     "~~~
~~~"
     (goto-char 1)
     (set-match-data (markdown-text-property-at-point
                      'markdown-tilde-fence-begin))
     (should (equal (markdown-get-fenced-block-from-start
                     'markdown-tilde-fence-begin)
                    (list 1 8)))
     (should (equal (markdown-code-block-at-pos (point)) (list 1 8)))

     ;; markdown-code-block-at-point-p should not modify match data
     (set-match-data (list 1 2 3 4))
     (should (markdown-code-block-at-point-p))
     (should (equal (match-data) (list 1 2 3 4)))

     (goto-char 5)
     (set-match-data (markdown-text-property-at-point
                      'markdown-tilde-fence-end))
     (should (equal (markdown-get-fenced-block-from-end
                     'markdown-tilde-fence-end)
                    (list 1 8)))
     (should (equal (markdown-code-block-at-pos (point)) (list 1 8))))
    (markdown-test-string
     "~~~

~~~"
     (goto-char 1)
     (set-match-data (markdown-text-property-at-point
                      'markdown-tilde-fence-begin))
     (should (equal (markdown-get-fenced-block-from-start
                     'markdown-tilde-fence-begin)
                    (list 1 9)))
     (should (equal (markdown-code-block-at-pos (point)) (list 1 9)))
     (goto-char 5)
     (set-match-data (markdown-text-property-at-point 'markdown-fenced-code))
     (should (equal (markdown-get-fenced-block-from-middle
                     'markdown-fenced-code)
                    (list 1 9)))
     (should (equal (markdown-code-block-at-pos (point)) (list 1 9)))
     (goto-char 6)
     (set-match-data (markdown-text-property-at-point
                      'markdown-tilde-fence-end))
     (should (equal (markdown-get-fenced-block-from-end
                     'markdown-tilde-fence-end)
                    (list 1 9)))
     (should (equal (markdown-code-block-at-pos (point)) (list 1 9))))))

(ert-deftest test-markdown-parsing/reference-definition-basic ()
  "Test reference definition function."
  (markdown-test-file "syntax.text"
   ;; Test accuracy of returned text and bounds
   (should (equal (markdown-reference-definition "1")
                  (list "http://docutils.sourceforge.net/mirror/setext.html" 1942 1992)))
   (should (equal (markdown-reference-definition "2")
                  (list "http://www.aaronsw.com/2002/atx/" 2000 2032)))
   ;; Test that match data remains intact
   (should (string-equal (match-string 5) "http://www.aaronsw.com/2002/atx/"))
   ;; Test anchor-only relative URL
   (should (equal (markdown-reference-definition "bq")
                  (list "#blockquote" 7536 7547)))
   ;; Example references that appear in pre blocks in the text
   (should (not (markdown-reference-definition "")))
   (should (not (markdown-reference-definition "id")))
   (should (not (markdown-reference-definition "foo")))
   (should (not (markdown-reference-definition "A")))
   (should (not (markdown-reference-definition "Google")))
   ;; Test that we don't pick up other text in square brackets
   (should (not (markdown-reference-definition "blockquoting")))
   (should (not (markdown-reference-definition "square brackets")))
   ;; Test case insensitivity
   (should (equal (markdown-reference-definition "SRC")
                  (list "/projects/markdown/syntax.text" 1245 1275)))))

(ert-deftest test-markdown-parsing/get-defined-references ()
  "Test `markdown-get-defined-references'."
  (markdown-test-file "syntax.text"
   (should (equal (markdown-get-defined-references)
                  '("src" "1" "2" "3" "4" "5" "6" "bq" "l"))))
  (markdown-test-file "outline.text"
   (should (equal (markdown-get-defined-references) nil)))
  (markdown-test-file "wiki-links.text"
   (should (equal (markdown-get-defined-references) nil))))

(ert-deftest test-markdown-parsing/get-used-uris ()
  "Test `markdown-get-used-uris'."
  (markdown-test-file "syntax.text"
    (let ((uris (markdown-get-used-uris)))
      (should (equal (nth 0 uris) "#overview"))
      (should (equal (nth 20 uris) "http://www.aaronsw.com/2002/atx/"))
      (should-not (member "http://example.com/" uris))
      (should-not (member "address@example.com" uris)))))

(defun markdown-test-test-region (beg end)
  (goto-char (1- beg))
  (should-not (markdown-inline-code-at-point-p))
  (goto-char (1+ end))
  (should-not (markdown-inline-code-at-point-p))
  (dolist (loc (number-sequence beg end))
    (goto-char loc)
    (should (markdown-inline-code-at-point))
    (should (equal (match-beginning 0) beg))
    (should (equal (match-end 0) end))))

(ert-deftest test-markdown-parsing/inline-code-at-point ()
  "Test `markdown-inline-code-at-point'."
  (markdown-test-file "inline.text"
    (markdown-test-test-region 45 51) ; Regular code span
    (markdown-test-test-region 61 90) ; Code containing backticks
    (markdown-test-test-region 228 240) ; Backquotes at beginning
    (markdown-test-test-region 341 352) ; Backquotes at end
    (markdown-test-test-region 460 469) ; Backslash as final character
    (markdown-test-test-region 657 667) ; A code span crossing lines
    (markdown-test-test-region 749 758) ; Three backquotes on same line
    (markdown-test-test-region 806 815) ; Three backquotes across lines
    ))

(ert-deftest test-markdown-parsing/inline-code-at-point-one-space ()
  "Test `markdown-inline-code-at-point' with multiple code spans in a row."
  (markdown-test-string "`foo` `bar` `baz`"
    (dolist (loc (number-sequence 1 6))
      (goto-char loc)
      ;; markdown-inline-code-at-point should set match data
      (should (markdown-inline-code-at-point))
      (should (equal (match-data) (list 1 6 1 2 2 5 5 6)))
      ;; markdown-inline-code-at-point-p should not modify match data
      (set-match-data (list 1 2 3 4))
      (should (markdown-inline-code-at-point-p))
      (should (equal (match-data) (list 1 2 3 4))))
    (dolist (loc (number-sequence 7 12))
      (goto-char loc)
      (should (markdown-inline-code-at-point))
      (should (equal (match-data) (list 7 12 7 8 8 11 11 12))))
    (dolist (loc (number-sequence 13 18))
      (goto-char loc)
      (should (markdown-inline-code-at-point))
      (should (equal (match-data) (list 13 18 13 14 14 17 17 18))))))

(ert-deftest test-markdown-parsing/inline-code-at-point-no-space ()
  "Test `markdown-inline-code-at-point' with multiple code spans in a row.."
  (markdown-test-string "a`foo`b`bar`c`baz`d"
    (goto-char 1)                       ; "a"
    (should-not (markdown-inline-code-at-point-p))
    (dolist (loc (number-sequence 2 7)) ; "`foo`b"
      (goto-char loc)
      (should (markdown-inline-code-at-point))
      (should (equal (match-data) (list 2 7 2 3 3 6 6 7))))
    (dolist (loc (number-sequence 8 13)) ; "`bar`c"
      (goto-char loc)
      (should (markdown-inline-code-at-point))
      (should (equal (match-data) (list 8 13 8 9 9 12 12 13))))
    (dolist (loc (number-sequence 14 19)) ; "`baz`d"
      (goto-char loc)
      (should (markdown-inline-code-at-point))
      (should (equal (match-data) (list 14 19 14 15 15 18 18 19))))))

(ert-deftest test-markdown-parsing/code-at-point-blank-line ()
  "Test `markdown-inline-code-at-point-p' at beginning of block."
  (markdown-test-string "----------\n\n## foo\n"
   (should-not (markdown-inline-code-at-point-p))
   (forward-line)
   (should-not (markdown-inline-code-at-point-p))
   (forward-line)
   (should-not (markdown-inline-code-at-point-p))))

(ert-deftest test-markdown-parsing/in-comment-p-position ()
  "Test `markdown-in-comment-p'."
  (markdown-test-string
   "HTML <!-- foo --> comment"
   (should (eq (point) (point-min)))
   (should-not (markdown-in-comment-p (point-max)))
   (should (eq (point) (point-min)))))

(ert-deftest test-markdown-parsing/match-comments ()
  "Test `markdown-match-comments'."
  (markdown-test-string
   "HTML <!-- foo --> comment"
   (should (markdown-match-comments (point-max)))
   (should (eq (point) 18))
   (should (equal (match-data) (list 6 18)))
   (should-not (markdown-match-comments (point-max)))))

(ert-deftest test-markdown-parsing/range-property-any ()
  "Test behavior of `markdown-range-property-any'."
  (markdown-test-file
   "inline.text"
   (should (markdown-range-property-any
            (point-min) (point-at-eol)
            'face '(markdown-markup-face markdown-italic-face)))
   (should-not (markdown-range-property-any
            (point-min) (point-at-eol)
            'face '(markdown-bold-face)))))

(ert-deftest test-markdown-parsing/inline-code ()
  "Don't cause infinite loop for inline code just after metadata block
Detail: https://github.com/jrblevin/markdown-mode/issues/115"
  (markdown-test-string "---
x: x
---
`x`
"
    (should (markdown-match-code (point-max)))
    (should (= (point) 18))
    (should (equal (match-data t) '(14 17 14 15 15 16 16 17)))
    (should-not (markdown-match-code (point-max)))))

(ert-deftest test-markdown-parsing/list-item-at-point ()
  "Test `markdown-list-item-at-point-p'."
  (markdown-test-file "lists.text"
    (let ((orig-match-data '(1 2 3 4))
          (not-list-points '(273 399 512 3615))
          (list-points '(1063 1063 1176 1283 1659 1830 1919 2150
                              2393 2484 2762 2853 3097 3188 3700
                              3903 4009)))
      ;; markdown-inline-code-at-point-p should not modify match data
      (set-match-data orig-match-data)
      ;; Not list items
      (dolist (pos not-list-points)
        (goto-char pos)
        (should-not (markdown-list-item-at-point-p))
        (should (equal (match-data) orig-match-data)))
      ;; List items
      (dolist (pos list-points)
        (goto-char pos)
        (should (markdown-list-item-at-point-p))
        (should (equal (match-data) orig-match-data))))))

(ert-deftest test-markdown-parsing/heading-at-point ()
  "Test `markdown-heading-at-point'."
  (save-match-data
    (markdown-test-file "outline.text"
      (should-not (markdown-heading-at-point))
      (markdown-test-goto-heading "An underline-style header")
      (forward-line -1)
      (should (markdown-heading-at-point))
      (should (equal (match-data t) (get-text-property (point) 'markdown-heading)))
      (should (equal (match-data t) (get-text-property (point) 'markdown-heading-1-setext))))))

(ert-deftest test-markdown-parsing/inline-link-in-code-block ()
  "Test `markdown-match-generic-links'."
  (markdown-test-string "    **bold**
    _italic_
    <!-- comment -->
    [link](url)
    * list"
    (goto-char (point-min))
    ;; The link inside the pre block should not match.
    (should-not (markdown-match-generic-links (point-max) nil))
    ;; Point should be left at limit.
    (should (= (point) (point-max)))))

(ert-deftest test-markdown-parsing/broken-inline-link ()
  "Test `markdown-match-generic-links' with an invalid link."
  (markdown-test-string "[site1](http://site1.com
[site2](http://site2.com)
[site3](http://site3.com)"
    (goto-char (point-min))
    (let ((limit (point-at-eol)))
      ;; The first link is broken and shouldn't match.
      (should-not (markdown-match-generic-links limit nil))
      ;; Subsequent search shouldn't match, so point should move to limit.
      (should (= (point) limit)))
    ;; The second link should still match, starting from (point-min).
    (let ((limit (point-at-eol 2)))
      (should (markdown-match-generic-links limit nil))
      (should (= (point) (match-end 0))))
    ;; The third link should match when starting past the second one.
    (goto-char (match-end 0))
    (should (markdown-match-generic-links (point-max) nil))
    (should (= (point) (match-end 0)))))

(ert-deftest test-markdown-parsing/code-block-lang ()
  "Test `markdown-code-block-lang'."
  ;; Test with GFM code blocks.
  (markdown-test-file "GFM.md"
    ;; Test a call with the optional argument.
    (should (string-equal
             (markdown-code-block-lang
              '(1455 . markdown-gfm-block-begin)) "js"))
    ;; Test a call without the optional argument.
    (goto-char 1504) ;; middle of a GFM code block
    (should (string-equal (markdown-code-block-lang) "js")))
  ;; Test with tilde-fenced cdoe blocks.
  (markdown-test-file "outline-code.text"
    (goto-char 107) ;; middle of a tilde fenced code block
    (should (string-equal (markdown-code-block-lang
                           '(83 . markdown-tilde-fence-begin)) "bash"))))

(ert-deftest test-markdown-parsing/code-block-lang-period ()
  "Test `markdown-code-block-lang' when language name begins with a period."
  (markdown-test-string "~~~ { .ruby }
puts 'hello, world'
~~~
"
    (should (string-equal (markdown-code-block-lang) "ruby"))))

;;; Reference Checking:

(ert-deftest test-markdown-references/goto-line-button ()
  "Create and test a goto line button."
  (markdown-test-string "line 1\nline 2\n"
   ;; Store the temporary buffer with the text
   (let ((target (current-buffer)))
     ;; Create a new buffer for inserting
     (with-temp-buffer
       ;; Verify that point is in a different buffer
       (should (not (equal (current-buffer) target)))
       ;; Insert and press the button
       (insert-button "goto line 2"
                      :type 'markdown-goto-line-button
                      'target-buffer target
                      'target-line 2)
       (should (string-equal (buffer-string) "goto line 2"))
       (backward-button 1)
       (call-interactively 'push-button)
       ;; Verify that point is on line 2 of target buffer
       (should (= (line-number-at-pos) 2))
       (should (looking-at "line 2"))
       (should (equal (current-buffer) target))))))

(ert-deftest test-markdown-references/button-map ()
  "Verify that button-buffer-map is used for check references buffer."
  (markdown-test-string "[undefined][ref]\n"
   (let* ((target (buffer-name))
          (check (format "*Undefined references for %s*" target)))
   (markdown-check-refs)
   (with-current-buffer (get-buffer check)
     (should (equal (local-key-binding (kbd "TAB")) 'forward-button))
     (should (equal (local-key-binding (kbd "<backtab>")) 'backward-button))))))

;;; Lists:

(ert-deftest test-markdown-lists/nested-list-file ()
  "Test list item propertization for a nested list."
  (markdown-test-file "nested-list.text"
    (let ((values '(((1 25 3 5 "- " nil))
                    ((26 189 3 5 "- " nil))
                    ((191 581 3 5 "- " nil))
                    ((217 482 7 9 "- " nil)
                     (191 581 3 5 "- " nil))
                    ((484 581 7 9 "- " nil)
                     (191 581 3 5 "- " nil))
                    ((514 581 11 13 "- " nil)
                     (484 581 7 9 "- " nil)
                     (191 581 3 5 "- " nil)))))
      (cl-loop
       for value in values
       do (should
           (equal (mapcar #'butlast (get-text-property (point) 'markdown-list-item)) value))
          (markdown-outline-next)))))

(ert-deftest test-markdown-lists/levels-1 ()
  "Test list levels function `markdown-calculate-list-levels'."
  (markdown-test-file "nested-list.text"
   (let ((values '(((1 . 1) . nil) ((2 . 13) . (3)) ((14 . 23) . (7 3))
                   ((24 . 26) . (11 7 3)))))
     (cl-loop for (range . value) in values
           do (goto-char (point-min))
              (forward-line (1- (car range)))
              (dotimes (n (- (cdr range) (car range)))
                (should (equal (markdown-calculate-list-levels) value))
                (forward-line))))))

(ert-deftest test-markdown-lists/levels-2 ()
  "Test list levels function `markdown-calculate-list-levels'."
  (markdown-test-file "syntax.text"
   (let ((values '(((1 . 13) . nil) ((14 . 14) . (0)) ((15 . 17) . (4 0))
                   ((18 . 18) . (0)) ((19 . 24) . (4 0)) ((25 . 25) . (0))
                   ((26 . 29) . (4 0)) ((30 . 30) . (0)) ((31 . 33) . (4 0))
                   ((34 . 588) . nil) ((589 . 595) . (0)) ((596 . 814) . nil)
                   ((815 . 820) . (0)) ((821 . 898) . nil))))
     (cl-loop for (range . value) in values
           do (goto-char (point-min))
              (forward-line (1- (car range)))
              (dotimes (n (- (cdr range) (car range)))
                (should (equal (markdown-calculate-list-levels) value))
                (forward-line))))))

(ert-deftest test-markdown-lists/levels-interior ()
  "Test `markdown-calculate-list-levels' from inside a list item."
  (markdown-test-file "nested-list.text"
    (goto-char 36)
    (should (equal (markdown-calculate-list-levels) (list 3)))
    (goto-char 267)
    (should (equal (markdown-calculate-list-levels) (list 7 3)))
    (goto-char 540)
    (should (equal (markdown-calculate-list-levels) (list 11 7 3)))))

(ert-deftest test-markdown-lists/bounds-1 ()
  "Test list item bounds function `markdown-cur-list-item-bounds'."
  (markdown-test-file "lists.text"
    (markdown-test-goto-heading "Case 9")
    (forward-line)
    (should (eq (point) 3699))
    (markdown-next-list-item 4)
    (should (eq (point) 3700))
    (should (equal (butlast (markdown-cur-list-item-bounds))
                   (list 3700 3901 0 4 "-   " nil)))
    (markdown-next-list-item 4)
    (should (eq (point) 3903))
    (should (equal (butlast (markdown-cur-list-item-bounds))
                   (list 3903 3937 0 4 "*   " nil)))))

(ert-deftest test-markdown-lists/bounds-2 ()
  "Function `markdown-cur-list-item-bounds' should return nil outside of list items."
  (markdown-test-string "line one\n\n* item\n"
    (should (null (markdown-cur-list-item-bounds)))
    (forward-line)
    (should (null (markdown-cur-list-item-bounds)))
    (forward-line)
    (should (markdown-cur-list-item-bounds))))

(ert-deftest test-markdown-lists/bounds-prev ()
  "Test list item bounds function `markdown-prev-list-item-bounds'."
  (markdown-test-file "lists.text"
    (markdown-test-goto-heading "Case 9")
    (markdown-next-list-item 4)
    (markdown-next-list-item 4)
    (should (eq (point) 3903))
    (should (equal (butlast (markdown-prev-list-item-bounds))
                   (list 3700 3901 0 4 "-   " nil)))))

(ert-deftest test-markdown-lists/bounds-next ()
  "Test list item bounds function `markdown-next-list-item-bounds'."
  (markdown-test-file "lists.text"
    (markdown-test-goto-heading "Case 2")
    (goto-char 1283)
    (should-not (markdown-next-list-item-bounds))
    (markdown-test-goto-heading "Case 9")
    (markdown-next-list-item 4)
    (should (eq (point) 3700))
    (should (equal (butlast (markdown-next-list-item-bounds))
                   (list 3903 3937 0 4 "*   " nil)))))

(ert-deftest test-markdown-lists/bounds-gfm-task-list-item ()
  "Test `markdown-cur-list-item-bounds' with a GFM task list item."
  (markdown-test-string "  - [ ] task name"
    (should (equal (butlast (markdown-cur-list-item-bounds))
                   '(1 18 2 4 "- " "[ ] ")))))

(ert-deftest test-markdown-lists/gfm-task-list-item-at-point-1 ()
  "Test `markdown-gfm-task-list-item-at-point' with regular list items."
  (markdown-test-file "nested-list.text"
    (dolist (pos '(1 26 36 267 514 540))
      (goto-char pos)
      (should-not (markdown-gfm-task-list-item-at-point)))))

(ert-deftest test-markdown-lists/gfm-task-list-item-at-point-2 ()
  "Test `markdown-gfm-task-list-item-at-point' with a task list item."
  (markdown-test-string "  - [ ] task"
    (should (markdown-gfm-task-list-item-at-point))))

(ert-deftest test-markdown-insertion/insert-gfm-task-list-item ()
  "Test `markdown-insert-list-item' in a GFM task list."
  (markdown-test-string "  - [ ] task"
    (goto-char (point-max))
    (call-interactively 'markdown-insert-list-item)
    (should (string-equal (buffer-string) "  - [ ] task\n  - [ ] "))))

(ert-deftest test-markdown-lists/promotion-and-demotion ()
  "Test function `markdown-promote-list-item'."
  (markdown-test-file "nested-list.text"
    (forward-line)
    (should (looking-at-p "   - List level 1 item 2

     Second paragraph of item 2

        Nested pre block in item 2
        Four spaces past the marker

     Another paragraph of item 2"))
    (markdown-demote-list-item)
    (should (looking-at-p "       - List level 1 item 2

         Second paragraph of item 2

            Nested pre block in item 2
            Four spaces past the marker

         Another paragraph of item 2"))
    (markdown-promote-list-item)
    (should (looking-at-p "   - List level 1 item 2

     Second paragraph of item 2

        Nested pre block in item 2
        Four spaces past the marker

     Another paragraph of item 2"))
    (goto-char (point-min))
    (forward-line 22)
    (should (looking-at-p "           - List level 3 item 1

                 Nested pre block"))
    (markdown-demote-list-item)
    (should (looking-at-p "               - List level 3 item 1

                     Nested pre block"))
    (markdown-promote-list-item)
    (should (looking-at-p "           - List level 3 item 1

                 Nested pre block"))))

(ert-deftest test-markdown-lists/promotion-and-demotion-custom ()
  "Test custom variable `markdown-list-indent-width'."
  (markdown-test-file "nested-list.text"
    (forward-line)
    (should (looking-at "   - List level 1 item 2

     Second paragraph of item 2

        Nested pre block in item 2
        Four spaces past the marker

     Another paragraph of item 2"))
    (let ((markdown-list-indent-width 2))
      (markdown-demote-list-item))
    (should (looking-at "     - List level 1 item 2

       Second paragraph of item 2

          Nested pre block in item 2
          Four spaces past the marker

       Another paragraph of item 2"))))

(ert-deftest test-markdown-lists/add-gfm-checkbox ()
  (markdown-test-file "check-items.text"
    (goto-char (point-min))
    (end-of-line)
    (should (markdown-insert-gfm-checkbox))
    (should (= (line-number-at-pos (point)) 1))
    (should (eolp))
    (should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
                          "  * [ ] "))

    (forward-line 2)
    (back-to-indentation)
    (should (markdown-insert-gfm-checkbox))
    (should (= (line-number-at-pos (point)) 3))
    (should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
                          "  * [ ] "))
    (should (string-equal (buffer-substring-no-properties (point) (line-end-position))
                          "item1"))

    (forward-line 2)
    (back-to-indentation)
    (forward-char 1)
    (should (markdown-insert-gfm-checkbox))
    (should (= (line-number-at-pos (point)) 5))
    (should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
                          "  * [ ] i"))
    (should (string-equal (buffer-substring-no-properties (point) (line-end-position))
                          "tem2"))

    (forward-line 2)
    (back-to-indentation)
    (forward-char 2)
    (should (markdown-insert-gfm-checkbox))
    (should (= (line-number-at-pos (point)) 7))
    (should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
                          "- [ ] "))
    (should (string-equal (buffer-substring-no-properties (point) (line-end-position))
                          "item3"))

    (forward-line 2)
    (back-to-indentation)
    (forward-char 3)
    (should (markdown-insert-gfm-checkbox))
    (should (= (line-number-at-pos (point)) 9))
    (should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
                          "- [ ] i"))
    (should (string-equal (buffer-substring-no-properties (point) (line-end-position))
                          "tem4"))

    (forward-line 2)
    (end-of-line)
    (should-not (markdown-insert-gfm-checkbox))
    (should (= (line-number-at-pos (point)) 11))
    (should (eolp))
    (should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
                          "*   [ ] item5"))

    (forward-line 1)
    (back-to-indentation)
    (should (markdown-insert-gfm-checkbox))
    (should (= (line-number-at-pos (point)) 12))
    (should (eolp))
    (should (string-equal (buffer-substring-no-properties
                           (line-beginning-position)
                           (point))
                          "*   [ ] "))))

(ert-deftest test-markdown-lists/toggle-gfm-checkbox ()
  (markdown-test-string "   -   [X] GFM task list item"
    (should (string-equal (markdown-toggle-gfm-checkbox) "[ ]"))
    (should (string-equal (buffer-string) "   -   [ ] GFM task list item"))
    (should (string-equal (markdown-toggle-gfm-checkbox) "[x]"))
    (should (string-equal (buffer-string) "   -   [x] GFM task list item"))))

(ert-deftest test-markdown-lists/beginning-of-list ()
  "Test `markdown-beginning-of-list'."
  (markdown-test-file "lists.text"
    ;; Case 1: not in a list
    (goto-char 399)
    (should-not (markdown-beginning-of-list))
    (should (= (point) 399))
    ;; Case 2
    (goto-char 1281)
    (should (= (markdown-beginning-of-list) 1063))
    (should (= (point) 1063))
    (goto-char 1395)
    (should (= (markdown-beginning-of-list) 1063))
    (should (= (point) 1063))
    ;; Case 3
    (goto-char 1848)
    (should (= (markdown-beginning-of-list) 1659))
    (should (= (point) 1659))
    ;; Case 4
    (goto-char 2041)
    (should (= (markdown-beginning-of-list) 1919))
    (should (= (point) 1919))
    ;; Case 8
    (goto-char 3553)
    (should (= (markdown-beginning-of-list) 3096))
    (should (= (point) 3096))))

(ert-deftest test-markdown-lists/end-of-list ()
  "Test `markdown-end-of-list'."
  (markdown-test-file "lists.text"
    ;; Case 1: not in a list
    (goto-char 399)
    (should-not (markdown-end-of-list))
    (should (= (point) 399))
    ;; Case 2
    (goto-char 1281)
    (should (= (markdown-end-of-list) 1396))
    (should (= (point) 1396))
    (goto-char 1395)
    (should (= (markdown-end-of-list) 1396))
    (should (= (point) 1396))
    ;; Case 3
    (goto-char 1659)
    (should (= (markdown-end-of-list) 1849))
    (should (= (point) 1849))
    ;; Case 4
    (goto-char 2041)
    (should (= (markdown-end-of-list) 2092))
    (should (= (point) 2092))
    ;; Case 8
    (goto-char 3553)
    (should (= (markdown-end-of-list) 3614))
    (should (= (point) 3614))))

(ert-deftest test-markdown-lists/up-list ()
  "Test `markdown-up-list'."
  (markdown-test-file "nested-list.text"
    (goto-char 581)
    (should (= (markdown-up-list) 484))
    (should (= (point) 484))
    (should (= (markdown-up-list) 191))
    (should (= (point) 191))
    ;; Return nil upon failure, but move out of list.
    (should-not (markdown-up-list))
    (should (= (point) (point-min)))))

;;; Outline minor mode tests:

(ert-deftest test-markdown-outline/navigation ()
  "Test outline navigation functions."
  (markdown-test-file "outline.text"
   ;; Navigate to the first visible heading
   (markdown-next-visible-heading 1)
   (should (eq (point) 19))
   (should (looking-at "^# A top-level header"))
   ;; Navigate forward at the same level
   (markdown-forward-same-level 1)
   (should (eq (point) 351))
   (should (looking-at "^An underline-style header$"))
   ;; Navigate backward by four visible headings
   (markdown-previous-visible-heading 4)
   (should (eq (point) 69))
   (should (looking-at "^## A second-level header$"))
   ;; Navigate up the hierarchy (atx)
   (call-interactively #'markdown-up-heading)
   (should (looking-at "^# A top-level header"))
   (should (eq (mark) 69))
   ;; Navigate up the hierarchy (setext)
   (goto-char 516)
   (call-interactively #'markdown-up-heading)
   (should (looking-at "^An underline-style header$"))
   (should (eq (mark) 516))
   ;; Navigate back in the outline (setext to atx)
   (forward-line) ;; move to setext underline
   (markdown-backward-same-level 1)
   (should (looking-at "^# A top-level header"))))

(ert-deftest test-markdown-outline/navigation-with-code ()
  "Test outline navigation functions with code blocks."
  (markdown-test-file "outline-code.text"
   ;; Navigate forward at the same level
   (markdown-forward-same-level 1)
   (should (eq (point) 159))
   (should (looking-at "^# Level one again"))))

(ert-deftest test-markdown-outline/back-to-heading-over-code-block ()
  "Test `markdown-back-to-heading-over-code-block' over."
  (markdown-test-file "outline-code.text"
    ;; Initialize match data to known quantity.
    (set-match-data '(1 2 3 4))
    (should (equal (match-data t) '(1 2 3 4)))
    ;; Function should navigate back over code blocks.
    (re-search-forward "^# In a code block")
    (should (= (markdown-back-to-heading-over-code-block) 69))
    ;; Match data should be set for markdown-regex-header.
    (should (equal (match-data t) (get-text-property (point) 'markdown-heading)))
    ;; Function should return t when at a heading.
    (should (equal (markdown-back-to-heading-over-code-block) t))
    ;; Insert some text before the first heading.
    (goto-char (point-min))
    (save-excursion (insert "foo\n\n"))
    ;; Function should throw an error if no previous heading.
    (should-error (markdown-back-to-heading-over-code-block))
    ;; Function should return nil without error if NO-ERROR is non-nil.
    (should-not (markdown-back-to-heading-over-code-block t t))))

(ert-deftest test-markdown-outline/visibility-atx ()
  "Test outline visibility cycling for ATX-style headers."
  (markdown-test-file "outline.text"
   (let (last-command this-command)
     ;; Navigate to the second visible heading
     (markdown-next-visible-heading 2)
     (should (eq (point) 69))
     (should (looking-at "^## A second-level header$"))
     ;; Cycle visibility of this subtree
     (setq this-command 'markdown-cycle)
     (markdown-cycle)
     (setq last-command 'markdown-cycle)
     (should (eq (point) 69))
     (should (looking-at "^## A second-level header$"))
     ;; Test that the entire subtree is invisible
     (markdown-test-range-has-property 93 349 'invisible 'outline)
     ;; Cycle visibility of this subtree again
     (markdown-cycle)
     (should (eq (point) 69))
     (should (looking-at "^## A second-level header$"))
     ;; Test that text is visible
     (markdown-test-range-has-property 95 121 'invisible nil)
     ;; Test that subheadings are visible
     (markdown-test-range-has-property 123 141 'invisible nil)
     ;; Cycle visibility of this subtree again
     (markdown-cycle)
     (should (eq (point) 69))
     (should (looking-at "^## A second-level header$"))
     ;; Verify that entire subtree is visible
     (markdown-test-range-has-property 93 349 'invisible nil))))

(ert-deftest test-markdown-outline/visibility-setext ()
  "Test outline visibility cycling for setext-style headers."
  (markdown-test-file "outline.text"
   ;; Navigate to the sixth visible heading
   (markdown-next-visible-heading 7)
   (markdown-previous-visible-heading 1)
   (should (looking-at markdown-regex-header))
   (should (string-equal (match-string-no-properties 1) "An underline-style header"))
   (should (string-equal (match-string-no-properties 2) "========================="))
   ;; Cycle visibility subtree, test that it's invisible
   (markdown-cycle)
   (markdown-test-range-has-property 404 515 'invisible 'outline)
   ;; Cycle visibility subtree, test that text and headers are visible
   (markdown-cycle)
   (markdown-test-range-has-property 404 417 'invisible nil)
   (markdown-test-range-has-property 420 451 'invisible nil)))

(ert-deftest test-markdown-outline/visibility-with-code ()
  "Test outline visibility cycling with code blocks."
  (markdown-test-file "outline-code.text"
   (let (last-command this-command)
     ;; Cycle global visibility to "overview" mode
     (setq this-command 'markdown-cycle)
     (markdown-cycle t)
     (setq last-command 'markdown-cycle)
     (should (eq (point) (point-min)))
     (should (looking-at "^# Level one"))
     ;; Test that the code block is invisible
     (markdown-test-range-has-property 83 157 'invisible 'outline)
     ;; Check subsequent headings
     (outline-next-visible-heading 1)
     (should (eq (point) 69))
     (should (looking-at "^## Level two"))
     (outline-next-visible-heading 1)
     (should (eq (point) 159))
     (should (looking-at "^# Level one again")))))

(ert-deftest test-markdown-outline/visibility-with-metadata ()
  "Test outline visibility cycling with metadata blocks."
  (markdown-test-string
   "---
layout = post
date = 2015-08-13 11:35:25 EST
---
"
   (let (last-command this-command)
     ;; Cycle global visibility to "overview" mode
     (setq this-command 'markdown-cycle)
     (markdown-cycle t)
     ;; Check that text is visible
     (markdown-test-range-has-property (point-min) (point-max) 'invisible nil))))

(ert-deftest test-markdown-outline/level ()
  "Test `markdown-outline-level'."
  (markdown-test-file "outline.text"
    (markdown-next-heading)
    (should (= (markdown-outline-level) 1))
    (markdown-forward-same-level 1)
    (should (= (markdown-outline-level) 1))
    (markdown-next-heading)
    (should (= (markdown-outline-level) 2))
    (markdown-next-heading)
    (should (= (markdown-outline-level) 1))
    (markdown-next-heading)
    (should (= (markdown-outline-level) 2))))

;;; Movement tests:

(ert-deftest test-markdown-movement/defun ()
  "Test defun navigation."
  (markdown-test-file "outline.text"
   ;; end-of-defun should go to point-max
   (end-of-defun 10)
   (should (= (point) (point-max)))
   ;; end-of-defun should stop just before the next header
   (goto-char (point-min))
   (end-of-defun)
   (should (looking-at "\n# A top-level header"))
   (end-of-defun)
   (should (looking-at "\n## A second-level header"))
   (end-of-defun)
   (should (looking-at "\n### Third level ###"))
   (end-of-defun)
   (should (looking-at "\n### Third level number two ###"))
   ;; beginning-of-defun should move to the start of the previous header
   (beginning-of-defun)
   (should (looking-at "### Third level ###"))
   (beginning-of-defun)
   (should (looking-at "## A second-level header"))
   (beginning-of-defun)
   (should (looking-at "# A top-level header"))
   (beginning-of-defun)
   ;; beginning-of-defun should move up to point-min
   (should (= (point) (point-min)))
   ;; (beginning-of-defun -1)  should move to the start of the next header
   (forward-line 2)
   (beginning-of-defun -1)
   (should (looking-at "## A second-level header"))
   (beginning-of-defun -1)
   (should (looking-at "### Third level ###"))
   (beginning-of-defun -1)
   (should (looking-at "### Third level number two ###"))))

(ert-deftest test-markdown-movement/beginning-of-defun-at-point-max ()
  "Test beginning of defun navigation at point-max."
  (markdown-test-file "outline.text"
    (goto-char (point-max))
    (beginning-of-defun)))

(ert-deftest test-markdown-movement/text-block ()
  "Test plain text block movement."
  (markdown-test-file "outline.text"
   (markdown-end-of-text-block)
   (should (looking-at "\n# A top-level header"))
   (markdown-end-of-text-block)
   (should (looking-at "\nfollowed by some body text"))
   (markdown-end-of-text-block)
   (should (looking-at "\n## A second-level header"))
   (markdown-end-of-text-block)
   (should (looking-at "\nfollowed by some body text"))
   (markdown-end-of-text-block)
   (should (looking-at "\n### Third level ###"))
   (markdown-end-of-text-block)
   (should (looking-at "\n\\* A list item"))
   (markdown-end-of-text-block)
   (should (looking-at "\n### Third level number two ###"))
   (markdown-end-of-text-block)
   (should (looking-at "\n### Level two again"))
   (markdown-end-of-text-block)
   (should (looking-at "\nfollowed by some body text"))

   (markdown-test-goto-heading "Level two")
   (markdown-end-of-text-block)
   (should (looking-at "\nbar"))
   (markdown-end-of-text-block)
   (should (= (point) (point-max)))
   (markdown-beginning-of-text-block)
   (should (looking-at "bar"))
   (markdown-beginning-of-text-block)
   (should (looking-at "## Level two"))
   (markdown-beginning-of-text-block)
   (should (looking-at "foo"))
   (markdown-beginning-of-text-block)
   (should (looking-at "# Level one"))
   (markdown-beginning-of-text-block)
   (should (looking-at "* With"))
   (markdown-beginning-of-text-block)
   (should (looking-at "And a level two underline header"))

   (goto-char (point-min))
   (markdown-test-goto-heading "A top-level header")
   (beginning-of-line)
   (markdown-beginning-of-text-block)
   (should (= (point) (point-min)))))

(ert-deftest test-markdown-movement/mark-text-block ()
  "Test `markdown-mark-text-block'."
  (markdown-test-file "outline.text"
    ;; Start in middle of nested list with no separating whitespace.
    (goto-char 193)
    (markdown-mark-text-block)
    (should (= (point) 143))
    (should (= (mark) 269))))

(ert-deftest test-markdown-movement/paragraph ()
  "Test Markdown paragraph movement."
  (markdown-test-file "outline.text"
   (markdown-forward-paragraph)
   (should (looking-at "\n# A top-level header"))
   (markdown-forward-paragraph)
   (should (looking-at "\nfollowed by some body text"))
   (markdown-forward-paragraph)
   (should (looking-at "\n## A second-level header"))
   (markdown-forward-paragraph)
   (should (looking-at "\nfollowed by some body text"))
   (markdown-forward-paragraph)
   (should (looking-at "\n### Third level ###"))
   (markdown-forward-paragraph)
   (should (looking-at "\n\\* A list item"))
   (markdown-forward-paragraph)
   (should (looking-at "\\* and another"))
   (markdown-forward-paragraph)
   (should (looking-at "   \\+ and a sublist"))
   (markdown-forward-paragraph)
   (should (looking-at "- And a third"))
   (markdown-forward-paragraph)
   (should (looking-at "\n### Third level number two ###"))
   (markdown-forward-paragraph)
   (should (looking-at "\n### Level two again"))
   (markdown-forward-paragraph)
   (should (looking-at "\nfollowed by some body text"))

   (markdown-test-goto-heading "Level two")
   (markdown-forward-paragraph)
   (should (looking-at "\nbar"))
   (markdown-forward-paragraph)
   (should (= (point) (point-max)))
   (markdown-backward-paragraph)
   (should (looking-at "bar"))
   (markdown-backward-paragraph)
   (should (looking-at "## Level two"))
   (markdown-backward-paragraph)
   (should (looking-at "foo"))
   (markdown-backward-paragraph)
   (should (looking-at "# Level one"))
   (markdown-backward-paragraph)
   (should (looking-at "\\* List"))
   (markdown-backward-paragraph)
   (should (looking-at "\\* an unordered"))
   (markdown-backward-paragraph)
   (should (looking-at "\\* With"))
   (markdown-backward-paragraph)
   (should (looking-at "And a level two underline header"))

   (goto-char (point-min))
   (markdown-test-goto-heading "A top-level header")
   (beginning-of-line)
   (markdown-backward-paragraph)
   (should (= (point) (point-min)))))

(ert-deftest test-markdown-movement/forward-paragraph-with-whitespace ()
  "Test Markdown paragraph movement."
  (markdown-test-file "blocks.md"
    (markdown-test-goto-heading "With Whitespace")
    (dolist (pos '(58 67 78 94 109 114 123 131 135 147 157 170 184 199))
      (markdown-forward-paragraph)
      (should (= (point) pos)))))

(ert-deftest test-markdown-movement/backward-paragraph-with-whitespace ()
  "Test Markdown paragraph movement."
  (markdown-test-file "blocks.md"
    (markdown-test-goto-heading "With Whitespace")
    (markdown-next-heading)
    (should (= (point) 200))
    (dolist (pos '(185 172 158 148 136 132 124 115 110 94 78 67 59))
      (markdown-backward-paragraph)
      (should (= (point) pos)))))

(ert-deftest test-markdown-movement/forward-paragraph-without-whitespace ()
  "Test Markdown paragraph movement."
  (markdown-test-file "blocks.md"
    (markdown-test-goto-heading "Without Whitespace")
    (dolist (pos '(222 230 240 255 270 275 283 291 294 305 314 326 340 354))
      (markdown-forward-paragraph)
      (should (= (point) pos)))))

(ert-deftest test-markdown-movement/backward-paragraph-without-whitespace ()
  "Test Markdown paragraph movement."
  (markdown-test-file "blocks.md"
    (goto-char (point-max))
    (dolist (pos '(340 328 314 305 294 291 284 275 271 255 240 230 223 200))
      (markdown-backward-paragraph)
      (should (= (point) pos)))))

(ert-deftest test-markdown-movement/block ()
  "Test Markdown block movement."
  (markdown-test-file "outline.text"
   (markdown-forward-block)
   (should (looking-at "\n# A top-level header"))
   (markdown-forward-block)
   (should (looking-at "\nfollowed by some body text"))
   (markdown-forward-block)
   (should (looking-at "\n## A second-level header"))
   (markdown-forward-block)
   (should (looking-at "\nfollowed by some body text"))
   (markdown-forward-block)
   (should (looking-at "\n### Third level ###"))
   (markdown-forward-block)
   (should (looking-at "\n\\* A list item"))
   (markdown-forward-block)
   (should (looking-at "\n### Third level number two ###"))
   (markdown-forward-block)
   (should (looking-at "\n### Level two again"))
   (markdown-forward-block)
   (should (looking-at "\nfollowed by some body text"))

   (markdown-test-goto-heading "Level two")
   (markdown-forward-block)
   (should (looking-at "\nbar"))
   (markdown-forward-block)
   (should (= (point) (point-max)))
   (markdown-backward-block)
   (should (looking-at "bar"))
   (markdown-backward-block)
   (should (looking-at "## Level two"))
   (markdown-backward-block)
   (should (looking-at "foo"))
   (markdown-backward-block)
   (should (looking-at "# Level one"))
   (markdown-backward-block)
   (should (looking-at "\\* With"))
   (markdown-backward-block)
   (should (looking-at "And a level two underline header"))

   (goto-char (point-min))
   (markdown-test-goto-heading "A top-level header")
   (beginning-of-line)
   (markdown-backward-block)
   (should (= (point) (point-min)))))

(ert-deftest test-markdown-movement/forward-block-with-whitespace ()
  "Test Markdown block movement."
  (markdown-test-file "blocks.md"
    (markdown-test-goto-heading "With Whitespace")
    (dolist (pos '(58 109 114 131 135 147 157 184 199))
      (markdown-forward-block)
      (should (= (point) pos)))))

(ert-deftest test-markdown-movement/backward-block-with-whitespace ()
  "Test Markdown block movement."
  (markdown-test-file "blocks.md"
    (markdown-test-goto-heading "With Whitespace")
    (markdown-next-heading)
    (dolist (pos '(185 158 148 136 132 115 110 59))
      (markdown-backward-block)
      (should (= (point) pos)))))

(ert-deftest test-markdown-movement/forward-block-without-whitespace ()
  "Test Markdown block movement."
  (markdown-test-file "blocks.md"
    (markdown-test-goto-heading "Without Whitespace")
    (dolist (pos '(222 270 275 291 294 305 314 340 354))
      (markdown-forward-block)
      (should (= (point) pos)))))

(ert-deftest test-markdown-movement/backward-block-without-whitespace ()
  "Test Markdown block movement."
  (markdown-test-file "blocks.md"
    (goto-char (point-max))
    (dolist (pos '(340 314 305 294 291 275 271 223 200))
      (markdown-backward-block)
      (should (= (point) pos)))))

(ert-deftest test-markdown-movement/page ()
  "Test Markdown page movement."
  (markdown-test-file "outline.text"
   (markdown-forward-page)
   (should (looking-at "# A top-level header"))
   (markdown-forward-page)
   (should (looking-at "An underline-style header"))
   (markdown-forward-page)
   (should (looking-at "# Level one"))
   (markdown-forward-page)
   (should (eobp))
   (markdown-backward-page)
   (should (looking-at "# Level one"))
   (markdown-backward-page)
   (should (looking-at "An underline-style header"))
   (markdown-backward-page)
   (should (looking-at "# A top-level header"))
   (markdown-backward-page)
   (should (bobp))))

(ert-deftest test-markdown-movement/blockquote-paragraphs ()
  "Test filling of blockquotes containing multiple paragraphs."
  (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n>\n> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n"
    (forward-paragraph)
    (should (looking-at "^>$"))
    (should (= (point) 128))
    (forward-paragraph)
    (should (= (point) (point-max)))))

(ert-deftest test-markdown-movement/reference-definition ()
  "Test jumping to reference definitions."
  ;; Jumping to explicit reference definition
  (markdown-test-string "[a][ref]\n\n[ref]: gopher://localhost/\n"
   (markdown-reference-goto-definition)
   (should (= (point) 18)))
  ;; Jumping to implicit reference definition
  (markdown-test-string "[a][]\n\n[a]: ftp://localhost/\n"
   (markdown-reference-goto-definition)
   (should (= (point) 13)))
  ;; Creating non-existent reference definition
  (markdown-test-string "[a][]\n"
   (markdown-reference-goto-definition)
   (should (= (point) 13))
   (should (string-equal (buffer-string) "[a][]\n\n[a]: \n"))))

(ert-deftest test-markdown-movement/back-to-same-level-over-code-block ()
  "`markdown-backward-same-level' over code block which contains header
like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75"
  (markdown-test-string "
## Header 2-1

## Header 2-2

```R
# Header Like Statement
```

## Header 2-3
"
    (search-forward "## Header 2-3")
    (let ((last-header-pos (point)))
      (forward-line -1)
      (call-interactively #'markdown-backward-same-level)
      (should (looking-at-p "## Header 2-1"))

      (goto-char last-header-pos)
      (call-interactively #'markdown-backward-same-level)
      (should (looking-at-p "## Header 2-2"))

      (goto-char last-header-pos)
      (markdown-backward-same-level 2)
      (should (looking-at-p "## Header 2-1"))

      (search-forward "# Header Like Statement")
      (call-interactively #'markdown-backward-same-level)
      (should (looking-at-p "## Header 2-1")))))

;;; Link tests:

(ert-deftest test-markdown-link/follow ()
  "Test link following in a browser and in Emacs."
  (markdown-test-string "[text](http://path?query=foo#id)"
    (let* ((opened-url nil)
           (browse-url-browser-function
            (lambda (url &rest args) (setq opened-url url))))
      (markdown-follow-thing-at-point nil)
      (should (equal opened-url "http://path?query=foo#id"))))
  (when (featurep 'url-parse)
    (markdown-test-string "[text](path?query=foo#id)"
      (markdown-follow-thing-at-point nil)
      (should (equal (file-name-nondirectory (buffer-file-name)) "path"))
      (kill-buffer))))

(ert-deftest test-markdown-link/inline-link-at-pos ()
  "Test `markdown-link-at-pos' return values with an inline link."
  (markdown-test-string "[text](url \"title\")"
    (should (equal (markdown-link-at-pos (point)) '(1 20 "text" "url" nil "title" nil)))))

(ert-deftest test-markdown-link/inline-image-at-pos ()
  "Test `markdown-link-at-pos' return values with an inline image."
  (markdown-test-string "![text](url \"title\")"
    (should (equal (markdown-link-at-pos (point)) '(1 21 "text" "url" nil "title" "!")))))

(ert-deftest test-markdown-link/reference-link-at-pos ()
  "Test `markdown-link-at-pos' return values with a reference link."
  (markdown-test-string "[text][ref]"
    (should (equal (markdown-link-at-pos (point)) '(1 12 "text" nil "ref" nil nil)))))

(ert-deftest test-markdown-link/reference-image-at-pos ()
  "Test `markdown-link-at-pos' return values with a reference image."
  (markdown-test-string "![text][ref]"
    (should (equal (markdown-link-at-pos (point)) '(1 13 "text" nil "ref" nil "!")))))

(ert-deftest test-markdown-link/angle-uri-at-pos ()
  "Test `markdown-link-at-pos' return values with an angle bracket inline link."
  (markdown-test-string "<http://jblevins.org/projects/markdown-mode/>"
    (should (equal (markdown-link-at-pos (point)) '(1 46 nil "http://jblevins.org/projects/markdown-mode/" nil nil nil)))))

(ert-deftest test-markdown-link/plain-uri-at-pos ()
  "Test `markdown-link-at-pos' return values with a plain URI."
  (markdown-test-string "http://jblevins.org/projects/markdown-mode/"
    (should (equal (markdown-link-at-pos (point)) '(1 44 nil "http://jblevins.org/projects/markdown-mode/" nil nil nil)))))

(ert-deftest test-markdown-link/follow-filename ()
  "Test that `markdown-follow-thing-at-pos' uses
`markdown-translate-filename-function' to translate filenames."
  (markdown-test-string "[text](/foo/bar/baz)"
    (cl-letf* ((visited-files ())
               ((symbol-function #'find-file)
                (lambda (filename)
                  (push filename visited-files)))
               (translated-files ())
               (markdown-translate-filename-function
                (lambda (filename)
                  (push filename translated-files)
                  (format "/root%s.md" filename))))
      (markdown-follow-thing-at-point nil)
      (should (equal translated-files '("/foo/bar/baz")))
      (should (equal visited-files '("/root/foo/bar/baz.md"))))))

;;; Wiki link tests:

(ert-deftest test-markdown-wiki-link/file-local-variables ()
  "Test enabling wiki links via file-local variables."
  (markdown-test-file "wiki-links.text"
   (should-not markdown-enable-wiki-links)
   (hack-local-variables)
   (should markdown-enable-wiki-links)))

(ert-deftest test-markdown-wiki-link/aliasing ()
  "Test filename extraction for aliased wiki links."
  (let ((markdown-enable-wiki-links t))
    (markdown-test-file "wiki-links.text"
      ;; Confirm location of first wiki link
      (should (eq (markdown-next-link) 8))
      ;; Confirm location of second wiki link
      (should (eq (markdown-next-link) 73))
      ;; Test predicate function
      (should (markdown-wiki-link-p))
      ;; Test alias-first filename extraction
      (setq markdown-wiki-link-alias-first t)
      (should (string-equal (markdown-wiki-link-link) "second"))
      ;; Test alias-second filename extraction
      (setq markdown-wiki-link-alias-first nil)
      (should (string-equal (markdown-wiki-link-link) "first")))))

(ert-deftest test-markdown-wiki-link/navigation ()
  "Test wiki link navigation."
  (let ((markdown-enable-wiki-links t))
    (markdown-test-file "wiki-links.text"
      ;; Advance to first link
      (should (eq (markdown-next-link) 8))
      ;; Advance to second link
      (should (eq (markdown-next-link) 73))
      ;; Avance to final link
      (should (eq (markdown-next-link) 155))
      ;; Return nil and don't advance point
      (should (eq (markdown-next-link) nil))
      (should (eq (point) 155))
      ;; Move back to second link
      (should (eq (markdown-previous-link) 73))
      ;; Move back to first link
      (should (eq (markdown-previous-link) 8))
      ;; Return nil and don't move point
      (should (eq (markdown-previous-link) nil))
      (should (eq (point) 8)))))

(ert-deftest test-markdown-wiki-link/font-lock ()
  "Test font lock faces for wiki links."
  ;; If `temporary-file-directory' contains an inaccessible
  ;; subdirectory, `markdown-fontify-buffer-wiki-links' fails because
  ;; it calls `directory-files-recursively' on the directory, which
  ;; fails because of
  ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=28567>.  To fix
  ;; this, we run the entire test in a new subdirectory of
  ;; `temporary-file-directory', which is guaranteed to not contain
  ;; any inaccessible directories.
  (let ((temporary-file-directory
         (file-name-as-directory (make-temp-file "markdown-test" :dir-flag))))
    (markdown-test-temp-file "wiki-links.text"
      (let* ((fn (concat (file-name-directory buffer-file-name)
                         "inline.text"))
             (markdown-enable-wiki-links t))
        ;; Create inline.text in the same temp directory, refontify
        (write-region "" nil fn nil 1)
        (markdown-fontify-buffer-wiki-links)
        ;; Confirm location of first wiki link
        (should (eq (markdown-next-link) 8))
        ;; First wiki link doesn't have a corresponding file
        (markdown-test-range-has-property 8 20 'font-lock-face 'markdown-missing-link-face)
        ;; Second wiki link doesn't have a corresponding file
        (should (eq (markdown-next-link) 73))
        (markdown-test-range-has-property 73 88 'font-lock-face 'markdown-missing-link-face)
        ;; Move to third wiki link, and create the missing file
        (should (eq (markdown-next-link) 155))
        (should (string-equal (markdown-wiki-link-link) "inline"))
        (markdown-test-range-has-property 155 164 'font-lock-face 'markdown-link-face)
        ;; Check wiki links in code blocks
        (markdown-test-range-has-face 360 395 'markdown-pre-face)
        ;; Remove temporary files
        (delete-file fn)))
    (delete-directory temporary-file-directory)))

(ert-deftest test-markdown-wiki-link/kill ()
  "Simple tests for `markdown-kill-thing-at-point' for wiki links."
  (let ((kill-ring nil)
        (markdown-enable-wiki-links t)
        (tests (list '("[[foo]]" . "foo")
                     '("[[foo|bar]]" . "bar"))))
    (dolist (test tests)
      ;; Load test string (the car), move to end of first line, kill
      ;; thing at point, and then verify that the kill ring contains cdr.
      (markdown-test-string (car test)
                            (end-of-line)
                            (call-interactively 'markdown-kill-thing-at-point)
                            (should (string-equal (current-kill 0) (cdr test)))))))

;;; Filling tests:

(ert-deftest test-markdown-filling/blockquote ()
  "Test filling of blockquotes.
See `adaptive-fill-first-line-regexp'."
  (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
   (fill-paragraph)
   (should (string-equal (buffer-string) "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n> eiusmod tempor incididunt ut labore et dolore magna aliqua."))))

(ert-deftest test-markdown-filling/blockquote-paragraphs ()
  "Test filling of blockquotes containing multiple paragraphs."
  (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n>\n> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n"
   (forward-paragraph)
   (fill-paragraph)
   (should (string-equal (buffer-string) "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n>\n> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\n> nisi ut aliquip ex ea commodo consequat.\n"))))

(ert-deftest test-markdown-filling/leanpub-block ()
  "Test adaptive filling of Leanpub blocks."
  (markdown-test-string "A> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
   (fill-paragraph)
   (should (string-equal (buffer-string) "A> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\nA> eiusmod tempor incididunt ut labore et dolore magna aliqua."))))

(ert-deftest test-markdown-filling/space-after-list-marker ()
  "`fill-paragraph' should preserve more than one space after a list marker,
since users may wish to indent their lists more than one space more than the
width of the marker. The examples on the Markdown Syntax page have three
spaces after the list marker for a total indentation of four."
  (let ((str "\n\n*   List item indented four spaces.\n*   Also four spaces."))
   (markdown-test-string str
    (forward-line 2)
    (fill-paragraph)
    (should (string-equal (buffer-string) str)))))

(ert-deftest test-markdown-filling/multi-line-list-with-more-space ()
  "`fill-paragraph' should preserve more than one space after a list marker
(see `test-preserve-space-after-list-marker')."
  (let ((str "*   This list item is continued on\n    the next line"))
   (markdown-test-string str
    ;; The first line is exactly 35 columns
    (let ((fill-column 35))
      (fill-paragraph)
      (should (string-equal (buffer-string) str))))))

(ert-deftest test-markdown-filling/definition-list-add-leading-spaces ()
  "`fill-paragraph' should adapt to spaces after list marker."
  (markdown-test-string
   ":   This list item is continued on the next line"
   (let ((fill-column 35))
     (fill-paragraph)
     (should (string-equal
              (buffer-string)
              ":   This list item is continued on\n    the next line")))))

(ert-deftest test-markdown-filling/definition-list-preserve-leading-spaces ()
  "`fill-paragraph' should preserve spaces after list marker."
  (let ((str ":   This list item is continued on\n    the next line")
        (fill-column 35))
    (markdown-test-string
     str (fill-paragraph)
     (should (string-equal (buffer-string) str)))))

(ert-deftest test-markdown-filling/list-item-plus ()
  "Test filling of list items with plus sign markers.
See `adaptive-fill-regexp'."
  (markdown-test-string "  + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
   (fill-paragraph)
   (should (string-equal (buffer-string) "  + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n    eiusmod tempor incididunt ut labore et dolore magna aliqua."))))

(ert-deftest test-markdown-filling/list-item-plus-in-blockquote ()
  "Test filling of list items with plus sign markers inside blockquote.
See `adaptive-fill-regexp'."
  (markdown-test-string ">  + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
   (fill-paragraph)
   (should (string-equal (buffer-string) ">  + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n>    eiusmod tempor incididunt ut labore et dolore magna aliqua."))))

(ert-deftest test-markdown-filling/line-break ()
  "Test filling of paragraphs with hard line breaks.
See `paragraph-separate'."
  (markdown-test-string "Lorem ipsum dolor sit amet,  \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
   (let ((fill-column 70))
     (fill-paragraph)
     (should (string-equal (buffer-string) "Lorem ipsum dolor sit amet,  \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut\nlabore et dolore magna aliqua.")))))

(ert-deftest test-markdown-filling/decimal-number-at-beginning ()
  "Test filling when a number with a decimal appears at the beginning of a line."
  (markdown-test-string "The circumference of a circle divided by it's radius is around\n3.14."
   (fill-paragraph)
   (should (string-equal (buffer-string) "The circumference of a circle divided by it's radius is around 3.14."))))

(ert-deftest test-markdown-filling/avoid-unintended-list-item ()
  "Avoid breaking lines where it would result in an unintended list item."
  (markdown-test-string "Lorem ipsum dolor sit 4. amet"
   (let ((fill-column 22))
     (fill-paragraph)
     (should (string-equal (buffer-string) "Lorem ipsum dolor\nsit 4. amet")))))

(ert-deftest test-markdown-filling/no-break-link-reference ()
  "Shouldn't break line between label and url, or combine two link references."
  (let ((str "[label1]: http://long-url.example.com\n[label2]: http://another-long-url.example.com/"))
    (markdown-test-string str
     (let ((fill-column 15)) ; after end of label, before end of URL
       (fill-paragraph)
       (should (string-equal (buffer-string) str))))))

(ert-deftest test-markdown-filling/no-break-before-list-item ()
  "There's no point in putting the first item of a list on the next line,
indented the same amount."
  (let ((str "*   [Link](http://way-too-long.example.com)\n"))
    (markdown-test-string str
      (auto-fill-mode 1)
      (let ((fill-column 10))
        (end-of-line)
        (funcall auto-fill-function)
        ;; This test was known to fail in Emacs 25 and earlier.
        (if (version< emacs-version "26.0")
            (should-not (string-equal (buffer-string) str))
          (should (string-equal (buffer-string) str)))))))

(ert-deftest test-markdown-filling/break-within-list-item ()
  "This doesn't suppress auto-fill within a multi-word list item."
  :expected-result :failed
  (markdown-test-string "*   [Link](http://example.com/) more text"
    (auto-fill-mode 1)
    (let ((fill-column 10))
      (end-of-line)
      (funcall auto-fill-function)
      (should (string-equal
               (buffer-string)
               "*   [Link](http://example.com/)\n    more text")))))

(ert-deftest test-markdown-filling/preserve-next-line-footnote ()
  "Footnote block can be after label"
  (let ((str "[^label1]:\n    Footnote block\n    more footnote")) ; six spaces
    (markdown-test-string str
     (let ((fill-column 20)) ; could fit "footnote" after label, but shouldn't
       (fill-paragraph)
       (should (string-equal (buffer-string) str))))))

(ert-deftest test-markdown-filling/wrap-same-line-footnote ()
  "Additional lines must be indented one level (four spaces) when wrapped."
  (markdown-test-string "[^label]: Long line should be wrapped"
     (let ((fill-column 25)) ; wrap before end of "should"
       (fill-paragraph)
       (should (string-equal (buffer-string) "[^label]: Long line\n    should be wrapped")))))

(ert-deftest test-markdown-filling/wrap-extra-hanging-indentation ()
  "Additional lines must be indented one level (four spaces) when wrapped."
  (markdown-test-string "[^label]: Long line\n      should be wrapped"
     (let ((fill-column 25)) ; wrap before end of "should"
       (fill-paragraph)
       (should (string-equal (buffer-string) "[^label]: Long line\n      should be wrapped")))))

(ert-deftest test-markdown-filling/full-justification ()
  "Test paragraph detection with lines with lots of whitespace."
  (markdown-test-string "Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Dolor Sit Amet Consectetur http://very-long-url.lorem.ipsum.sic.dolor.sit.amet.com"
     (setq default-justification 'full)
     (fill-paragraph)
     (should (string-equal (buffer-string) "Lorem  Ipsum Lorem  Ipsum Lorem  Ipsum Lorem  Ipsum Lorem  Ipsum Lorem\nDolor                Sit               Amet                Consectetur\nhttp://very-long-url.lorem.ipsum.sic.dolor.sit.amet.com"))
     (backward-paragraph)
     (forward-paragraph)
     (should (= (point) 198))))

(ert-deftest test-markdown-filling/list-line ()
  "Test fill-paragraph for list line. Don't insert bullet automatically.
Detail: https://github.com/jrblevin/markdown-mode/issues/79"
  (markdown-test-string "* foo foo *foo* foo foo foo foo foo foo"
    (let ((fill-column 10))
      (fill-paragraph)
      (fill-paragraph)
      (forward-line 2)
      (back-to-indentation)
      (should-not (looking-at-p "\\*foo"))
      (forward-line 1)
      (back-to-indentation)
      (should-not (looking-at-p "\\*foo")))))

(ert-deftest test-markdown-filling/ignore-header ()
  "# Test fill-paragraph for containing header line paragraph.
https://github.com/jrblevin/markdown-mode/issues/159"
  (markdown-test-string "# this is header line
this is not header line
"
    (let ((fill-column 10))
      (fill-paragraph)
      (should (string= (buffer-substring (point) (line-end-position)) "# this is header line")))))

(ert-deftest test-markdown-filling/unclosed-square-bracket ()
  "Test fill-paragraph following an unclosed square bracket."
  (markdown-test-string "```\n[3\n```\n\naaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbb"
    (let ((fill-column 20))
      (forward-line 4)
      (fill-paragraph)
      (should (looking-at "aaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbb")))))

(ert-deftest test-markdown-filling/skip-code-blocks ()
  "Test `markdown-fill-paragraph' on code blocks."
  (let ((text "test\n\n```\nhello\nworld\n```"))
    (markdown-test-string text
      (dotimes (n 5)
        ;; Fill at each line; buffer should not change.
        (fill-paragraph)
        (should (string-equal (buffer-string) text))))))

(ert-deftest test-markdown-filling/fill-region-skip-code-blocks ()
  "Test `fill-region' on code blocks."
  (let ((text "testing\n\n```\nhello\nworld\n```\n\n123"))
    (markdown-test-string text
      ;; Fill entire buffer; buffer should not change.
      (fill-region (point-min) (point-max))
      (should (string-equal (buffer-string) text)))))

(ert-deftest test-markdown-filling/fill-region-skip-code-blocks-2 ()
  "Test `fill-region' on a buffer with a code block with long paragraphs."
  (markdown-test-string "long unwrapped paragraph 1

```
code
block

foo
bar
```

long unwrapped paragraph 2"
    ;; Test markdown-fill-forward-paragraph movement.
    (should (= (markdown-fill-forward-paragraph 1) 0))
    (should (= (point) 28)) ;; Point just after par. 1.
    (should (= (markdown-fill-forward-paragraph 1) 0))
    (should (= (point) 84)) ;; Point at end of par. 2.
    ;; Test filling the entire buffer with `fill-region'.
    (let ((fill-column 12))
      (fill-region (point-min) (point-max))
      (should (string-equal (buffer-string)
                            "long
unwrapped
paragraph 1

```
code
block

foo
bar
```

long
unwrapped
paragraph 2")))))

(ert-deftest test-markdown-filling/fill-region-skip-code-blocks-3 ()
  "Test `fill-region' on a lone code block with no surrounding text."
  (let ((text "```\ncode\nblock\n```\n"))
    (markdown-test-string text
      ;; Fill entire buffer; buffer should not change.
      (fill-region (point-min) (point-max))
      (should (string-equal (buffer-string) text)))))

(ert-deftest test-markdown-filling/long-paragraph-with-link ()
  "Test `fill-paragraph' on a long paragraph with a long link.
See GH-173."
  (markdown-test-string
   "aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa [aaa aaa aaa aaa](aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) aaa aaa aaa aaa aaa."
   (let ((fill-column 79)) (fill-paragraph))
   (should (string-equal (buffer-string) "aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa [aaa aaa aaa
aaa](aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) aaa aaa aaa aaa aaa."))))

(ert-deftest test-markdown-filling/pandoc-line-blocks ()
  "Filling should leave Pandoc line blocks undisturbed.
This includes preserving whitespace after the pipe."
  (let ((text "| The limerick packs laughs anatomical
| In space that is quite economical.
|    But the good ones I've seen
|    So seldom are clean
| And the clean ones so seldom are comical

| 200 Main St.
| Berkeley, CA 94718"))
    (markdown-test-string text
      (fill-region (point-min) (point-max))
      (should (string-equal (buffer-string) text)))))

;;; Export tests:

(ert-deftest test-markdown-hook/xhtml-standalone ()
  "Test `markdown-xhtml-standalone-regexp' and `markdown-output-standalone-p'."
  (should (string-match markdown-xhtml-standalone-regexp
                        "<?xml version='1.0' encoding='UTF-8'?>"))
  (should (string-match markdown-xhtml-standalone-regexp
                        "<!DOCTYPE html>"))
  (should (string-match markdown-xhtml-standalone-regexp
                        "<html>"))
  (should-not (string-match markdown-xhtml-standalone-regexp
                            "<h1>title</h1>"))
  (should-not (string-match markdown-xhtml-standalone-regexp
                            "<div id=\"name\">")))

(ert-deftest test-markdown-export/kill-buffer-nil ()
  "Test `markdown-export-kill-buffer' equal to nil."
  (markdown-test-temp-file "inline.text"
    (let* ((markdown-export-kill-buffer nil)
           (export-file (markdown-export))
           (export-buffer (get-file-buffer export-file)))
      ;; Output buffer should remain open.
      (should (member export-buffer (buffer-list))))))

(ert-deftest test-markdown-export/kill-buffer-t ()
  "Test `markdown-export-kill-buffer' equal to t."
  (markdown-test-temp-file "inline.text"
    (let* ((markdown-export-kill-buffer t)
           (export-file (markdown-export))
           (export-buffer (get-file-buffer export-file)))
      ;; Output buffer should be killed.
      (should-not export-buffer))))

;;; Hook tests:

(ert-deftest test-markdown-hook/before-export ()
  "Test hook run before export XHTML."
  (markdown-test-temp-file "lists.text"
   (let* ((before-hook-run nil)
          (orig-point (point))
          (func (lambda ()
                  ;; Change value of a variable
                  (setq before-hook-run t)
                  ;; Insert some text
                  (goto-char (point-min))
                  (insert "#")
                  ;; Deliberately move the point
                  (end-of-line)
                  ;; Verify changes
                  (should (looking-back "^## List Cases" nil))
                  (should-not (= (point) orig-point))))
          (ofile (progn
                   ;; Register hook
                   (add-hook 'markdown-before-export-hook func)
                   ;; Export XHTML and return filename
                   (markdown-export)))
          (obuffer (get-file-buffer ofile)))
     ;; Test side effects of hook
     (should (eq before-hook-run t))
     ;; Test position of point
     (should (= (point) orig-point))
     ;; Test that buffer was restored to original state
     (goto-char (point-min))
     (should (looking-at "^# List Cases"))
     ;; Clean
     (remove-hook 'markdown-before-export-hook func)
     (kill-buffer obuffer)
     (delete-file ofile))))

(ert-deftest test-markdown-hook/after-export ()
  "Test hook run after export XHTML."
  (markdown-test-temp-file "lists.text"
   (let* ((after-hook-run nil)
          (markdown-export-kill-buffer nil)
          (func (lambda ()
                  ;; Change variable value
                  (setq after-hook-run t)
                  ;; Add comment to output buffer
                  (goto-char (point-min))
                  (insert "<!-- after-export-hook -->\n")))
          (ofile (progn
                   ;; Register hook
                   (add-hook 'markdown-after-export-hook func)
                   ;; Export XHTML and return filename
                   (markdown-export)))
          (obuffer (get-file-buffer ofile)))
     (message "obuffer = %S" obuffer)
     ;; Test that variable was changed
     (should (eq after-hook-run t))
     ;; Test that output buffer remains open
     (should (get-buffer obuffer))
     ;; Test that output buffer modification remains
     (with-current-buffer obuffer
       (goto-char (point-min))
       (should (looking-at "<!-- after-export-hook -->\n")))
     ;; Test that buffer modification was saved
     (should-not (buffer-modified-p obuffer))
     ;; Clean up
     (remove-hook 'markdown-after-export-hook func)
     (kill-buffer obuffer)
     (delete-file ofile))))

;;; Extension: math support

(ert-deftest test-markdown-math/file-local-variable ()
  "Test enabling math mode via `hack-local-variables-hook'."
  (markdown-test-file "math.text"
   (should-not markdown-enable-math)
   (hack-local-variables)
   (should markdown-enable-math)))

(ert-deftest test-markdown-math/reload ()
  "Test enabling math mode via variable `markdown-enable-math'."
  (let ((markdown-enable-math t))
    (markdown-test-file "math.text"
      ;; Flag should be set to t
      (should markdown-enable-math)
      ;; Font-lock keywords should be updated.
      (should (member (car markdown-mode-font-lock-keywords-math)
                      (cadr font-lock-keywords))))))

(ert-deftest test-markdown-math/preserve-user-keywords ()
  "Test preserving user-specified font-lock keywords."
  (let ((user-keyword '("\\<\\(FIXME\\):" 1 font-lock-warning-face t)))
    ;; Add user font-lock keyword using `font-lock-add-keywords'.
    (font-lock-add-keywords 'markdown-mode (list user-keyword))
    ;; Visit a file using `markdown-mode'.
    (markdown-test-file "math.text"
      ;; User keyword should be present initially.
      (should (member user-keyword (cadr font-lock-keywords)))
      ;; User keyword should persist after calling `markdown-reload-extensions'.
      (markdown-reload-extensions)
      (should (member user-keyword (cadr font-lock-keywords))))
    ;; Remove the user keyword using `font-lock-remove-keywords'.
    (font-lock-remove-keywords 'markdown-mode (list user-keyword))
    ;; Visit a file using `markdown-mode'.
    (markdown-test-file "inline.text"
      ;; User keyword should not be present after removal.
      (should-not (member user-keyword (cadr font-lock-keywords))))))

(ert-deftest test-markdown-math/preserve-local-user-keywords ()
  "Test preserving buffer-specific user-specified font-lock keywords."
  (let ((user-keyword '("\\<\\(FIXME\\):" 1 font-lock-warning-face t)))
    ;; Visit a file using `markdown-mode'.
    (markdown-test-file "math.text"
      ;; Add user font-lock keyword using `font-lock-add-keywords'.
      (font-lock-add-keywords nil (list user-keyword))
      ;; User keyword should be present initially.
      (should (member user-keyword (cadr font-lock-keywords)))
      ;; User keyword should persist after calling `markdown-reload-extensions'.
      (markdown-reload-extensions)
      (should (member user-keyword (cadr font-lock-keywords)))
      ;; Remove the user keyword using `font-lock-remove-keywords'.
      (font-lock-remove-keywords nil (list user-keyword))
      ;; User keyword should not be present after removal.
      (should-not (member user-keyword (cadr font-lock-keywords))))))

(ert-deftest test-markdown-math/font-lock ()
  "Test markdown math mode."
  (let ((markdown-enable-math t))
    (markdown-test-file "math.text"
      (markdown-test-range-has-face 1 32 nil)
      (markdown-test-range-has-face 33 33 'markdown-markup-face)
      (markdown-test-range-has-face 34 45 'markdown-math-face)
      (markdown-test-range-has-face 46 46 'markdown-markup-face)
      (markdown-test-range-has-face 47 49 nil)
      (markdown-test-range-has-face 50 51 'markdown-markup-face)
      (markdown-test-range-has-face 52 63 'markdown-math-face)
      (markdown-test-range-has-face 64 65 'markdown-markup-face)
      (markdown-test-range-has-face 66 98 nil)
      (markdown-test-range-has-face 99 100 'markdown-markup-face)
      (markdown-test-range-has-face 101 112 'markdown-math-face)
      (markdown-test-range-has-face 113 114 'markdown-markup-face)
      (markdown-test-range-has-face 113 114 'markdown-markup-face)
      (markdown-test-range-has-face 117 117 'markdown-header-delimiter-face)
      (markdown-test-range-has-face 119 152 'markdown-header-face-1)
      (markdown-test-range-has-face 129 129 'markdown-markup-face)
      (markdown-test-range-has-face 136 136 'markdown-markup-face)

      (markdown-test-range-has-face 174 177 'markdown-markup-face)
      (markdown-test-range-has-face 179 179 'markdown-markup-face)
      (markdown-test-range-has-face 180 187 'markdown-language-keyword-face)
      (markdown-test-range-has-face 188 188 'markdown-markup-face)
      (markdown-test-range-has-face 190 210 'markdown-pre-face)
      (markdown-test-range-has-face 212 215 'markdown-markup-face)

      (markdown-test-range-has-face 218 218 'markdown-markup-face)
      (markdown-test-range-has-face 219 223 'markdown-math-face)
      (markdown-test-range-has-face 224 224 'markdown-markup-face)
      (markdown-test-range-has-face 350 351 'markdown-markup-face)
      (markdown-test-range-has-face 352 356 'markdown-math-face)
      (markdown-test-range-has-face 357 358 'markdown-markup-face)
      (markdown-test-range-has-face 359 391 nil)
      (markdown-test-range-has-face 392 393 'markdown-markup-face)
      (markdown-test-range-has-face 394 398 'markdown-math-face)
      (markdown-test-range-has-face 399 400 'markdown-markup-face))))

(ert-deftest test-markdown-math/double-slash-display-math ()
  "Test double slash display math font lock."
  (let ((markdown-enable-math t))
    (markdown-test-file "math.text"
      (markdown-test-range-has-face 403 474 nil)
      (markdown-test-range-has-face 475 477 'markdown-markup-face)
      (markdown-test-range-has-face 478 543 'markdown-math-face)
      (markdown-test-range-has-face 544 546 'markdown-markup-face))))

(ert-deftest test-markdown-math/indented-double-slash-display-math ()
  "Test font lock for indented double slash display math.
See GH-288."
  (let ((markdown-enable-math t))
    (markdown-test-string "- Einstein's equation:

    \\\\[ E = m c^2 \\\\]"
      (markdown-test-range-has-face 29 31 'markdown-markup-face)
      (markdown-test-range-has-face 32 42 'markdown-math-face)
      (markdown-test-range-has-face 43 45 'markdown-markup-face))))

(ert-deftest test-markdown-math/font-lock-italics ()
  "Test markdown math mode with underscores."
  (let ((markdown-enable-math t))
    (markdown-test-file "math.text"
      (markdown-test-range-has-face 227 227 'markdown-markup-face)
      (markdown-test-range-has-face 228 233 'markdown-math-face)
      (markdown-test-range-has-face 234 234 'markdown-markup-face)
      (markdown-test-range-has-face 235 270 nil)
      (markdown-test-range-has-face 271 271 'markdown-markup-face)
      (markdown-test-range-has-face 272 274 'markdown-math-face)
      (markdown-test-range-has-face 275 275 'markdown-markup-face))))

(ert-deftest test-markdown-math/font-lock-no-bold ()
  "Bold markers in math should not trigger bold."
  (let ((markdown-enable-math t))
    (markdown-test-file "math.text"
      (markdown-test-range-has-face 279 299 'markdown-math-face)
      (markdown-test-range-has-face 301 308 nil)
      (markdown-test-range-has-face 310 312 'markdown-math-face))))

;;; Extension: pipe table editing

(ert-deftest test-markdown-table/table-begin-top-of-file ()
  "Test beginning of table detection at top of file."
  (markdown-test-string "\n| 1 | 2 |\n"
    (should-not (markdown-table-at-point-p))
    (forward-line)
    (should (markdown-table-at-point-p))
    (should (= (markdown-table-begin) 2))))


;;; gfm-mode tests:

(ert-deftest test-markdown-gfm/pre-1 ()
  "GFM pre block font lock test."
  (markdown-test-file-gfm "gfm.text"
    (markdown-test-range-has-face 2626 2637 nil)
    (markdown-test-range-has-face 2639 2641 'markdown-markup-face)
    (markdown-test-range-has-face 2642 2645 'markdown-language-keyword-face)
    (markdown-test-range-has-face 2647 2728 'markdown-pre-face)
    (markdown-test-range-has-face 2730 2732 'markdown-markup-face)))

(ert-deftest test-markdown-gfm/italic-1 ()
  "GFM italic font lock test."
  (markdown-test-file-gfm "gfm.text"
    (markdown-test-range-has-face 1483 1483 'markdown-markup-face)
    (markdown-test-range-has-face 1484 1487 'markdown-italic-face)
    (markdown-test-range-has-face 1488 1488 'markdown-markup-face)
    (markdown-test-range-has-face 1729 1790 nil)))

(ert-deftest test-markdown-gfm/strike-through-1 ()
  "GFM strike through font lock test."
  (markdown-test-string-gfm "one ~~two~~ three"
    (markdown-test-range-has-face 1 4 nil)
    (markdown-test-range-has-face 5 6 'markdown-markup-face)
    (markdown-test-range-has-face 7 9 'markdown-strike-through-face)
    (markdown-test-range-has-face 10 11 'markdown-markup-face)
    (markdown-test-range-has-face 12 17 nil)))

(ert-deftest test-markdown-gfm/toggle-strike-through ()
  "Test toggling functionality of `markdown-insert-strike-through'."
  (markdown-test-string-gfm "one ~~two~~ three"
   (forward-word 2)
   (markdown-insert-strike-through)
   (should (string-equal (buffer-string) "one two three"))
   (should (= (point) 8))
   (forward-word)
   (markdown-insert-strike-through)
   (should (= (point) 16))
   (should (string-equal (buffer-string) "one two ~~three~~"))))

(ert-deftest test-markdown-gfm/insert-code-block-empty-markup ()
  "Test GFM code block insertion with empty code section."
  (markdown-test-string-gfm "line 1\nline 2\n"
   (end-of-line)
   (markdown-insert-gfm-code-block "elisp")
   (should (equal (car markdown-gfm-used-languages) "elisp"))
   (should (equal (car (markdown-gfm-get-corpus)) "elisp"))
   (should (string-equal (buffer-string)
                         "line 1\n\n``` elisp\n\n```\n\nline 2\n"))))

(ert-deftest test-markdown-gfm/markdown-spaces-after-code-fence ()
  "Test `markdown-spaces-after-code-fence'."
  (markdown-test-string-gfm ""
    (let ((markdown-spaces-after-code-fence 0))
      (markdown-insert-gfm-code-block "elisp")
      (should (equal (buffer-string) "```elisp\n\n```")))))

(ert-deftest test-markdown-gfm/insert-code-block-active-region ()
  "Test GFM code block insertion with active region."
  (markdown-test-string-gfm "line 1\nline 2\nline 3\n"
   (forward-line)
   (transient-mark-mode)
   (push-mark (point) t t)
   (end-of-line)
   (should (markdown-use-region-p))
   (markdown-insert-gfm-code-block "elisp")
   (should (string-equal (buffer-string)
                         "line 1\n\n``` elisp\nline 2\n```\n\nline 3\n"))))

(ert-deftest test-markdown-gfm/insert-code-block-indented-list-item ()
  "Test GFM code block insertion with an indented list item."
  (markdown-test-string-gfm "1. foo\n   "
    (goto-char (point-max))
    (markdown-insert-gfm-code-block "elisp")
    (should (equal (buffer-substring-no-properties (point-min) (point-max))
                   "1. foo\n\n   ``` elisp\n   \n   ```"))
    (should (equal (buffer-substring-no-properties (point) (point-max))
                   "\n   ```"))))

(ert-deftest test-markdown-gfm/insert-code-block-indented-list-item-active-region ()
  "Test GFM code block insertion with an indented list item and active region."
  (markdown-test-string-gfm "1.  foo\n    bar\n"
    (let ((transient-mark-mode t))
      (forward-line)
      (push-mark nil :nomsg :activate)
      (end-of-line)
      (should (markdown-use-region-p))
      (markdown-insert-gfm-code-block "elisp"))
    (should (equal (buffer-substring-no-properties (point-min) (point-max))
                   "1.  foo\n\n    ``` elisp\n    bar\n    ```\n\n"))
    (should (equal (buffer-substring-no-properties (point) (point-max))
                   "\n    bar\n    ```\n\n"))))

(ert-deftest test-markdown-gfm/gfm-parse-buffer-for-languages ()
  "Parse buffer for existing languages for `markdown-gfm-used-languages' test."
  (markdown-test-string-gfm "``` MADEUP\n\n```\n``` LANGUAGES\n\n```\n```MaDeUp\n\n```\n```\n\n```\n``` \n\n```\n"
    (markdown-gfm-parse-buffer-for-languages)
    (should (equal markdown-gfm-used-languages
                   (list "MaDeUp" "LANGUAGES" "MADEUP")))
    (should (equal (car markdown-gfm-used-languages) "MaDeUp"))
    (should (equal (car (markdown-gfm-get-corpus)) "MaDeUp"))
    (goto-char (point-max))
    (markdown-insert-gfm-code-block "newlang")
    (should (equal markdown-gfm-used-languages
                   (list "newlang" "MaDeUp" "LANGUAGES" "MADEUP")))
    (should (equal (car markdown-gfm-used-languages) "newlang"))
    (should (equal (car (markdown-gfm-get-corpus)) "newlang"))
    (let ((markdown-gfm-downcase-languages nil))
      (should
       (equal (markdown-gfm-get-corpus)
              (append markdown-gfm-used-languages
                      markdown-gfm-additional-languages
                      markdown-gfm-recognized-languages))))
    (let ((markdown-gfm-downcase-languages t))
      (should
       (equal
        (markdown-gfm-get-corpus)
        (append markdown-gfm-used-languages
                (cl-mapcar #'downcase
                           (append markdown-gfm-additional-languages
                                   markdown-gfm-recognized-languages))))))))

(ert-deftest test-markdown-gfm/code-block-font-lock ()
  "GFM code block font lock test."
  (markdown-test-file-gfm "gfm.text"
    (markdown-test-range-has-face 2639 2641 'markdown-markup-face) ; ```
    (markdown-test-range-has-face 2642 2645 'markdown-language-keyword-face) ; lang
    (markdown-test-range-has-face 2647 2728 'markdown-pre-face) ; code
    (markdown-test-range-has-face 2730 2732 'markdown-markup-face))) ; ```

(ert-deftest test-markdown-gfm/code-block-font-lock-2 ()
  "GFM code block font lock test without language identifier."
  (markdown-test-string-gfm "Plain code block:\n\n```\nfoo\n```\n"
    (markdown-test-range-has-face 20 22 'markdown-markup-face)
    (markdown-test-range-has-face 24 26 'markdown-pre-face)
    (markdown-test-range-has-face 28 30 'markdown-markup-face)))

;;; Extension: GFM simplified tables

(ert-deftest test-markdown-gfm/false-table-first-line ()
  "Test beginning of table detection at beginning of buffer.
Based on GH-298."
  (markdown-test-string
   "[|"
   (should-not (gfm--table-at-point-p))))

(ert-deftest test-markdown-gfm/true-table-first-line ()
  "Test beginning of table detection at beginning of buffer."
  (markdown-test-string
   "[|
-|-
x|"
   (dotimes (x 3)
     (should (gfm--table-at-point-p))
     (forward-line))))

(ert-deftest test-markdown-gfm/table-begin-top-of-file ()
  "Test beginning of table detection at top of file."
  (markdown-test-string "[|"
    (should-not (gfm--table-at-point-p))))

;;; Tests for other extensions:

(ert-deftest test-markdown-ext/pandoc-fancy-lists ()
  "Test basic support for font lock and filling of Pandoc 'fancy lists'."
  (markdown-test-string " #. abc\ndef\n"
    ;; font lock
    (markdown-test-range-has-face 1 1 nil)
    (markdown-test-range-has-face 2 3 'markdown-list-face)
    (markdown-test-range-has-face 4 11 nil)
    ;; filling
    (forward-line)
    (markdown-indent-region (line-beginning-position) (line-end-position) nil)
    (should (string-equal (buffer-string) " #. abc\n def\n"))
    (markdown-indent-region (line-beginning-position) (line-end-position) nil)
    (should (string-equal (buffer-string) " #. abc\n    def\n"))))

(ert-deftest test-markdown-ext/wiki-link-rules ()
  "Test wiki link search rules and font lock for missing pages."
  (let ((markdown-enable-wiki-links t)
        (markdown-wiki-link-fontify-missing t)
        (markdown-wiki-link-search-subdirectories t)
        (markdown-wiki-link-search-parent-directories t))
    (progn
      (find-file "wiki/root")
      (unwind-protect
          (progn
            (markdown-mode)
            ;; search rules
            (should (string-match-p
                     "/sub/foo$"
                     (markdown-convert-wiki-link-to-filename "foo")))
            (should (string-equal
                     (markdown-convert-wiki-link-to-filename "doesnotexist")
                     "doesnotexist"))
            ;; font lock
            (markdown-test-range-has-property 1 11 'font-lock-face 'markdown-link-face)
            (markdown-test-range-has-property 14 33 'font-lock-face 'markdown-missing-link-face)
            (markdown-test-range-has-property 36 42 'font-lock-face 'markdown-link-face)
            (markdown-test-range-has-property 45 60 'font-lock-face 'markdown-missing-link-face))
        (kill-buffer)))
    (progn
      (find-file "wiki/sub/foo")
      (unwind-protect
          (progn
            (markdown-mode)
            ;; search rules
            (should (string-match-p
                     "/wiki/root$"
                     (markdown-convert-wiki-link-to-filename "root")))
            (should (string-equal
                     (markdown-convert-wiki-link-to-filename "doesnotexist")
                     "doesnotexist"))
            ;; font lock
            (markdown-test-range-has-property 1 16 'font-lock-face 'markdown-missing-link-face)
            (markdown-test-range-has-property 19 26 'font-lock-face 'markdown-link-face))
        (kill-buffer)))))

(defadvice markdown-live-preview-window-eww
    (around markdown-test-create-fake-eww disable)
  (setq ad-return-value (get-buffer-create "*eww*")))

(defmacro markdown-test-fake-eww (&rest body)
  `(progn
     ,@(if (and (fboundp 'libxml-parse-html-region) (require 'eww nil t)) body
         `((ad-enable-advice #'markdown-live-preview-window-eww
                             'around 'markdown-test-create-fake-eww)
           (ad-activate #'markdown-live-preview-window-eww)
           ,@body
           (ad-disable-advice #'markdown-live-preview-window-eww
                              'around 'markdown-test-create-fake-eww)
           (ad-activate #'markdown-live-preview-window-eww)))))

(defmacro markdown-test-eww-or-nothing (test &rest body)
  (if (and (fboundp 'libxml-parse-html-region) (require 'eww nil t)
           (executable-find markdown-command))
      `(progn ,@body)
    (message "no eww, no libxml2, or no %s found: skipping %s" markdown-command test)
    nil))

(ert-deftest test-markdown-ext/live-preview-no-file ()
  "Live-preview a `markdown-mode' buffer without a file."
  (with-temp-buffer
    (markdown-mode)

    ;; Activating `markdown-live-preview-mode' signals error
    (should-error (markdown-live-preview-mode))

    ;; After trying to activate live preview mode, mode is not activated
    (should-not markdown-live-preview-mode)

    ;; `markdown-live-preview-export' does nothing
    (should-not (markdown-live-preview-export))

    ;; `markdown-live-preview-remove' does nothing
    (should-not (markdown-live-preview-remove))))

(ert-deftest test-markdown-ext/live-preview-exports ()
  (markdown-test-temp-file "inline.text"
    (unless (and (fboundp 'libxml-parse-html-region) (require 'eww nil t))
      (should-error (markdown-live-preview-mode)))
    (markdown-test-fake-eww
     (markdown-live-preview-mode)
     (should (buffer-live-p markdown-live-preview-buffer))
     (should (eq (current-buffer)
                 (with-current-buffer markdown-live-preview-buffer
                   markdown-live-preview-source-buffer)))
     (kill-buffer markdown-live-preview-buffer)
     (should (null markdown-live-preview-buffer))
     (set-buffer-modified-p t)
     (save-buffer)                      ; should create new export
     (should (buffer-live-p markdown-live-preview-buffer)))))

(ert-deftest test-markdown-ext/live-preview-delete-exports ()
  (markdown-test-fake-eww
   (let ((markdown-live-preview-delete-export 'delete-on-destroy)
         file-output)
     (markdown-test-temp-file "inline.text"
       (markdown-live-preview-mode)
       (setq file-output (markdown-export-file-name)))
     (should-not (file-exists-p file-output)))
   (let ((markdown-live-preview-delete-export 'delete-on-export)
         file-output)
     (markdown-test-temp-file "inline.text"
       (markdown-live-preview-mode)
       (setq file-output (markdown-export-file-name))
       (should-not (file-exists-p file-output))))
   (let ((markdown-live-preview-delete-export nil)
         file-output)
     (unwind-protect
         (markdown-test-temp-file "inline.text"
           (markdown-live-preview-mode)
           (setq file-output (markdown-export-file-name))
           (should (file-exists-p file-output)))
       (delete-file file-output)))))

(ert-deftest test-markdown-ext/live-preview-follow-min-max ()
  (markdown-test-eww-or-nothing "live-preview-follow-min-max"
   (markdown-test-temp-file "inline.text"
     (markdown-live-preview-mode)
     (should (buffer-live-p markdown-live-preview-buffer))
     (should (window-live-p (get-buffer-window markdown-live-preview-buffer)))
     (with-selected-window (get-buffer-window markdown-live-preview-buffer)
       (goto-char (point-min)))
     (goto-char (point-min))
     (insert "a test ")
     (markdown-live-preview-export)
     (let (final-pt final-win-st-diff)
       ;; test that still starts at point-min
       (with-selected-window (get-buffer-window markdown-live-preview-buffer)
         (should (= (window-point) 1))
         (should (= (markdown-visual-lines-between-points
                     (window-start) (window-point))
                    0))
         (set-window-point (selected-window) (point-max))
         (setq final-pt (window-point)
               final-win-st-diff (markdown-visual-lines-between-points
                                  (window-start) (window-point))))
       (goto-char (point-min))
       (insert "this is ")
       (markdown-live-preview-export)
       (with-selected-window (get-buffer-window markdown-live-preview-buffer)
         (should (= (window-point) (+ final-pt (length "this is "))))
         (should (= (markdown-visual-lines-between-points
                     (window-start) (window-point))
                    final-win-st-diff))
         ;; test that still starts at point-max, with correct line difference
         (goto-char (floor (/ (float (- (point-max) (point-min))) 2)))
         (setq final-pt (window-point)
               final-win-st-diff (markdown-visual-lines-between-points
                                  (window-start) final-pt)))
       (markdown-live-preview-export)
       ;; test that still starts at same point, with correct line difference
       (with-selected-window (get-buffer-window markdown-live-preview-buffer)
         (should (= (window-point) final-pt))
         (should (= (markdown-visual-lines-between-points
                     (window-start) (window-point))
                    final-win-st-diff)))))))

;; Tests for imenu

(ert-deftest test-markdown-imenu/metadata ()
  "Don't correct header like statement in metadata.
https://github.com/jrblevin/markdown-mode/issues/145"
  (markdown-test-string "---
title = \"Blah\"
comments = false
---

# Header1

## Header2
"
    (let ((headers (mapcar #'car (markdown-imenu-create-flat-index))))
      (should (member "Header1" headers))
      (should (member "Header2" headers))
      (should-not (member "comments = false" headers)))))

(ert-deftest test-markdown-imenu/include-footnotes ()
  "Check that footnotes are added to the imenu.
https://github.com/jrblevin/markdown-mode/issues/235"
  (markdown-test-string "# H1

[^fn1]: footnote 1

## H2

[^fn2]: footnote 2
"
    (let* ((markdown-add-footnotes-to-imenu t)
           (entries (mapcar #'car (markdown-imenu-create-flat-index))))
      (should (member "^fn1" entries))
      (should (member "^fn2" entries)))))

(ert-deftest test-markdown-imenu/no-footnotes ()
  "Check that footnotes are not added to the imenu.
https://github.com/jrblevin/markdown-mode/issues/235"
  (markdown-test-string "# H1

[^fn1]: footnote 1

## H2

[^fn2]: footnote 2
"
    (let* ((markdown-add-footnotes-to-imenu nil)
           (entries (mapcar #'car (markdown-imenu-create-flat-index))))
      (should-not (member "^fn1" entries))
      (should-not (member "^fn2" entries)))))

(ert-deftest test-markdown-command/function ()
  "Test ‘markdown’ with ‘markdown-command’ being a function."
  (markdown-test-string "foo"
    (let* ((calls ())
           (markdown-command (lambda (&rest args) (push args calls)))
           (buffer-name (markdown))
           (buffer (get-buffer buffer-name)))
      (should (stringp buffer-name))
      (should (buffer-live-p buffer))
      (should (equal calls `((1 4 ,buffer)))))))

(ert-deftest test-markdown-open-command/function ()
  "Test ‘markdown-open’ with ‘markdown-open-command’ being a function."
  (markdown-test-string ""
    (let* ((calls 0)
           (markdown-open-command (lambda () (cl-incf calls))))
      (markdown-open)
      (should (equal calls 1)))))

(provide 'markdown-test)

;;; markdown-test.el ends here