mq-markdown 0.10.2

Markdown parsing and manipulation utilities for mq
Documentation
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
use super::*;
use rstest::rstest;

#[rstest]
#[case::dash('-', Some(ListMarker::Dash))]
#[case::plus('+', Some(ListMarker::Plus))]
#[case::star('*', Some(ListMarker::Star))]
#[case::period('.', Some(ListMarker::Period))]
#[case::paren(')', Some(ListMarker::Paren))]
#[case::underscore('_', None)]
#[case::digit('1', None)]
fn list_marker_from_char(#[case] char: char, #[case] expected: Option<ListMarker>) {
    assert_eq!(ListMarker::from_char(char), expected);
    assert_eq!(expected.map(ListMarker::as_char), expected.map(|_| char));
}

#[rstest]
#[case::dash(ListMarker::Dash, Some(ListStyle::Dash))]
#[case::plus(ListMarker::Plus, Some(ListStyle::Plus))]
#[case::star(ListMarker::Star, Some(ListStyle::Star))]
#[case::period(ListMarker::Period, None)]
#[case::paren(ListMarker::Paren, None)]
fn list_marker_style(#[case] marker: ListMarker, #[case] expected: Option<ListStyle>) {
    assert_eq!(marker.style(), expected);
}

#[rstest]
#[case::dash(ListStyle::Dash, ListMarker::Dash)]
#[case::plus(ListStyle::Plus, ListMarker::Plus)]
#[case::star(ListStyle::Star, ListMarker::Star)]
fn list_marker_from_style(#[case] style: ListStyle, #[case] expected: ListMarker) {
    assert_eq!(ListMarker::from(style), expected);
}

#[rstest]
#[case::star('*', Some(HorizontalRuleMarker::Star))]
#[case::dash('-', Some(HorizontalRuleMarker::Dash))]
#[case::underscore('_', Some(HorizontalRuleMarker::Underscore))]
#[case::plus('+', None)]
fn horizontal_rule_marker_from_char(#[case] char: char, #[case] expected: Option<HorizontalRuleMarker>) {
    assert_eq!(HorizontalRuleMarker::from_char(char), expected);
    assert_eq!(expected.map(HorizontalRuleMarker::as_char), expected.map(|_| char));
}

#[rstest]
#[case::bullet_dash(false, Some(ListMarker::Dash), "- a")]
#[case::bullet_plus(false, Some(ListMarker::Plus), "+ a")]
#[case::bullet_star(false, Some(ListMarker::Star), "* a")]
#[case::bullet_unknown(false, None, "- a")]
#[case::bullet_with_an_ordered_marker(false, Some(ListMarker::Paren), "- a")]
#[case::ordered_period(true, Some(ListMarker::Period), "1. a")]
#[case::ordered_paren(true, Some(ListMarker::Paren), "1) a")]
#[case::ordered_unknown(true, None, "1. a")]
#[case::ordered_with_a_bullet_marker(true, Some(ListMarker::Star), "1. a")]
fn list_renders_its_marker(#[case] ordered: bool, #[case] marker: Option<ListMarker>, #[case] expected: &str) {
    let node = Node::List(List {
        index: 0,
        level: 0,
        checked: None,
        values: vec![Node::Text(Text {
            value: "a".into(),
            position: None,
        })],
        ordered,
        start: None,
        spread: false,
        marker,
        position: None,
    });
    assert_eq!(
        node.render_with_theme(&RenderOptions::default(), &ColorTheme::PLAIN),
        expected
    );
}

#[rstest]
#[case::star(Some(HorizontalRuleMarker::Star), "***")]
#[case::dash(Some(HorizontalRuleMarker::Dash), "---")]
#[case::underscore(Some(HorizontalRuleMarker::Underscore), "___")]
#[case::unknown(None, "***")]
fn horizontal_rule_renders_its_marker(#[case] marker: Option<HorizontalRuleMarker>, #[case] expected: &str) {
    let node = Node::HorizontalRule(HorizontalRule { marker, position: None });
    assert_eq!(
        node.render_with_theme(&RenderOptions::default(), &ColorTheme::PLAIN),
        expected
    );
}

/// The JSON of the markers is the character they are written with, as it was when they were chars.
#[cfg(feature = "json")]
#[rstest]
#[case::dash(ListMarker::Dash, "\"-\"")]
#[case::plus(ListMarker::Plus, "\"+\"")]
#[case::star(ListMarker::Star, "\"*\"")]
#[case::period(ListMarker::Period, "\".\"")]
#[case::paren(ListMarker::Paren, "\")\"")]
fn list_marker_json(#[case] marker: ListMarker, #[case] expected: &str) {
    assert_eq!(serde_json::to_string(&marker).unwrap(), expected);
    assert_eq!(serde_json::from_str::<ListMarker>(expected).unwrap(), marker);
}

#[cfg(feature = "json")]
#[rstest]
#[case::star(HorizontalRuleMarker::Star, "\"*\"")]
#[case::dash(HorizontalRuleMarker::Dash, "\"-\"")]
#[case::underscore(HorizontalRuleMarker::Underscore, "\"_\"")]
fn horizontal_rule_marker_json(#[case] marker: HorizontalRuleMarker, #[case] expected: &str) {
    assert_eq!(serde_json::to_string(&marker).unwrap(), expected);
    assert_eq!(serde_json::from_str::<HorizontalRuleMarker>(expected).unwrap(), marker);
}

#[cfg(feature = "json")]
#[test]
fn list_json_keeps_the_marker_as_a_string() {
    let json =
        r#"{"type":"list","values":[],"index":0,"level":0,"ordered":false,"checked":null,"spread":false,"marker":"+"}"#;
    let list = serde_json::from_str::<List>(json).unwrap();
    assert_eq!(list.marker, Some(ListMarker::Plus));
    assert!(serde_json::to_string(&list).unwrap().contains(r#""marker":"+""#));
}

#[test]
fn map_values_into_transforms_owned_fragments() {
    let node = Node::Fragment(Fragment {
        values: vec![Node::Text(Text {
            value: "before".to_string(),
            position: None,
        })],
    });

    let mapped = node
        .map_values_into(&mut |node| -> Result<Node, std::io::Error> {
            Ok(match node {
                Node::Text(text) => Node::Text(Text {
                    value: text.value.to_uppercase(),
                    position: text.position.clone(),
                }),
                node => node.clone(),
            })
        })
        .unwrap();

    assert_eq!(mapped.to_string(), "BEFORE");
}

#[test]
fn map_values_into_owned_moves_nodes_through_the_callback() {
    let node = Node::Fragment(Fragment {
        values: vec![Node::Text(Text {
            value: "before".to_string(),
            position: None,
        })],
    });

    let mapped = node
        .map_values_into_owned(&mut |node| -> Result<Node, std::io::Error> {
            Ok(match node {
                Node::Text(mut text) => {
                    text.value.make_ascii_uppercase();
                    Node::Text(text)
                }
                node => node,
            })
        })
        .unwrap();

    assert_eq!(mapped.to_string(), "BEFORE");
}

#[rstest]
#[case::text(Node::Text(Text{value: "".to_string(), position: None}),
       "test".to_string(),
       Node::Text(Text{value: "test".to_string(), position: None }))]
#[case::blockquote(Node::Blockquote(Blockquote{values: vec!["test".to_string().into()], position: None }),
       "test".to_string(),
       Node::Blockquote(Blockquote{values: vec!["test".to_string().into()], position: None }))]
#[case::delete(Node::Delete(Delete{values: vec!["test".to_string().into()], position: None }),
       "test".to_string(),
       Node::Delete(Delete{values: vec!["test".to_string().into()], position: None }))]
#[case::emphasis(Node::Emphasis(Emphasis{values: vec!["test".to_string().into()], position: None }),
       "test".to_string(),
       Node::Emphasis(Emphasis{values: vec!["test".to_string().into()], position: None }))]
#[case::strong(Node::Strong(Strong{values: vec!["test".to_string().into()], position: None }),
       "test".to_string(),
       Node::Strong(Strong{values: vec!["test".to_string().into()], position: None }))]
#[case::heading(Node::Heading(Heading {depth: HeadingDepth::H1, values: vec!["test".to_string().into()], position: None }),
       "test".to_string(),
       Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["test".to_string().into()], position: None }))]
#[case::link(Node::Link(Link {url: Url::new("test".to_string()), values: Vec::new(), title: None, position: None }),
       "test".to_string(),
       Node::Link(Link{url: Url::new("test".to_string()), values: Vec::new(), title: None, position: None }))]
#[case::image(Node::Image(Image {alt: "test".to_string(), url: "test".to_string(), title: None, position: None }),
       "test".to_string(),
       Node::Image(Image{alt: "test".to_string(), url: "test".to_string(), title: None, position: None }))]
#[case::code(Node::Code(Code {value: "test".to_string(), lang: None, fence: true, meta: None, position: None }),
       "test".to_string(),
       Node::Code(Code{value: "test".to_string(), lang: None, fence: true, meta: None, position: None }))]
#[case::footnote_ref(Node::FootnoteRef(FootnoteRef {ident: "test".to_string(), label: None, position: None }),
       "test".to_string(),
       Node::FootnoteRef(FootnoteRef{ident: "test".to_string(), label: Some("test".to_string()), position: None }))]
#[case::footnote(Node::Footnote(Footnote {ident: "test".to_string(), values: Vec::new(), position: None }),
       "test".to_string(),
       Node::Footnote(Footnote{ident: "test".to_string(), values: Vec::new(), position: None }))]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec!["test".to_string().into()], position: None }),
       "test".to_string(),
       Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec!["test".to_string().into()], position: None }))]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 1, level: 1, checked: Some(true), ordered: false, values: vec!["test".to_string().into()], position: None }),
       "test".to_string(),
       Node::List(List{ marker: None,start: None, spread: false, index: 1, level: 1, checked: Some(true), ordered: false, values: vec!["test".to_string().into()], position: None }))]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 2, level: 2, checked: Some(false), ordered: false, values: vec!["test".to_string().into()], position: None }),
       "test".to_string(),
       Node::List(List{ marker: None,start: None, spread: false, index: 2, level: 2, checked: Some(false), ordered: false, values: vec!["test".to_string().into()], position: None }))]
#[case::code_inline(Node::CodeInline(CodeInline{ value: "t".into(), position: None }),
       "test".to_string(),
       Node::CodeInline(CodeInline{ value: "test".into(), position: None }))]
#[case::math_inline(Node::MathInline(MathInline{ value: "t".into(), position: None }),
       "test".to_string(),
       Node::MathInline(MathInline{ value: "test".into(), position: None }))]
#[case::toml(Node::Toml(Toml{ value: "t".to_string(), position: None }),
       "test".to_string(),
       Node::Toml(Toml{ value: "test".to_string(), position: None }))]
#[case::yaml(Node::Yaml(Yaml{ value: "t".to_string(), position: None }),
       "test".to_string(),
       Node::Yaml(Yaml{ value: "test".to_string(), position: None }))]
#[case::html(Node::Html(Html{ value: "t".to_string(), position: None }),
       "test".to_string(),
       Node::Html(Html{ value: "test".to_string(), position: None }))]
#[case::table_row(Node::TableRow(TableRow{ values: vec![
                    Node::TableCell(TableCell{values: vec!["test1".to_string().into()], row:0, column:1,  position: None}),
                    Node::TableCell(TableCell{values: vec!["test2".to_string().into()], row:0, column:2,  position: None})
                ]
                , position: None }),
       "test3,test4".to_string(),
       Node::TableRow(TableRow{ values: vec![
                    Node::TableCell(TableCell{values: vec!["test3".to_string().into()], row:0, column:1, position: None}),
                    Node::TableCell(TableCell{values: vec!["test4".to_string().into()], row:0, column:2, position: None})
                ]
                , position: None }))]
#[case::table_cell(Node::TableCell(TableCell{values: vec!["test1".to_string().into()], row:0, column:1, position: None}),
        "test2".to_string(),
        Node::TableCell(TableCell{values: vec!["test2".to_string().into()], row:0, column:1, position: None}),)]
#[case::link_ref(Node::LinkRef(LinkRef{ident: "test2".to_string(), values: vec![attr_keys::VALUE.to_string().into()], label: Some("test2".to_string()), position: None}),
        "test2".to_string(),
        Node::LinkRef(LinkRef{ident: "test2".to_string(), values: vec![attr_keys::VALUE.to_string().into()], label: Some("test2".to_string()), position: None}),)]
#[case::image_ref(Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "test1".to_string(), label: None, position: None}),
        "test2".to_string(),
        Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "test2".to_string(), label: Some("test2".to_string()), position: None}),)]
#[case::definition(Node::Definition(Definition{ url: Url::new(attr_keys::URL.to_string()), title: None, ident: "test1".to_string(), label: None, position: None}),
        "test2".to_string(),
        Node::Definition(Definition{url: Url::new("test2".to_string()), title: None, ident: "test1".to_string(), label: None, position: None}),)]
#[case::break_(Node::Break(Break{ position: None}),
        "test".to_string(),
        Node::Break(Break{position: None}))]
#[case::horizontal_rule(Node::HorizontalRule(HorizontalRule{ marker: None, position: None}),
        "test".to_string(),
        Node::HorizontalRule(HorizontalRule{ marker: None,position: None}))]
#[case::mdx_flow_expression(Node::MdxFlowExpression(MdxFlowExpression{value: "test".into(), position: None}),
       "updated".to_string(),
       Node::MdxFlowExpression(MdxFlowExpression{value: "updated".into(), position: None}))]
#[case::mdx_text_expression(Node::MdxTextExpression(MdxTextExpression{value: "test".into(), position: None}),
       "updated".to_string(),
       Node::MdxTextExpression(MdxTextExpression{value: "updated".into(), position: None}))]
#[case::mdx_js_esm(Node::MdxJsEsm(MdxJsEsm{value: "test".into(), position: None}),
       "updated".to_string(),
       Node::MdxJsEsm(MdxJsEsm{value: "updated".into(), position: None}))]
#[case(Node::MdxJsxFlowElement(MdxJsxFlowElement{
        name: Some("div".to_string()),
        attributes: Vec::new(),
        children: vec!["test".to_string().into()],
        position: None
    }),
    "updated".to_string(),
    Node::MdxJsxFlowElement(MdxJsxFlowElement{
        name: Some("div".to_string()),
        attributes: Vec::new(),
        children: vec!["updated".to_string().into()],
        position: None
    }))]
#[case::mdx_jsx_text_element(Node::MdxJsxTextElement(MdxJsxTextElement{
        name: Some("span".into()),
        attributes: Vec::new(),
        children: vec!["test".to_string().into()],
        position: None
    }),
    "updated".to_string(),
    Node::MdxJsxTextElement(MdxJsxTextElement{
        name: Some("span".into()),
        attributes: Vec::new(),
        children: vec!["updated".to_string().into()],
        position: None
    }))]
#[case(Node::Math(Math{ value: "x^2".to_string(), position: None }),
       "test".to_string(),
       Node::Math(Math{ value: "test".to_string(), position: None }))]
fn test_with_value(#[case] node: Node, #[case] input: String, #[case] expected: Node) {
    assert_eq!(node.clone().with_value(input.as_str()), expected);
    assert_eq!(node.into_with_value(input.as_str()), expected);
}

#[rstest]
#[case(Node::Blockquote(Blockquote{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}),
    "new",
    0,
    Node::Blockquote(Blockquote{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None}),
        Node::Text(Text{value: "second".to_string(), position: None})
    ], position: None}))]
#[case(Node::Blockquote(Blockquote{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}),
    "new",
    1,
    Node::Blockquote(Blockquote{values: vec![
        Node::Text(Text{value: "first".to_string(), position: None}),
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None}))]
#[case(Node::Delete(Delete{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}),
    "new",
    0,
    Node::Delete(Delete{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None}),
        Node::Text(Text{value: "second".to_string(), position: None})
    ], position: None}))]
#[case(Node::Emphasis(Emphasis{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}),
    "new",
    1,
    Node::Emphasis(Emphasis{values: vec![
        Node::Text(Text{value: "first".to_string(), position: None}),
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None}))]
#[case(Node::Strong(Strong{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}),
    "new",
    0,
    Node::Strong(Strong{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None}),
        Node::Text(Text{value: "second".to_string(), position: None})
    ], position: None}))]
#[case(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}),
    "new",
    1,
    Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![
        Node::Text(Text{value: "first".to_string(), position: None}),
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None}))]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}),
    "new",
    0,
    Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false,  values: vec![
        Node::Text(Text{value: "new".to_string(), position: None}),
        Node::Text(Text{value: "second".to_string(), position: None})
    ], position: None}))]
#[case(Node::TableCell(TableCell{column: 0, row: 0, values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}),
    "new",
    1,
    Node::TableCell(TableCell{column: 0, row: 0, values: vec![
        Node::Text(Text{value: "first".to_string(), position: None}),
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None}))]
#[case(Node::Text(Text{value: "plain text".to_string(), position: None}),
    "new",
    0,
    Node::Text(Text{value: "plain text".to_string(), position: None}))]
#[case(Node::Code(Code{value: "code".to_string(), lang: None, fence: true, meta: None, position: None}),
    "new",
    0,
    Node::Code(Code{value: "code".to_string(), lang: None, fence: true, meta: None, position: None}))]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: Some(true), ordered: false, values: vec![
    Node::Text(Text{value: "first".to_string(), position: None})
], position: None}),
    "new",
    0,
    Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: Some(true), ordered: false, values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None}))]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: None, ordered: false, values: vec![
    Node::Text(Text{value: "first".to_string(), position: None})
], position: None}),
    "new",
    2,
    Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: None, ordered: false, values: vec![
        Node::Text(Text{value: "first".to_string(), position: None})
    ], position: None}))]
#[case::link_ref(Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![
        Node::Text(Text{value: "first".to_string(), position: None}),
        Node::Text(Text{value: "second".to_string(), position: None})
    ], label: None, position: None}), "new", 0, Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![
        Node::Text(Text{value: "new".to_string(), position: None}),
        Node::Text(Text{value: "second".to_string(), position: None})
    ], label: None, position: None}))]
