summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2016-02-21 22:21:06 -0500
committerThibaut Horel <thibaut.horel@gmail.com>2016-02-21 22:21:06 -0500
commit99e5114a8b22ce692fb6cbf26b0b11908dde69e8 (patch)
treea4e24d38735634b7acad5b5af2493ab0495f66fb
parent86cddf333ee9a80a4ea0a68814face6a32ced48b (diff)
downloadbibtex-99e5114a8b22ce692fb6cbf26b0b11908dde69e8.tar.gz
Add a few tests
-rw-r--r--names_test.go100
-rw-r--r--test/Makefile2
-rw-r--r--test/anonbib.aux3
-rw-r--r--test/btxdoc.aux3
-rw-r--r--test/econcs.aux3
-rw-r--r--test/parsing.aux3
-rw-r--r--test/pdos.aux3
-rw-r--r--test/xampl.aux3
-rw-r--r--testdata/anonbib.bib (renamed from test/anonbib.bib)0
-rw-r--r--testdata/author.bib87
-rw-r--r--testdata/btxdoc.bib (renamed from test/btxdoc.bib)0
-rw-r--r--testdata/econcs.bib (renamed from test/econcs.bib)0
-rw-r--r--testdata/names.bst38
-rw-r--r--testdata/parsing.bib (renamed from test/parsing.bib)15
-rw-r--r--testdata/pdos.bib (renamed from test/pdos.bib)0
-rw-r--r--testdata/xampl.bib (renamed from test/xampl.bib)0
16 files changed, 236 insertions, 24 deletions
diff --git a/names_test.go b/names_test.go
new file mode 100644
index 0000000..0406a85
--- /dev/null
+++ b/names_test.go
@@ -0,0 +1,100 @@
+package bibtex
+
+import (
+ "io/ioutil"
+ "log"
+ "os"
+ "os/exec"
+ "strings"
+ "testing"
+ "text/template"
+)
+
+func bibtex(bib string, style string, entry string) string {
+ f, err := os.Create("test.aux")
+ if err != nil {
+ log.Print(err)
+ }
+ defer f.Close()
+
+ tpl := `
+\relax
+\citation{[.Entry]}
+\bibstyle{[.Style]}
+\bibdata{[.Bib]}`
+ type params struct {
+ Entry string
+ Style string
+ Bib string
+ }
+ t := template.New("")
+ t.Delims("[", "]")
+ t = template.Must(t.Parse(tpl))
+ err = t.Execute(f, params{entry, style, bib})
+ if err != nil {
+ log.Print(err)
+ }
+ exec.Command("bibtex", "test").Output()
+ in, _ := ioutil.ReadFile("test.bbl")
+ return string(in)
+}
+
+func TestName(t *testing.T) {
+ os.Chdir("testdata")
+ f, _ := os.Open("parsing.bib")
+ defer f.Close()
+ parser := NewParser(f)
+ err := parser.Parse(false)
+ if err != nil {
+ log.Print(err)
+ }
+
+ for _, entry := range parser.entries {
+ lines := strings.Split(bibtex("parsing", "names", entry.key), "\n")
+ names := SplitNames(entry.fields["author"])
+ i := 0
+ var s string
+ for _, name := range names {
+ s = name.Format("{ff}|{vv}|{ll}|{jj}")
+ if lines[i] != s {
+ t.Errorf("Expected: %q, actual: %q", lines[i], s)
+ }
+ i += 1
+ s = name.Format("{f}|{v}|{l}|{j}")
+ if lines[i] != s {
+ t.Errorf("Expected: %q, actual: %q", lines[i], s)
+ }
+ i += 1
+ s = name.Format("{ff~}{vv~}{ll}{, jj}")
+ if lines[i] != s {
+ t.Errorf("Expected: %q, actual: %q", lines[i], s)
+ }
+ i += 1
+ s = name.Format("{f~}{vv~}{ll}{, jj}")
+ if lines[i] != s {
+ t.Errorf("Expected: %q, actual: %q", lines[i], s)
+ }
+ i += 1
+ }
+ }
+
+}
+
+func TestLength(t *testing.T) {
+ tests := []struct {
+ s string
+ expected int
+ }{
+ {"", 0},
+ {"a", 1},
+ {"{ab}", 4},
+ {"{\\abc}", 1},
+ {"{ab{c}}a{\\a{bc}}a", 10},
+ }
+ for _, test := range tests {
+ actual := length(test.s)
+ if test.expected != actual {
+ t.Errorf("Expected: %d, actual: %d", test.expected, actual)
+ }
+ }
+}
diff --git a/test/Makefile b/test/Makefile
deleted file mode 100644
index 646964b..0000000
--- a/test/Makefile
+++ /dev/null
@@ -1,2 +0,0 @@
-clean:
- rm *.blg *.bbl
diff --git a/test/anonbib.aux b/test/anonbib.aux
deleted file mode 100644
index f2e812d..0000000
--- a/test/anonbib.aux
+++ /dev/null
@@ -1,3 +0,0 @@
-\citation{*}
-\bibstyle{plain}
-\bibdata{anonbib}
diff --git a/test/btxdoc.aux b/test/btxdoc.aux
deleted file mode 100644
index 21c84c0..0000000
--- a/test/btxdoc.aux
+++ /dev/null
@@ -1,3 +0,0 @@
-\citation{*}
-\bibstyle{plain}
-\bibdata{btxdoc}
diff --git a/test/econcs.aux b/test/econcs.aux
deleted file mode 100644
index 3da389a..0000000
--- a/test/econcs.aux
+++ /dev/null
@@ -1,3 +0,0 @@
-\citation{*}
-\bibstyle{plain}
-\bibdata{econcs}
diff --git a/test/parsing.aux b/test/parsing.aux
deleted file mode 100644
index 0ab9d59..0000000
--- a/test/parsing.aux
+++ /dev/null
@@ -1,3 +0,0 @@
-\citation{*}
-\bibstyle{plain}
-\bibdata{parsing}
diff --git a/test/pdos.aux b/test/pdos.aux
deleted file mode 100644
index 0d71863..0000000
--- a/test/pdos.aux
+++ /dev/null
@@ -1,3 +0,0 @@
-\citation{*}
-\bibstyle{plain}
-\bibdata{pdos}
diff --git a/test/xampl.aux b/test/xampl.aux
deleted file mode 100644
index 9ec329c..0000000
--- a/test/xampl.aux
+++ /dev/null
@@ -1,3 +0,0 @@
-\citation{*}
-\bibstyle{plain}
-\bibdata{xampl}
diff --git a/test/anonbib.bib b/testdata/anonbib.bib
index d3bcd63..d3bcd63 100644
--- a/test/anonbib.bib
+++ b/testdata/anonbib.bib
diff --git a/testdata/author.bib b/testdata/author.bib
new file mode 100644
index 0000000..4572866
--- /dev/null
+++ b/testdata/author.bib
@@ -0,0 +1,87 @@
+@article {1,
+ author = "Last1--,--First1-- and Last2,-First2 and Last3 -, First3"
+}
+@article {2,
+ author = "Last1, First1,and and and,Last2 and T1,T2,T3,T4"
+}
+@article {3,
+ author = ""
+}
+@article {4,
+ author = "and"
+}
+@article {5,
+ author = "AA BB",
+}
+@article {6,
+ author = "AA",
+}
+@article {7,
+ author = "AA bb",
+}
+@article {8,
+ author = "aa",
+}
+@article {9,
+ author = "AA bb CC",
+}
+@article {10,
+ author = "AA bb CC dd EE",
+}
+@article {11,
+ author = "AA 1B cc dd",
+}
+@article {12,
+ author = "AA 1b cc dd",
+}
+@article {13,
+ author = "AA {b}B cc dd",
+}
+@article {14,
+ author = "AA {b}b cc dd",
+}
+@article {15,
+ author = "AA {B}b cc dd",
+}
+@article {16,
+ author = "AA {B}B cc dd",
+}
+@article {17,
+ author = "AA \\BB{b} cc dd",
+}
+@article {18,
+ author = "AA \\bb{b} cc dd",
+}
+@article {19,
+ author = "AA {bb} cc DD",
+}
+@article {20,
+ author = "AA bb {cc} DD",
+}
+@article {21,
+ author = "AA {bb} CC",
+}
+@article {22,
+ author = "bb CC, AA",
+}
+@article {23,
+ author = "bb CC, aa",
+}
+@article {24,
+ author = "bb CC dd EE, AA",
+}
+@article {25,
+ author = "bb, AA",
+}
+@article {26,
+ author = "BB,",
+}
+@article {27,
+ author = "bb CC,XX, AA",
+}
+@article {28,
+ author = "bb CC,xx, AA",
+}
+@article {29,
+ author = "BB,, AA",
+}
diff --git a/test/btxdoc.bib b/testdata/btxdoc.bib
index 5a30c68..5a30c68 100644
--- a/test/btxdoc.bib
+++ b/testdata/btxdoc.bib
diff --git a/test/econcs.bib b/testdata/econcs.bib
index edd808c..edd808c 100644
--- a/test/econcs.bib
+++ b/testdata/econcs.bib
diff --git a/testdata/names.bst b/testdata/names.bst
new file mode 100644
index 0000000..a72e888
--- /dev/null
+++ b/testdata/names.bst
@@ -0,0 +1,38 @@
+ENTRY
+ {
+ author
+ }
+ {}
+ { label }
+
+INTEGERS { nameptr namesleft numnames }
+
+STRINGS { s t }
+
+FUNCTION {format.names}
+{ 's :=
+ #1 'nameptr :=
+ s num.names$ 'numnames :=
+ numnames 'namesleft :=
+ { namesleft #0 > }
+ {
+ s nameptr "{ff}|{vv}|{ll}|{jj}" format.name$ write$ newline$
+ s nameptr "{f}|{v}|{l}|{j}" format.name$ write$ newline$
+ s nameptr "{ff~}{vv~}{ll}{, jj}" format.name$ write$ newline$
+ s nameptr "{f~}{vv~}{ll}{, jj}" format.name$ write$ newline$
+ namesleft #1 - 'namesleft :=
+ nameptr #1 + 'nameptr :=
+ }
+ while$
+}
+
+FUNCTION {format.authors}
+{ author empty$
+ { "" }
+ { author format.names }
+ if$
+}
+
+READ
+
+ITERATE {format.authors}
diff --git a/test/parsing.bib b/testdata/parsing.bib
index 849c7f0..21b32bb 100644
--- a/test/parsing.bib
+++ b/testdata/parsing.bib
@@ -16,16 +16,20 @@ key can be empty
@misc(,)
-key can contain weird things
+key can contain weird things. The following three lines are only defining one
+entry
+
+@misc(
+@misc{author="test"},
+)
-@misc( (}cr@zy{,)
@misc{ ()cr@zy{,}
Now testing entries with unknown type.
@cr@zy {key1,
year = 2010,
- title = @f0/0\:0 # "jou{"}rn@l" # { {j}our"n@{l} } # @f0/0\:0,
+ title = @f0/0\:0 # "jou{"}rn@l" # { {j}ou#"n@{l} } # @f0/0\:0
author = "Jœhn
Nash",
@@ -33,8 +37,11 @@ Now testing entries with unknown type.
@comment{ This is a comment
- The following line starts a new entry, even though few parsers will catch it
+ field names can contain weird things, including at-signs but probably not
+ a good idea. The following line starts a new entry, even though few
+ parsers will catch it
@article{key2,
+ @my_field = "test",
year = "2010",
}
diff --git a/test/pdos.bib b/testdata/pdos.bib
index d17957b..d17957b 100644
--- a/test/pdos.bib
+++ b/testdata/pdos.bib
diff --git a/test/xampl.bib b/testdata/xampl.bib
index 85ee5cb..85ee5cb 100644
--- a/test/xampl.bib
+++ b/testdata/xampl.bib