Skip to content

Commit

Permalink
Deflake tests
Browse files Browse the repository at this point in the history
 - no palindromic dates
 - no digits in arbitrary bytestrings
  • Loading branch information
olorin committed Jul 19, 2016
1 parent 00fd1c0 commit 3b7136d
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions test/Test/Warden/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import qualified Data.ByteString.Lazy as BL
import Data.Char
import Data.Csv
import qualified Data.Set as S
import Data.List (nub)
import Data.List ((\\), nub)
import qualified Data.List as L
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
import Data.String (String)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, decodeUtf8', encodeUtf8)
import Data.Time.Calendar (Day(..))
Expand Down Expand Up @@ -807,34 +808,45 @@ genSensibleDateTime = do
renderedDate :: Gen BS.ByteString
renderedDate = do
dt <- genSensibleDateTime
render <- frequency [(1, renderNoSeparator), (9, renderSeparator)]
render <- frequency [(1, renderDateNoSeparator), (9, renderDateSeparator)]
pure . BSC.pack $ render dt
where
renderNoSeparator = do
tp <- timePart
pure (formatTime defaultTimeLocale ("%Y%m%d" <> tp))

renderSeparator = do
tp <- timePart
sep <- elements ["-", ".", "/"]
pure (formatTime defaultTimeLocale ((concat ["%Y", sep, "%m", sep, "%d"]) <> tp))

timePart = elements [
""
, "%H%M"
, "%H:%M"
, "%H%M%S"
, "%H:%M:%S"
, "T%H:%M:%SZ"
]

renderDateNoSeparator :: Gen (UTCTime -> String)
renderDateNoSeparator = do
tp <- timePart
pure (formatTime defaultTimeLocale ("%Y%m%d" <> tp))

renderDateSeparator :: Gen (UTCTime -> String)
renderDateSeparator = do
tp <- timePart
sep <- elements ["-", ".", "/"]
pure (formatTime defaultTimeLocale ((concat ["%Y", sep, "%m", sep, "%d"]) <> tp))

timePart :: Gen String
timePart = elements [
""
, "%H%M"
, "%H:%M"
, "%H%M%S"
, "%H:%M:%S"
, "T%H:%M:%SZ"
]


renderedNonDate :: Gen BS.ByteString
renderedNonDate = do
x <- fmap BS.pack $ listOf1 arbitrary
x <- fmap BS.pack $ listOf1 noDigits
dt <- renderedDate
oneof [
pure $ x <> dt
, pure $ BS.reverse dt
, reversed
, encodeUtf8 <$> elements muppets
]
where
reversed = do
dt <- genSensibleDateTime
render <- renderDateSeparator
pure . BSC.pack . reverse $ render dt

noDigits =
elements $ [0x00..0xff] \\ [0x30..0x39]

0 comments on commit 3b7136d

Please sign in to comment.