Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from neotoolkit/feat/zero-deps
Browse files Browse the repository at this point in the history
feat: remove dependencies
  • Loading branch information
sashamelentyev committed Jun 11, 2022
2 parents 9d885b8 + af0df8e commit a8a061d
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 65 deletions.
6 changes: 3 additions & 3 deletions boolean_test.go
Expand Up @@ -4,13 +4,13 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/neotoolkit/faker"
)

func TestBoolean_Boolean(t *testing.T) {
f := faker.NewFaker().Boolean()

require.Equal(t, "bool", reflect.TypeOf(f.Boolean()).String())
if reflect.TypeOf(f.Boolean()).String() != "bool" {
t.Fatal("Boolean type must be bool")
}
}
22 changes: 16 additions & 6 deletions faker_test.go
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/neotoolkit/faker"
)

Expand Down Expand Up @@ -33,9 +31,19 @@ func TestIntBetween(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
value := f.IntBetween(tc.min, tc.max)

require.Equal(t, fmt.Sprintf("%T", value), "int")
require.True(t, value >= tc.min)
require.True(t, value <= tc.max)
valueType := fmt.Sprintf("%T", value)

if valueType != "int" {
t.Fatalf("value type want int, got %T", value)
}

if value < tc.min {
t.Fatalf("value must be less %d", tc.min)
}

if value > tc.max {
t.Fatalf("value must be greater %d", tc.max)
}
})
}
}
Expand Down Expand Up @@ -156,7 +164,9 @@ func TestByName(t *testing.T) {
f := faker.NewFaker()
got := f.ByName(tc.faker)

require.True(t, got != nil)
if nil == got {
t.Fatal("faker by name is nil")
}
})
}
}
13 changes: 1 addition & 12 deletions go.mod
@@ -1,14 +1,3 @@
module github.com/neotoolkit/faker

go 1.17

require (
github.com/google/uuid v1.3.0
github.com/stretchr/testify v1.7.2
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
go 1.18
13 changes: 0 additions & 13 deletions go.sum
@@ -1,13 +0,0 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 12 additions & 8 deletions internet_test.go
@@ -1,39 +1,43 @@
package faker_test

import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/neotoolkit/faker"
)

func TestUsername(t *testing.T) {
i := faker.NewFaker().Internet()
username := i.Username()

require.Equal(t, true, len(username) > 0)
require.Equal(t, false, strings.Contains(username, " "))
if len(username) == 0 {
t.Fatal("username is empty")
}
}

func TestGTLD(t *testing.T) {
i := faker.NewFaker().Internet()
gTLD := i.GTLD()

require.True(t, len(gTLD) > 0)
if len(gTLD) == 0 {
t.Fatal("gTLD is empty")
}
}

func TestDomain(t *testing.T) {
i := faker.NewFaker().Internet()
d := i.Domain()

require.True(t, len(d) > 0)
if len(d) == 0 {
t.Fatal("domain is empty")
}
}

func TestEmail(t *testing.T) {
i := faker.NewFaker().Internet()
e := i.Email()

require.True(t, len(e) > 0)
if len(e) == 0 {
t.Fatal("domain is empty")
}
}
57 changes: 42 additions & 15 deletions person_test.go
Expand Up @@ -4,88 +4,115 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/neotoolkit/faker"
)

func TestPerson_FirstName(t *testing.T) {
f := faker.NewFaker()
firstName := f.Person().FirstName()

require.True(t, len(firstName) > 0)
if len(firstName) == 0 {
t.Fatal("firstName is empty")
}
}

func TestPerson_LastName(t *testing.T) {
f := faker.NewFaker()
p := f.Person()
lastName := p.LastName()

require.True(t, len(lastName) > 0)
if len(lastName) == 0 {
t.Fatal("lastName is empty")
}
}

