xacro-rs 0.2.2

A xml preprocessor for xacro files to generate URDF files
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
pub mod document;
pub mod macro_def;
pub mod xml;

// Re-export only what's used internally
pub(crate) use document::*;
#[cfg(test)]
mod macro_tests {
    use std::collections::{HashMap, HashSet};

    use super::macro_def::{Element, MacroDefinition, MacroProcessor, ParamDefault};

    use crate::XacroError;
    use xmltree::XMLNode;

    #[test]
    fn test_empty_param_name_with_default() {
        let result = MacroProcessor::parse_params(":=foo");

        let err = result.expect_err("Should error for empty parameter name");
        assert!(
            matches!(
                err,
                XacroError::InvalidParameterName { ref param } if param == ":=foo"
            ),
            "Expected InvalidParameterName error, got: {:?}",
            err
        );
    }

    #[test]
    fn test_duplicate_param_block_and_regular() {
        let result = MacroProcessor::parse_params("*foo foo");

        let err = result.expect_err("Should error for duplicate param declaration");
        assert!(
            matches!(
                err,
                XacroError::DuplicateParamDeclaration { ref param } if param == "foo"
            ),
            "Expected DuplicateParamDeclaration error, got: {:?}",
            err
        );
    }

    #[test]
    fn test_duplicate_param_with_defaults() {
        let result = MacroProcessor::parse_params("x:=1 x:=2");

        let err = result.expect_err("Should error for duplicate param with defaults");
        assert!(
            matches!(
                err,
                XacroError::DuplicateParamDeclaration { ref param } if param == "x"
            ),
            "Expected DuplicateParamDeclaration error, got: {:?}",
            err
        );
    }

    #[test]
    fn test_block_param_attribute_collision() {
        // Create a macro definition with block param "*content"
        let (params, param_order, block_params, _lazy_block_params) =
            MacroProcessor::parse_params("*content").expect("Valid params");

        let macro_def = MacroDefinition {
            name: "test".to_string(),
            params,
            param_order,
            block_params,
            lazy_block_params: HashSet::new(),
            content: Element::new("dummy"),
        };

        // Create a call element with attribute content="bar"
        let mut call_elem = Element::new("test");
        call_elem
            .attributes
            .insert(xmltree::AttributeName::local("content"), "bar".to_string());

        // Should error - block param specified as attribute
        let result = MacroProcessor::collect_macro_args(&call_elem, &macro_def);
        let err = result.expect_err("Should error for block param as attribute");
        assert!(
            matches!(
                err,
                XacroError::BlockParameterAttributeCollision { ref param } if param == "content"
            ),
            "Expected BlockParameterAttributeCollision error, got: {:?}",
            err
        );
    }

    #[test]
    fn test_namespaced_macro_parameters_rejected() {
        // Test that namespaced attributes are REJECTED on macro calls
        // (Python xacro behavior: "Invalid parameter 'foo:x'")
        let mut params = HashMap::new();
        params.insert("x".to_string(), ParamDefault::None);

        let param_order = vec!["x".to_string()];
        let block_params = HashSet::new();

        let macro_def = MacroDefinition {
            name: "test".to_string(),
            params,
            param_order,
            block_params,
            lazy_block_params: HashSet::new(),
            content: Element::new("dummy"),
        };

        // Create call with namespaced foo:x attribute
        let mut call_elem = Element::new("test");
        call_elem.attributes.insert(
            xmltree::AttributeName {
                local_name: "x".to_string(),
                namespace: Some("http://example.com/foo".to_string()),
                prefix: Some("foo".to_string()),
            },
            "1".to_string(),
        );

        // Should error - macro parameters cannot have namespace prefixes
        let result = MacroProcessor::collect_macro_args(&call_elem, &macro_def);
        let err = result.expect_err("Should reject namespaced macro parameter");
        assert!(
            matches!(
                err,
                XacroError::InvalidMacroParameter { ref param, .. } if param == "foo:x"
            ),
            "Expected InvalidMacroParameter for 'foo:x', got: {:?}",
            err
        );
    }

