Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fre5h committed Mar 7, 2023
1 parent 0fb3e27 commit 6c673d8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
8 changes: 4 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ var ErrLessThan10Digits = errors.New("less than 10 symbols, expects exactly 10 s
var ErrStringDoesNotConsistOfDigits = errors.New("string does not consist of digits")

type ErrNotAllowedDate struct {
date time.Time
Date time.Time
}

func (e *ErrNotAllowedDate) Error() string {
return fmt.Sprintf("the allowed dates start from 01.01.1900, but your date %s is earlier", e.date.Format("02.04.2006"))
return fmt.Sprintf("the allowed dates start from 01.01.1900, but your date %s is earlier", e.Date.Format("02.04.2006"))
}

type ErrDateInFuture struct {
date time.Time
Date time.Time
}

func (e *ErrDateInFuture) Error() string {
return fmt.Sprintf("it is allowed to use only dates in past or current date, but your date is in the future %s", e.date.Format("02.04.2006"))
return fmt.Sprintf("it is allowed to use only dates in past or current date, but your date is in the future %s", e.Date.Format("02.04.2006"))
}
26 changes: 26 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package rnokpp_test

import (
"testing"
"time"

"github.com/fre5h/rnokpp"
)

func TestErrNotAllowedDate(t *testing.T) {
date, _ := time.Parse("02.04.2006", "31.12.1899")
x := &rnokpp.ErrNotAllowedDate{Date: date}

if x.Error() != "the allowed dates start from 01.01.1900, but your date 31.12.1899 is earlier" {
t.Error("Wrong error message for ErrNotAllowedDate")
}
}

func TestErrDateInFuture(t *testing.T) {
date, _ := time.Parse("02.04.2006", "01.01.3000")
x := &rnokpp.ErrDateInFuture{Date: date}

if x.Error() != "it is allowed to use only dates in past or current date, but your date is in the future 01.01.3000" {
t.Error("Wrong error message for ErrDateInFuture")
}
}
4 changes: 2 additions & 2 deletions rnokpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ func GetGender(rnokpp string) (*Gender, error) {
// GenerateRnokpp generates a valid RNOKPP by date and gender
func GenerateRnokpp(date time.Time, gender Gender) (rnokpp string, err error) {
if date.Before(internal.BaseDate) {
err = &ErrNotAllowedDate{date: date}
err = &ErrNotAllowedDate{Date: date}

return
}

if date.After(time.Now()) {
err = &ErrDateInFuture{date: date}
err = &ErrDateInFuture{Date: date}

return
}
Expand Down

0 comments on commit 6c673d8

Please sign in to comment.