@@ -460,3 +460,51 @@ def test_empty_dict_vs_empty_list_behavior(self):
460
460
461
461
# They should be different
462
462
self .assertNotEqual (compact_dict , compact_list )
463
+
464
+ def test_non_string_text_with_attributes (self ):
465
+ """Test that non-string #text values work when tag has attributes.
466
+
467
+ This test covers GitHub issue #366: Tag value (#text) must be a string
468
+ when tag has additional parameters - unparse.
469
+
470
+ Also tests that plain values and explicit #text values are treated
471
+ consistently (both go through the same conversion logic).
472
+ """
473
+ # Test cases for explicit #text values with attributes
474
+ self .assertEqual (unparse ({"a" : {"@param" : "test" , "#text" : 1 }}, full_document = False ),
475
+ '<a param="test">1</a>' )
476
+
477
+ self .assertEqual (unparse ({"a" : {"@param" : 42 , "#text" : 3.14 }}, full_document = False ),
478
+ '<a param="42">3.14</a>' )
479
+
480
+ self .assertEqual (unparse ({"a" : {"@param" : "flag" , "#text" : True }}, full_document = False ),
481
+ '<a param="flag">true</a>' )
482
+
483
+ self .assertEqual (unparse ({"a" : {"@param" : "test" , "#text" : None }}, full_document = False ),
484
+ '<a param="test">None</a>' )
485
+
486
+ self .assertEqual (unparse ({"a" : {"@param" : "test" , "#text" : "string" }}, full_document = False ),
487
+ '<a param="test">string</a>' )
488
+
489
+ self .assertEqual (unparse ({"a" : {"@attr1" : "value1" , "@attr2" : 2 , "#text" : 100 }}, full_document = False ),
490
+ '<a attr1="value1" attr2="2">100</a>' )
491
+
492
+ # Test cases for plain values (should be treated the same as #text)
493
+ self .assertEqual (unparse ({"a" : 1 }, full_document = False ), '<a>1</a>' )
494
+ self .assertEqual (unparse ({"a" : 3.14 }, full_document = False ), '<a>3.14</a>' )
495
+ self .assertEqual (unparse ({"a" : True }, full_document = False ), '<a>true</a>' )
496
+ self .assertEqual (unparse ({"a" : "hello" }, full_document = False ), '<a>hello</a>' )
497
+ self .assertEqual (unparse ({"a" : None }, full_document = False ), '<a></a>' )
498
+
499
+ # Consistency tests: plain values should match explicit #text values
500
+ self .assertEqual (unparse ({"a" : 42 }, full_document = False ),
501
+ unparse ({"a" : {"#text" : 42 }}, full_document = False ))
502
+
503
+ self .assertEqual (unparse ({"a" : 3.14 }, full_document = False ),
504
+ unparse ({"a" : {"#text" : 3.14 }}, full_document = False ))
505
+
506
+ self .assertEqual (unparse ({"a" : True }, full_document = False ),
507
+ unparse ({"a" : {"#text" : True }}, full_document = False ))
508
+
509
+ self .assertEqual (unparse ({"a" : "hello" }, full_document = False ),
510
+ unparse ({"a" : {"#text" : "hello" }}, full_document = False ))
0 commit comments