    #[test]
    fn test_parameterless_macro() {
        // Empty params string should parse successfully
        let result = MacroProcessor::parse_params("");
        assert!(result.is_ok(), "Empty params should be valid");

        let (params, param_order, block_params, lazy_block_params) = result.unwrap();
        assert!(params.is_empty(), "Should have no params");
        assert!(param_order.is_empty(), "Should have no param order");
        assert!(block_params.is_empty(), "Should have no block params");
        assert!(
            lazy_block_params.is_empty(),
            "Should have no lazy block params"
        );
    }

    #[test]
    fn test_lazy_block_param_single() {
        // Single lazy block param using ** syntax
        let result = MacroProcessor::parse_params("**blk");
        assert!(
            result.is_ok(),
            "Single lazy block param using ** syntax should parse"
        );

        let (params, param_order, block_params, lazy_block_params) = result.unwrap();
        assert_eq!(params.len(), 1, "Exactly one param should be parsed");
        assert!(params.contains_key("blk"), "Param map should contain `blk`");
        assert_eq!(
            param_order,
            vec!["blk"],
            "Param order should contain only `blk`"
        );
        assert!(
            block_params.contains("blk"),
            "`blk` should be treated as a block param"
        );
        assert_eq!(
            block_params.len(),
            1,
            "Only `blk` should be in block_params for **blk"
        );
        assert!(
            lazy_block_params.contains("blk"),
            "`blk` should be tracked as a lazy block param"
        );
        assert_eq!(
            lazy_block_params.len(),
            1,
            "Only `blk` should be in lazy_block_params for **blk"
        );
    }

    #[test]
    fn test_regular_block_param_single() {
        // Single regular block param using * syntax
        let result = MacroProcessor::parse_params("*blk");
        assert!(
            result.is_ok(),
            "Single regular block param using * syntax should parse"
        );

        let (params, param_order, block_params, lazy_block_params) = result.unwrap();
        assert_eq!(params.len(), 1);
        assert!(params.contains_key("blk"));
        assert_eq!(param_order, vec!["blk"]);
        assert!(
            block_params.contains("blk"),
            "`blk` should be a block param"
        );
        assert_eq!(block_params.len(), 1);
        assert!(
            !lazy_block_params.contains("blk"),
            "`blk` should NOT be lazy (only one star)"
        );
        assert!(
            lazy_block_params.is_empty(),
            "No lazy block params for *blk"
        );
    }

    #[test]
    fn test_mixed_block_params() {
        // Mixed block / lazy-block / normal params
        let result = MacroProcessor::parse_params("*a **b c");
        assert!(
            result.is_ok(),
            "Mixed '*a **b c' params should parse correctly"
        );

        let (params, param_order, block_params, lazy_block_params) = result.unwrap();
        assert_eq!(
            params.len(),
            3,
            "Three params should be parsed from '*a **b c'"
        );
        assert!(params.contains_key("a"));
        assert!(params.contains_key("b"));
        assert!(params.contains_key("c"));

        assert_eq!(
            param_order,
            vec!["a", "b", "c"],
            "Param order should follow the declaration order for '*a **b c'"
        );

        // Block params: both *a and **b are block parameters
        assert!(
            block_params.contains("a"),
            "`a` should be treated as a block param"
        );
        assert!(
            block_params.contains("b"),
            "`b` should be treated as a block param"
        );
        assert!(
            !block_params.contains("c"),
            "`c` should not be treated as a block param"
        );
        assert_eq!(
            block_params.len(),
            2,
            "Only `a` and `b` should be in block_params for '*a **b c'"
        );

        // Lazy-block params: only **b is lazy
        assert!(
            !lazy_block_params.contains("a"),
            "`a` should not be treated as a lazy block param"
        );
        assert!(
            lazy_block_params.contains("b"),
            "`b` should be treated as a lazy block param"
        );
        assert!(
            !lazy_block_params.contains("c"),
            "`c` should not be treated as a lazy block param"
        );
        assert_eq!(
            lazy_block_params.len(),
            1,
            "Only `b` should be in lazy_block_params for '*a **b c'"
        );
    }