func TestPerson_FirstNameMale(t *testing.T) {
f := faker.NewFaker()
firstNameMale := f.Person().FirstNameMale()

require.True(t, len(firstNameMale) > 0)
if len(firstNameMale) == 0 {
t.Fatal("firstNameMale is empty")
}
}

func TestPerson_FirstNameFemale(t *testing.T) {
f := faker.NewFaker()
p := f.Person()
firstNameFemale := p.FirstNameFemale()

require.True(t, len(firstNameFemale) > 0)
if len(firstNameFemale) == 0 {
t.Fatal("firstNameFemale is empty")
}
}

func TestPerson_Name(t *testing.T) {
f := faker.NewFaker()
p := f.Person()
name := p.Name()

require.True(t, len(name) > 0)
require.False(t, strings.Contains(name, "{{FirstNameMale}}"))
require.False(t, strings.Contains(name, "{{FirstNameFemale}}"))
require.False(t, strings.Contains(name, "{{LastName}}"))
if len(name) == 0 {
t.Fatal("name is empty")
}

if strings.Contains(name, "{{FirstNameMale}}") {
t.Fatal("name is format")
}

if strings.Contains(name, "{{FirstNameFemale}}") {
t.Fatal("name is format")
}

if strings.Contains(name, "{{LastName}}") {
t.Fatal("name is format")
}
}

func TestPerson_NameMale(t *testing.T) {
f := faker.NewFaker()
p := f.Person()
nameMale := p.NameMale()

require.True(t, len(nameMale) > 0)
if len(nameMale) == 0 {
t.Fatal("nameMale is empty")
}
}

func TestPerson_NameFemale(t *testing.T) {
f := faker.NewFaker()
p := f.Person()
nameFemale := p.NameFemale()

require.True(t, len(nameFemale) > 0)
if len(nameFemale) == 0 {
t.Fatal("nameFemale is empty")
}
}

func TestPerson_Gender(t *testing.T) {
f := faker.NewFaker()
p := f.Person()
gender := p.Gender()

require.True(t, gender == "Male" || gender == "Female")
if !(gender == "Male" || gender == "Female") {
t.Fatal("gender must be male or female")
}
}

func TestPerson_GenderMale(t *testing.T) {
f := faker.NewFaker()
p := f.Person()
genderMale := p.GenderMale()

require.True(t, genderMale == "Male")
if genderMale != "Male" {
t.Fatal("genderMale must be male")
}
}

func TestPerson_GenderFemale(t *testing.T) {
f := faker.NewFaker()
p := f.Person()
genderFemale := p.GenderFemale()

require.True(t, genderFemale == "Female")
if genderFemale != "Female" {
t.Fatal("genderFemale must be female")
}
}
10 changes: 8 additions & 2 deletions uuid.go
@@ -1,7 +1,9 @@
package faker

import (
"github.com/google/uuid"
"crypto/rand"
"fmt"
"io"
)

// UUID is struct for UUID
Expand All @@ -11,5 +13,9 @@ type UUID struct {

// V4 returns UUID V4 as string
func (u UUID) V4() string {
return uuid.New().String()
var uuid [16]byte
io.ReadFull(rand.Reader, uuid[:])
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
}
15 changes: 9 additions & 6 deletions uuid_test.go
Expand Up @@ -4,24 +4,27 @@ import (
"regexp"
"testing"

"github.com/stretchr/testify/require"

"github.com/neotoolkit/faker"
)

func TestUUID_v4(t *testing.T) {
f := faker.NewFaker()
value := f.UUID().V4()
match, err := regexp.MatchString("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$", value)

require.NoError(t, err)
require.True(t, match)
if err != nil {
t.Fatal(err)
}
if !match {
t.Fatal("want true, got false")
}
}

func TestUUID_V4UniqueInSequence(t *testing.T) {
f := faker.NewFaker()
last := f.UUID().V4()
current := f.UUID().V4()

require.Equal(t, true, last != current)
if last == current {
t.Fatal("want unique uuid")
}
}

0 comments on commit a8a061d

Please sign in to comment.