1- """Testing the parse_file function."""
1+ """Testing the parse_file and write_file functions."""
2+
3+ import os
4+ import tempfile
25
36from bibtexparser import parse_file
7+ from bibtexparser import write_file
8+ from bibtexparser .library import Library
9+ from bibtexparser .model import Entry
10+ from bibtexparser .model import Field
411
512
613def test_gbk ():
@@ -9,3 +16,77 @@ def test_gbk():
916 assert library .entries [0 ]["title" ] == "Test Title"
1017 assert library .entries [0 ]["year" ] == "2013"
1118 assert library .entries [0 ]["journal" ] == "测试期刊"
19+
20+
21+ def test_write_file_default_encoding ():
22+ """Test write_file uses UTF-8 by default."""
23+ entry = Entry (
24+ entry_type = "article" ,
25+ key = "test2024" ,
26+ fields = [
27+ Field (key = "author" , value = "Müller" ),
28+ Field (key = "title" , value = "Ångström measurements" ),
29+ ],
30+ )
31+ library = Library ([entry ])
32+
33+ with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".bib" , delete = False ) as f :
34+ temp_path = f .name
35+
36+ try :
37+ write_file (temp_path , library )
38+ # Read back and verify
39+ with open (temp_path , encoding = "UTF-8" ) as f :
40+ content = f .read ()
41+ assert "Müller" in content
42+ assert "Ångström" in content
43+ finally :
44+ os .unlink (temp_path )
45+
46+
47+ def test_write_file_gbk_encoding ():
48+ """Test write_file with GBK encoding for Chinese characters."""
49+ entry = Entry (
50+ entry_type = "article" ,
51+ key = "test2024" ,
52+ fields = [
53+ Field (key = "author" , value = "凯撒" ),
54+ Field (key = "title" , value = "Test Title" ),
55+ Field (key = "journal" , value = "测试期刊" ),
56+ ],
57+ )
58+ library = Library ([entry ])
59+
60+ with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".bib" , delete = False ) as f :
61+ temp_path = f .name
62+
63+ try :
64+ write_file (temp_path , library , encoding = "gbk" )
65+ # Read back with GBK and verify
66+ with open (temp_path , encoding = "gbk" ) as f :
67+ content = f .read ()
68+ assert "凯撒" in content
69+ assert "测试期刊" in content
70+ finally :
71+ os .unlink (temp_path )
72+
73+
74+ def test_write_file_roundtrip_gbk ():
75+ """Test round-trip: parse GBK file, write with GBK, parse again."""
76+ # Parse original GBK file
77+ library = parse_file ("tests/resources/gbk_test.bib" , encoding = "gbk" )
78+ original_author = library .entries [0 ]["author" ]
79+ original_journal = library .entries [0 ]["journal" ]
80+
81+ with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".bib" , delete = False ) as f :
82+ temp_path = f .name
83+
84+ try :
85+ # Write with GBK encoding
86+ write_file (temp_path , library , encoding = "gbk" )
87+ # Parse back
88+ library2 = parse_file (temp_path , encoding = "gbk" )
89+ assert library2 .entries [0 ]["author" ] == original_author
90+ assert library2 .entries [0 ]["journal" ] == original_journal
91+ finally :
92+ os .unlink (temp_path )
0 commit comments