-
-
Notifications
You must be signed in to change notification settings - Fork 148
fix: DayOfWeek now returns random day #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ package faker | |
|
||
import ( | ||
"fmt" | ||
"github.com/bxcodec/faker/support/slice" | ||
"log" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/bxcodec/faker/support/slice" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSetDateTimer(t *testing.T) { | ||
|
@@ -82,6 +84,17 @@ func TestDayOfWeek(t *testing.T) { | |
} | ||
} | ||
|
||
func TestDayOfWeekReturnsDifferentValues(t *testing.T) { | ||
dayMap := make(map[string]struct{}) | ||
iterations := 5 // sufficiently large to assure we don't randomly get the same value again | ||
for i := 0; i < iterations; i++ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loop creates a mathematical set of all returned values. Each call should return a random value but, by pure randomness, a couple calls might return the same value. Hence the loop to call enough times to make sure there are different values returned. |
||
day := GetDateTimer().DayOfWeek() | ||
//t.Log(day) | ||
dayMap[day] = struct{}{} | ||
} | ||
assert.True(t, len(dayMap) > 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is more than one value in the set, we know we have that the underlying function is not always returning the same value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Referring to my first comment about testify, could you please change this into manual assertion without using testify? :D Thank you |
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: If you uncomment the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because you already using map, I think you can also utilize the map to avoid the same value repeated. day := GetDateTimer().DayOfWeek()
if _, ok := dayMap[day]; ok {
i--
continue
}
dayMap[day] = struct{}{}
t.Log(day) // Will print random and different day 5 times. |
||
func TestDayOfMonth(t *testing.T) { | ||
d := GetDateTimer() | ||
_, err := time.Parse(DayOfMonth, d.DayOfMonth()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know
testify
is really helpful. But I would recommend to not using it here. I'd prefer if we just using a simple assertion manually like other test-case. It's just to avoid any possible future conflict related to dependencies because we don't use any dependency management tools here (Dep, Glide etc).