Skip to content

Commit

Permalink
fake
Browse files Browse the repository at this point in the history
  • Loading branch information
emarx committed Apr 13, 2024
1 parent 3a01f53 commit 874ffbc
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
18 changes: 18 additions & 0 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,3 +1223,21 @@ func EqualAsString(t testRunner, expected any, actual any, msg ...any) {

Equal(t, fmt.Sprint(expected), fmt.Sprint(actual), msg...)
}

func EqualLength[T any, U any](t testRunner, expected []T, actual []U, msg ...any) {
if test, ok := t.(helper); ok {
test.Helper()
}

if len(expected) != len(actual) {
internal.Fail(
t,
fmt.Sprintf("Expected length (%d) does not match actual length (%d).", len(expected), len(actual)),
internal.Objects{
internal.NewObjectsSingleNamed("Expected", expected)[0],
internal.NewObjectsSingleNamed("Actual", actual)[0],
},
msg...,
)
}
}
15 changes: 15 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1654,3 +1654,18 @@ func TestAssertNotInRange_fails(t *testing.T) {
InRange(t, 0, 1, 3)
})
}

func TestEqualLength(t *testing.T) {
EqualLength(t, []int{1, 2, 3}, []int{4, 5, 6})
EqualLength(t, []string{"foo", "bar"}, []string{"baz", "foo"})
}

func TestEqualLength_fails(t *testing.T) {
TestFails(t, func(t TestingPackageWithFailFunctions) {
EqualLength(t, []int{1, 2, 3}, []int{4, 5, 6, 7})
})

TestFails(t, func(t TestingPackageWithFailFunctions) {
EqualLength(t, []string{"foo", "bar"}, []string{"baz"})
})
}
57 changes: 57 additions & 0 deletions fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package assert

import (
"fmt"
"github.com/lucsky/cuid"
"math/rand"
)

type FakeProfile struct {
FirstName string
LastName string
FullName string
Email string
}

func FakeFirstName() string {
randomNames := []string{
"John", "Jane", "Bob", "Alice", "Charlie", "David", "Eve", "Frank",
"Grace", "Heidi", "Ivan", "Judy", "Kevin", "Laura", "Michael", "Nancy",
"Olivia", "Peter", "Quincy", "Rachel", "Steve", "Tina", "Ursula", "Victor",
"Wendy", "Xander", "Yvonne", "Zach",
}
return randomNames[rand.Intn(len(randomNames))]
}

func FakeLastName() string {
randomLastNames := []string{
"Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller",
"Davis", "Rodriguez", "Martinez", "Hernandez", "Lopez", "Gonzalez",
"Wilson", "Anderson", "Thomas", "Taylor", "Moore", "Jackson", "Martin",
"Lee", "Perez", "Thompson", "White", "Harris", "Sanchez", "Clark",
"Ramirez", "Lewis", "Robinson", "Walker", "Young", "Allen", "King",
"Wright", "Scott", "Torres", "Nguyen", "Hill", "Flores", "Green",
"Adams", "Nelson", "Baker", "Hall", "Rivera", "Campbell", "Mitchell",
"Carter", "Roberts",
}
return randomLastNames[rand.Intn(len(randomLastNames))]
}

func GetFakeProfile() *FakeProfile {
firstName := FakeFirstName()
lastName := FakeLastName()
domains := []string{"gmail.com", "yahoo.com", "outlook.com", "aol.com", "protonmail.com"}

return &FakeProfile{
FirstName: firstName,
LastName: lastName,
FullName: fmt.Sprintf("%s %s", firstName, lastName),
Email: fmt.Sprintf(
"%s-%s-%s@%s",
firstName,
lastName,
cuid.New(),
domains[rand.Intn(len(domains))],
),
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/josephburnett/jd v1.8.1
github.com/klauspost/cpuid/v2 v2.2.7
github.com/lucsky/cuid v1.2.1
github.com/pterm/pterm v0.12.79
github.com/sergi/go-diff v1.3.1
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
github.com/lucsky/cuid v1.2.1 h1:MtJrL2OFhvYufUIn48d35QGXyeTC8tn0upumW9WwTHg=
github.com/lucsky/cuid v1.2.1/go.mod h1:QaaJqckboimOmhRSJXSx/+IT+VTfxfPGSo/6mfgUfmE=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
Expand Down

0 comments on commit 874ffbc

Please sign in to comment.