forked from nickng/bibtex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bibtex_test.go
191 lines (168 loc) · 4.18 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
package bibtex
import (
"bytes"
"fmt"
"io/ioutil"
"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 := ioutil.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)
}
}
}
// 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",
}
var bibs []*BibTex
for _, ex := range examples {
t.Logf("Parsing example: %s", ex)
b, err := ioutil.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 := ioutil.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 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 := ioutil.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()
}
}