summaryrefslogtreecommitdiffstats
path: root/names_test.go
blob: 2d74f1cb03f50b2547c87dd9fe1dc35ef252ea65 (plain)
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
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("author.bib")
	defer f.Close()
	db, err := Parse(f, false)
	if err != nil {
		log.Print(err)
	}

	for _, entry := range db.Entries {
		lines := strings.Split(bibtex("author", "names", entry.Key), "\n")
		names := SplitNames(entry.Fields["author"].String())
		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("%s, expected: %d, actual: %d", test.s,
				test.expected, actual)
		}
	}
}

func TestIsLower(t *testing.T) {
	tests := []struct {
		s        string
		expected bool
	}{
		{"", false},
		{"abc", true},
		{"Abc", false},
		{"a{bc}", true},
		{"A{bc}", false},
		{"{a}A", false},
		{"{A}a", true},
		{"{\\i B}A", true},
		{"{\\AA a}a", false},
		{"{abc}A", false},
		{"{Abc}a", true},
	}
	for _, test := range tests {
		actual := isLower(test.s)
		if test.expected != actual {
			t.Errorf("%s, expected: %t, actual: %t", test.s,
				test.expected, actual)
		}
	}
}

func TestRemoveLatex(t *testing.T) {
	tests := []struct {
		s        string
		expected string
	}{
		{"\\' a\\OE{\\c {cd}}\\&{\\emph{test}}", "áŒç&test"},
	}
	for _, test := range tests {
		actual := removeLatexCommands(test.s)
		if test.expected != actual {
			t.Errorf("%q, expected: %q, actual: %q", test.s,
				test.expected, actual)
		}
	}

}