Skip to content

Instantly share code, notes, and snippets.

@InsulaVentus
Created August 30, 2019 09:21
Show Gist options
  • Save InsulaVentus/b8a060068997ebd4d66bae11f8af88ee to your computer and use it in GitHub Desktop.
Save InsulaVentus/b8a060068997ebd4d66bae11f8af88ee to your computer and use it in GitHub Desktop.
package record
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidLicensePlate(t *testing.T) {
tests := map[string]struct {
input string
wantLicencePlate *LicensePlate
wantErr error
}{
"simple": {input: "12345678", wantLicencePlate: &LicensePlate{"12345678"}, wantErr: nil},
"standard norwegian": {input: "ÆÅ 12345", wantLicencePlate: &LicensePlate{"ÆÅ12345"}, wantErr: nil},
"standard german": {input: "AB CD 1234", wantLicencePlate: &LicensePlate{"ABCD1234"}, wantErr: nil},
"leading and trailing whitespace": {input: " AB12345 ", wantLicencePlate: &LicensePlate{"AB12345"}, wantErr: nil},
"whitespace separated": {input: " A B 1 2 3 4 5 6 ", wantLicencePlate: &LicensePlate{"AB123456"}, wantErr: nil},
"upper and lower case": {input: "aB 12345", wantLicencePlate: &LicensePlate{"AB12345"}, wantErr: nil},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
lp := NewLicensePlate(test.input)
nlp, err := lp.Normalize()
assert.Equal(t, test.wantErr, err)
assert.Equal(t, test.wantLicencePlate, nlp)
})
}
}
func TestInvalidLicensePlate(t *testing.T) {
tests := map[string]struct {
input string
wantLicencePlate *LicensePlate
wantErr error
}{
"special characters": {input: "\xf0\x9f\x90\x94", wantLicencePlate: nil, wantErr: errors.New("invalid character U+1F414 '🐔' at byte position 0")},
"too many characters": {input: "123456789", wantLicencePlate: nil, wantErr: errors.New("more than 8 characters")},
"all whitespace": {input: " ", wantLicencePlate: nil, wantErr: errors.New("no alphanumerics")},
"empty": {input: "", wantLicencePlate: nil, wantErr: errors.New("empty string")},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
lp := NewLicensePlate(test.input)
nlp, err := lp.Normalize()
assert.Equal(t, test.wantErr, err)
assert.Equal(t, test.wantLicencePlate, nlp)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment