Skip to content

Commit

Permalink
feat: add test coverage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinntas committed Feb 25, 2024
1 parent 99504a7 commit ea05555
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 0 additions & 2 deletions api/user/domain/valueObjects/userEmail.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package userValueObjects

import (
"go-rest-template/internal/app/utils"
"go-rest-template/internal/app/web"
"net/http"
"regexp"
Expand All @@ -14,7 +13,6 @@ type Email struct {
func ValidateEmail(value string) (*Email, *web.HttpError) {
emailRegex := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
if result := emailRegex.MatchString(value); result == false {
utils.Print(result)
return nil, &web.HttpError{
Code: http.StatusUnprocessableEntity,
Body: map[string]interface{}{
Expand Down
38 changes: 38 additions & 0 deletions api/user/domain/valueObjects/userEmail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package userValueObjects

import (
"testing"
)

func TestEmptyEmail(t *testing.T) {
t.Parallel()
email, err := ValidateEmail("")
if email != nil {
t.Errorf("Expected nil, got %v", email)
}
if err == nil {
t.Errorf("Expected error, got nil")
}
}

func TestInvalidEmail(t *testing.T) {
t.Parallel()
email, err := ValidateEmail("invalidemail")
if email != nil {
t.Errorf("Expected nil, got %v", email)
}
if err == nil {
t.Errorf("Expected error, got nil")
}
}

func TestValidEmail(t *testing.T) {
t.Parallel()
email, err := ValidateEmail("validemail@gmail.com")
if email == nil {
t.Errorf("Expected email, got nil")
}
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
}

0 comments on commit ea05555

Please sign in to comment.