Files

85 lines
2.5 KiB
Go
Raw Permalink Normal View History

2026-09-18 12:03:35 +02:00
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
package identifiers
import "testing"
func TestNormalizeDOI(t *testing.T) {
cases := map[string]string{
"10.5281/zenodo.1234567": "10.5281/zenodo.1234567",
" https://doi.org/10.5281/zenodo.1234567": "10.5281/zenodo.1234567",
"DOI: 10.1234/abcd": "10.1234/abcd",
"http://dx.doi.org/10.1/x": "http://dx.doi.org/10.1/x", // unknown host stays as it is
"": "",
}
for in, want := range cases {
if got := NormalizeDOI(in); got != want {
t.Errorf("NormalizeDOI(%q) = %q, want %q", in, got, want)
}
}
}
func TestValidDOI(t *testing.T) {
for _, ok := range []string{
"10.1234/x", "10.5281/zenodo.1234567", "https://doi.org/10.1000/18.2011.01",
"10.1109/5.77101",
} {
if !ValidDOI(ok) {
t.Errorf("ValidDOI(%q) = false, want true", ok)
}
}
for _, bad := range []string{
"", "10.123/x", // needle too short
"10./x", // empty needle
"20.1234/x", // not the 10. prefix
"10.1234", // no suffix
"10.1234/ spaced suffix", // whitespace in the suffix
"doi:10.1234/", // empty suffix after the slash
} {
if ValidDOI(bad) {
t.Errorf("ValidDOI(%q) = true, want false", bad)
}
}
}
func TestValidORCID(t *testing.T) {
// A real public iD, and its computed X-check sibling.
for _, ok := range []string{
"0000-0002-1825-0097",
"0000-0000-0000-001X",
"0000-0000-0000-001x", // lower-case x is upper-cased first
" 0000-0002-1825-0097 ",
} {
if !ValidORCID(ok) {
t.Errorf("ValidORCID(%q) = false, want true", ok)
}
}
for _, bad := range []string{
"",
"0000-0002-1825-0098", // wrong check digit
"0000-0002-1825-009x", // right length, wrong check digit
"0000-0002-1825-009", // too short
"0000000218250097", // dashes missing
} {
if ValidORCID(bad) {
t.Errorf("ValidORCID(%q) = true, want false", bad)
}
}
}
func TestURLs(t *testing.T) {
if got := DOIURL("https://doi.org/10.1234/x"); got != "https://doi.org/10.1234/x" {
t.Errorf("DOIURL = %q", got)
}
if got := DOIURL("nonsense"); got != "" {
t.Errorf("DOIURL(nonsense) = %q, want empty", got)
}
if got := ORCIDURL("0000-0002-1825-0097"); got != "https://orcid.org/0000-0002-1825-0097" {
t.Errorf("ORCIDURL = %q", got)
}
if got := ORCIDURL("0000-0002-1825-0098"); got != "" {
t.Errorf("ORCIDURL(bad checksum) = %q, want empty", got)
}
}