-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbibtex_test.go
270 lines (236 loc) · 6.78 KB
/
bibtex_test.go
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
package bibtex
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
// Tests basic usage of bibtex library.
func TestBasic(t *testing.T) {
bibtex := NewBibTex()
entry := NewBibEntry("article", "abcd1234")
entry.AddField("title", NewBibConst("HelloWorld"))
bibtex.AddEntry(entry)
expected := `@article{abcd1234,
title = {HelloWorld}
}
`
if bibtex.String() != expected {
fmt.Printf("%s\n%s\n", bibtex.String(), expected)
t.Error("Output does not match.")
}
}
// Tests usage of bibtex library of strings with spaces.
// The tag content should be unchanged, other strings will have spaces removed.
func TestSpaces(t *testing.T) {
bibtex := NewBibTex()
entry := NewBibEntry("ar t icle", "ab cd 1234")
entry.AddField("title", NewBibConst("Hello World "))
bibtex.AddEntry(entry)
expected := `@article{abcd1234,
title = {Hello World}
}
`
if bibtex.String() != expected {
fmt.Printf("%s\n%s\n", bibtex.String(), expected)
t.Error("Output does not match.")
}
}
func TestString(t *testing.T) {
bibtex := NewBibTex()
bibtex.AddStringVar("cat", &BibVar{Key: "cat", Value: NewBibConst("meowmeow")})
entry := NewBibEntry("article", "abcd")
entry.AddField("title", bibtex.GetStringVar("cat"))
bibtex.AddEntry(entry)
expected := `@article{abcd,
title = {meowmeow}
}
`
if bibtex.String() != expected {
fmt.Printf("%s\n%s\n", bibtex.String(), expected)
t.Error("Output does not match.")
}
}
// Test that the parser accepts all valid bibtex files in the example/ dir.
func TestParser(t *testing.T) {
examples, err := filepath.Glob("example/*.bib")
if err != nil {
t.Fatal(err)
}
for _, ex := range examples {
t.Logf("Parsing example: %s", ex)
b, err := os.ReadFile(ex)
if err != nil {
t.Errorf("Cannot read %s: %v", ex, err)
}
_, err = Parse(bytes.NewReader(b))
if err != nil {
t.Errorf("Cannot parse valid bibtex file %s: %v", ex, err)
}
}
}
// Test bug (Issue #24) where there is no parse error, but fields are missing
func TestTextOutsideEntries(t *testing.T) {
// Re-create the exact failing scenario
expected := NewBibTex()
entry := NewBibEntry("article", "CitekeyArticle")
entry.AddField("author", NewBibConst("John Doe"))
entry.AddField("title", NewBibConst("The independence of the continuum hypothesis"))
entry.AddField("journal", NewBibConst("Proceedings of the National Academy of Sciences"))
entry.AddField("year", NewBibConst("1963"))
entry.AddField("volume", NewBibConst("50"))
entry.AddField("number", NewBibConst("6"))
entry.AddField("pages", NewBibConst("1143--1148"))
expected.AddEntry(entry)
// Parse file with same data as above, also with text in between the entries
ex := "example/text-outside-entries.bib"
b, err := os.ReadFile(ex)
if err != nil {
t.Errorf("Cannot read %s: %v", ex, err)
}
s, err := Parse(bytes.NewReader(b))
if err != nil {
t.Errorf("Cannot parse valid bibtex file %s: %v", ex, err)
}
// Check equality
AssertEntryListsEqual(t, expected.Entries, s.Entries)
}
// Tests that multiple parse returns different instances of the parsed BibTex.
// Otherwise the number of entries will pile up. (Issue #4)
func TestMultiParse(t *testing.T) {
examples := []string{
"example/simple.bib",
"example/simple.bib",
"example/simple.bib",
"example/simple2.bib", // simple but with comment
}
var bibs []*BibTex
for _, ex := range examples {
t.Logf("Parsing example: %s", ex)
b, err := os.ReadFile(ex)
if err != nil {
t.Errorf("Cannot read %s: %v", ex, err)
}
s, err := Parse(bytes.NewReader(b))
if err != nil {
t.Errorf("Cannot parse valid bibtex file %s: %v", ex, err)
}
if want, got := 2, len(s.Entries); want != got {
t.Errorf("Expecting %d entries but got %d", want, got)
}
bibs = append(bibs, s)
}
for _, bib := range bibs {
if want, got := 2, len(bib.Entries); want != got {
t.Errorf("Expecting %d entries but got %d", want, got)
}
}
}
func TestPrettyStringRoundTrip(t *testing.T) {
examples, err := filepath.Glob("example/*.bib")
if err != nil {
t.Fatal(err)
}
for _, ex := range examples {
// Read input.
b, err := os.ReadFile(ex)
if err != nil {
t.Fatal(err)
}
// Parse into BibTeX.
bib, err = Parse(bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}
// Pretty print it and parse it again.
s := bib.PrettyString()
bib2, err := Parse(strings.NewReader(s))
if err != nil {
t.Fatal(err)
}
// Check equality.
AssertEntryListsEqual(t, bib.Entries, bib2.Entries)
}
}
func TestUnexpectedAtSign(t *testing.T) {
// Tests correct syntax but scanning error
b, err := os.ReadFile("example/unexpected-at-sign.badbib")
if err != nil {
t.Fatal(err)
}
_, err = Parse(bytes.NewReader(b))
if err == nil {
t.Fatal("Expected error but got none")
}
if !errors.Is(err, ErrUnexpectedAtsign) {
t.Fatalf("expected error %+v but got %+v", ErrUnexpectedAtsign, err)
}
}
func AssertEntryListsEqual(t *testing.T, a, b []*BibEntry) {
t.Helper()
if len(a) != len(b) {
t.Fatalf("length mismatch")
}
for i := range a {
AssertEntriesEqual(t, a[i], b[i])
}
}
func AssertEntriesEqual(t *testing.T, a, b *BibEntry) {
if a.Type != b.Type {
t.Error("type mismatch")
}
if a.CiteName != b.CiteName {
t.Error("cite name mismatch")
}
if len(a.Fields) != len(b.Fields) {
t.Fatal("different number of fields")
}
for key := range a.Fields {
if a.Fields[key].String() != b.Fields[key].String() {
t.Fatalf("mismatch on field %q", key)
}
}
}
func BenchmarkStringPerformance(b *testing.B) {
exampleFileBytes, err := os.ReadFile("example/biblatex-examples.bib")
if err != nil {
b.Fatal(err)
}
bib, err := Parse(bytes.NewReader(exampleFileBytes))
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = bib.String()
}
}
func TestBibEntry_PrettyStringCustomOrder(t *testing.T) {
wantPrettyString := `@inproceedings{bibtexKey,
author = "A a and B b and C c",
editor = "D d and E e",
title = "Some title",
booktitle = "Some booktitle",
}
`
entry := NewBibEntry("inproceedings", "bibtexKey")
entry.AddField("author", NewBibConst("A a and B b and C c"))
entry.AddField("editor", NewBibConst("D d and E e"))
entry.AddField("title", NewBibConst("Some title"))
entry.AddField("booktitle", NewBibConst("Some booktitle"))
keyOrder := []string{"author", "editor", "title", "booktitle"}
gotPrettyString := entry.PrettyString(WithKeyOrder(keyOrder))
if wantPrettyString != gotPrettyString {
t.Errorf("Format error\nWant: %s\nGot:%s\n", wantPrettyString, gotPrettyString)
}
// pretty print same entry with different order and check that result no longer matchnes
// wantPrettyString
errorKeyOrder := []string{"editor", "author", "title", "booktitle"}
gotPrettyString = entry.PrettyString(WithKeyOrder(errorKeyOrder))
if wantPrettyString == gotPrettyString {
t.Errorf("Format error. Expected missmatch but got match")
}
}