diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2016-02-21 22:21:06 -0500 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2016-02-21 22:21:06 -0500 |
| commit | 99e5114a8b22ce692fb6cbf26b0b11908dde69e8 (patch) | |
| tree | a4e24d38735634b7acad5b5af2493ab0495f66fb /names_test.go | |
| parent | 86cddf333ee9a80a4ea0a68814face6a32ced48b (diff) | |
| download | bibtex-99e5114a8b22ce692fb6cbf26b0b11908dde69e8.tar.gz | |
Add a few tests
Diffstat (limited to 'names_test.go')
| -rw-r--r-- | names_test.go | 100 |
1 files changed, 100 insertions, 0 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) + } + } +} |