    #[test]
    fn test_regular_params_with_defaults() {
        // Regular params with defaults should not be block params
        let result = MacroProcessor::parse_params("x:=1 y:=2.5 z:=foo");
        assert!(result.is_ok(), "Regular params with defaults should parse");

        let (params, param_order, block_params, lazy_block_params) = result.unwrap();

        assert_eq!(params.len(), 3);
        assert_eq!(param_order, vec!["x", "y", "z"]);

        // None of these should be block params
        assert!(
            block_params.is_empty(),
            "Defaulted params are not block params"
        );
        assert!(
            lazy_block_params.is_empty(),
            "Defaulted params are not lazy block params"
        );
    }

    #[test]
    fn test_triple_asterisk_rejected() {
        // Triple asterisk should be rejected (invalid)
        let result = MacroProcessor::parse_params("***foo");
        assert!(
            result.is_err(),
            "Triple asterisk should be rejected as invalid"
        );
        assert!(
            matches!(result.unwrap_err(), XacroError::InvalidParameterName { .. }),
            "Should return InvalidParameterName error"
        );
    }

    #[test]
    fn test_insert_block_empty_param_name() {
        // Block param with empty name (just "*") should error
        let result = MacroProcessor::parse_params("*");

        let err = result.expect_err("Empty block param name should error");
        assert!(
            matches!(
                err,
                XacroError::InvalidParameterName { ref param } if param == "*"
            ),
            "Expected InvalidParameterName error, got: {:?}",
            err
        );
    }

    #[test]
    fn test_insert_block_with_default_param() {
        // Block params cannot have defaults
        let result = MacroProcessor::parse_params("*content:=default");

        let err = result.expect_err("Block param with default should error");
        assert!(
            matches!(
                err,
                XacroError::BlockParameterWithDefault { ref param } if param.contains("content")
            ),
            "Expected BlockParameterWithDefault error, got: {:?}",
            err
        );
    }

    // ========== Additional Unit Tests for parse_params ==========

    #[test]
    fn test_parse_params_regular_with_defaults() {
        let result = MacroProcessor::parse_params("x:=1 y:=2.5 z:=foo");
        assert!(result.is_ok(), "Regular params with defaults should parse");

        let (params, param_order, block_params, _lazy_block_params) = result.unwrap();

        // Check params map
        assert_eq!(params.len(), 3);
        assert_eq!(params.get("x"), Some(&ParamDefault::Value("1".to_string())));
        assert_eq!(
            params.get("y"),
            Some(&ParamDefault::Value("2.5".to_string()))
        );
        assert_eq!(
            params.get("z"),
            Some(&ParamDefault::Value("foo".to_string()))
        );

        // Check param order preservation
        assert_eq!(param_order, vec!["x", "y", "z"]);

        // Check no block params
        assert!(block_params.is_empty());
    }

    #[test]
    fn test_parse_params_regular_without_defaults() {
        let result = MacroProcessor::parse_params("a b c");
        assert!(
            result.is_ok(),
            "Regular params without defaults should parse"
        );

        let (params, param_order, block_params, _lazy_block_params) = result.unwrap();

        // Check params map (None = no default)
        assert_eq!(params.len(), 3);
        assert_eq!(params.get("a"), Some(&ParamDefault::None));
        assert_eq!(params.get("b"), Some(&ParamDefault::None));
        assert_eq!(params.get("c"), Some(&ParamDefault::None));

        // Check param order
        assert_eq!(param_order, vec!["a", "b", "c"]);

        // Check no block params
        assert!(block_params.is_empty());
    }

    #[test]
    fn test_parse_params_block_params() {
        let result = MacroProcessor::parse_params("*origin *geometry");
        assert!(result.is_ok(), "Block params should parse");

        let (params, param_order, block_params, _lazy_block_params) = result.unwrap();

        // Check params map (block params have None value)
        assert_eq!(params.len(), 2);
        assert_eq!(params.get("origin"), Some(&ParamDefault::None));
        assert_eq!(params.get("geometry"), Some(&ParamDefault::None));

        // Check param order
        assert_eq!(param_order, vec!["origin", "geometry"]);

        // Check block params set
        assert_eq!(block_params.len(), 2);
        assert!(block_params.contains("origin"));
        assert!(block_params.contains("geometry"));
    }