#[case(Node::MdxJsxFlowElement(MdxJsxFlowElement{
        name: Some("div".to_string()),
        attributes: Vec::new(),
        children: vec![
            Node::Text(Text{value: "first".to_string(), position: None}),
            Node::Text(Text{value: "second".to_string(), position: None})
        ],
        position: None
    }),
    "new",
    0,
    Node::MdxJsxFlowElement(MdxJsxFlowElement{
        name: Some("div".to_string()),
        attributes: Vec::new(),
        children: vec![
            Node::Text(Text{value: "new".to_string(), position: None}),
            Node::Text(Text{value: "second".to_string(), position: None})
        ],
        position: None
    }))]
#[case(Node::MdxJsxFlowElement(MdxJsxFlowElement{
        name: Some("div".to_string()),
        attributes: Vec::new(),
        children: vec![
            Node::Text(Text{value: "first".to_string(), position: None}),
            Node::Text(Text{value: "second".to_string(), position: None})
        ],
        position: None
    }),
    "new",
    1,
    Node::MdxJsxFlowElement(MdxJsxFlowElement{
        name: Some("div".to_string()),
        attributes: Vec::new(),
        children: vec![
            Node::Text(Text{value: "first".to_string(), position: None}),
            Node::Text(Text{value: "new".to_string(), position: None})
        ],
        position: None
    }))]
#[case(Node::MdxJsxTextElement(MdxJsxTextElement{
        name: Some("span".into()),
        attributes: Vec::new(),
        children: vec![
            Node::Text(Text{value: "first".to_string(), position: None}),
            Node::Text(Text{value: "second".to_string(), position: None})
        ],
        position: None
    }),
    "new",
    0,
    Node::MdxJsxTextElement(MdxJsxTextElement{
        name: Some("span".into()),
        attributes: Vec::new(),
        children: vec![
            Node::Text(Text{value: "new".to_string(), position: None}),
            Node::Text(Text{value: "second".to_string(), position: None})
        ],
        position: None
    }))]
#[case(Node::MdxJsxTextElement(MdxJsxTextElement{
        name: Some("span".into()),
        attributes: Vec::new(),
        children: vec![
            Node::Text(Text{value: "first".to_string(), position: None}),
            Node::Text(Text{value: "second".to_string(), position: None})
        ],
        position: None
    }),
    "new",
    1,
    Node::MdxJsxTextElement(MdxJsxTextElement{
        name: Some("span".into()),
        attributes: Vec::new(),
        children: vec![
            Node::Text(Text{value: "first".to_string(), position: None}),
            Node::Text(Text{value: "new".to_string(), position: None})
        ],
        position: None
    }))]
fn test_with_children_value(#[case] node: Node, #[case] value: &str, #[case] index: usize, #[case] expected: Node) {
    assert_eq!(node.clone().with_children_value(value, index), expected);
    assert_eq!(node.into_with_children_value(value, index), expected);
}

#[rstest]
#[case(Node::Text(Text{value: "test".to_string(), position: None }),
       "test".to_string())]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 2, checked: None, ordered: false, values: vec!["test".to_string().into()], position: None}),
       "    - test".to_string())]
fn test_display(#[case] node: Node, #[case] expected: String) {
    assert_eq!(node.to_string_with(&RenderOptions::default()), expected);
}

#[rstest]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), true)]
#[case(Node::CodeInline(CodeInline{value: "test".into(), position: None}), false)]
#[case(Node::MathInline(MathInline{value: "test".into(), position: None}), false)]
fn test_is_text(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_text(), expected);
}

#[rstest]
#[case(Node::CodeInline(CodeInline{value: "test".into(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_inline_code(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_inline_code(), expected);
}

#[rstest]
#[case(Node::MathInline(MathInline{value: "test".into(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_inline_math(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_inline_math(), expected);
}

#[rstest]
#[case(Node::Strong(Strong{values: vec!["test".to_string().into()], position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_strong(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_strong(), expected);
}

#[rstest]
#[case(Node::Delete(Delete{values: vec!["test".to_string().into()], position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_delete(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_delete(), expected);
}

#[rstest]
#[case(Node::Link(Link{url: Url::new("test".to_string()), values: Vec::new(), title: None, position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_link(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_link(), expected);
}

#[rstest]
#[case(Node::LinkRef(LinkRef{ident: "test".to_string(), values: Vec::new(), label: None, position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_link_ref(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_link_ref(), expected);
}

#[rstest]
#[case(Node::Image(Image{alt: attr_keys::ALT.to_string(), url: "test".to_string(), title: None, position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_image(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_image(), expected);
}

#[rstest]
#[case(Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "test".to_string(), label: None, position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_image_ref(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_image_ref(), expected);
}

#[rstest]
#[case(Node::Code(Code{value: "code".to_string(), lang: Some("rust".to_string()), fence: true, meta: None, position: None}), true, Some("rust".into()))]
#[case(Node::Code(Code{value: "code".to_string(), lang: Some("rust".to_string()), fence: true, meta: None, position: None}), false, Some("python".into()))]
#[case(Node::Code(Code{value: "code".to_string(), lang: None, fence: true, meta: None, position: None}), true, None)]
#[case(Node::Code(Code{value: "code".to_string(), lang: None, fence: false, meta: None, position: None}), true, None)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false, None)]
fn test_is_code(#[case] node: Node, #[case] expected: bool, #[case] lang: Option<SmolStr>) {
    assert_eq!(node.is_code(lang), expected);
}

#[rstest]
#[case(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["test".to_string().into()], position: None}), true, Some(1))]
#[case(Node::Heading(Heading{depth: HeadingDepth::H2, values: vec!["test".to_string().into()], position: None}), false, Some(1))]
#[case(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["test".to_string().into()], position: None}), true, None)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false, None)]
fn test_is_heading(#[case] node: Node, #[case] expected: bool, #[case] depth: Option<u8>) {
    assert_eq!(node.is_heading(depth), expected);
}

#[rstest]
#[case(Node::HorizontalRule(HorizontalRule{ marker: None,position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_horizontal_rule(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_horizontal_rule(), expected);
}

#[rstest]
#[case(Node::Blockquote(Blockquote{values: vec!["test".to_string().into()], position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_blockquote(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_blockquote(), expected);
}

#[cfg(feature = "wikilink")]
#[rstest]
#[case(Node::WikiLink(WikiLink{target: "target".to_string(), text: None, position: None}), true)]
#[case(Node::WikiLink(WikiLink{target: "Three laws of motion".to_string(), text: Some("Newton".to_string()), position: None}), true)]
#[case(Node::Link(Link{url: Url::new("https://example.com".to_string()), values: Vec::new(), title: None, position: None}), false)]
#[case(Node::Link(Link{url: Url::new("relative.md".to_string()), values: Vec::new(), title: None, position: None}), false)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
#[case(Node::Text(Text{value: "[[target]]".to_string(), position: None}), false)]
fn test_is_wikilink(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_wikilink(), expected);
}

#[cfg(feature = "wikilink")]
#[rstest]
#[case(Node::WikiLink(WikiLink{target: "target".to_string(), text: None, position: None}), true)]
#[case(Node::WikiLink(WikiLink{target: "Three laws of motion".to_string(), text: Some("Newton".to_string()), position: None}), true)]
#[case(Node::Link(Link{url: Url::new("https://example.com".to_string()), values: Vec::new(), title: None, position: None}), true)]
#[case(Node::Link(Link{url: Url::new("relative.md".to_string()), values: Vec::new(), title: None, position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_link_includes_wikilink(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_link(), expected);
}

#[cfg(feature = "wikilink")]
#[rstest]
#[case(Node::WikiLink(WikiLink{target: "target".to_string(), text: None, position: None}), "url", Some(AttrValue::String("target".to_string())))]
#[case(Node::WikiLink(WikiLink{target: "target".to_string(), text: None, position: None}), "value", Some(AttrValue::String("target".to_string())))]
#[case(Node::WikiLink(WikiLink{target: "target".to_string(), text: Some("display".to_string()), position: None}), "url", Some(AttrValue::String("target".to_string())))]
#[case(Node::WikiLink(WikiLink{target: "target".to_string(), text: Some("display".to_string()), position: None}), "value", Some(AttrValue::String("display".to_string())))]
#[case(Node::WikiLink(WikiLink{target: "target".to_string(), text: None, position: None}), "title", None)]
fn test_wikilink_attr(#[case] node: Node, #[case] attr: &str, #[case] expected: Option<AttrValue>) {
    assert_eq!(node.attr(attr), expected);
}

#[cfg(feature = "wikilink")]
#[rstest]
// no display text: with_value sets target
#[case(
    Node::WikiLink(WikiLink{target: "target".to_string(), text: None, position: None}),
    "new-target",
    Node::WikiLink(WikiLink{target: "new-target".to_string(), text: None, position: None})
)]
// with display text: with_value sets text, not target
#[case(
    Node::WikiLink(WikiLink{target: "page".to_string(), text: Some("Display Text".to_string()), position: None}),
    "DISPLAY TEXT",
    Node::WikiLink(WikiLink{target: "page".to_string(), text: Some("DISPLAY TEXT".to_string()), position: None})
)]
fn test_wikilink_with_value(#[case] node: Node, #[case] value: &str, #[case] expected: Node) {
    assert_eq!(node.clone().with_value(value), expected);
    assert_eq!(node.into_with_value(value), expected);
}

#[rstest]
#[case(Node::Html(Html{value: "<div>test</div>".to_string(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_html(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_html(), expected);
}

#[rstest]
#[case(Node::node_values(
       &Node::Strong(Strong{values: vec!["test".to_string().into()], position: None})),
       vec!["test".to_string().into()])]
#[case(Node::node_values(
       &Node::Text(Text{value: "test".to_string(), position: None})),
       vec!["test".to_string().into()])]
#[case(Node::node_values(
       &Node::Blockquote(Blockquote{values: vec!["test".to_string().into()], position: None})),
       vec!["test".to_string().into()])]
#[case(Node::node_values(
       &Node::Delete(Delete{values: vec!["test".to_string().into()], position: None})),
       vec!["test".to_string().into()])]
#[case(Node::node_values(
       &Node::Emphasis(Emphasis{values: vec!["test".to_string().into()], position: None})),
       vec!["test".to_string().into()])]
#[case(Node::node_values(
       &Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["test".to_string().into()], position: None})),
       vec!["test".to_string().into()])]
#[case(Node::node_values(
       &Node::List(List{ marker: None,values: vec!["test".to_string().into()], ordered: false, level: 1, checked: Some(false), index: 0, start: None, spread: false, position: None})),
       vec!["test".to_string().into()])]
fn test_node_value(#[case] actual: Vec<Node>, #[case] expected: Vec<Node>) {
    assert_eq!(actual, expected);
}

#[rstest]
#[case(Node::Footnote(Footnote{ident: "test".to_string(), values: Vec::new(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_footnote(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_footnote(), expected);
}

#[rstest]
#[case(Node::FootnoteRef(FootnoteRef{ident: "test".to_string(), label: None, position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_footnote_ref(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_footnote_ref(), expected);
}

#[rstest]
#[case(Node::Math(Math{value: "x^2".to_string(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_math(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_math(), expected);
}

#[rstest]
#[case(Node::Break(Break{position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_break(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_break(), expected);
}

#[rstest]
#[case(Node::Yaml(Yaml{value: "key: value".to_string(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_yaml(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_yaml(), expected);
}

#[rstest]
#[case(Node::Toml(Toml{value: "key = \"value\"".to_string(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_toml(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_toml(), expected);
}

#[rstest]
#[case(Node::Definition(Definition{ident: attr_keys::IDENT.to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: None, position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_definition(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_definition(), expected);
}

#[rstest]
#[case(Node::Emphasis(Emphasis{values: vec!["test".to_string().into()], position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_emphasis(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_emphasis(), expected);
}

#[rstest]
#[case(Node::MdxFlowExpression(MdxFlowExpression{value: "test".into(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_mdx_flow_expression(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_mdx_flow_expression(), expected);
}

#[rstest]
#[case(Node::MdxTextExpression(MdxTextExpression{value: "test".into(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_mdx_text_expression(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_mdx_text_expression(), expected);
}

#[rstest]
#[case(Node::MdxJsxFlowElement(MdxJsxFlowElement{name: None, attributes: Vec::new(), children: Vec::new(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_mdx_jsx_flow_element(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_mdx_jsx_flow_element(), expected);
}

#[rstest]
#[case(Node::MdxJsxTextElement(MdxJsxTextElement{name: None, attributes: Vec::new(), children: Vec::new(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_mdx_jsx_text_element(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_mdx_jsx_text_element(), expected);
}

#[rstest]
#[case(Node::MdxJsEsm(MdxJsEsm{value: "test".into(), position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_msx_js_esm(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_mdx_js_esm(), expected);
}

