44from bibtexparser .model import Entry
55from bibtexparser .model import Field
66from bibtexparser .model import ImplicitComment
7+ from bibtexparser .model import String
78
89
910def get_dummy_entry ():
@@ -17,6 +18,10 @@ def get_dummy_entry():
1718 )
1819
1920
21+ def get_dummy_string ():
22+ return String (key = "duplicateKey" , value = '"A value"' )
23+
24+
2025def test_replace_with_duplicates ():
2126 """Test that replace() works when there are duplicate values. See issue 404."""
2227 library = Library ()
@@ -97,6 +102,26 @@ def test_constructor_fails_on_duplicate_by_default():
97102 assert len (library .failed_blocks ) == 1
98103
99104
105+ def test_add_string_fails_on_duplicate_by_default ():
106+ """Strict duplicate handling applies to string blocks as well as entries."""
107+ library = Library (blocks = [get_dummy_string ()])
108+
109+ with pytest .raises (ValueError , match = "duplicateKey" ):
110+ library .add (get_dummy_string ())
111+
112+ assert len (library .strings ) == 1
113+ assert len (library .failed_blocks ) == 0
114+
115+
116+ def test_entry_and_string_keys_use_separate_namespaces ():
117+ """An entry and string may share a key without becoming duplicate blocks."""
118+ library = Library (blocks = [get_dummy_entry (), get_dummy_string ()])
119+
120+ assert len (library .entries ) == 1
121+ assert len (library .strings ) == 1
122+ assert len (library .failed_blocks ) == 0
123+
124+
100125def test_remove_prefers_identical_instance_over_equal_block ():
101126 """With two equal blocks, remove() must remove the passed instance. See issue 537."""
102127 first_comment = ImplicitComment ("#same" )
0 commit comments