    #[test]
    fn test_parse_params_mixed_regular_and_block() {
        let result = MacroProcessor::parse_params("prefix *origin suffix:=default");
        assert!(result.is_ok(), "Mixed params should parse");

        let (params, param_order, block_params, _lazy_block_params) = result.unwrap();

        // Check all params present
        assert_eq!(params.len(), 3);
        assert_eq!(params.get("prefix"), Some(&ParamDefault::None));
        assert_eq!(params.get("origin"), Some(&ParamDefault::None));
        assert_eq!(
            params.get("suffix"),
            Some(&ParamDefault::Value("default".to_string()))
        );

        // Check param order preservation
        assert_eq!(param_order, vec!["prefix", "origin", "suffix"]);

        // Check only "origin" is a block param
        assert_eq!(block_params.len(), 1);
        assert!(block_params.contains("origin"));
    }

    #[test]
    fn test_parse_params_default_with_expression() {
        let result = MacroProcessor::parse_params("angle:=${pi/2} scale:=${2*base}");
        assert!(result.is_ok(), "Defaults with expressions should parse");

        let (params, _param_order, _block_params, _lazy_block_params) = result.unwrap();

        // Parser doesn't evaluate, just stores the raw string
        assert_eq!(
            params.get("angle"),
            Some(&ParamDefault::Value("${pi/2}".to_string()))
        );
        assert_eq!(
            params.get("scale"),
            Some(&ParamDefault::Value("${2*base}".to_string()))
        );
    }

    // ========== Additional Unit Tests for collect_macro_args ==========

    #[test]
    fn test_collect_macro_args_attributes_only() {
        // Create macro definition with regular params
        let (params, param_order, block_params, _lazy_block_params) =
            MacroProcessor::parse_params("x y z:=default").expect("Valid params");

        let macro_def = MacroDefinition {
            name: "test".to_string(),
            params,
            param_order,
            block_params,
            lazy_block_params: HashSet::new(),
            content: Element::new("dummy"),
        };

        // Create call element with attributes
        let mut call_elem = Element::new("test");
        call_elem
            .attributes
            .insert(xmltree::AttributeName::local("x"), "1".to_string());
        call_elem
            .attributes
            .insert(xmltree::AttributeName::local("y"), "2".to_string());

        // Should successfully collect attributes
        let result = MacroProcessor::collect_macro_args(&call_elem, &macro_def);
        assert!(result.is_ok(), "Should collect attributes successfully");

        let (args, blocks) = result.unwrap();
        assert_eq!(args.len(), 2);
        assert_eq!(args.get("x"), Some(&"1".to_string()));
        assert_eq!(args.get("y"), Some(&"2".to_string()));
        assert!(blocks.is_empty(), "Should have no blocks");
    }

    #[test]
    fn test_collect_macro_args_blocks_only() {
        // Create macro definition with block params
        let (params, param_order, block_params, _lazy_block_params) =
            MacroProcessor::parse_params("*origin *geometry").expect("Valid params");

        let macro_def = MacroDefinition {
            name: "test".to_string(),
            params,
            param_order,
            block_params,
            lazy_block_params: HashSet::new(),
            content: Element::new("dummy"),
        };

        // Create call element with child elements
        let mut call_elem = Element::new("test");

        let mut origin_elem = Element::new("origin");
        origin_elem
            .attributes
            .insert(xmltree::AttributeName::local("xyz"), "0 0 0".to_string());

        let mut geometry_elem = Element::new("cylinder");
        geometry_elem
            .attributes
            .insert(xmltree::AttributeName::local("radius"), "0.1".to_string());

        call_elem
            .children
            .push(XMLNode::Element(origin_elem.clone()));
        call_elem
            .children
            .push(XMLNode::Element(geometry_elem.clone()));

        // Should successfully collect blocks
        let result = MacroProcessor::collect_macro_args(&call_elem, &macro_def);
        assert!(result.is_ok(), "Should collect blocks successfully");

        let (args, blocks) = result.unwrap();
        assert!(args.is_empty(), "Should have no args");
        assert_eq!(blocks.len(), 2);

        // Verify blocks captured in order
        let origin_block = blocks.get("origin").expect("origin block");
        assert_eq!(origin_block.name, "origin");
        assert_eq!(
            origin_block.get_attribute("xyz").map(String::as_str),
            Some("0 0 0")
        );

        let geometry_block = blocks.get("geometry").expect("geometry block");
        assert_eq!(geometry_block.name, "cylinder");
    }