#[rstest]
#[case::text(Node::Text(Text{value: "test".to_string(), position: None }), RenderOptions::default(), "test")]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 2, checked: None, ordered: false, values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "    - test")]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: None, ordered: false, values: vec!["test".to_string().into()], position: None}), RenderOptions { list_style: Some(ListStyle::Plus), ..Default::default() }, "  + test")]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: Some(true), ordered: false, values: vec!["test".to_string().into()], position: None}), RenderOptions { list_style: Some(ListStyle::Star), ..Default::default() }, "  * [x] test")]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: Some(false), ordered: false, values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "  - [ ] test")]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: None, ordered: true, values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "  1. test")]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: Some(false), ordered: true, values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "  1. [ ] test")]
#[case::table_row(Node::TableRow(TableRow{values: vec![Node::TableCell(TableCell{column: 0, row: 0, values: vec!["test".to_string().into()], position: None})], position: None}), RenderOptions::default(), "|test|")]
#[case::table_row(Node::TableRow(TableRow{values: vec![Node::TableCell(TableCell{column: 0, row: 0, values: vec!["test".to_string().into()], position: None})], position: None}), RenderOptions::default(), "|test|")]
#[case::table_cell(Node::TableCell(TableCell{column: 0, row: 0, values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "test")]
#[case::table_cell_break(Node::TableCell(TableCell{column: 0, row: 0, values: vec!["a".to_string().into(), Node::Break(Break{position: None}), "b".to_string().into()], position: None}), RenderOptions::default(), "a<br>b")]
#[case::table_cell_code_newline(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::CodeInline(CodeInline{value: "a\nb".into(), position: None})], position: None}), RenderOptions::default(), "`a b`")]
#[case::table_cell_two_breaks(Node::TableCell(TableCell{column: 0, row: 0, values: vec!["a".to_string().into(), Node::Break(Break{position: None}), Node::Break(Break{position: None}), "b".to_string().into()], position: None}), RenderOptions::default(), "a<br><br>b")]
#[case::table_cell_literal_backslash_no_newline(Node::TableCell(TableCell{column: 0, row: 0, values: vec!["a\\".to_string().into(), "b".to_string().into()], position: None}), RenderOptions::default(), "a\\b")]
#[case::table_cell_escaped_backslash_newline(Node::TableCell(TableCell{column: 0, row: 0, values: vec!["a\\\\\n".to_string().into(), "b".to_string().into()], position: None}), RenderOptions::default(), "a\\\\<br>b")]
#[case::table_cell_break_in_strong(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Strong(Strong{values: vec!["a".to_string().into(), Node::Break(Break{position: None}), "b".to_string().into()], position: None})], position: None}), RenderOptions::default(), "**a<br>b**")]
#[case::table_cell_code_crlf(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::CodeInline(CodeInline{value: "a\r\nb".into(), position: None})], position: None}), RenderOptions::default(), "`a b`")]
#[case::table_cell_code_cr(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::CodeInline(CodeInline{value: "a\rb".into(), position: None})], position: None}), RenderOptions::default(), "`a b`")]
#[case::table_cell_math_newline(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::MathInline(MathInline{value: "a\nb".into(), position: None})], position: None}), RenderOptions::default(), "$a b$")]
#[case::table_cell_code_in_strong(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Strong(Strong{values: vec![Node::CodeInline(CodeInline{value: "a\nb".into(), position: None})], position: None})], position: None}), RenderOptions::default(), "**`a b`**")]
#[case::table_cell_code_in_emphasis(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Emphasis(Emphasis{values: vec![Node::CodeInline(CodeInline{value: "a\nb".into(), position: None})], position: None})], position: None}), RenderOptions::default(), "*`a b`*")]
#[case::table_cell_code_pipe(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::CodeInline(CodeInline{value: "a|b".into(), position: None})], position: None}), RenderOptions::default(), "`a\\|b`")]
#[case::table_cell_code_then_break(Node::TableCell(TableCell{column: 0, row: 0, values: vec!["x".to_string().into(), Node::CodeInline(CodeInline{value: "a\nb".into(), position: None}), Node::Break(Break{position: None}), "y".to_string().into()], position: None}), RenderOptions::default(), "x`a b`<br>y")]
#[case::table_cell_newline(Node::TableCell(TableCell{column: 0, row: 0, values: vec!["a\nb\r\nc".to_string().into()], position: None}), RenderOptions::default(), "a<br>b<br>c")]
#[case::table_cell(Node::TableCell(TableCell{column: 0, row: 0, values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "test")]
#[case::table_align(Node::TableAlign(TableAlign{align: vec![TableAlignKind::Left, TableAlignKind::Right, TableAlignKind::Center, TableAlignKind::None], position: None}), RenderOptions::default(), "|:---|---:|:---:|---|")]
#[case::block_quote(Node::Blockquote(Blockquote{values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "> test")]
#[case::block_quote(Node::Blockquote(Blockquote{values: vec!["test\ntest2".to_string().into()], position: None}), RenderOptions::default(), "> test\n> test2")]
#[case::code(Node::Code(Code{value: "code".to_string(), lang: Some("rust".to_string()), fence: true, meta: None, position: None}), RenderOptions::default(), "```rust\ncode\n```")]
#[case::code(Node::Code(Code{value: "code".to_string(), lang: None, fence: true, meta: None, position: None}), RenderOptions::default(), "```\ncode\n```")]
#[case::code(Node::Code(Code{value: "code".to_string(), lang: None, fence: false, meta: None, position: None}), RenderOptions::default(), "    code")]
#[case::code(Node::Code(Code{value: "code".to_string(), lang: Some("rust".to_string()), fence: true, meta: Some("meta".to_string()), position: None}), RenderOptions::default(), "```rust meta\ncode\n```")]
#[case::code_empty_body_no_blank_line(Node::Code(Code{value: "".to_string(), lang: None, fence: true, meta: None, position: None}), RenderOptions::default(), "```\n```")]
#[case::code_fence_escalates_past_backtick_run_in_body(Node::Code(Code{value: "aaa\n```".to_string(), lang: None, fence: true, meta: None, position: None}), RenderOptions::default(), "````\naaa\n```\n````")]
#[case::code_fence_escalates_past_longer_run(Node::Code(Code{value: "`````".to_string(), lang: None, fence: true, meta: None, position: None}), RenderOptions::default(), "``````\n`````\n``````")]
#[case::definition(Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: Some(attr_keys::LABEL.to_string()), position: None}), RenderOptions::default(), "[label]: url")]
#[case::definition(Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: Some(Title::new(attr_keys::TITLE.to_string())), label: Some(attr_keys::LABEL.to_string()), position: None}), RenderOptions::default(), "[label]: url \"title\"")]
#[case::definition(Node::Definition(Definition{ident: "id".to_string(), url: Url::new("".to_string()), title: None, label: Some(attr_keys::LABEL.to_string()), position: None}), RenderOptions::default(), "[label]: <>")]
#[case::delete(Node::Delete(Delete{values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "~~test~~")]
#[case::emphasis(Node::Emphasis(Emphasis{values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "*test*")]
#[case::emphasis_nested_in_emphasis_alternates_delimiter(Node::Emphasis(Emphasis{values: vec![Node::Emphasis(Emphasis{values: vec!["foo".to_string().into()], position: None})], position: None}), RenderOptions::default(), "_*foo*_")]
#[case::footnote(Node::Footnote(Footnote{ident: "id".to_string(), values: vec![attr_keys::LABEL.to_string().into()], position: None}), RenderOptions::default(), "[^id]: label")]
#[case::footnote_ref(Node::FootnoteRef(FootnoteRef{ident: attr_keys::LABEL.to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}), RenderOptions::default(), "[^label]")]
#[case::heading(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "# test")]
#[case::heading(Node::Heading(Heading{depth: HeadingDepth::H3, values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "### test")]
#[case::heading_multiline_h1_stays_setext(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["Foo\nBar".to_string().into()], position: None}), RenderOptions::default(), "Foo\nBar\n===")]
#[case::heading_multiline_h2_stays_setext(Node::Heading(Heading{depth: HeadingDepth::H2, values: vec!["Foo\nBar".to_string().into()], position: None}), RenderOptions::default(), "Foo\nBar\n---")]
#[case::heading_multiline_h3_joins_with_space(Node::Heading(Heading{depth: HeadingDepth::H3, values: vec!["Foo\nBar".to_string().into()], position: None}), RenderOptions::default(), "### Foo Bar")]
#[case::heading_trailing_hash_escaped(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["foo #".to_string().into()], position: None}), RenderOptions::default(), "# foo \\#")]
#[case::heading_trailing_hash_run_escaped(Node::Heading(Heading{depth: HeadingDepth::H3, values: vec!["foo ###".to_string().into()], position: None}), RenderOptions::default(), "### foo \\###")]
#[case::heading_trailing_hash_after_multibyte_char(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["foo あ#".to_string().into()], position: None}), RenderOptions::default(), "# foo あ\\#")]
#[case::heading_no_trailing_hash_unaffected(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["foo bar".to_string().into()], position: None}), RenderOptions::default(), "# foo bar")]
#[case::html(Node::Html(Html{value: "<div>test</div>".to_string(), position: None}), RenderOptions::default(), "<div>test</div>")]
#[case::image(Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: None, position: None}), RenderOptions::default(), "![alt](url)")]
#[case::image(Node::Image(Image{alt: attr_keys::ALT.to_string(), url: "url with space".to_string(), title: Some(attr_keys::TITLE.to_string()), position: None}), RenderOptions::default(), "![alt](<url with space> \"title\")")]
#[case::image_ref(Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: Some("id".to_string()), position: None}), RenderOptions::default(), "![alt][id]")]
#[case::image_ref(Node::ImageRef(ImageRef{alt: "id".to_string(), ident: "id".to_string(), label: Some("id".to_string()), position: None}), RenderOptions::default(), "![id]")]
#[case::code_inline(Node::CodeInline(CodeInline{value: "code".into(), position: None}), RenderOptions::default(), "`code`")]
#[case::code_inline_trailing_backtick_needs_padding(Node::CodeInline(CodeInline{value: "\\[\\`".into(), position: None}), RenderOptions::default(), "`` \\[\\` ``")]
#[case::code_inline_internal_backtick_escalates_fence(Node::CodeInline(CodeInline{value: "foo ` bar".into(), position: None}), RenderOptions::default(), "``foo ` bar``")]
#[case::code_inline_both_ends_backtick_needs_longer_padded_fence(Node::CodeInline(CodeInline{value: "``".into(), position: None}), RenderOptions::default(), "``` `` ```")]
#[case::code_inline_leading_trailing_space_needs_extra_padding(Node::CodeInline(CodeInline{value: " `` ".into(), position: None}), RenderOptions::default(), "```  ``  ```")]
#[case::code_inline_all_spaces_untouched(Node::CodeInline(CodeInline{value: "   ".into(), position: None}), RenderOptions::default(), "`   `")]
#[case::math_inline(Node::MathInline(MathInline{value: "x^2".into(), position: None}), RenderOptions::default(), "$x^2$")]
#[case::link(Node::Link(Link{url: Url::new(attr_keys::URL.to_string()), title: Some(Title::new(attr_keys::TITLE.to_string())), values: vec![attr_keys::VALUE.to_string().into()], position: None}), RenderOptions::default(), "[value](url \"title\")")]
#[case::link(Node::Link(Link{url: Url::new("".to_string()), title: None, values: vec![attr_keys::VALUE.to_string().into()], position: None}), RenderOptions::default(), "[value](<>)")]
#[case::link(Node::Link(Link{url: Url::new(attr_keys::URL.to_string()), title: None, values: vec![attr_keys::VALUE.to_string().into()], position: None}), RenderOptions::default(), "[value](url)")]
#[case::link_ref(Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec!["id".to_string().into()], label: Some("id".to_string()), position: None}), RenderOptions::default(), "[id]")]
#[case::link_ref(Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec!["open".to_string().into()], label: Some("id".to_string()), position: None}), RenderOptions::default(), "[open][id]")]
#[case::math(Node::Math(Math{value: "x^2".to_string(), position: None}), RenderOptions::default(), "$$\nx^2\n$$")]
#[case::strong(Node::Strong(Strong{values: vec!["test".to_string().into()], position: None}), RenderOptions::default(), "**test**")]
#[case::yaml(Node::Yaml(Yaml{value: "key: value".to_string(), position: None}), RenderOptions::default(), "---\nkey: value\n---")]
#[case::toml(Node::Toml(Toml{value: "key = \"value\"".to_string(), position: None}), RenderOptions::default(), "+++\nkey = \"value\"\n+++")]
#[case::break_(Node::Break(Break{position: None}), RenderOptions::default(), "\\\n")]
#[case::horizontal_rule(Node::HorizontalRule(HorizontalRule{ marker: None,position: None}), RenderOptions::default(), "***")]
#[case::mdx_jsx_flow_element(Node::MdxJsxFlowElement(MdxJsxFlowElement{
    name: Some("div".to_string()),
    attributes: vec![
        MdxAttributeContent::Property(MdxJsxAttribute {
            name: "className".into(),
            value: Some(MdxAttributeValue::Literal("container".into()))
        })
    ],
    children: vec![
        "content".to_string().into()
    ],
    position: None
}), RenderOptions::default(), "<div className=\"container\">\n  content\n</div>")]
#[case::mdx_jsx_flow_element(Node::MdxJsxFlowElement(MdxJsxFlowElement{
    name: Some("div".to_string()),
    attributes: vec![
        MdxAttributeContent::Property(MdxJsxAttribute {
            name: "className".into(),
            value: Some(MdxAttributeValue::Literal("container".into()))
        })
    ],
    children: Vec::new(),
    position: None
}), RenderOptions::default(), "<div className=\"container\" />")]
#[case::mdx_jsx_flow_element(Node::MdxJsxFlowElement(MdxJsxFlowElement{
    name: Some("div".to_string()),
    attributes: Vec::new(),
    children: Vec::new(),
    position: None
}), RenderOptions::default(), "<div />")]
#[case::mdx_jsx_text_element(Node::MdxJsxTextElement(MdxJsxTextElement{
    name: Some("span".into()),
    attributes: vec![
        MdxAttributeContent::Expression("...props".into())
    ],
    children: vec![
        "inline".to_string().into()
    ],
    position: None
}), RenderOptions::default(), "<span {...props}>inline</span>")]
#[case::mdx_jsx_text_element(Node::MdxJsxTextElement(MdxJsxTextElement{
    name: Some("span".into()),
    attributes: vec![
        MdxAttributeContent::Expression("...props".into())
    ],
    children: vec![
    ],
    position: None
}), RenderOptions::default(), "<span {...props} />")]
#[case::mdx_jsx_text_element(Node::MdxJsxTextElement(MdxJsxTextElement{
    name: Some("span".into()),
    attributes: vec![
    ],
    children: vec![
    ],
    position: None
}), RenderOptions::default(), "<span />")]
#[case(Node::MdxTextExpression(MdxTextExpression{
    value: "count + 1".into(),
    position: None,
}), RenderOptions::default(), "{count + 1}")]
#[case(Node::MdxJsEsm(MdxJsEsm{
    value: "import React from 'react'".into(),
    position: None,
}), RenderOptions::default(), "import React from 'react'")]
#[case::fragment_empty(Node::Fragment(Fragment{values: vec![]}), RenderOptions::default(), "")]
#[case::fragment_single(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "hello".to_string(), position: None})
]}), RenderOptions::default(), "hello")]
#[case::fragment_multiple(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "hello".to_string(), position: None}),
    Node::Text(Text{value: "world".to_string(), position: None})
]}), RenderOptions::default(), "hello\nworld")]
#[case::fragment_filters_empty(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "hello".to_string(), position: None}),
    Node::Empty,
    Node::Text(Text{value: "world".to_string(), position: None})
]}), RenderOptions::default(), "hello\nworld")]
#[case::fragment_all_empty(Node::Fragment(Fragment{values: vec![
    Node::Empty,
    Node::Empty,
]}), RenderOptions::default(), "")]
#[case::fragment_empty_text_as_blank_line(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "hello".to_string(), position: None}),
    Node::Text(Text{value: "".to_string(), position: None}),
    Node::Text(Text{value: "world".to_string(), position: None})
]}), RenderOptions::default(), "hello\n\nworld")]
#[case::fragment_filters_nested_empty_fragment(Node::Fragment(Fragment{values: vec![
    Node::Fragment(Fragment{values: vec![Node::Empty, Node::Empty]}),
    Node::Text(Text{value: "hello".to_string(), position: None}),
    Node::Fragment(Fragment{values: vec![Node::Empty]}),
]}), RenderOptions::default(), "hello")]
#[case::fragment_filters_deeply_nested_empty_fragment(Node::Fragment(Fragment{values: vec![
    Node::Fragment(Fragment{values: vec![
        Node::Fragment(Fragment{values: vec![Node::Empty]}),
        Node::Empty,
    ]}),
    Node::Text(Text{value: "hello".to_string(), position: None}),
]}), RenderOptions::default(), "hello")]
#[case::fragment_keeps_nested_fragment_with_blank_line_text(Node::Fragment(Fragment{values: vec![
    Node::Fragment(Fragment{values: vec![Node::Empty]}),
    Node::Text(Text{value: "hello".to_string(), position: None}),
    Node::Fragment(Fragment{values: vec![Node::Text(Text{value: "".to_string(), position: None})]}),
    Node::Text(Text{value: "world".to_string(), position: None}),
]}), RenderOptions::default(), "hello\n\nworld")]
#[cfg_attr(feature = "wikilink", case::wikilink(Node::WikiLink(WikiLink{target: "target".to_string(), text: None, position: None}), RenderOptions::default(), "[[target]]"))]
#[cfg_attr(feature = "wikilink", case::wikilink_with_text(Node::WikiLink(WikiLink{target: "target".to_string(), text: Some("display text".to_string()), position: None}), RenderOptions::default(), "[[target|display text]]"))]
#[cfg_attr(feature = "wikilink", case::wikilink_same_text(Node::WikiLink(WikiLink{target: "target".to_string(), text: Some("target".to_string()), position: None}), RenderOptions::default(), "[[target]]"))]
fn test_to_string_with(#[case] node: Node, #[case] options: RenderOptions, #[case] expected: &str) {
    assert_eq!(node.to_string_with(&options), expected);
}

#[test]
fn test_node_partial_ord() {
    let node1 = Node::Text(Text {
        value: "test1".to_string(),
        position: Some(Position {
            start: Point { line: 1, column: 1 },
            end: Point { line: 1, column: 5 },
        }),
    });

    let node2 = Node::Text(Text {
        value: "test2".to_string(),
        position: Some(Position {
            start: Point { line: 1, column: 6 },
            end: Point { line: 1, column: 10 },
        }),
    });

    let node3 = Node::Text(Text {
        value: "test3".to_string(),
        position: Some(Position {
            start: Point { line: 2, column: 1 },
            end: Point { line: 2, column: 5 },
        }),
    });

    assert_eq!(node1.partial_cmp(&node2), Some(std::cmp::Ordering::Less));
    assert_eq!(node2.partial_cmp(&node1), Some(std::cmp::Ordering::Greater));

    assert_eq!(node1.partial_cmp(&node3), Some(std::cmp::Ordering::Less));
    assert_eq!(node3.partial_cmp(&node1), Some(std::cmp::Ordering::Greater));

    let node4 = Node::Text(Text {
        value: "test4".to_string(),
        position: None,
    });

    assert_eq!(node1.partial_cmp(&node4), Some(std::cmp::Ordering::Less));
    assert_eq!(node4.partial_cmp(&node1), Some(std::cmp::Ordering::Greater));

    let node5 = Node::Text(Text {
        value: "test5".to_string(),
        position: None,
    });

    assert_eq!(node4.partial_cmp(&node5), Some(std::cmp::Ordering::Equal));

    let node6 = Node::Code(Code {
        value: "code".to_string(),
        lang: None,
        fence: true,
        meta: None,
        position: None,
    });

    assert_eq!(node6.partial_cmp(&node4), Some(std::cmp::Ordering::Less));
    assert_eq!(node4.partial_cmp(&node6), Some(std::cmp::Ordering::Greater));
}

#[rstest]
#[case(Node::Blockquote(Blockquote{values: Vec::new(), position: None}), "blockquote")]
#[case(Node::Break(Break{position: None}), "break")]
#[case(Node::Definition(Definition{ident: "".to_string(), url: Url::new("".to_string()), title: None, label: None, position: None}), "definition")]
#[case(Node::Delete(Delete{values: Vec::new(), position: None}), "delete")]
#[case(Node::Heading(Heading{depth: HeadingDepth::H1, values: Vec::new(), position: None}), "h1")]
#[case(Node::Heading(Heading{depth: HeadingDepth::H2, values: Vec::new(), position: None}), "h2")]
#[case(Node::Heading(Heading{depth: HeadingDepth::H3, values: Vec::new(), position: None}), "h3")]
#[case(Node::Heading(Heading{depth: HeadingDepth::H4, values: Vec::new(), position: None}), "h4")]
#[case(Node::Heading(Heading{depth: HeadingDepth::H5, values: Vec::new(), position: None}), "h5")]
#[case(Node::Heading(Heading{depth: HeadingDepth::H6, values: Vec::new(), position: None}), "h6")]
#[case(Node::Emphasis(Emphasis{values: Vec::new(), position: None}), "emphasis")]
#[case(Node::Footnote(Footnote{ident: "".to_string(), values: Vec::new(), position: None}), "footnote")]
#[case(Node::FootnoteRef(FootnoteRef{ident: "".to_string(), label: None, position: None}), "footnoteref")]
#[case(Node::Html(Html{value: "".to_string(), position: None}), "html")]
#[case(Node::Yaml(Yaml{value: "".to_string(), position: None}), "yaml")]
#[case(Node::Toml(Toml{value: "".to_string(), position: None}), "toml")]
#[case(Node::Image(Image{alt: "".to_string(), url: "".to_string(), title: None, position: None}), "image")]
#[case(Node::ImageRef(ImageRef{alt: "".to_string(), ident: "".to_string(), label: None, position: None}), "image_ref")]
#[case(Node::CodeInline(CodeInline{value: "".into(), position: None}), "code_inline")]
#[case(Node::MathInline(MathInline{value: "".into(), position: None}), "math_inline")]
#[case(Node::Link(Link{url: Url::new("".to_string()), title: None, values: Vec::new(), position: None}), "link")]
#[case(Node::LinkRef(LinkRef{ident: "".to_string(), values: Vec::new(), label: None, position: None}), "link_ref")]
#[case(Node::Math(Math{value: "".to_string(), position: None}), "math")]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: Vec::new(), position: None}), "list")]
#[case(Node::TableAlign(TableAlign{align: Vec::new(), position: None}), "table_align")]
#[case(Node::TableRow(TableRow{values: Vec::new(), position: None}), "table_row")]
#[case(Node::TableCell(TableCell{column: 0, row: 0, values: Vec::new(), position: None}), "table_cell")]
#[case(Node::Code(Code{value: "".to_string(), lang: None, fence: true, meta: None, position: None}), "code")]
#[case(Node::Strong(Strong{values: Vec::new(), position: None}), "strong")]
#[case(Node::HorizontalRule(HorizontalRule{ marker: None,position: None}), "Horizontal_rule")]
#[case(Node::MdxFlowExpression(MdxFlowExpression{value: "".into(), position: None}), "mdx_flow_expression")]
#[case(Node::MdxJsxFlowElement(MdxJsxFlowElement{name: None, attributes: Vec::new(), children: Vec::new(), position: None}), "mdx_jsx_flow_element")]
#[case(Node::MdxJsxTextElement(MdxJsxTextElement{name: None, attributes: Vec::new(), children: Vec::new(), position: None}), "mdx_jsx_text_element")]
#[case(Node::MdxTextExpression(MdxTextExpression{value: "".into(), position: None}), "mdx_text_expression")]
#[case(Node::MdxJsEsm(MdxJsEsm{value: "".into(), position: None}), "mdx_js_esm")]
#[case(Node::Text(Text{value: "".to_string(), position: None}), "text")]
fn test_name(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.name(), expected);
}

