@@ -21,8 +21,13 @@ import (
21
21
22
22
func TestRandom (t * testing.T ) {
23
23
s := testcase .NewSpec (t )
24
+
25
+ source := let .Var (s , func (t * testcase.T ) rand.Source {
26
+ return rand .NewSource (int64 (t .Random .Int ()))
27
+ })
28
+
24
29
rnd := testcase .Let (s , func (t * testcase.T ) * random.Random {
25
- return & random.Random {Source : rand . NewSource ( time . Now (). Unix () )}
30
+ return & random.Random {Source : source . Get ( t )}
26
31
})
27
32
28
33
SpecRandomMethods (s , rnd )
@@ -54,6 +59,10 @@ func TestRandom(t *testing.T) {
54
59
t .Must .Equal (u1 , u2 )
55
60
})
56
61
})
62
+
63
+ s .Context ("between-methods-behaviour" , func (s * testcase.Spec ) {
64
+ SpecRandomBetweenMethodBehaviour (s , rnd )
65
+ })
57
66
}
58
67
59
68
func SpecRandomMethods (s * testcase.Spec , rnd testcase.Var [* random.Random ]) {
@@ -825,6 +834,21 @@ func specFloatBetween(s *testcase.Spec, subject func(t *testcase.T, min, max flo
825
834
assert .Must (t ).True (out <= max .Get (t ), `expected that out is <= than max` )
826
835
})
827
836
837
+ s .Then ("min and max are possible results" , func (t * testcase.T ) {
838
+ var gotMin , gotMax bool
839
+ t .Eventually (func (t * testcase.T ) {
840
+ out := act (t )
841
+ if out == min .Get (t ) {
842
+ gotMin = true
843
+ }
844
+ if out == max .Get (t ) {
845
+ gotMax = true
846
+ }
847
+ assert .True (t , gotMin , "expected that min is part of the possible results" )
848
+ assert .True (t , gotMax , "expected that max is part of the possible results" )
849
+ })
850
+ })
851
+
828
852
s .And (`min and max is in the negative range` , func (s * testcase.Spec ) {
829
853
min .LetValue (s , - 128 )
830
854
max .LetValue (s , - 64 )
@@ -836,12 +860,20 @@ func specFloatBetween(s *testcase.Spec, subject func(t *testcase.T, min, max flo
836
860
})
837
861
})
838
862
839
- s .And (`min and max equal` , func (s * testcase.Spec ) {
840
- max .Let (s , min .Get )
841
-
842
- s .Then (`it returns the min and max value since the range can only have one value` , func (t * testcase.T ) {
843
- t .Must .Equal (max .Get (t ), act (t ))
844
- })
863
+ s .Test ("smoke" , func (t * testcase.T ) {
864
+ var smoke = func (min , max float64 ) {
865
+ t .Eventually (func (t * testcase.T ) {
866
+ out := subject (t , min , max )
867
+ assert .NotEqual (t , out , min )
868
+ assert .NotEqual (t , out , max )
869
+ assert .True (t , min < out )
870
+ assert .True (t , out < max )
871
+ })
872
+ }
873
+ smoke (0 , 0.1 )
874
+ smoke (0 , 0.01 )
875
+ smoke (0 , 0.001 )
876
+ smoke (0 , 0.0001 )
845
877
})
846
878
}
847
879
@@ -970,18 +1002,18 @@ func SpecTimeBetween(s *testcase.Spec, rnd testcase.Var[*random.Random], sbj fun
970
1002
toTime := testcase .Let (s , func (t * testcase.T ) time.Time {
971
1003
return fromTime .Get (t ).Add (24 * time .Hour )
972
1004
})
973
- var subject = func (t * testcase.T ) time.Time {
1005
+ act := let . Act ( func (t * testcase.T ) time.Time {
974
1006
return sbj (t )(fromTime .Get (t ), toTime .Get (t ))
975
- }
1007
+ })
976
1008
977
1009
s .Then (`it will return a date between the given time range including 'from' and excluding 'to'` , func (t * testcase.T ) {
978
- out := subject (t )
1010
+ out := act (t )
979
1011
assert .Must (t ).True (fromTime .Get (t ).Unix () <= out .Unix (), `expected that from <= than out` )
980
1012
assert .Must (t ).True (out .Unix () < toTime .Get (t ).Unix (), `expected that out is < than to` )
981
1013
})
982
1014
983
1015
s .Then (`it will generate different time on each call` , func (t * testcase.T ) {
984
- assert .Must (t ).NotEqual (subject (t ), subject (t ))
1016
+ assert .Must (t ).NotEqual (act (t ), act (t ))
985
1017
})
986
1018
987
1019
s .And (`from is before 1970-01-01 (unix timestamp 0)` , func (s * testcase.Spec ) {
@@ -993,14 +1025,14 @@ func SpecTimeBetween(s *testcase.Spec, rnd testcase.Var[*random.Random], sbj fun
993
1025
})
994
1026
995
1027
s .Then (`it will generate a random time between 'from' and 'to'` , func (t * testcase.T ) {
996
- out := subject (t )
1028
+ out := act (t )
997
1029
assert .Must (t ).True (fromTime .Get (t ).Unix () <= out .Unix (), `expected that from <= than out` )
998
1030
assert .Must (t ).True (out .Unix () < toTime .Get (t ).Unix (), `expected that out is < than to` )
999
1031
})
1000
1032
})
1001
1033
1002
1034
s .Then (`result is safe to format into RFC3339` , func (t * testcase.T ) {
1003
- t1 := subject (t )
1035
+ t1 := act (t )
1004
1036
t2 , _ := time .Parse (time .RFC3339 , t1 .Format (time .RFC3339 ))
1005
1037
t .Must .Equal (t1 .UTC (), t2 .UTC ())
1006
1038
})
@@ -1013,16 +1045,10 @@ func SpecTimeBetween(s *testcase.Spec, rnd testcase.Var[*random.Random], sbj fun
1013
1045
return fromTime .Get (t ).Add (- 1 * time .Second )
1014
1046
})
1015
1047
1016
- s .Then ("" , func (t * testcase.T ) {
1017
- out := assert .Panic (t , func () { subject (t ) })
1018
- assert .NotNil (t , out )
1019
- panicMessage := fmt .Sprintf ("%v" , out )
1020
- assert .Contain (t , panicMessage , `invalid` )
1021
- assert .Contain (t , panicMessage , `[to]` )
1022
- assert .Contain (t , panicMessage , `earlier` )
1023
- assert .Contain (t , panicMessage , `[from]` )
1024
- assert .Contain (t , panicMessage , fromTime .Get (t ).Format (time .RFC3339 ))
1025
- assert .Contain (t , panicMessage , toTime .Get (t ).Format (time .RFC3339 ))
1048
+ s .Then ("to and from swapped" , func (t * testcase.T ) {
1049
+ out := act (t )
1050
+ assert .True (t , toTime .Get (t ).Before (out ) || toTime .Get (t ).Equal (out ))
1051
+ assert .True (t , fromTime .Get (t ).Equal (out ) || fromTime .Get (t ).After (out ))
1026
1052
})
1027
1053
})
1028
1054
}
@@ -1094,3 +1120,37 @@ func TestPick(t *testing.T) {
1094
1120
})
1095
1121
})
1096
1122
}
1123
+
1124
+ func SpecRandomBetweenMethodBehaviour (s * testcase.Spec , rnd testcase.Var [* random.Random ]) {
1125
+ s .Test ("IntBetween" , func (t * testcase.T ) {
1126
+ min := - 100
1127
+ max := 100
1128
+ out := rnd .Get (t ).IntBetween (max , min )
1129
+ assert .True (t , min <= out )
1130
+ assert .True (t , out <= max )
1131
+ })
1132
+
1133
+ s .Test ("DurationBetween" , func (t * testcase.T ) {
1134
+ min := - time .Hour
1135
+ max := - time .Second
1136
+ out := rnd .Get (t ).DurationBetween (max , min )
1137
+ assert .True (t , min <= out )
1138
+ assert .True (t , out <= max )
1139
+ })
1140
+
1141
+ s .Test ("FloatBetween" , func (t * testcase.T ) {
1142
+ min := - 10.0
1143
+ max := 10.0
1144
+ out := rnd .Get (t ).FloatBetween (max , min )
1145
+ assert .True (t , min <= out )
1146
+ assert .True (t , out <= max )
1147
+ })
1148
+
1149
+ s .Test ("TimeBetween" , func (t * testcase.T ) {
1150
+ min := time .Now ()
1151
+ max := min .AddDate (0 , 0 , 1 )
1152
+ out := rnd .Get (t ).TimeBetween (max , min )
1153
+ assert .True (t , min .Before (out ) || min .Equal (out ))
1154
+ assert .True (t , max .Equal (out ) || max .After (out ))
1155
+ })
1156
+ }
0 commit comments