    #[test]
    fn test_collect_macro_args_missing_block_parameter() {
        // Create macro definition expecting a block param
        let (params, param_order, block_params, _lazy_block_params) =
            MacroProcessor::parse_params("*content").expect("Valid params");

        let macro_def = MacroDefinition {
            name: "test".to_string(),
            params,
            param_order,
            block_params,
            lazy_block_params: HashSet::new(),
            content: Element::new("dummy"),
        };

        // Create call element with NO children
        let call_elem = Element::new("test");

        // Should error - missing required block parameter
        let result = MacroProcessor::collect_macro_args(&call_elem, &macro_def);
        let err = result.expect_err("Should error for missing block parameter");

        assert!(
            matches!(
                err,
                XacroError::MissingBlockParameter { ref macro_name, ref param }
                if macro_name == "test" && param == "content"
            ),
            "Expected MissingBlockParameter error, got: {:?}",
            err
        );
    }

    #[test]
    fn test_collect_macro_args_extra_children() {
        // Create macro definition expecting 1 block param
        let (params, param_order, block_params, _lazy_block_params) =
            MacroProcessor::parse_params("*content").expect("Valid params");

        let macro_def = MacroDefinition {
            name: "test".to_string(),
            params,
            param_order,
            block_params,
            lazy_block_params: HashSet::new(),
            content: Element::new("dummy"),
        };

        // Create call element with MORE children than expected
        let mut call_elem = Element::new("test");
        call_elem
            .children
            .push(XMLNode::Element(Element::new("child1")));
        call_elem
            .children
            .push(XMLNode::Element(Element::new("child2")));
        call_elem
            .children
            .push(XMLNode::Element(Element::new("child3")));

        // Should error - too many children provided
        let result = MacroProcessor::collect_macro_args(&call_elem, &macro_def);
        let err = result.expect_err("Should error for extra children");

        assert!(
            matches!(
                err,
                XacroError::UnusedBlock { ref macro_name, extra_count }
                if macro_name == "test" && extra_count == 2
            ),
            "Expected UnusedBlock error with extra_count=2, got: {:?}",
            err
        );
    }

    #[test]
    fn test_collect_macro_args_mixed_params_and_blocks() {
        // Create macro definition with both types
        let (params, param_order, block_params, _lazy_block_params) =
            MacroProcessor::parse_params("prefix *content suffix").expect("Valid params");

        let macro_def = MacroDefinition {
            name: "test".to_string(),
            params,
            param_order,
            block_params,
            lazy_block_params: HashSet::new(),
            content: Element::new("dummy"),
        };

        // Create call element with attributes and child
        let mut call_elem = Element::new("test");
        call_elem
            .attributes
            .insert(xmltree::AttributeName::local("prefix"), "pre_".to_string());
        call_elem
            .attributes
            .insert(xmltree::AttributeName::local("suffix"), "_post".to_string());

        let mut content_elem = Element::new("link");
        content_elem
            .attributes
            .insert(xmltree::AttributeName::local("name"), "base".to_string());
        call_elem.children.push(XMLNode::Element(content_elem));

        // Should successfully collect both
        let result = MacroProcessor::collect_macro_args(&call_elem, &macro_def);
        assert!(result.is_ok(), "Should collect mixed args successfully");

        let (args, blocks) = result.unwrap();

        // Verify attributes
        assert_eq!(args.len(), 2);
        assert_eq!(args.get("prefix"), Some(&"pre_".to_string()));
        assert_eq!(args.get("suffix"), Some(&"_post".to_string()));

        // Verify block
        assert_eq!(blocks.len(), 1);
        let content_block = blocks.get("content").expect("content block");
        assert_eq!(content_block.name, "link");
        assert_eq!(
            content_block.get_attribute("name").map(String::as_str),
            Some("base")
        );
    }