#[rstest]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), "test")]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}), "test")]
#[case(Node::Blockquote(Blockquote{values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}), "test")]
#[case(Node::Delete(Delete{values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}), "test")]
#[case(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}), "test")]
#[case(Node::Emphasis(Emphasis{values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}), "test")]
#[case(Node::Footnote(Footnote{ident: "test".to_string(), values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}), "test")]
#[case(Node::FootnoteRef(FootnoteRef{ident: "test".to_string(), label: None, position: None}), "test")]
#[case(Node::Html(Html{value: "test".to_string(), position: None}), "test")]
#[case(Node::Yaml(Yaml{value: "test".to_string(), position: None}), "test")]
#[case(Node::Toml(Toml{value: "test".to_string(), position: None}), "test")]
#[case(Node::Image(Image{alt: attr_keys::ALT.to_string(), url: "test".to_string(), title: None, position: None}), "test")]
#[case(Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "test".to_string(), label: None, position: None}), "test")]
#[case(Node::CodeInline(CodeInline{value: "test".into(), position: None}), "test")]
#[case(Node::MathInline(MathInline{value: "test".into(), position: None}), "test")]
#[case(Node::Link(Link{url: Url::new("test".to_string()), title: None, values: Vec::new(), position: None}), "test")]
#[case(Node::LinkRef(LinkRef{ident: "test".to_string(), values: Vec::new(), label: None, position: None}), "test")]
#[case(Node::Math(Math{value: "test".to_string(), position: None}), "test")]
#[case(Node::Code(Code{value: "test".to_string(), lang: None, fence: true, meta: None, position: None}), "test")]
#[case(Node::Strong(Strong{values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}), "test")]
#[case(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}), "test")]
#[case(Node::TableRow(TableRow{values: vec![Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None})], position: None}), "test")]
#[case(Node::Break(Break{position: None}), "")]
#[case(Node::HorizontalRule(HorizontalRule{ marker: None,position: None}), "")]
#[case(Node::TableAlign(TableAlign{align: Vec::new(), position: None}), "")]
#[case(Node::MdxFlowExpression(MdxFlowExpression{value: "test".into(), position: None}), "test")]
#[case(Node::MdxTextExpression(MdxTextExpression{value: "test".into(), position: None}), "test")]
#[case(Node::MdxJsEsm(MdxJsEsm{value: "test".into(), position: None}), "test")]
#[case(Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some(attr_keys::NAME.to_string()), attributes: Vec::new(), children: vec![Node::Text(Text{value: "test".to_string(), position: None})],  position: None}), "test")]
#[case(Node::Definition(Definition{ident: "test".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: None, position: None}), attr_keys::URL)]
#[case(Node::Fragment(Fragment {values: vec![Node::Text(Text{value: "test".to_string(), position: None})]}), "test")]
fn test_value(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.value(), expected);
}

#[rstest]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), None)]
#[case(Node::Text(Text{value: "test".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Blockquote(Blockquote{values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Delete(Delete{values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Heading(Heading{depth: HeadingDepth::H1, values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Emphasis(Emphasis{values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Footnote(Footnote{ident: "".to_string(), values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::FootnoteRef(FootnoteRef{ident: "".to_string(), label: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Html(Html{value: "".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Yaml(Yaml{value: "".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Toml(Toml{value: "".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Image(Image{alt: "".to_string(), url: "".to_string(), title: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::ImageRef(ImageRef{alt: "".to_string(), ident: "".to_string(), label: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::CodeInline(CodeInline{value: "".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::MathInline(MathInline{value: "".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Link(Link{url: Url("".to_string()), title: None, values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::LinkRef(LinkRef{ident: "".to_string(), values: Vec::new(), label: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Math(Math{value: "".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Code(Code{value: "".to_string(), lang: None, fence: true, meta: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Strong(Strong{values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::TableCell(TableCell{column: 0, row: 0, values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::TableRow(TableRow{values: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::TableAlign(TableAlign{align: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Break(Break{position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::HorizontalRule(HorizontalRule{ marker: None,position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::MdxFlowExpression(MdxFlowExpression{value: "test".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::MdxTextExpression(MdxTextExpression{value: "test".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::MdxJsEsm(MdxJsEsm{value: "test".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some("div".to_string()), attributes: Vec::new(), children: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("span".into()), attributes: Vec::new(), children: Vec::new(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Definition(Definition{ident: "".to_string(), url: Url("".to_string()), title: None, label: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Fragment(Fragment{values: Vec::new()}), None)]
#[case(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "test1".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}),
    Node::Text(Text{value: "test2".to_string(), position: Some(Position{start: Point{line: 1, column: 6}, end: Point{line: 1, column: 10}})})
]}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}}))]
#[case(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "test".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}),
    Node::Text(Text{value: "test2".to_string(), position: None})
]}), Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}}))]
#[case(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "test".to_string(), position: None}),
    Node::Text(Text{value: "test2".to_string(), position: Some(Position{start: Point{line: 1, column: 6}, end: Point{line: 1, column: 10}})})
]}), Some(Position{start: Point{line: 1, column: 6}, end: Point{line: 1, column: 10}}))]
#[case(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "test2".to_string(), position: Some(Position{start: Point{line: 1, column: 6}, end: Point{line: 1, column: 10}})}),
    Node::Text(Text{value: "test".to_string(), position: None})
]}), Some(Position{start: Point{line: 1, column: 6}, end: Point{line: 1, column: 10}}))]
#[case(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "test".to_string(), position: None}),
    Node::Text(Text{value: "test2".to_string(), position: None})
]}), None)]
#[case(Node::Empty, None)]
fn test_position(#[case] node: Node, #[case] expected: Option<Position>) {
    assert_eq!(node.position(), expected);
}

#[rstest]
#[case(Node::Blockquote(Blockquote{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}), 0, Some(Node::Text(Text{value: "first".to_string(), position: None})))]
#[case(Node::Blockquote(Blockquote{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}), 1, Some(Node::Text(Text{value: "second".to_string(), position: None})))]
#[case(Node::Blockquote(Blockquote{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None})
], position: None}), 1, None)]
#[case(Node::Delete(Delete{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}), 0, Some(Node::Text(Text{value: "first".to_string(), position: None})))]
#[case(Node::Emphasis(Emphasis{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}), 1, Some(Node::Text(Text{value: "second".to_string(), position: None})))]
#[case(Node::Strong(Strong{values: vec![
    Node::Text(Text{value: "first".to_string(), position: None})
], position: None}), 0, Some(Node::Text(Text{value: "first".to_string(), position: None})))]
#[case(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}), 0, Some(Node::Text(Text{value: "first".to_string(), position: None})))]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None})
], position: None}), 1, Some(Node::Text(Text{value: "second".to_string(), position: None})))]
#[case(Node::TableCell(TableCell{column: 0, row: 0, values: vec![
    Node::Text(Text{value: "cell content".to_string(), position: None})
], position: None}), 0, Some(Node::Text(Text{value: "cell content".to_string(), position: None})))]
#[case(Node::TableRow(TableRow{values: vec![
    Node::TableCell(TableCell{column: 0, row: 0, values: Vec::new(), position: None}),
    Node::TableCell(TableCell{column: 1, row: 0, values: Vec::new(), position: None})
], position: None}), 1, Some(Node::TableCell(TableCell{column: 1, row: 0, values: Vec::new(), position: None})))]
#[case(Node::Text(Text{value: "plain text".to_string(), position: None}), 0, None)]
#[case(Node::Code(Code{value: "code".to_string(), lang: None, fence: true, meta: None, position: None}), 0, None)]
#[case(Node::Html(Html{value: "<div>".to_string(), position: None}), 0, None)]
fn test_find_at_index(#[case] node: Node, #[case] index: usize, #[case] expected: Option<Node>) {
    assert_eq!(node.find_at_index(index), expected);
}

#[rstest]
#[case(Node::Blockquote(Blockquote{values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::Delete(Delete{values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::Emphasis(Emphasis{values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::Strong(Strong{values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::Link(Link{url: Url(attr_keys::URL.to_string()), title: None, values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec!["test".to_string().into()], label: None, position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::Footnote(Footnote{ident: "id".to_string(), values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::TableCell(TableCell{column: 0, row: 0, values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::TableRow(TableRow{values: vec!["test".to_string().into()], position: None}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::Fragment(Fragment{values: vec!["test".to_string().into()]}),
       Node::Fragment(Fragment{values: vec!["test".to_string().into()]}))]
#[case(Node::Text(Text{value: "test".to_string(), position: None}),
       Node::Empty)]
#[case(Node::Code(Code{value: "test".to_string(), lang: None, fence: true, meta: None, position: None}),
       Node::Empty)]
#[case(Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: None, position: None}),
       Node::Empty)]
#[case(Node::Empty, Node::Empty)]
fn test_to_fragment(#[case] node: Node, #[case] expected: Node) {
    assert_eq!(node.clone().to_fragment(), expected);
    assert_eq!(node.into_fragment(), expected);
}

// Regression coverage for the 0.6.2 blank-line bug: `eval_markdown_node`
// replaces a non-matching `select()` result with `child_node.to_fragment()`,
// not `Node::Empty` directly. For container nodes this yields a `Fragment`
// wrapping the (now `Empty`) children, which must still render to "" with
// no stray blank lines, while a partially-matching container must render
// only the surviving content.
#[rstest]
#[case::blockquote_all_empty(Node::Blockquote(Blockquote{values: vec![Node::Empty, Node::Empty], position: None}), "")]
#[case::blockquote_mixed(Node::Blockquote(Blockquote{values: vec![Node::Empty, Node::Text(Text{value: "kept".to_string(), position: None})], position: None}), "kept")]
#[case::delete_all_empty(Node::Delete(Delete{values: vec![Node::Empty, Node::Empty], position: None}), "")]
#[case::delete_mixed(Node::Delete(Delete{values: vec![Node::Text(Text{value: "kept".to_string(), position: None}), Node::Empty], position: None}), "kept")]
#[case::heading_all_empty(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![Node::Empty, Node::Empty], position: None}), "")]
#[case::heading_mixed(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![Node::Empty, Node::Text(Text{value: "kept".to_string(), position: None})], position: None}), "kept")]
#[case::emphasis_all_empty(Node::Emphasis(Emphasis{values: vec![Node::Empty, Node::Empty], position: None}), "")]
#[case::emphasis_mixed(Node::Emphasis(Emphasis{values: vec![Node::Empty, Node::Text(Text{value: "kept".to_string(), position: None})], position: None}), "kept")]
#[case::list_all_empty(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![Node::Empty, Node::Empty], position: None}), "")]
#[case::list_mixed(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![Node::Empty, Node::Text(Text{value: "kept".to_string(), position: None})], position: None}), "kept")]
#[case::strong_all_empty(Node::Strong(Strong{values: vec![Node::Empty, Node::Empty], position: None}), "")]
#[case::strong_mixed(Node::Strong(Strong{values: vec![Node::Text(Text{value: "kept".to_string(), position: None}), Node::Empty], position: None}), "kept")]
#[case::link_all_empty(Node::Link(Link{url: Url(attr_keys::URL.to_string()), title: None, values: vec![Node::Empty, Node::Empty], position: None}), "")]
#[case::link_mixed(Node::Link(Link{url: Url(attr_keys::URL.to_string()), title: None, values: vec![Node::Empty, Node::Text(Text{value: "kept".to_string(), position: None})], position: None}), "kept")]
#[case::link_ref_all_empty(Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![Node::Empty, Node::Empty], label: None, position: None}), "")]
#[case::link_ref_mixed(Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![Node::Empty, Node::Text(Text{value: "kept".to_string(), position: None})], label: None, position: None}), "kept")]
#[case::footnote_all_empty(Node::Footnote(Footnote{ident: "id".to_string(), values: vec![Node::Empty, Node::Empty], position: None}), "")]
#[case::footnote_mixed(Node::Footnote(Footnote{ident: "id".to_string(), values: vec![Node::Empty, Node::Text(Text{value: "kept".to_string(), position: None})], position: None}), "kept")]
#[case::table_cell_all_empty(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Empty, Node::Empty], position: None}), "")]
#[case::table_cell_mixed(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Empty, Node::Text(Text{value: "kept".to_string(), position: None})], position: None}), "kept")]
fn test_to_fragment_then_render_skips_non_matching(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.to_fragment().to_string_with(&RenderOptions::default()), expected);
}

#[rstest]
#[case(
    &mut Node::Blockquote(Blockquote{values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::Blockquote(Blockquote{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::Delete(Delete{values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::Delete(Delete{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::Emphasis(Emphasis{values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::Emphasis(Emphasis{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::Strong(Strong{values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::Strong(Strong{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::Link(Link{url: Url(attr_keys::URL.to_string()), title: None, values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::Link(Link{url: Url(attr_keys::URL.to_string()), title: None, values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], label: None, position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], label: None, position: None})
)]
#[case(
    &mut Node::Footnote(Footnote{ident: "id".to_string(), values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::Footnote(Footnote{ident: "id".to_string(), values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::TableCell(TableCell{column: 0, row: 0, values: vec![
        Node::Text(Text{value: "old".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::TableCell(TableCell{column: 0, row: 0, values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::TableRow(TableRow{values: vec![
        Node::TableCell(TableCell{column: 0, row: 0, values: vec![
            Node::Text(Text{value: "old".to_string(), position: None})
        ], position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::TableCell(TableCell{column: 0, row: 0, values: vec![
            Node::Text(Text{value: "new".to_string(), position: None})
        ], position: None})
    ]}),
    Node::TableRow(TableRow{values: vec![
        Node::TableCell(TableCell{column: 0, row: 0, values: vec![
            Node::Text(Text{value: "new".to_string(), position: None})
        ], position: None})
    ], position: None})
)]
#[case(
    &mut Node::Text(Text{value: "old".to_string(), position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new".to_string(), position: None})
    ]}),
    Node::Text(Text{value: "old".to_string(), position: None})
)]
#[case(
    &mut Node::Blockquote(Blockquote{values: vec![
        Node::Text(Text{value: "text1".to_string(), position: None}),
        Node::Text(Text{value: "text2".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new1".to_string(), position: None}),
        Node::Text(Text{value: "new2".to_string(), position: None})
    ]}),
    Node::Blockquote(Blockquote{values: vec![
        Node::Text(Text{value: "new1".to_string(), position: None}),
        Node::Text(Text{value: "new2".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::Strong(Strong{values: vec![
        Node::Text(Text{value: "text1".to_string(), position: None}),
        Node::Text(Text{value: "text2".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Empty,
        Node::Text(Text{value: "new2".to_string(), position: None})
    ]}),
    Node::Strong(Strong{values: vec![
        Node::Text(Text{value: "text1".to_string(), position: None}),
        Node::Text(Text{value: "new2".to_string(), position: None})
    ], position: None})
)]
#[case(
    &mut Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![
        Node::Text(Text{value: "text1".to_string(), position: None}),
        Node::Text(Text{value: "text2".to_string(), position: None})
    ], position: None}),
    Node::Fragment(Fragment{values: vec![
        Node::Text(Text{value: "new1".to_string(), position: None}),
        Node::Fragment(Fragment{values: Vec::new()})
    ]}),
    Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![
        Node::Text(Text{value: "new1".to_string(), position: None}),
        Node::Text(Text{value: "text2".to_string(), position: None})
    ], position: None})
)]
fn test_apply_fragment(#[case] node: &mut Node, #[case] fragment: Node, #[case] expected: Node) {
    node.apply_fragment(fragment);
    assert_eq!(*node, expected);
}

#[rstest]
#[case(Node::Text(Text{value: "test".to_string(), position: None}),
   Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
   Node::Text(Text{value: "test".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::Code(Code{value: "code".to_string(), lang: None, fence: true, meta: None, position: None}),
   Position{start: Point{line: 1, column: 1}, end: Point{line: 3, column: 3}},
   Node::Code(Code{value: "code".to_string(), lang: None, fence: true, meta: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 3, column: 3}})}))]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: None, ordered: false, values: vec![], position: None}),
   Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
   Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 1, checked: None, ordered: false, values: vec![], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: None, position: None}),
   Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}},
   Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})}))]
#[case(Node::Delete(Delete{values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
    Node::Delete(Delete{values: vec![Node::Text(Text{value: "test".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})})], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::Emphasis(Emphasis{values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
    Node::Emphasis(Emphasis{values: vec![Node::Text(Text{value: "test".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})})], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::Footnote(Footnote{ident: "id".to_string(), values: vec![Node::Text(Text{value: "test".to_string(), position: None})], position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
    Node::Footnote(Footnote{ident: "id".to_string(), values: vec![Node::Text(Text{value: "test".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})})], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
    Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::Html(Html{value: "<div>test</div>".to_string(), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 15}},
    Node::Html(Html{value: "<div>test</div>".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 15}})}))]
#[case(Node::Yaml(Yaml{value: "key: value".to_string(), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 3, column: 4}},
    Node::Yaml(Yaml{value: "key: value".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 3, column: 4}})}))]
#[case(Node::Toml(Toml{value: "key = \"value\"".to_string(), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 3, column: 4}},
    Node::Toml(Toml{value: "key = \"value\"".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 3, column: 4}})}))]
#[case(Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: None, position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 12}},
    Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 12}})}))]
#[case(Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: None, position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}},
    Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})}))]
#[case(Node::CodeInline(CodeInline{value: "code".into(), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 7}},
    Node::CodeInline(CodeInline{value: "code".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 7}})}))]
#[case(Node::MathInline(MathInline{value: "x^2".into(), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
    Node::MathInline(MathInline{value: "x^2".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::Link(Link{url: Url::new(attr_keys::URL.to_string()), title: None, values: vec![Node::Text(Text{value: "text".to_string(), position: None})], position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}},
    Node::Link(Link{url: Url::new(attr_keys::URL.to_string()), title: None, values: vec![Node::Text(Text{value: "text".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})})], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})}))]
