50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package gtin
|
|
|
|
import "testing"
|
|
|
|
func TestNormalizeValid(t *testing.T) {
|
|
cases := []struct{ in, want, typ string }{
|
|
{" 5449000000996 ", "5449000000996", "EAN13"}, // Coca-Cola EAN-13
|
|
{"3017624010701", "3017624010701", "EAN13"}, // Nutella EAN-13
|
|
{"036000291452", "036000291452", "UPC"}, // UPC-A
|
|
{"96385074", "96385074", "EAN8"}, // EAN-8
|
|
{"00012345600012", "00012345600012", "GTIN14"},
|
|
{"6901234567892", "6901234567892", "EAN13"}, // China 690 prefix
|
|
}
|
|
for _, c := range cases {
|
|
got, err := Normalize(c.in)
|
|
if err != nil {
|
|
t.Errorf("Normalize(%q) unexpected error: %v", c.in, err)
|
|
continue
|
|
}
|
|
if got != c.want {
|
|
t.Errorf("Normalize(%q) = %q, want %q", c.in, got, c.want)
|
|
}
|
|
if InferType(got) != c.typ {
|
|
t.Errorf("InferType(%q) = %q, want %q", got, InferType(got), c.typ)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNormalizeRejects(t *testing.T) {
|
|
cases := []struct {
|
|
in string
|
|
want error
|
|
}{
|
|
{"", ErrEmpty},
|
|
{"12ab5678", ErrFormat},
|
|
{"12345", ErrFormat},
|
|
{"5449000000997", ErrCheck}, // bad check digit
|
|
{"2012345678903", ErrRestricted}, // 20-29 in-store EAN-13
|
|
{"0212345678909", ErrRestricted}, // 02x variable measure
|
|
{"212345678909", ErrRestricted}, // UPC number system 2
|
|
{"02345673", ErrRestricted}, // EAN-8 in-store
|
|
}
|
|
for _, c := range cases {
|
|
_, err := Normalize(c.in)
|
|
if err != c.want {
|
|
t.Errorf("Normalize(%q) error = %v, want %v", c.in, err, c.want)
|
|
}
|
|
}
|
|
}
|