    /// Test parsing params with single-quoted default value containing spaces
    #[test]
    fn test_parse_params_quoted_single_word() {
        let result = MacroProcessor::parse_params("name:='value'");
        assert!(result.is_ok(), "Should parse single-quoted value");

        let (params, order, blocks, _lazy_blocks) = result.unwrap();
        assert_eq!(params.len(), 1);
        assert_eq!(order.len(), 1);
        assert_eq!(blocks.len(), 0);
        assert_eq!(
            params.get("name"),
            Some(&ParamDefault::Value("value".to_string()))
        );
    }

    /// Test parsing params with single-quoted default value containing spaces
    #[test]
    fn test_parse_params_quoted_multi_word_single_quotes() {
        let result = MacroProcessor::parse_params("rpy:='0 0 0'");
        assert!(result.is_ok(), "Should parse multi-word quoted value");

        let (params, order, blocks, _lazy_blocks) = result.unwrap();
        assert_eq!(params.len(), 1);
        assert_eq!(order.len(), 1);
        assert_eq!(blocks.len(), 0);
        assert_eq!(
            params.get("rpy"),
            Some(&ParamDefault::Value("0 0 0".to_string()))
        );
    }

    /// Test parsing params with double-quoted default value containing spaces
    #[test]
    fn test_parse_params_quoted_multi_word_double_quotes() {
        let result = MacroProcessor::parse_params("xyz:=\"1 2 3\"");
        assert!(
            result.is_ok(),
            "Should parse multi-word double-quoted value"
        );

        let (params, _order, _blocks, _lazy_blocks) = result.unwrap();
        assert_eq!(params.len(), 1);
        assert_eq!(
            params.get("xyz"),
            Some(&ParamDefault::Value("1 2 3".to_string()))
        );
    }

    /// Test parsing multiple params with quoted defaults (real Franka example)
    #[test]
    fn test_parse_params_franka_hand_style() {
        let result = MacroProcessor::parse_params(
            "connected_to:='' ns:='' rpy:='0 0 0' xyz:='0 0 0' safety_distance:=0",
        );
        assert!(
            result.is_ok(),
            "Should parse Franka hand-style params: {:?}",
            result.err()
        );

        let (params, order, blocks, _lazy_blocks) = result.unwrap();
        assert_eq!(params.len(), 5);
        assert_eq!(order.len(), 5);
        assert_eq!(blocks.len(), 0);

        assert_eq!(
            params.get("connected_to"),
            Some(&ParamDefault::Value("".to_string()))
        );
        assert_eq!(params.get("ns"), Some(&ParamDefault::Value("".to_string())));
        assert_eq!(
            params.get("rpy"),
            Some(&ParamDefault::Value("0 0 0".to_string()))
        );
        assert_eq!(
            params.get("xyz"),
            Some(&ParamDefault::Value("0 0 0".to_string()))
        );
        assert_eq!(
            params.get("safety_distance"),
            Some(&ParamDefault::Value("0".to_string()))
        );
    }

    /// Test parsing mixed params: quoted defaults, unquoted defaults, and block params
    #[test]
    fn test_parse_params_mixed_quoted_unquoted_blocks() {
        let result = MacroProcessor::parse_params("pos:='1 2 3' scale:=0.5 *content name:=test");
        assert!(
            result.is_ok(),
            "Should parse mixed params: {:?}",
            result.err()
        );

        let (params, order, blocks, _lazy_blocks) = result.unwrap();

        // Verify we parsed all 4 parameters
        assert_eq!(params.len(), 4, "Expected 4 parameters total");

        // Verify the exact ordering of parameters (critical for block param matching)
        let expected_order: Vec<String> = vec!["pos", "scale", "content", "name"]
            .into_iter()
            .map(String::from)
            .collect();
        assert_eq!(
            order, expected_order,
            "Parameter order should match declaration sequence"
        );

        // Verify the exact set of block parameters
        assert_eq!(blocks.len(), 1, "Expected exactly one block parameter");
        assert!(
            blocks.contains("content"),
            "Blocks set should contain 'content'"
        );

        // Verify parameter values
        assert_eq!(
            params.get("pos"),
            Some(&ParamDefault::Value("1 2 3".to_string()))
        );
        assert_eq!(
            params.get("scale"),
            Some(&ParamDefault::Value("0.5".to_string()))
        );
        assert_eq!(params.get("content"), Some(&ParamDefault::None)); // Block param has no default
        assert_eq!(
            params.get("name"),
            Some(&ParamDefault::Value("test".to_string()))
        );
    }