#[case(Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![Node::Text(Text{value: "text".to_string(), position: None})], label: None, position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}},
    Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![Node::Text(Text{value: "text".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})})], label: None, position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})}))]
#[case(Node::Math(Math{value: "x^2".to_string(), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 3, column: 3}},
    Node::Math(Math{value: "x^2".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 3, column: 3}})}))]
#[case(Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Text(Text{value: "cell".to_string(), position: None})], position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 6}},
    Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Text(Text{value: "cell".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 6}})})], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 6}})}))]
#[case(Node::TableAlign(TableAlign{align: vec![TableAlignKind::Left, TableAlignKind::Right], position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 15}},
    Node::TableAlign(TableAlign{align: vec![TableAlignKind::Left, TableAlignKind::Right], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 15}})}))]
#[case(Node::MdxFlowExpression(MdxFlowExpression{value: "test".into(), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 7}},
    Node::MdxFlowExpression(MdxFlowExpression{value: "test".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 7}})}))]
#[case(Node::MdxTextExpression(MdxTextExpression{value: "test".into(), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 7}},
    Node::MdxTextExpression(MdxTextExpression{value: "test".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 7}})}))]
#[case(Node::MdxJsEsm(MdxJsEsm{value: "import React from 'react'".into(), position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 25}},
    Node::MdxJsEsm(MdxJsEsm{value: "import React from 'react'".into(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 25}})}))]
#[case(Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("span".into()), attributes: Vec::new(), children: vec![Node::Text(Text{value: "text".to_string(), position: None})], position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 20}},
    Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("span".into()), attributes: Vec::new(), children: vec![Node::Text(Text{value: "text".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 20}})})], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 20}})}))]
#[case(Node::Break(Break{position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 2}},
    Node::Break(Break{position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 2}})}))]
#[case(Node::Empty,
   Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
   Node::Empty)]
#[case(Node::Fragment(Fragment{values: vec![
       Node::Text(Text{value: "test1".to_string(), position: None}),
       Node::Text(Text{value: "test2".to_string(), position: None})
   ]}),
   Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}},
   Node::Fragment(Fragment{values: vec![
       Node::Text(Text{value: "test1".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})}),
       Node::Text(Text{value: "test2".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})})
   ]}))]
#[case(Node::Blockquote(Blockquote{values: vec![
    Node::Text(Text{value: "test".to_string(), position: None})], position: None}),
    Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
    Node::Blockquote(Blockquote{values: vec![
        Node::Text(Text{value: "test".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})})
    ], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![
        Node::Text(Text{value: "test".to_string(), position: None})], position: None}),
        Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
        Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![
            Node::Text(Text{value: "test".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})})
        ], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::Strong(Strong{values: vec![
        Node::Text(Text{value: "test".to_string(), position: None})], position: None}),
        Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}},
        Node::Strong(Strong{values: vec![
            Node::Text(Text{value: "test".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})})
        ], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 5}})}))]
#[case(Node::TableRow(TableRow{values: vec![
        Node::TableCell(TableCell{column: 0, row: 0, values: vec![
            Node::Text(Text{value: "cell".to_string(), position: None})
        ], position: None})
    ], position: None}),
        Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}},
        Node::TableRow(TableRow{values: vec![
            Node::TableCell(TableCell{column: 0, row: 0, values: vec![
                Node::Text(Text{value: "cell".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})})
            ], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})})
        ], position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 10}})}))]
#[case(Node::MdxJsxFlowElement(MdxJsxFlowElement{
        name: Some("div".to_string()),
        attributes: Vec::new(),
        children: vec![Node::Text(Text{value: "content".to_string(), position: None})],
        position: None
    }),
        Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 20}},
        Node::MdxJsxFlowElement(MdxJsxFlowElement{
            name: Some("div".to_string()),
            attributes: Vec::new(),
            children: vec![Node::Text(Text{value: "content".to_string(), position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 20}})})],
            position: Some(Position{start: Point{line: 1, column: 1}, end: Point{line: 1, column: 20}})
        }))]
fn test_set_position(#[case] mut node: Node, #[case] position: Position, #[case] expected: Node) {
    node.set_position(Some(position));
    assert_eq!(node, expected);
}

fn some_position() -> Option<Position> {
    Some(Position {
        start: Point { line: 1, column: 1 },
        end: Point { line: 1, column: 5 },
    })
}

#[test]
fn test_strip_positions_top_level() {
    let mut node = Node::Text(Text {
        value: "test".to_string(),
        position: some_position(),
    });
    node.strip_positions();
    assert_eq!(node.position(), None);
}

#[test]
fn test_strip_positions_recurses_into_children() {
    let mut node = Node::Blockquote(Blockquote {
        values: vec![Node::Strong(Strong {
            values: vec![Node::Text(Text {
                value: "test".to_string(),
                position: some_position(),
            })],
            position: some_position(),
        })],
        position: some_position(),
    });

    node.strip_positions();

    assert_eq!(node.position(), None);
    let strong = &node.children()[0];
    assert_eq!(strong.position(), None);
    assert_eq!(strong.children()[0].position(), None);
}

#[test]
fn test_clear_text_position_at_leaf_only() {
    let mut node = Node::Heading(Heading {
        depth: HeadingDepth::H1,
        values: vec![Node::Text(Text {
            value: "title".to_string(),
            position: some_position(),
        })],
        position: some_position(),
    });

    node.clear_text_position_at(0);

    assert_eq!(node.position(), some_position(), "outer node keeps its position");
    assert_eq!(node.children()[0].position(), None, "escaped leaf loses its position");
}

#[test]
fn test_clear_text_position_at_selected_index() {
    let mut node = Node::List(List {
        marker: None,
        start: None,
        spread: false,
        index: 0,
        level: 0,
        checked: None,
        ordered: false,
        values: vec![
            Node::Text(Text {
                value: "a".to_string(),
                position: some_position(),
            }),
            Node::Text(Text {
                value: "b".to_string(),
                position: some_position(),
            }),
        ],
        position: some_position(),
    });

    node.clear_text_position_at(1);

    assert_eq!(
        node.children()[0].position(),
        some_position(),
        "untouched sibling keeps its position"
    );
    assert_eq!(node.children()[1].position(), None, "selected leaf loses its position");
}

#[rstest]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec!["test".to_string().into()], position: None}), true)]
#[case(Node::List(List{ marker: None,start: None, spread: false, index: 1, level: 2, checked: Some(true), ordered: false, values: vec!["test".to_string().into()], position: None}), true)]
#[case(Node::Text(Text{value: "test".to_string(), position: None}), false)]
fn test_is_list(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_list(), expected);
}

#[rstest]
#[case(Url::new("https://example.com".to_string()), RenderOptions{link_url_style: UrlSurroundStyle::None, ..Default::default()}, "https://example.com")]
#[case(Url::new("https://example.com".to_string()), RenderOptions{link_url_style: UrlSurroundStyle::Angle, ..Default::default()}, "<https://example.com>")]
#[case(Url::new("".to_string()), RenderOptions::default(), "<>")]
fn test_url_to_string_with(#[case] url: Url, #[case] options: RenderOptions, #[case] expected: &str) {
    assert_eq!(url.to_string_with(&options), expected);
}

#[rstest]
#[case(Title::new(attr_keys::TITLE.to_string()), RenderOptions::default(), "\"title\"")]
#[case(Title::new(r#"title with "quotes""#.to_string()), RenderOptions::default(), r#""title with \"quotes\"""#)]
#[case(Title::new("title with spaces".to_string()), RenderOptions::default(), "\"title with spaces\"")]
#[case(Title::new("".to_string()), RenderOptions::default(), "\"\"")]
#[case(Title::new(attr_keys::TITLE.to_string()), RenderOptions{link_title_style: TitleSurroundStyle::Single, ..Default::default()}, "'title'")]
#[case(Title::new("title with 'quotes'".to_string()), RenderOptions{link_title_style: TitleSurroundStyle::Double, ..Default::default()}, "\"title with 'quotes'\"")]
#[case(Title::new(attr_keys::TITLE.to_string()), RenderOptions{link_title_style: TitleSurroundStyle::Paren, ..Default::default()}, "(title)")]
fn test_title_to_string_with(#[case] title: Title, #[case] options: RenderOptions, #[case] expected: &str) {
    assert_eq!(title.to_string_with(&options), expected);
}

#[rstest]
#[case(Node::Fragment(Fragment{values: vec![]}), true)]
#[case(Node::Fragment(Fragment{values: vec![
    Node::Text(Text{value: "not_empty".to_string(), position: None})
]}), false)]
#[case(Node::Fragment(Fragment{values: vec![
    Node::Fragment(Fragment{values: vec![]}),
    Node::Fragment(Fragment{values: vec![]})
]}), true)]
#[case(Node::Fragment(Fragment{values: vec![
    Node::Fragment(Fragment{values: vec![]}),
    Node::Text(Text{value: "not_empty".to_string(), position: None})
]}), false)]
#[case(Node::Text(Text{value: "not_fragment".to_string(), position: None}), false)]
fn test_is_empty_fragment(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_empty_fragment(), expected);
}

#[rstest]
#[case::footnote(Node::Footnote(Footnote{ident: "id".to_string(), values: Vec::new(), position: None}), attr_keys::IDENT, Some(AttrValue::String("id".to_string())))]
#[case::footnote(Node::Footnote(Footnote{ident: "id".to_string(), values: Vec::new(), position: None}), "unknown", None)]
#[case::html(Node::Html(Html{value: "<div>test</div>".to_string(), position: None}), attr_keys::VALUE, Some(AttrValue::String("<div>test</div>".to_string())))]
#[case::html(Node::Html(Html{value: "<div>test</div>".to_string(), position: None}), "unknown", None)]
#[case::text(Node::Text(Text{value: "text".to_string(), position: None}), attr_keys::VALUE, Some(AttrValue::String("text".to_string())))]
#[case::text(Node::Text(Text{value: "text".to_string(), position: None}), "unknown", None)]
#[case::code(Node::Code(Code{value: "code".to_string(), lang: Some("rust".to_string()), meta: Some("meta".to_string()), fence: true, position: None}), attr_keys::VALUE, Some(AttrValue::String("code".to_string())))]
#[case::code(Node::Code(Code{value: "code".to_string(), lang: Some("rust".to_string()), meta: Some("meta".to_string()), fence: true, position: None}), attr_keys::LANG, Some(AttrValue::String("rust".to_string())))]
#[case::code(Node::Code(Code{value: "code".to_string(), lang: Some("rust".to_string()), meta: Some("meta".to_string()), fence: true, position: None}), "meta", Some(AttrValue::String("meta".to_string())))]
#[case::code(Node::Code(Code{value: "code".to_string(), lang: Some("rust".to_string()), meta: Some("meta".to_string()), fence: true, position: None}), attr_keys::FENCE, Some(AttrValue::Boolean(true)))]
#[case::code(Node::Code(Code{value: "code".to_string(), lang: None, meta: None, fence: false, position: None}), attr_keys::FENCE, Some(AttrValue::Boolean(false)))]
#[case::code_inline(Node::CodeInline(CodeInline{value: "inline".into(), position: None}), attr_keys::VALUE, Some(AttrValue::String("inline".to_string())))]
#[case::math_inline(Node::MathInline(MathInline{value: "math".into(), position: None}), attr_keys::VALUE, Some(AttrValue::String("math".to_string())))]
#[case::math(Node::Math(Math{value: "math".to_string(), position: None}), attr_keys::VALUE, Some(AttrValue::String("math".to_string())))]
#[case::yaml(Node::Yaml(Yaml{value: "yaml".to_string(), position: None}), attr_keys::VALUE, Some(AttrValue::String("yaml".to_string())))]
#[case::toml(Node::Toml(Toml{value: "toml".to_string(), position: None}), attr_keys::VALUE, Some(AttrValue::String("toml".to_string())))]
#[case::image(Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: Some(attr_keys::TITLE.to_string()), position: None}), attr_keys::ALT, Some(AttrValue::String(attr_keys::ALT.to_string())))]
#[case::image(Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: Some(attr_keys::TITLE.to_string()), position: None}), attr_keys::URL, Some(AttrValue::String(attr_keys::URL.to_string())))]
#[case::image(Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: Some(attr_keys::TITLE.to_string()), position: None}), attr_keys::TITLE, Some(AttrValue::String(attr_keys::TITLE.to_string())))]
#[case::image_ref(Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::ALT, Some(AttrValue::String(attr_keys::ALT.to_string())))]
#[case::image_ref(Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::IDENT, Some(AttrValue::String("id".to_string())))]
#[case::image_ref(Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::LABEL, Some(AttrValue::String(attr_keys::LABEL.to_string())))]
#[case::link(Node::Link(Link{url: Url::new(attr_keys::URL.to_string()), title: Some(Title::new(attr_keys::TITLE.to_string())), values: Vec::new(), position: None}), attr_keys::URL, Some(AttrValue::String(attr_keys::URL.to_string())))]
#[case::link(Node::Link(Link{url: Url::new(attr_keys::URL.to_string()), title: Some(Title::new(attr_keys::TITLE.to_string())), values: Vec::new(), position: None}), attr_keys::TITLE, Some(AttrValue::String(attr_keys::TITLE.to_string())))]
#[case::link_ref(Node::LinkRef(LinkRef{ident: "id".to_string(), values: Vec::new(), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::IDENT, Some(AttrValue::String("id".to_string())))]
#[case::link_ref(Node::LinkRef(LinkRef{ident: "id".to_string(), values: Vec::new(), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::LABEL, Some(AttrValue::String(attr_keys::LABEL.to_string())))]
#[case::footnote_ref(Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::IDENT, Some(AttrValue::String("id".to_string())))]
#[case::footnote_ref(Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::LABEL, Some(AttrValue::String(attr_keys::LABEL.to_string())))]
#[case::definition(Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: Some(Title::new(attr_keys::TITLE.to_string())), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::IDENT, Some(AttrValue::String("id".to_string())))]
#[case::definition(Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: Some(Title::new(attr_keys::TITLE.to_string())), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::URL, Some(AttrValue::String(attr_keys::URL.to_string())))]
#[case::definition(Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: Some(Title::new(attr_keys::TITLE.to_string())), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::TITLE, Some(AttrValue::String(attr_keys::TITLE.to_string())))]
#[case::definition(Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: Some(Title::new(attr_keys::TITLE.to_string())), label: Some(attr_keys::LABEL.to_string()), position: None}), attr_keys::LABEL, Some(AttrValue::String(attr_keys::LABEL.to_string())))]
#[case::heading(Node::Heading(Heading{depth: HeadingDepth::H3, values: Vec::new(), position: None}), "depth", Some(AttrValue::Integer(3)))]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 2, level: 1, checked: Some(true), ordered: true, values: Vec::new(), position: None}), "index", Some(AttrValue::Integer(2)))]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 2, level: 1, checked: Some(true), ordered: true, values: Vec::new(), position: None}), "level", Some(AttrValue::Integer(1)))]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 2, level: 1, checked: Some(true), ordered: true, values: Vec::new(), position: None}), "ordered", Some(AttrValue::Boolean(true)))]
#[case::list(Node::List(List{ marker: None,start: None, spread: false, index: 2, level: 1, checked: Some(true), ordered: true, values: Vec::new(), position: None}), attr_keys::CHECKED, Some(AttrValue::Boolean(true)))]
#[case::table_cell(Node::TableCell(TableCell{column: 1, row: 2, values: Vec::new(), position: None}), "column", Some(AttrValue::Integer(1)))]
#[case::table_cell(Node::TableCell(TableCell{column: 1, row: 2, values: Vec::new(), position: None}), "row", Some(AttrValue::Integer(2)))]
#[case::table_align(Node::TableAlign(TableAlign{align: vec![TableAlignKind::Left, TableAlignKind::Right], position: None}), "align", Some(AttrValue::String(":---,---:".to_string())))]
#[case::mdx_flow_expression(Node::MdxFlowExpression(MdxFlowExpression{value: "expr".into(), position: None}), attr_keys::VALUE, Some(AttrValue::String("expr".to_string())))]
#[case::mdx_flow_expression(Node::MdxTextExpression(MdxTextExpression{value: "expr".into(), position: None}), attr_keys::VALUE, Some(AttrValue::String("expr".to_string())))]
#[case::mdx_js_esm(Node::MdxJsEsm(MdxJsEsm{value: "esm".into(), position: None}), attr_keys::VALUE, Some(AttrValue::String("esm".to_string())))]
#[case::mdx_jsx_flow_element(Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some("div".to_string()), attributes: Vec::new(), children: Vec::new(), position: None}), attr_keys::NAME, Some(AttrValue::String("div".to_string())))]
#[case::mdx_jsx_flow_element(Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("span".into()), attributes: Vec::new(), children: Vec::new(), position: None}), attr_keys::NAME, Some(AttrValue::String("span".to_string())))]
#[case::break_(Node::Break(Break{position: None}), attr_keys::VALUE, None)]
#[case::horizontal_rule(Node::HorizontalRule(HorizontalRule{ marker: None,position: None}), attr_keys::VALUE, None)]
#[case::fragment(Node::Fragment(Fragment{values: Vec::new()}), attr_keys::VALUE, Some(AttrValue::String("".to_string())))]
#[case::heading(Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![Node::Text(Text{value: "heading text".to_string(), position: None})], position: None}), attr_keys::VALUE, Some(AttrValue::String("heading text".to_string())))]
#[case::heading(Node::Heading(Heading{depth: HeadingDepth::H2, values: vec![], position: None}), attr_keys::VALUE, Some(AttrValue::String("".to_string())))]
#[case::heading(Node::Heading(Heading{depth: HeadingDepth::H3, values: vec![
    Node::Text(Text{value: "first".to_string(), position: None}),
    Node::Text(Text{value: "second".to_string(), position: None}),
], position: None}), attr_keys::VALUE, Some(AttrValue::String("firstsecond".to_string())))]
#[case(
    Node::List(List { marker: None,
        index: 0,
        level: 1,
        checked: None,
        ordered: false,
        start: None, spread: false,
        values: vec![
            Node::Text(Text { value: "item1".to_string(), position: None }),
            Node::Text(Text { value: "item2".to_string(), position: None }),
        ],
        position: None,
    }),
    attr_keys::VALUE,
    Some(AttrValue::String("item1item2".to_string()))
)]
#[case(
    Node::TableCell(TableCell {
        column: 1,
        row: 2,
        values: vec![Node::Text(Text {
            value: "cell_value".to_string(),
            position: None,
        })],
        position: None,
    }),
    attr_keys::VALUE,
    Some(AttrValue::String("cell_value".to_string()))
)]
#[case::footnote(
    Node::Footnote(Footnote {
        ident: "id".to_string(),
        values: vec![Node::Text(Text {
            value: "footnote value".to_string(),
            position: None,
        })],
        position: None,
    }),
    attr_keys::VALUE,
    Some(AttrValue::String("footnote value".to_string()))
)]
#[case::link(
    Node::Link(Link {
        url: Url::new("https://example.com".to_string()),
        title: Some(Title::new("Example".to_string())),
        values: vec![Node::Text(Text {
            value: "link text".to_string(),
            position: None,
        })],
        position: None,
    }),
    attr_keys::VALUE,
    Some(AttrValue::String("link text".to_string()))
)]
#[case::empty(Node::Empty, attr_keys::VALUE, None)]
#[case::heading(
    Node::Heading(Heading {
        depth: HeadingDepth::H1,
        values: vec![
        Node::Text(Text {
            value: "child1".to_string(),
            position: None,
        }),
        Node::Text(Text {
            value: "child2".to_string(),
            position: None,
        }),
        ],
        position: None,
    }),
    attr_keys::CHILDREN,
    Some(AttrValue::Array(vec![
        Node::Text(Text {
        value: "child1".to_string(),
        position: None,
        }),
        Node::Text(Text {
        value: "child2".to_string(),
        position: None,
        }),
    ]))
    )]
