bib_test.go (5543B)
1 package bib 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "os" 8 "path/filepath" 9 "strings" 10 "testing" 11 ) 12 13 const sample = `% comment line 14 @string{jgl = "J. Glaciol."} 15 @article{Boulton1993, 16 doi = {10.1017/S0022143000015690}, 17 year = 1993, 18 author = {G. S. Boulton and K. E. Dobbie}, 19 title = {Consolidation of {sediments} by glaciers}, 20 journal = "J. Glaciol." 21 } 22 23 @Book{Paterson1994, 24 title={The Physics of Glaciers}, 25 author={Paterson, W. S. B.}, 26 year={1994} 27 } 28 ` 29 30 func TestParse(t *testing.T) { 31 es, err := Parse(sample) 32 if err != nil { 33 t.Fatal(err) 34 } 35 if len(es) != 2 { 36 t.Fatalf("got %d entries", len(es)) 37 } 38 e := es[0] 39 if e.Key != "Boulton1993" || e.Fields["year"] != "1993" || e.Fields["title"] != "Consolidation of {sediments} by glaciers" || e.Fields["journal"] != "J. Glaciol." { 40 t.Fatalf("fields = %+v", e.Fields) 41 } 42 if e.DOI() != "10.1017/s0022143000015690" { 43 t.Fatalf("doi = %s", e.DOI()) 44 } 45 if !strings.HasPrefix(e.Raw, "@article{Boulton1993,") || !strings.HasSuffix(e.Raw, "}") { 46 t.Fatalf("raw = %q", e.Raw) 47 } 48 if es[1].Type != "book" { 49 t.Fatalf("type = %s", es[1].Type) 50 } 51 } 52 53 func TestParseUnbalanced(t *testing.T) { 54 if _, err := Parse("@article{x, title={a}"); err == nil { 55 t.Fatal("expected error") 56 } 57 } 58 59 func TestNormalize(t *testing.T) { 60 cases := map[string]string{ 61 "Geoffrey S. Boulton and Kevin E. Dobbie": "G. S. Boulton and K. E. Dobbie", 62 "Boulton, Geoffrey S.": "Boulton, G. S.", 63 "Jean-Pierre van der Veen": "J.-P. van der Veen", 64 } 65 for in, want := range cases { 66 if got := AbbrevAuthors(in); got != want { 67 t.Errorf("AbbrevAuthors(%q) = %q, want %q", in, got, want) 68 } 69 } 70 if got := AbbrevJournal("Journal of Geophysical Research: Earth Surface"); got != "J. Geophys. Res.: Earth Surf." { 71 t.Errorf("journal = %q", got) 72 } 73 if got := AbbrevJournal("Science"); got != "Science" { 74 t.Errorf("single word journal = %q", got) 75 } 76 } 77 78 func TestIndexAndDuplicates(t *testing.T) { 79 es, _ := Parse(sample + "\n@article{Boulton1993, doi={https://doi.org/10.1017/s0022143000015690}}\n") 80 ix := NewIndex(es) 81 if ix.FreeKey("Boulton1993") != "Boulton1993a" || ix.FreeKey("New2020") != "New2020" { 82 t.Fatal("FreeKey") 83 } 84 keys, dois := Duplicates(es) 85 if len(keys) != 1 || len(dois) != 1 { 86 t.Fatalf("dups keys=%v dois=%v", keys, dois) 87 } 88 e := es[2] 89 Rekey(e, "Boulton1993b") 90 if !strings.HasPrefix(e.Raw, "@article{Boulton1993b,") { 91 t.Fatalf("raw = %q", e.Raw) 92 } 93 } 94 95 func TestAppend(t *testing.T) { 96 p := filepath.Join(t.TempDir(), "x.bib") 97 os.WriteFile(p, []byte(sample), 0o644) 98 es, _ := Parse("@misc{K, title={t}}") 99 if err := Append(p, es); err != nil { 100 t.Fatal(err) 101 } 102 b, _ := os.ReadFile(p) 103 if !strings.HasPrefix(string(b), sample) || !strings.HasSuffix(string(b), "\n@misc{K, title={t}}\n") { 104 t.Fatalf("file = %q", b) 105 } 106 } 107 108 func TestClient(t *testing.T) { 109 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 110 switch { 111 case r.URL.Path == "/works": 112 if r.URL.Query().Get("query") != "boulton glacier" { 113 t.Errorf("query = %q", r.URL.Query().Get("query")) 114 } 115 w.Write([]byte(`{"message":{"items":[{"DOI":"10.1017/S0022143000015690"}]}}`)) 116 case r.URL.Path == "/10.1017/s0022143000015690": 117 if r.Header.Get("Accept") != "application/x-bibtex" { 118 t.Errorf("accept = %q", r.Header.Get("Accept")) 119 } 120 w.Write([]byte(` @article{Boulton_1993, title={Consolidation}, volume={39}, url={http://x}, DOI={10.1017/S0022143000015690}, journal={Journal of Glaciology}, author={Boulton, Geoffrey S. and Dobbie, Kevin E.}, year={1993}, month=jan }`)) 121 default: 122 http.NotFound(w, r) 123 } 124 })) 125 defer srv.Close() 126 c := &Client{HTTP: srv.Client(), CrossrefURL: srv.URL + "/works", DOIURL: srv.URL} 127 doi, err := c.Lookup("boulton glacier") 128 if err != nil || doi != "10.1017/s0022143000015690" { 129 t.Fatalf("lookup = %q, %v", doi, err) 130 } 131 if d, _ := c.Lookup("https://doi.org/10.5555/ABC"); d != "10.5555/abc" { 132 t.Fatalf("doi passthrough = %q", d) 133 } 134 e, err := c.Fetch(doi) 135 if err != nil { 136 t.Fatal(err) 137 } 138 if e.Key != "Boulton1993" || e.Fields["journal"] != "J. Glaciol." || e.Fields["author"] != "Boulton, G. S. and Dobbie, K. E." { 139 t.Fatalf("entry = %+v", e.Fields) 140 } 141 if strings.Contains(e.Raw, "url") || strings.Contains(e.Raw, "month") || !strings.Contains(e.Raw, "\tdoi = {10.1017/s0022143000015690},") { 142 t.Fatalf("raw = %s", e.Raw) 143 } 144 if _, err := c.Fetch("10.9/missing"); err == nil { 145 t.Fatal("expected error for missing DOI") 146 } 147 } 148 149 func TestExport(t *testing.T) { 150 es, _ := Parse(`@article{A, author={M{\"u}ller, Hans and {GEUS}}, title={{\O}resund--flow}, journal={J. Glaciol.}, year={2001}, pages={1--5}, doi={10.1/x}}`) 151 b, err := CSL(es) 152 if err != nil { 153 t.Fatal(err) 154 } 155 var items []map[string]any 156 if err := json.Unmarshal(b, &items); err != nil { 157 t.Fatal(err) 158 } 159 if items[0]["title"] != "Øresund–flow" || items[0]["type"] != "article-journal" { 160 t.Fatalf("csl = %s", b) 161 } 162 au := items[0]["author"].([]any) 163 if au[0].(map[string]any)["family"] != "Mu\u0308ller" || au[1].(map[string]any)["literal"] != "GEUS" { 164 t.Fatalf("authors = %v", au) 165 } 166 ris := RIS(es) 167 for _, want := range []string{"TY - JOUR", "AU - Mu\u0308ller, Hans", "SP - 1", "EP - 5", "DO - 10.1/x", "ER - "} { 168 if !strings.Contains(ris, want) { 169 t.Errorf("RIS missing %q:\n%s", want, ris) 170 } 171 } 172 } 173 174 func TestSearch(t *testing.T) { 175 es, _ := Parse(sample) 176 if r := Search(es, "paterson 1994"); len(r) != 1 || r[0].Key != "Paterson1994" { 177 t.Fatalf("search = %v", r) 178 } 179 }