    /// Test that quoted defaults with ':=' inside quotes are handled correctly
    #[test]
    fn test_parse_params_complex_quoted_content() {
        let result = MacroProcessor::parse_params("expr:='x:=5 y:=10' name:=test");
        assert!(
            result.is_ok(),
            "Should handle ':=' inside quoted string: {:?}",
            result.err()
        );

        let (params, order, _blocks, _lazy_blocks) = result.unwrap();
        assert_eq!(params.len(), 2);
        assert_eq!(order, vec!["expr", "name"]);
        assert_eq!(
            params.get("expr"),
            Some(&ParamDefault::Value("x:=5 y:=10".to_string()))
        );
        assert_eq!(
            params.get("name"),
            Some(&ParamDefault::Value("test".to_string()))
        );
    }

    /// Test that block parameters with quoted defaults are rejected
    #[test]
    fn test_parse_params_block_param_with_quoted_default_is_error() {
        // "*content" is a block parameter; it must not have a default, quoted or otherwise
        let result = MacroProcessor::parse_params("*content:='foo bar'");

        // Ensure we still reject block parameters with defaults when the default is quoted
        assert!(
            matches!(result, Err(XacroError::BlockParameterWithDefault { .. })),
            "Expected BlockParameterWithDefault error for quoted default on block param, got: {:?}",
            result
        );
    }

    /// Test that duplicate parameters are rejected when one has a quoted default
    #[test]
    fn test_parse_params_duplicate_with_quoted_default_is_error() {
        // Same parameter name "rpy" appears twice, once with a quoted default
        let result = MacroProcessor::parse_params("rpy:='0 0 0' rpy:=1");

        // Ensure duplicate detection still works with quoted defaults and String-based token handling
        assert!(
            matches!(result, Err(XacroError::DuplicateParamDeclaration { .. })),
            "Expected DuplicateParamDeclaration error for duplicate param with quoted default, got: {:?}",
            result
        );
    }

    /// Test edge case: single-character value that's not a quote
    #[test]
    fn test_parse_params_single_char_value() {
        // This was a potential panic case before using strip_prefix/strip_suffix
        // A value like "p:=x" (single char) should work fine
        let result = MacroProcessor::parse_params("p:=x");

        assert!(
            result.is_ok(),
            "Should parse single-char non-quoted value: {:?}",
            result.err()
        );

        let (params, _, _, _) = result.unwrap();
        assert_eq!(params.get("p"), Some(&ParamDefault::Value("x".to_string())));
    }

    /// Test edge case: properly quoted single-character string
    #[test]
    fn test_parse_params_single_char_quoted_properly() {
        // A properly quoted single character: "p:='x'"
        let result = MacroProcessor::parse_params("p:='x'");

        assert!(
            result.is_ok(),
            "Should parse single-char quoted value: {:?}",
            result.err()
        );

        let (params, _, _, _) = result.unwrap();
        // Quote-stripping should extract the 'x'
        assert_eq!(params.get("p"), Some(&ParamDefault::Value("x".to_string())));
    }

    /// Test escape sequences in quoted defaults (NOT YET SUPPORTED)
    ///
    /// This test documents a limitation: the tokenizer does not support escape
    /// sequences like \' or \" within quoted strings.
    ///
    /// Decision: Deferred pending verification that Python xacro supports this.
    #[test]
    #[ignore = "Escape sequences not yet supported"]
    fn test_parse_params_escape_sequences_not_supported() {
        // Example: Literal single quote inside single-quoted string
        // Expected: name="it's here"
        // Actual: Parse error - tokenizer exits quote mode at \'
        let result = MacroProcessor::parse_params(r"name:='it\'s here'");

        // When escape sequences ARE implemented, this should parse correctly:
        // assert!(result.is_ok());
        // let (params, _, _) = result.unwrap();
        // assert_eq!(params.get("name"), Some(&Some("it's here".to_string())));

        // For now, document that it's expected to fail or produce wrong tokens
        // (This test is ignored, so it won't run in CI)
        let _ = result; // Avoid unused variable warning
    }