#[case::list(
    Node::List(List { marker: None,
        index: 0,
        level: 1,
        checked: None,
        ordered: false,
        start: None, spread: false,
        values: vec![
        Node::Text(Text {
            value: "item1".to_string(),
            position: None,
        }),
        ],
        position: None,
    }),
    attr_keys::CHILDREN,
    Some(AttrValue::Array(vec![
        Node::Text(Text {
        value: "item1".to_string(),
        position: None,
        }),
    ]))
    )]
#[case::blockquote(
    Node::Blockquote(Blockquote {
        values: vec![
        Node::Text(Text {
            value: "quote".to_string(),
            position: None,
        }),
        ],
        position: None,
    }),
    attr_keys::VALUES,
    Some(AttrValue::Array(vec![
        Node::Text(Text {
        value: "quote".to_string(),
        position: None,
        }),
    ]))
    )]
#[case::link(
    Node::Link(Link {
        url: Url::new(attr_keys::URL.to_string()),
        title: None,
        values: vec![
        Node::Text(Text {
            value: "link".to_string(),
            position: None,
        }),
        ],
        position: None,
    }),
    attr_keys::VALUES,
    Some(AttrValue::Array(vec![
        Node::Text(Text {
        value: "link".to_string(),
        position: None,
        }),
    ]))
    )]
#[case::table_cell(
    Node::TableCell(TableCell {
        column: 0,
        row: 0,
        values: vec![
        Node::Text(Text {
            value: "cell".to_string(),
            position: None,
        }),
        ],
        position: None,
    }),
    attr_keys::CHILDREN,
    Some(AttrValue::Array(vec![
        Node::Text(Text {
        value: "cell".to_string(),
        position: None,
        }),
    ]))
    )]
#[case::strong(
    Node::Strong(Strong {
        values: vec![
        Node::Text(Text {
            value: "bold".to_string(),
            position: None,
        }),
        ],
        position: None,
    }),
    attr_keys::CHILDREN,
    Some(AttrValue::Array(vec![
        Node::Text(Text {
        value: "bold".to_string(),
        position: None,
        }),
    ]))
    )]
#[case::em(
    Node::Emphasis(Emphasis {
        values: vec![],
        position: None,
    }),
    attr_keys::CHILDREN,
    Some(AttrValue::Array(vec![]))
    )]
fn test_attr(#[case] node: Node, #[case] attr: &str, #[case] expected: Option<AttrValue>) {
    assert_eq!(node.attr(attr), expected);
}

#[rstest]
#[case::heading_with_position(
    Node::Heading(Heading {
        depth: HeadingDepth::H1,
        values: vec![],
        position: Some(Position {
            start: Point { line: 3, column: 1 },
            end: Point { line: 5, column: 4 },
        }),
    }),
    attr_keys::LINE,
    Some(AttrValue::Integer(3))
)]
#[case::code_end_line(
    Node::Code(Code {
        value: "x".to_string(),
        lang: None,
        meta: None,
        fence: true,
        position: Some(Position {
            start: Point { line: 3, column: 1 },
            end: Point { line: 5, column: 4 },
        }),
    }),
    attr_keys::END_LINE,
    Some(AttrValue::Integer(5))
)]
#[case::synthetic_node_has_no_line(
    Node::Heading(Heading {
        depth: HeadingDepth::H1,
        values: vec![],
        position: None,
    }),
    attr_keys::LINE,
    None
)]
#[case::synthetic_node_has_no_end_line(
    Node::Heading(Heading {
        depth: HeadingDepth::H1,
        values: vec![],
        position: None,
    }),
    attr_keys::END_LINE,
    None
)]
fn test_line_attr(#[case] node: Node, #[case] attr: &str, #[case] expected: Option<AttrValue>) {
    assert_eq!(node.attr(attr), expected);
}

#[rstest]
#[case::heading(
    Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![], position: None}),
    vec![Node::Text(Text{value: "child".to_string(), position: None})],
    Node::Heading(Heading{depth: HeadingDepth::H1, values: vec![Node::Text(Text{value: "child".to_string(), position: None})], position: None})
)]
#[case::list(
    Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![], position: None}),
    vec![Node::Text(Text{value: "item".to_string(), position: None})],
    Node::List(List{ marker: None,start: None, spread: false, index: 0, level: 0, checked: None, ordered: false, values: vec![Node::Text(Text{value: "item".to_string(), position: None})], position: None})
)]
#[case::blockquote(
    Node::Blockquote(Blockquote{values: vec![], position: None}),
    vec![Node::Text(Text{value: "quote".to_string(), position: None})],
    Node::Blockquote(Blockquote{values: vec![Node::Text(Text{value: "quote".to_string(), position: None})], position: None})
)]
#[case::link(
    Node::Link(Link{url: Url::new(attr_keys::URL.to_string()), title: None, values: vec![], position: None}),
    vec![Node::Text(Text{value: "link".to_string(), position: None})],
    Node::Link(Link{url: Url::new(attr_keys::URL.to_string()), title: None, values: vec![Node::Text(Text{value: "link".to_string(), position: None})], position: None})
)]
#[case::footnote(
    Node::Footnote(Footnote{ident: "1".to_string(), values: vec![], position: None}),
    vec![Node::Text(Text{value: "note".to_string(), position: None})],
    Node::Footnote(Footnote{ident: "1".to_string(), values: vec![Node::Text(Text{value: "note".to_string(), position: None})], position: None})
)]
#[case::table_cell(
    Node::TableCell(TableCell{column: 0, row: 0, values: vec![], position: None}),
    vec![Node::Text(Text{value: "cell".to_string(), position: None})],
    Node::TableCell(TableCell{column: 0, row: 0, values: vec![Node::Text(Text{value: "cell".to_string(), position: None})], position: None})
)]
#[case::table_row(
    Node::TableRow(TableRow{values: vec![], position: None}),
    vec![Node::Text(Text{value: "row".to_string(), position: None})],
    Node::TableRow(TableRow{values: vec![Node::Text(Text{value: "row".to_string(), position: None})], position: None})
)]
#[case::strong(
    Node::Strong(Strong{values: vec![], position: None}),
    vec![Node::Text(Text{value: "bold".to_string(), position: None})],
    Node::Strong(Strong{values: vec![Node::Text(Text{value: "bold".to_string(), position: None})], position: None})
)]
#[case::delete(
    Node::Delete(Delete{values: vec![], position: None}),
    vec![Node::Text(Text{value: "del".to_string(), position: None})],
    Node::Delete(Delete{values: vec![Node::Text(Text{value: "del".to_string(), position: None})], position: None})
)]
#[case::emphasis(
    Node::Emphasis(Emphasis{values: vec![], position: None}),
    vec![Node::Text(Text{value: "em".to_string(), position: None})],
    Node::Emphasis(Emphasis{values: vec![Node::Text(Text{value: "em".to_string(), position: None})], position: None})
)]
#[case::fragment(
    Node::Fragment(Fragment{values: vec![]}),
    vec![Node::Text(Text{value: "frag".to_string(), position: None})],
    Node::Fragment(Fragment{values: vec![Node::Text(Text{value: "frag".to_string(), position: None})]})
)]
#[case::mdx_jsx_flow_element(
    Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some("div".to_string()), attributes: Vec::new(), children: vec![], position: None}),
    vec![Node::Text(Text{value: "mdx".to_string(), position: None})],
    Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some("div".to_string()), attributes: Vec::new(), children: vec![Node::Text(Text{value: "mdx".to_string(), position: None})], position: None})
)]
#[case::mdx_jsx_text_element(
    Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("span".into()), attributes: Vec::new(), children: vec![], position: None}),
    vec![Node::Text(Text{value: "mdx".to_string(), position: None})],
    Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("span".into()), attributes: Vec::new(), children: vec![Node::Text(Text{value: "mdx".to_string(), position: None})], position: None})
)]
#[case::leaf_node_is_noop(
    Node::Text(Text{value: "leaf".to_string(), position: None}),
    vec![Node::Text(Text{value: "ignored".to_string(), position: None})],
    Node::Text(Text{value: "leaf".to_string(), position: None})
)]
fn test_set_children(#[case] mut node: Node, #[case] children: Vec<Node>, #[case] expected: Node) {
    node.set_children(children.clone());
    assert_eq!(node, expected);
    assert_eq!(
        node.children(),
        if matches!(expected, Node::Text(_)) {
            Vec::new()
        } else {
            children
        }
    );
}

#[rstest]
#[case(
    Node::Text(Text{value: "old".to_string(), position: None}),
    attr_keys::VALUE,
    "new",
    Node::Text(Text{value: "new".to_string(), position: None})
)]
#[case(
    Node::Code(Code{value: "old".to_string(), lang: Some("rust".to_string()), fence: true, meta: None, position: None}),
    attr_keys::VALUE,
    "new_code",
    Node::Code(Code{value: "new_code".to_string(), lang: Some("rust".to_string()), fence: true, meta: None, position: None})
)]
#[case(
    Node::Code(Code{value: "code".to_string(), lang: Some("rust".to_string()), fence: true, meta: None, position: None}),
    attr_keys::LANG,
    "python",
    Node::Code(Code{value: "code".to_string(), lang: Some("python".to_string()), fence: true, meta: None, position: None})
)]
#[case(
    Node::Code(Code{value: "code".to_string(), lang: None, fence: false, meta: None, position: None}),
    attr_keys::FENCE,
    "true",
    Node::Code(Code{value: "code".to_string(), lang: None, fence: true, meta: None, position: None})
)]
#[case(
    Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: None, position: None}),
    attr_keys::ALT,
    "new_alt",
    Node::Image(Image{alt: "new_alt".to_string(), url: attr_keys::URL.to_string(), title: None, position: None})
)]
#[case(
    Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: None, position: None}),
    attr_keys::URL,
    "new_url",
    Node::Image(Image{alt: attr_keys::ALT.to_string(), url: "new_url".to_string(), title: None, position: None})
)]
#[case(
    Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: Some(attr_keys::TITLE.to_string()), position: None}),
    attr_keys::TITLE,
    "new_title",
    Node::Image(Image{alt: attr_keys::ALT.to_string(), url: attr_keys::URL.to_string(), title: Some("new_title".to_string()), position: None})
)]
#[case(
    Node::Heading(Heading{depth: HeadingDepth::H2, values: vec![], position: None}),
    "depth",
    "3",
    Node::Heading(Heading{depth: HeadingDepth::H3, values: vec![], position: None})
)]
#[case(
    Node::List(List{ marker: None,start: None, spread: false, index: 1, level: 2, checked: Some(true), ordered: false, values: vec![], position: None}),
    attr_keys::CHECKED,
    "false",
    Node::List(List{ marker: None,start: None, spread: false, index: 1, level: 2, checked: Some(false), ordered: false, values: vec![], position: None})
)]
#[case(
    Node::List(List{ marker: None,start: None, spread: false, index: 1, level: 2, checked: Some(true), ordered: false, values: vec![], position: None}),
    "ordered",
    "true",
    Node::List(List{ marker: None,start: None, spread: false, index: 1, level: 2, checked: Some(true), ordered: true, values: vec![], position: None})
)]
#[case(
    Node::TableCell(TableCell{column: 1, row: 2, values: vec![], position: None}),
    "column",
    "3",
    Node::TableCell(TableCell{column: 3, row: 2, values: vec![], position: None})
)]
#[case(
    Node::TableCell(TableCell{column: 1, row: 2, values: vec![], position: None}),
    "row",
    "5",
    Node::TableCell(TableCell{column: 1, row: 5, values: vec![], position: None})
)]
#[case(
    Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: None, position: None}),
    attr_keys::IDENT,
    "new_id",
    Node::Definition(Definition{ident: "new_id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: None, position: None})
)]
#[case(
    Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: None, position: None}),
    attr_keys::URL,
    "new_url",
    Node::Definition(Definition{ident: "id".to_string(), url: Url::new("new_url".to_string()), title: None, label: None, position: None})
)]
#[case(
    Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: None, position: None}),
    attr_keys::LABEL,
    "new_label",
    Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: Some("new_label".to_string()), position: None})
)]
#[case(
    Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: None, label: None, position: None}),
    attr_keys::TITLE,
    "new_title",
    Node::Definition(Definition{ident: "id".to_string(), url: Url::new(attr_keys::URL.to_string()), title: Some(Title::new("new_title".to_string())), label: None, position: None})
)]
#[case(
    Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}),
    attr_keys::ALT,
    "new_alt",
    Node::ImageRef(ImageRef{alt: "new_alt".to_string(), ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None})
)]
#[case(
    Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}),
    attr_keys::IDENT,
    "new_id",
    Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "new_id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None})
)]
#[case(
    Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}),
    attr_keys::LABEL,
    "new_label",
    Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: Some("new_label".to_string()), position: None})
)]
#[case(
    Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: None, position: None}),
    attr_keys::LABEL,
    "new_label",
    Node::ImageRef(ImageRef{alt: attr_keys::ALT.to_string(), ident: "id".to_string(), label: Some("new_label".to_string()), position: None})
)]
#[case(
    Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![], label: Some(attr_keys::LABEL.to_string()), position: None}),
    attr_keys::IDENT,
    "new_id",
    Node::LinkRef(LinkRef{ident: "new_id".to_string(), values: vec![], label: Some(attr_keys::LABEL.to_string()), position: None})
)]
#[case(
    Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![], label: Some(attr_keys::LABEL.to_string()), position: None}),
    attr_keys::LABEL,
    "new_label",
    Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![], label: Some("new_label".to_string()), position: None})
)]
#[case(
    Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![], label: None, position: None}),
    attr_keys::LABEL,
    "new_label",
    Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![], label: Some("new_label".to_string()), position: None})
)]
#[case(
    Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![], label: Some(attr_keys::LABEL.to_string()), position: None}),
    "unknown",
    "ignored",
    Node::LinkRef(LinkRef{ident: "id".to_string(), values: vec![], label: Some(attr_keys::LABEL.to_string()), position: None})
)]
#[case(
    Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}),
    attr_keys::IDENT,
    "new_id",
    Node::FootnoteRef(FootnoteRef{ident: "new_id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None})
)]
#[case(
    Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}),
    attr_keys::LABEL,
    "new_label",
    Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some("new_label".to_string()), position: None})
)]
#[case(
    Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: None, position: None}),
    attr_keys::LABEL,
    "new_label",
    Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some("new_label".to_string()), position: None})
)]
#[case(
    Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None}),
    "unknown",
    "ignored",
    Node::FootnoteRef(FootnoteRef{ident: "id".to_string(), label: Some(attr_keys::LABEL.to_string()), position: None})
)]
#[case(Node::Empty, attr_keys::VALUE, "ignored", Node::Empty)]
#[case(
    Node::TableAlign(TableAlign{align: vec![TableAlignKind::Left, TableAlignKind::Right], position: None}),
    "align",
    "---,:---:",
    Node::TableAlign(TableAlign{align: vec![TableAlignKind::None, TableAlignKind::Center], position: None})
)]
#[case(
    Node::TableAlign(TableAlign{align: vec![], position: None}),
    "align",
    ":---,---:",
    Node::TableAlign(TableAlign{align: vec![TableAlignKind::Left, TableAlignKind::Right], position: None})
)]
#[case(
    Node::TableAlign(TableAlign{align: vec![TableAlignKind::Left], position: None}),
    "unknown",
    "ignored",
    Node::TableAlign(TableAlign{align: vec![TableAlignKind::Left], position: None})
)]
#[case(
    Node::MdxFlowExpression(MdxFlowExpression{value: "old".into(), position: None}),
    attr_keys::VALUE,
    "new_expr",
    Node::MdxFlowExpression(MdxFlowExpression{value: "new_expr".into(), position: None})
)]
#[case(
    Node::MdxFlowExpression(MdxFlowExpression{value: "expr".into(), position: None}),
    "unknown",
    "ignored",
    Node::MdxFlowExpression(MdxFlowExpression{value: "expr".into(), position: None})
)]
#[case(
    Node::MdxTextExpression(MdxTextExpression{value: "old".into(), position: None}),
    attr_keys::VALUE,
    "new_expr",
    Node::MdxTextExpression(MdxTextExpression{value: "new_expr".into(), position: None})
)]
#[case(
    Node::MdxTextExpression(MdxTextExpression{value: "expr".into(), position: None}),
    "unknown",
    "ignored",
    Node::MdxTextExpression(MdxTextExpression{value: "expr".into(), position: None})
)]
#[case(
    Node::MdxJsEsm(MdxJsEsm{value: "import x".into(), position: None}),
    attr_keys::VALUE,
    "import y",
    Node::MdxJsEsm(MdxJsEsm{value: "import y".into(), position: None})
)]
#[case(
    Node::MdxJsEsm(MdxJsEsm{value: "import x".into(), position: None}),
    "unknown",
    "ignored",
    Node::MdxJsEsm(MdxJsEsm{value: "import x".into(), position: None})
)]
#[case(
    Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some("div".to_string()), attributes: Vec::new(), children: Vec::new(), position: None}),
    attr_keys::NAME,
    "section",
    Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some("section".to_string()), attributes: Vec::new(), children: Vec::new(), position: None})
)]
#[case(
    Node::MdxJsxFlowElement(MdxJsxFlowElement{name: None, attributes: Vec::new(), children: Vec::new(), position: None}),
    attr_keys::NAME,
    "main",
    Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some("main".to_string()), attributes: Vec::new(), children: Vec::new(), position: None})
)]
#[case(
    Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some("div".to_string()), attributes: Vec::new(), children: Vec::new(), position: None}),
    "unknown",
    "ignored",
    Node::MdxJsxFlowElement(MdxJsxFlowElement{name: Some("div".to_string()), attributes: Vec::new(), children: Vec::new(), position: None})
)]
#[case(
    Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("span".into()), attributes: Vec::new(), children: Vec::new(), position: None}),
    attr_keys::NAME,
    "b",
    Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("b".into()), attributes: Vec::new(), children: Vec::new(), position: None})
)]
#[case(
    Node::MdxJsxTextElement(MdxJsxTextElement{name: None, attributes: Vec::new(), children: Vec::new(), position: None}),
    attr_keys::NAME,
    "i",
    Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("i".into()), attributes: Vec::new(), children: Vec::new(), position: None})
)]
#[case(
    Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("span".into()), attributes: Vec::new(), children: Vec::new(), position: None}),
    "unknown",
    "ignored",
    Node::MdxJsxTextElement(MdxJsxTextElement{name: Some("span".into()), attributes: Vec::new(), children: Vec::new(), position: None})
)]
fn test_set_attr(#[case] mut node: Node, #[case] attr: &str, #[case] value: &str, #[case] expected: Node) {
    let _ = node.set_attr(attr, value);
    assert_eq!(node, expected);
}

#[rstest]
#[case(AttrValue::String("test".to_string()), AttrValue::String("test".to_string()), true)]
#[case(AttrValue::String("test".to_string()), AttrValue::String("other".to_string()), false)]
#[case(AttrValue::Integer(42), AttrValue::Integer(42), true)]
#[case(AttrValue::Integer(42), AttrValue::Integer(0), false)]
#[case(AttrValue::Boolean(true), AttrValue::Boolean(true), true)]
#[case(AttrValue::Boolean(true), AttrValue::Boolean(false), false)]
#[case(AttrValue::String("42".to_string()), AttrValue::Integer(42), false)]
#[case(AttrValue::Boolean(false), AttrValue::Integer(0), false)]
fn test_attr_value_eq(#[case] a: AttrValue, #[case] b: AttrValue, #[case] expected: bool) {
    assert_eq!(a == b, expected);
}

#[rstest]
#[case(AttrValue::String("test".to_string()), "test")]
#[case(AttrValue::Integer(42), "42")]
#[case(AttrValue::Boolean(true), "true")]
fn test_attr_value_as_str(#[case] value: AttrValue, #[case] expected: &str) {
    assert_eq!(&value.as_string(), expected);
}

#[rstest]
#[case(AttrValue::Integer(42), Some(42))]
#[case(AttrValue::String("42".to_string()), Some(42))]
#[case(AttrValue::Boolean(false), Some(0))]
fn test_attr_value_as_i64(#[case] value: AttrValue, #[case] expected: Option<i64>) {
    assert_eq!(value.as_i64(), expected);
}

// --- Callout tests ---

#[cfg(feature = "callout")]
#[rstest]
// basic render
#[case(
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None,
        values: vec![Node::Text(Text { value: "content".to_string(), position: None })],
        position: None }),
    "> [!NOTE]\n> content"
)]
// with title
#[case(
    Node::Callout(Callout { fold: None, kind: "WARNING".to_string(), title: Some("Heads up".to_string()),
        values: vec![Node::Text(Text { value: "watch out".to_string(), position: None })],
        position: None }),
    "> [!WARNING] Heads up\n> watch out"
)]
// empty body
#[case(
    Node::Callout(Callout { fold: None, kind: "TIP".to_string(), title: None, values: vec![], position: None }),
    "> [!TIP]"
)]
// multiline body: single Text with embedded '\n': each line prefixed with "> "
#[case(
    Node::Callout(Callout { fold: None, kind: "INFO".to_string(), title: None,
        values: vec![Node::Text(Text { value: "line one\nline two".to_string(), position: None })],
        position: None }),
    "> [!INFO]\n> line one\n> line two"
)]
// two separate position-less Text values are paragraphs: the inline nodes of one never are two Texts
#[case(
    Node::Callout(Callout { fold: None, kind: "INFO".to_string(), title: None,
        values: vec![
            Node::Text(Text { value: "part a".to_string(), position: None }),
            Node::Text(Text { value: "part b".to_string(), position: None }),
        ],
        position: None }),
    "> [!INFO]\n> part a\n> \n> part b"
)]
fn test_callout_render(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.to_string_with(&RenderOptions::default()), expected);
}

#[cfg(feature = "callout")]
#[rstest]
#[case(Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None, values: vec![], position: None }), "kind", Some(AttrValue::String("NOTE".to_string())))]
#[case(Node::Callout(Callout { fold: None, kind: "WARNING".to_string(), title: Some("Title".to_string()), values: vec![], position: None }), "title", Some(AttrValue::String("Title".to_string())))]
#[case(Node::Callout(Callout { fold: None, kind: "TIP".to_string(), title: None, values: vec![], position: None }), "title", None)]
#[case(Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None, values: vec![Node::Text(Text { value: "body".to_string(), position: None })], position: None }), "value", Some(AttrValue::String("body".to_string())))]
#[case(Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None, values: vec![Node::Text(Text { value: "body".to_string(), position: None })], position: None }), "children", Some(AttrValue::Array(vec![Node::Text(Text { value: "body".to_string(), position: None })])))]
fn test_callout_attr(#[case] node: Node, #[case] attr: &str, #[case] expected: Option<AttrValue>) {
    assert_eq!(node.attr(attr), expected);
}

#[cfg(feature = "callout")]
#[rstest]
#[case(
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None, values: vec![], position: None }),
    "kind", "WARNING",
    Node::Callout(Callout { fold: None, kind: "WARNING".to_string(), title: None, values: vec![], position: None })
)]
// set_attr stores kind as-is without case conversion
#[case(
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None, values: vec![], position: None }),
    "kind", "tip",
    Node::Callout(Callout { fold: None, kind: "tip".to_string(), title: None, values: vec![], position: None })
)]
#[case(
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None, values: vec![], position: None }),
    "title", "My title",
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: Some("My title".to_string()), values: vec![], position: None })
)]
// empty string clears title
#[case(
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: Some("old".to_string()), values: vec![], position: None }),
    "title", "",
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None, values: vec![], position: None })
)]
fn test_callout_set_attr(#[case] mut node: Node, #[case] attr: &str, #[case] value: &str, #[case] expected: Node) {
    let _ = node.set_attr(attr, value);
    assert_eq!(node, expected);
}

#[cfg(feature = "callout")]
#[rstest]
// with_value changes first body node
#[case(
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None,
        values: vec![Node::Text(Text { value: "old".to_string(), position: None })],
        position: None }),
    "new",
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None,
        values: vec![Node::Text(Text { value: "new".to_string(), position: None })],
        position: None })
)]
fn test_callout_with_value(#[case] node: Node, #[case] value: &str, #[case] expected: Node) {
    assert_eq!(node.clone().with_value(value), expected);
    assert_eq!(node.into_with_value(value), expected);
}

#[cfg(feature = "callout")]
#[rstest]
#[case(Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None, values: vec![], position: None }), "callout")]
fn test_callout_name(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.name(), expected);
}

#[cfg(feature = "callout")]
#[rstest]
#[case(
    Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None,
        values: vec![Node::Text(Text { value: "body".to_string(), position: None })],
        position: None }),
    "body"
)]
fn test_callout_value(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.value(), expected);
}

#[cfg(feature = "callout")]
#[rstest]
#[case(Node::Callout(Callout { fold: None, kind: "NOTE".to_string(), title: None, values: vec![], position: None }), true)]
#[case(Node::Blockquote(Blockquote { values: vec![], position: None }), false)]
#[case(Node::Text(Text { value: "test".to_string(), position: None }), false)]
fn test_is_callout(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_callout(), expected);
}

#[cfg(feature = "callout")]
#[test]
fn test_callout_end_to_end() {
    use crate::Markdown;
    // plain blockquote stays as Blockquote
    let plain = Markdown::from_markdown_str("> just a quote").unwrap();
    assert!(plain.nodes[0].is_blockquote());

    // [!NOTE] is parsed as Callout
    let note = Markdown::from_markdown_str("> [!NOTE]\n> body").unwrap();
    assert!(note.nodes[0].is_callout());
    assert_eq!(note.nodes[0].attr("kind"), Some(AttrValue::String("NOTE".to_string())));

    // [!WARNING] with title
    let warn = Markdown::from_markdown_str("> [!WARNING] Watch out\n> content").unwrap();
    assert!(warn.nodes[0].is_callout());
    assert_eq!(
        warn.nodes[0].attr("title"),
        Some(AttrValue::String("Watch out".to_string()))
    );
}

// --- Embed tests ---

#[cfg(feature = "embed")]
#[rstest]
// plain embed renders without display
#[case(
    Node::Embed(Embed { target: "note.md".to_string(), display: None, position: None }),
    "![[note.md]]"
)]
// embed with display hint
#[case(
    Node::Embed(Embed { target: "image.png".to_string(), display: Some("400".to_string()), position: None }),
    "![[image.png|400]]"
)]
// embed with section reference
#[case(
    Node::Embed(Embed { target: "note#Intro".to_string(), display: None, position: None }),
    "![[note#Intro]]"
)]
fn test_embed_render(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.to_string_with(&RenderOptions::default()), expected);
}

#[cfg(feature = "embed")]
#[rstest]
#[case(Node::Embed(Embed { target: "note".to_string(), display: None, position: None }), "url", Some(AttrValue::String("note".to_string())))]
// value returns target when no display
#[case(Node::Embed(Embed { target: "note".to_string(), display: None, position: None }), "value", Some(AttrValue::String("note".to_string())))]
// url always returns target regardless of display
#[case(Node::Embed(Embed { target: "note".to_string(), display: Some("400".to_string()), position: None }), "url", Some(AttrValue::String("note".to_string())))]
// value returns display when present
#[case(Node::Embed(Embed { target: "note".to_string(), display: Some("400".to_string()), position: None }), "value", Some(AttrValue::String("400".to_string())))]
// unknown attr returns None
#[case(Node::Embed(Embed { target: "note".to_string(), display: None, position: None }), "unknown", None)]
fn test_embed_attr(#[case] node: Node, #[case] attr: &str, #[case] expected: Option<AttrValue>) {
    assert_eq!(node.attr(attr), expected);
}

#[cfg(feature = "embed")]
#[rstest]
// set url changes target
#[case(
    Node::Embed(Embed { target: "old".to_string(), display: None, position: None }),
    "url", "new",
    Node::Embed(Embed { target: "new".to_string(), display: None, position: None })
)]
// set value changes display
#[case(
    Node::Embed(Embed { target: "note".to_string(), display: None, position: None }),
    "value", "800",
    Node::Embed(Embed { target: "note".to_string(), display: Some("800".to_string()), position: None })
)]
// empty value clears display
#[case(
    Node::Embed(Embed { target: "note".to_string(), display: Some("400".to_string()), position: None }),
    "value", "",
    Node::Embed(Embed { target: "note".to_string(), display: None, position: None })
)]
fn test_embed_set_attr(#[case] mut node: Node, #[case] attr: &str, #[case] value: &str, #[case] expected: Node) {
    let _ = node.set_attr(attr, value);
    assert_eq!(node, expected);
}

#[cfg(feature = "embed")]
#[rstest]
// no display: with_value sets target
#[case(
    Node::Embed(Embed { target: "old".to_string(), display: None, position: None }),
    "new",
    Node::Embed(Embed { target: "new".to_string(), display: None, position: None })
)]
// with display: with_value sets display
#[case(
    Node::Embed(Embed { target: "note".to_string(), display: Some("400".to_string()), position: None }),
    "800",
    Node::Embed(Embed { target: "note".to_string(), display: Some("800".to_string()), position: None })
)]
fn test_embed_with_value(#[case] node: Node, #[case] value: &str, #[case] expected: Node) {
    assert_eq!(node.clone().with_value(value), expected);
    assert_eq!(node.into_with_value(value), expected);
}

#[cfg(feature = "embed")]
#[rstest]
#[case(Node::Embed(Embed { target: "note".to_string(), display: None, position: None }), "embed")]
fn test_embed_name(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.name(), expected);
}

#[cfg(feature = "embed")]
#[rstest]
// value returns display when present
#[case(Node::Embed(Embed { target: "note".to_string(), display: Some("400".to_string()), position: None }), "400")]
// value returns target when no display
#[case(Node::Embed(Embed { target: "note".to_string(), display: None, position: None }), "note")]
fn test_embed_value(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.value(), expected);
}

#[cfg(feature = "embed")]
#[rstest]
#[case(Node::Embed(Embed { target: "note".to_string(), display: None, position: None }), true)]
#[case(Node::Text(Text { value: "test".to_string(), position: None }), false)]
#[case(Node::Image(Image { alt: "".to_string(), url: "img.png".to_string(), title: None, position: None }), false)]
fn test_is_embed(#[case] node: Node, #[case] expected: bool) {
    assert_eq!(node.is_embed(), expected);
}

#[cfg(feature = "embed")]
#[test]
fn test_embed_end_to_end() {
    use crate::Markdown;
    let md = Markdown::from_markdown_str("See ![[note.md]] for details.").unwrap();
    let embed = md.nodes.iter().find(|n| n.is_embed());
    assert!(embed.is_some());
    assert_eq!(
        embed.unwrap().attr("url"),
        Some(AttrValue::String("note.md".to_string()))
    );
}

#[cfg(all(feature = "callout", feature = "wikilink"))]
#[rstest]
// callout body containing a wikilink
#[case("> [!NOTE]\n> See [[note]]", 1, "> [!NOTE]\n> See [[note]]\n")]
fn test_callout_with_wikilink(#[case] input: &str, #[case] expected_nodes: usize, #[case] expected_output: &str) {
    use crate::Markdown;
    let md = Markdown::from_markdown_str(input).unwrap();
    assert_eq!(md.nodes.len(), expected_nodes);
    assert_eq!(md.to_string(), expected_output);
}

#[cfg(feature = "callout")]
#[rstest]
// callout body with bold
#[case("> [!NOTE]\n> **important**", 1, "> [!NOTE]\n> **important**\n")]
// callout body with inline code
#[case("> [!NOTE]\n> Use `code`", 1, "> [!NOTE]\n> Use `code`\n")]
// multiple callouts in one document
#[case(
    "> [!NOTE]\n> first\n\n> [!WARNING]\n> second",
    2,
    "> [!NOTE]\n> first\n\n> [!WARNING]\n> second\n"
)]
// callout after heading
#[case("# Title\n\n> [!NOTE]\n> body", 2, "# Title\n\n> [!NOTE]\n> body\n")]
// callout body with a multi-item list: second item must not be over-indented
#[case("> [!NOTE]\n> - item 1\n> - item 2", 1, "> [!NOTE]\n> - item 1\n> - item 2\n")]
fn test_callout_combined_with_other_nodes(
    #[case] input: &str,
    #[case] expected_nodes: usize,
    #[case] expected_output: &str,
) {
    use crate::Markdown;
    let md = Markdown::from_markdown_str(input).unwrap();
    assert_eq!(md.nodes.len(), expected_nodes);
    assert_eq!(md.to_string(), expected_output);
}

#[cfg(feature = "embed")]
#[rstest]
// embed inside list item
#[case("- ![[note.md]]", 1, "- ![[note.md]]\n")]
// embed inside heading
#[case("# See ![[note]]", 1, "# See ![[note]]\n")]
// embed inside bold
#[case("**![[note]]**", 1, "**![[note]]**\n")]
fn test_embed_combined_with_other_nodes(
    #[case] input: &str,
    #[case] expected_nodes: usize,
    #[case] expected_output: &str,
) {
    use crate::Markdown;
    let md = Markdown::from_markdown_str(input).unwrap();
    assert_eq!(md.nodes.len(), expected_nodes);
    assert_eq!(md.to_string(), expected_output);
}

#[cfg(all(feature = "callout", feature = "embed"))]
#[rstest]
// wikilink inside callout body + embed as separate node
#[case("> [!NOTE]\n> [[link]]\n\n![[embed]]", 2, "> [!NOTE]\n> [[link]]\n\n![[embed]]\n")]
fn test_callout_and_embed_together(#[case] input: &str, #[case] expected_nodes: usize, #[case] expected_output: &str) {
    use crate::Markdown;
    let md = Markdown::from_markdown_str(input).unwrap();
    assert_eq!(md.nodes.len(), expected_nodes);
    assert_eq!(md.to_string(), expected_output);
}

#[cfg(all(feature = "embed", feature = "wikilink"))]
#[test]
fn test_embed_does_not_conflict_with_wikilink() {
    use crate::Markdown;
    // ![[embed]] must become Embed, not WikiLink with preceding "!"
    let md = Markdown::from_markdown_str("![[embed]] and [[link]]").unwrap();
    let embed = md.nodes.iter().find(|n| n.is_embed());
    let wikilink = md.nodes.iter().find(|n| n.is_wikilink());
    assert!(embed.is_some(), "expected Embed node");
    assert!(wikilink.is_some(), "expected WikiLink node");
}