    /// Test edge case: unbalanced quotes
    ///
    /// The tokenizer validates that all quotes are properly closed and returns
    /// an error if an unclosed quote is detected.
    #[test]
    fn test_parse_params_unbalanced_quotes() {
        // Missing closing quote - should return UnbalancedQuote error
        let result = MacroProcessor::parse_params("rpy:='0 0 0");

        assert!(result.is_err(), "Unbalanced quotes should return error");

        // Verify it's the correct error type
        assert!(
            matches!(result, Err(XacroError::UnbalancedQuote { .. })),
            "Expected UnbalancedQuote error, got: {:?}",
            result
        );
    }

    /// Test edge case: adjacent quoted strings without space
    ///
    /// Documents behavior when quoted params have no whitespace between them.
    /// The tokenizer requires whitespace to separate tokens, so adjacent quotes
    /// are treated as a single token.
    #[test]
    fn test_parse_params_adjacent_quoted_strings() {
        // Two quoted params with no space between closing ' and next param
        let result = MacroProcessor::parse_params("a:='val1'b:='val2'");

        // Currently, the tokenizer treats this as ONE token because there's
        // no whitespace between 'val1' and b
        assert!(
            result.is_ok(),
            "Adjacent quoted strings parse as single token: {:?}",
            result.err()
        );

        let (params, order, _, _) = result.unwrap();
        // Only one parameter is parsed - the entire string is one token
        assert_eq!(params.len(), 1, "Adjacent quotes treated as single token");
        assert_eq!(order, vec!["a"]);
        // The value includes everything after := including the adjacent param
        assert_eq!(
            params.get("a"),
            Some(&ParamDefault::Value("val1'b:='val2".to_string()))
        );
    }

    /// Test edge case: quote character in parameter name causes unbalanced quote
    #[test]
    fn test_parse_params_quote_in_param_name() {
        // A quote in the parameter name starts quote mode: "param':=value"
        // The tokenizer sees param' and enters quote mode, then reads :=value
        // Since there's no closing ', this is an unbalanced quote
        let result = MacroProcessor::parse_params("param':=value");

        assert!(
            result.is_err(),
            "Quote in param name causes unbalanced quote error"
        );

        // Verify it's the correct error type
        assert!(
            matches!(result, Err(XacroError::UnbalancedQuote { .. })),
            "Expected UnbalancedQuote error, got: {:?}",
            result
        );
    }

    #[test]
    fn test_parse_params_compat_mode_duplicate() {
        // Test that compat mode accepts duplicates with last-declaration-wins
        let result = MacroProcessor::parse_params_compat("x:=1 y:=2 x:=3");
        assert!(result.is_ok(), "Compat mode should accept duplicates");

        let (params, param_order, block_params, _lazy_block_params) = result.unwrap();

        // Last declaration wins for value
        assert_eq!(params.get("x"), Some(&ParamDefault::Value("3".to_string())));
        assert_eq!(params.get("y"), Some(&ParamDefault::Value("2".to_string())));

        // Order should only contain unique params, in first-seen order
        assert_eq!(param_order, vec!["x", "y"]);

        assert!(block_params.is_empty());
    }

    #[test]
    fn test_parse_params_compat_mode_block_duplicate() {
        // Test that compat mode accepts duplicate block params
        let result = MacroProcessor::parse_params_compat("*body *body");
        assert!(
            result.is_ok(),
            "Compat mode should accept duplicate block params"
        );

        let (params, param_order, block_params, _lazy_block_params) = result.unwrap();

        // Should have one entry
        assert_eq!(params.len(), 1);
        assert_eq!(params.get("body"), Some(&ParamDefault::None));

        // Order should only contain unique params
        assert_eq!(param_order, vec!["body"]);

        // Should be marked as block param
        assert!(block_params.contains("body"));
    }
}