#[rstest]
#[case::escaped_when_parsed_from_source(
    Node::Text(Text {
        value: "*a*".to_string(),
        position: Some(Position {
            start: Point { line: 1, column: 1 },
            end: Point { line: 1, column: 4 },
        }),
    }),
    "\\*a\\*"
)]
#[case::untouched_when_built_programmatically(
    Node::Text(Text { value: "*a*".to_string(), position: None }),
    "*a*"
)]
fn test_text_escaping_gated_by_position(#[case] node: Node, #[case] expected: &str) {
    assert_eq!(node.to_string_with(&RenderOptions::default()), expected);
}

#[rstest]
#[case::bare_safe("https://example.com/x", UrlSurroundStyle::None, "https://example.com/x")]
#[case::space_forces_angle("my uri", UrlSurroundStyle::None, "<my uri>")]
#[case::empty_forces_angle("", UrlSurroundStyle::None, "<>")]
#[case::unbalanced_paren_escaped("b)c", UrlSurroundStyle::None, "b\\)c")]
#[case::both_parens_escaped("foo(and(bar)", UrlSurroundStyle::None, "foo\\(and\\(bar\\)")]
#[case::forced_angle_style("plain", UrlSurroundStyle::Angle, "<plain>")]
#[case::angle_escapes_inner_angle_and_backslash("foo\\>", UrlSurroundStyle::None, "<foo\\\\\\>>")]
fn test_render_link_destination(#[case] url: &str, #[case] style: UrlSurroundStyle, #[case] expected: &str) {
    assert_eq!(render_link_destination(url, &style), expected);
}

#[rstest]
#[case::double_quote_escaped(r#"a "b" c"#, TitleSurroundStyle::Double, r#""a \"b\" c""#)]
#[case::single_quote_escaped("a 'b' c", TitleSurroundStyle::Single, "'a \\'b\\' c'")]
#[case::paren_escapes_both("a (b) c", TitleSurroundStyle::Paren, "(a \\(b\\) c)")]
#[case::backslash_escaped("a\\b", TitleSurroundStyle::Double, "\"a\\\\b\"")]
fn test_render_link_title(#[case] title: &str, #[case] style: TitleSurroundStyle, #[case] expected: &str) {
    assert_eq!(render_link_title(title, &style), expected);
}

#[rstest]
#[case::shortcut_kept_when_ident_matches_label_present("foo", "foo", Some("foo".to_string()), "[foo]")]
#[case::shortcut_kept_when_ident_matches_label_absent("foo", "foo", None, "[foo]")]
#[case::full_form_when_ident_differs("foo", "bar", Some("bar".to_string()), "[foo][bar]")]
#[case::full_form_when_plain_needs_broad_escaping("foo*bar", "foo*bar", Some("foo*bar".to_string()), "[foo*bar][foo*bar]")]
fn test_link_ref_shortcut_vs_full(
    #[case] text: &str,
    #[case] ident: &str,
    #[case] label: Option<String>,
    #[case] expected: &str,
) {
    let node = Node::LinkRef(LinkRef {
        ident: ident.to_string(),
        values: vec![text.to_string().into()],
        label,
        position: None,
    });
    assert_eq!(node.to_string_with(&RenderOptions::default()), expected);
}

#[test]
fn test_link_ref_display_escaping_does_not_leak_into_matching_label() {
    // The display text (first bracket) is escaped for safety; the matching key
    // (second bracket) must stay the definition-compatible, narrowly-escaped label.
    let node = Node::LinkRef(LinkRef {
        ident: "foo*bar".to_string(),
        values: vec![Node::Text(Text {
            value: "foo*bar".to_string(),
            position: Some(Position {
                start: Point { line: 1, column: 1 },
                end: Point { line: 1, column: 8 },
            }),
        })],
        label: Some("foo*bar".to_string()),
        position: None,
    });
    assert_eq!(node.to_string_with(&RenderOptions::default()), "[foo\\*bar][foo*bar]");
}

#[rstest]
#[case::shortcut_kept_when_ident_matches("foo bar", "foo bar", "![foo bar]")]
#[case::full_form_when_ident_differs("foo bar", "foo*bar*", "![foo bar][foo*bar*]")]
fn test_image_ref_shortcut_vs_full(#[case] alt: &str, #[case] ident: &str, #[case] expected: &str) {
    let node = Node::ImageRef(ImageRef {
        alt: alt.to_string(),
        ident: ident.to_string(),
        label: None,
        position: None,
    });
    assert_eq!(node.to_string_with(&RenderOptions::default()), expected);
}

#[rstest]
#[case::no_newline_still_shifted("abc", 3, "   abc")]
#[case::zero_delta_unchanged("a\n  b", 0, "a\n  b")]
#[case::first_line_also_shifted("a\nb", 2, "  a\n  b")]
#[case::negative_delta_clamped_at_zero("  a\n  b", -5, "a\nb")]
fn test_reindent_all_lines(#[case] content: &str, #[case] delta: isize, #[case] expected: &str) {
    assert_eq!(reindent_all_lines(content, delta), expected);
}

#[rstest]
#[case::unordered_bullet(false, 0, None, 2)]
#[case::ordered_single_digit(true, 0, None, 3)]
#[case::ordered_double_digit(true, 9, None, 4)]
#[case::ordered_with_start(true, 0, Some(10), 4)]
fn test_list_own_prefix_width(
    #[case] ordered: bool,
    #[case] index: usize,
    #[case] start: Option<u32>,
    #[case] expected: usize,
) {
    assert_eq!(list_own_prefix_width(ordered, index, start), expected);
}

#[rstest]
#[case(0, None)]
#[case(1, Some(HeadingDepth::H1))]
#[case(6, Some(HeadingDepth::H6))]
#[case(7, None)]
fn heading_depth_new(#[case] depth: u8, #[case] expected: Option<HeadingDepth>) {
    assert_eq!(HeadingDepth::new(depth), expected);
    assert_eq!(HeadingDepth::try_from(depth).ok(), expected);
}

#[rstest]
#[case(i64::MIN, HeadingDepth::H1)]
#[case(0, HeadingDepth::H1)]
#[case(3, HeadingDepth::H3)]
#[case(257, HeadingDepth::H6)]
fn heading_depth_saturating(#[case] depth: i64, #[case] expected: HeadingDepth) {
    assert_eq!(HeadingDepth::saturating(depth), expected);
}

#[rstest]
#[case(HeadingDepth::H2, 3, Some(HeadingDepth::H5))]
#[case(HeadingDepth::H6, 1, None)]
#[case(HeadingDepth::H6, u8::MAX, None)]
fn heading_depth_checked_add(#[case] depth: HeadingDepth, #[case] levels: u8, #[case] expected: Option<HeadingDepth>) {
    assert_eq!(depth.checked_add(levels), expected);
}

#[rstest]
#[case(HeadingDepth::H4, 2, HeadingDepth::H2)]
#[case(HeadingDepth::H2, u8::MAX, HeadingDepth::H1)]
fn heading_depth_saturating_sub(#[case] depth: HeadingDepth, #[case] levels: u8, #[case] expected: HeadingDepth) {
    assert_eq!(depth.saturating_sub(levels), expected);
}

#[rstest]
#[case::integer(AttrValue::Integer(3), HeadingDepth::H3)]
#[case::string(AttrValue::String("4".to_string()), HeadingDepth::H4)]
#[case::too_deep(AttrValue::Integer(7), HeadingDepth::H6)]
#[case::large(AttrValue::Integer(257), HeadingDepth::H6)]
#[case::negative(AttrValue::Integer(-1), HeadingDepth::H1)]
#[case::zero_string(AttrValue::String("0".to_string()), HeadingDepth::H1)]
#[case::huge_float(AttrValue::Number(1e20), HeadingDepth::H6)]
#[case::huge_negative_float(AttrValue::Number(-1e20), HeadingDepth::H1)]
fn set_attr_clamps_heading_depth(#[case] value: AttrValue, #[case] expected: HeadingDepth) {
    let mut node = Node::Heading(Heading {
        depth: HeadingDepth::H2,
        values: vec!["a".to_string().into()],
        position: None,
    });
    node.set_attr(attr_keys::DEPTH, value).unwrap();
    assert!(matches!(node, Node::Heading(Heading { depth, .. }) if depth == expected));
}

#[rstest]
#[case::unknown_attr(Node::Text(Text { value: "a".into(), position: None }), "lang", AttrValue::String("x".into()))]
#[case::read_only(Node::Text(Text { value: "a".into(), position: None }), "line", AttrValue::Integer(1))]
#[case::bad_integer(Node::Heading(Heading { depth: HeadingDepth::H1, values: Vec::new(), position: None }), "depth", AttrValue::String("deep".into()))]
#[case::bad_bool(Node::Code(Code { value: "a".into(), lang: None, meta: None, fence: true, position: None }), "fence", AttrValue::String("yes".into()))]
#[case::negative_index(Node::List(List { values: Vec::new(), ordered: false, level: 0, index: 0, checked: None, marker: None, spread: false, start: None, position: None }), "index", AttrValue::Integer(-1))]
#[case::float_beyond_usize(Node::List(List { values: Vec::new(), ordered: false, level: 0, index: 0, checked: None, marker: None, spread: false, start: None, position: None }), "index", AttrValue::Number(1e20))]
#[case::float_beyond_usize_cell(Node::TableCell(TableCell { values: Vec::new(), row: 0, column: 0, position: None }), "row", AttrValue::Number(1e20))]
#[case::nan(Node::Heading(Heading { depth: HeadingDepth::H1, values: Vec::new(), position: None }), "depth", AttrValue::Number(f64::NAN))]
fn set_attr_rejects_invalid(#[case] mut node: Node, #[case] attr: &str, #[case] value: AttrValue) {
    let before = node.clone();
    assert!(node.set_attr(attr, value).is_err());
    assert_eq!(node, before);
}

fn list_node(index: usize, level: u8, ordered: bool, checked: Option<bool>) -> Node {
    Node::List(List {
        values: Vec::new(),
        ordered,
        level,
        index,
        checked,
        marker: None,
        spread: false,
        start: None,
        position: None,
    })
}

fn cell_node(row: usize, column: usize) -> Node {
    Node::TableCell(TableCell {
        values: Vec::new(),
        row,
        column,
        position: None,
    })
}

fn code_node(lang: Option<&str>, meta: Option<&str>, fence: bool) -> Node {
    Node::Code(Code {
        value: "a".into(),
        lang: lang.map(Into::into),
        meta: meta.map(Into::into),
        fence,
        position: None,
    })
}

fn link_node(url: &str, title: Option<&str>) -> Node {
    Node::Link(Link {
        url: Url::new(url.to_string()),
        title: title.map(|t| Title::new(t.to_string())),
        values: Vec::new(),
        position: None,
    })
}

fn heading_node(depth: HeadingDepth) -> Node {
    Node::Heading(Heading {
        depth,
        values: Vec::new(),
        position: None,
    })
}

#[rstest]
#[case::list_index_integer(
    list_node(0, 0, false, None),
    "index",
    AttrValue::Integer(3),
    list_node(3, 0, false, None)
)]
#[case::list_index_string(list_node(0, 0, false, None), "index", AttrValue::String("4".into()), list_node(4, 0, false, None))]
#[case::list_index_float(
    list_node(0, 0, false, None),
    "index",
    AttrValue::Number(5.0),
    list_node(5, 0, false, None)
)]
#[case::list_level(
    list_node(0, 0, false, None),
    "level",
    AttrValue::Integer(2),
    list_node(0, 2, false, None)
)]
#[case::list_ordered_bool(
    list_node(0, 0, false, None),
    "ordered",
    AttrValue::Boolean(true),
    list_node(0, 0, true, None)
)]
#[case::list_ordered_string(list_node(0, 0, true, None), "ordered", AttrValue::String("false".into()), list_node(0, 0, false, None))]
#[case::list_checked_bool(
    list_node(0, 0, false, None),
    "checked",
    AttrValue::Boolean(true),
    list_node(0, 0, false, Some(true))
)]
#[case::list_checked_empty_clears(
    list_node(0, 0, false, Some(true)),
    "checked",
    AttrValue::String(String::new()),
    list_node(0, 0, false, None)
)]
#[case::cell_row(cell_node(0, 0), "row", AttrValue::Integer(2), cell_node(2, 0))]
#[case::cell_column_string(cell_node(0, 0), "column", AttrValue::String("3".into()), cell_node(0, 3))]
#[case::cell_column_float(cell_node(0, 0), "column", AttrValue::Number(1.0), cell_node(0, 1))]
#[case::code_lang(code_node(None, None, true), "lang", AttrValue::String("rust".into()), code_node(Some("rust"), None, true))]
#[case::code_language_alias(code_node(None, None, true), "language", AttrValue::String("rust".into()), code_node(Some("rust"), None, true))]
#[case::code_lang_empty_clears(
    code_node(Some("rust"), None, true),
    "lang",
    AttrValue::String(String::new()),
    code_node(None, None, true)
)]
#[case::code_meta(code_node(None, None, true), "meta", AttrValue::String("title=x".into()), code_node(None, Some("title=x"), true))]
#[case::code_meta_empty_clears(
    code_node(None, Some("m"), true),
    "meta",
    AttrValue::String(String::new()),
    code_node(None, None, true)
)]
#[case::code_fence_bool(
    code_node(None, None, true),
    "fence",
    AttrValue::Boolean(false),
    code_node(None, None, false)
)]
#[case::code_fence_string(code_node(None, None, false), "fence", AttrValue::String("true".into()), code_node(None, None, true))]
#[case::link_url(link_node("a", None), "url", AttrValue::String("b".into()), link_node("b", None))]
#[case::link_title(link_node("a", None), "title", AttrValue::String("t".into()), link_node("a", Some("t")))]
#[case::link_title_empty_clears(
    link_node("a", Some("t")),
    "title",
    AttrValue::String(String::new()),
    link_node("a", None)
)]
#[case::heading_depth_float(
    heading_node(HeadingDepth::H1),
    "depth",
    AttrValue::Number(3.0),
    heading_node(HeadingDepth::H3)
)]
#[case::heading_level_alias(
    heading_node(HeadingDepth::H1),
    "level",
    AttrValue::Integer(2),
    heading_node(HeadingDepth::H2)
)]
fn set_attr_typed_values(#[case] mut node: Node, #[case] attr: &str, #[case] value: AttrValue, #[case] expected: Node) {
    assert_eq!(node.set_attr(attr, value), Ok(()));
    assert_eq!(node, expected);
}

#[rstest]
#[case::unknown_on_text(Node::Text(Text { value: "a".into(), position: None }), "nope")]
#[case::unknown_on_link(link_node("a", None), "lang")]
#[case::unknown_on_list(list_node(0, 0, false, None), "url")]
#[case::unknown_on_cell(cell_node(0, 0), "index")]
#[case::unknown_on_code(code_node(None, None, true), "depth")]
#[case::unknown_on_heading(heading_node(HeadingDepth::H1), "url")]
fn set_attr_unknown_attr_error(#[case] mut node: Node, #[case] attr: &str) {
    let before = node.clone();
    let kind = node.kind();
    assert_eq!(
        node.set_attr(attr, "x"),
        Err(SetAttrError::UnknownAttr {
            attr: attr.to_string(),
            kind
        })
    );
    assert_eq!(node, before);
}

#[rstest]
#[case::list_index_fraction(list_node(1, 0, false, None), "index", AttrValue::Number(1.5))]
#[case::list_index_text(list_node(1, 0, false, None), "index", AttrValue::String("x".into()))]
#[case::list_index_bool(list_node(1, 0, false, None), "index", AttrValue::Boolean(true))]
#[case::list_level_overflow(list_node(1, 1, false, None), "level", AttrValue::Integer(256))]
#[case::list_ordered_int(list_node(1, 0, false, None), "ordered", AttrValue::Integer(1))]
#[case::list_checked_text(list_node(1, 0, false, None), "checked", AttrValue::String("maybe".into()))]
#[case::cell_row_negative(cell_node(1, 1), "row", AttrValue::Integer(-1))]
#[case::cell_column_negative_float(cell_node(1, 1), "column", AttrValue::Number(-1.0))]
#[case::cell_column_infinite(cell_node(1, 1), "column", AttrValue::Number(f64::INFINITY))]
#[case::code_fence_int(code_node(None, None, true), "fence", AttrValue::Integer(0))]
#[case::heading_infinite(heading_node(HeadingDepth::H2), "depth", AttrValue::Number(f64::INFINITY))]
fn set_attr_invalid_value_error(#[case] mut node: Node, #[case] attr: &str, #[case] value: AttrValue) {
    let before = node.clone();
    assert!(matches!(
        node.set_attr(attr, value),
        Err(SetAttrError::InvalidValue { attr: a, .. }) if a == attr
    ));
    assert_eq!(node, before);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn set_attr_accepts_float_above_i64_max_within_usize() {
    let mut node = Node::List(List {
        values: Vec::new(),
        ordered: false,
        level: 0,
        index: 0,
        checked: None,
        marker: None,
        spread: false,
        start: None,
        position: None,
    });
    node.set_attr(attr_keys::INDEX, AttrValue::Number(1e19)).unwrap();
    assert!(matches!(node, Node::List(List { index, .. }) if index == 10_000_000_000_000_000_000usize));
}

#[cfg(feature = "json")]
#[test]
fn heading_depth_is_a_number_in_json() {
    let heading = Heading {
        depth: HeadingDepth::H3,
        values: Vec::new(),
        position: None,
    };
    let json = serde_json::to_string(&heading).unwrap();
    assert!(json.contains("\"depth\":3"), "{json}");
    assert_eq!(serde_json::from_str::<Heading>(&json).unwrap(), heading);
    assert!(serde_json::from_str::<Heading>(&json.replace("\"depth\":3", "\"depth\":7")).is_err());
}

#[test]
fn heading_depth_compares_with_u8() {
    assert!(HeadingDepth::H2 == 2u8);
    assert!(2u8 == HeadingDepth::H2);
    assert!(HeadingDepth::H2 < 3u8);
    assert!(3u8 > HeadingDepth::H2);
    assert_eq!(usize::from(HeadingDepth::H3), 3);
    assert_eq!(i64::from(HeadingDepth::H3), 3);
}