Skip to content

Commit

Permalink
use purescripts-now and Milliseconds instead of Int
Browse files Browse the repository at this point in the history
  • Loading branch information
safareli committed Jan 16, 2019
1 parent 30d68f6 commit 00f8589
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
3 changes: 2 additions & 1 deletion bower.json
Expand Up @@ -26,6 +26,7 @@
"purescript-pipes": "^6.0.0",
"purescript-ansi": "^5.0.0",
"purescript-generics-rep": "^6.0.0",
"purescript-fork": "^4.0.0"
"purescript-fork": "^4.0.0",
"purescript-now": "^4.0.0"
}
}
6 changes: 4 additions & 2 deletions src/Test/Spec/Reporter/Spec.purs
Expand Up @@ -7,9 +7,11 @@ import Control.Monad.Writer (class MonadWriter)
import Data.Array (length)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Int as Int
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..), isNothing)
import Data.Time.Duration (Milliseconds(..))
import Test.Spec.Console (tellLn)
import Test.Spec.Reporter.Base (RunningItem(..), defaultReporter, defaultSummary, defaultUpdate)
import Test.Spec.Result (Result(..))
Expand Down Expand Up @@ -68,11 +70,11 @@ print
print path = case _ of
PrintSuite name -> do
tellLn $ indent path <> name
PrintTest name (Success speed ms) -> do
PrintTest name (Success speed (Milliseconds ms)) -> do
let
speedDetails = case speed of
Speed.Fast -> ""
_ -> styled (Speed.toStyle speed) $ " (" <> show ms <> "ms)"
_ -> styled (Speed.toStyle speed) $ " (" <> show (Int.round ms) <> "ms)"
tellLn $ (indent path) <> styled Style.green "✓︎ " <> styled Style.dim name <> speedDetails
PrintTest name (Failure err) -> do
{numFailures} <- modify \s -> s{numFailures = s.numFailures +1}
Expand Down
4 changes: 2 additions & 2 deletions src/Test/Spec/Result.purs
Expand Up @@ -3,14 +3,14 @@ module Test.Spec.Result where
import Prelude

import Data.Function (on)
import Data.Time.Duration (Milliseconds)
import Effect.Exception (Error)
import Effect.Exception as Error
import Test.Spec.Speed (Speed)


type Duration = Int
data Result
= Success Speed Duration
= Success Speed Milliseconds
| Failure Error

instance showResult :: Show Result where
Expand Down
4 changes: 0 additions & 4 deletions src/Test/Spec/Runner.js
Expand Up @@ -3,10 +3,6 @@

// module Test.Spec.Runner

exports.dateNow = function () {
return Date.now();
}

exports.exit = function(code) {
return function() {
try {
Expand Down
34 changes: 18 additions & 16 deletions src/Test/Spec/Runner.purs
Expand Up @@ -18,10 +18,12 @@ import Control.Monad.Writer (execWriterT)
import Control.Parallel (parTraverse, parallel, sequential)
import Data.Array (groupBy, mapWithIndex)
import Data.Array.NonEmpty as NEA
import Data.DateTime.Instant (unInstant)
import Data.Either (Either(..), either)
import Data.Foldable (foldl)
import Data.Function (on)
import Data.Identity (Identity(..))
import Data.Int (toNumber)
import Data.Int as Int
import Data.Maybe (Maybe(..))
import Data.Newtype (un)
import Data.Time.Duration (Milliseconds(..))
Expand All @@ -31,47 +33,46 @@ import Effect.Aff (Aff, attempt, delay, forkAff, joinFiber, makeAff, throwError,
import Effect.Aff.AVar as AV
import Effect.Class (liftEffect)
import Effect.Exception (Error, error)
import Effect.Now (now)
import Pipes ((>->), yield)
import Pipes.Core (Pipe, Producer, (//>))
import Pipes.Core (runEffectRec) as P
import Test.Spec (Item(..), Spec, SpecM, SpecTree, Tree(..))
import Test.Spec.Style (styled)
import Test.Spec.Style as Style
import Test.Spec.Console (logWriter, tellLn)
import Test.Spec.Result (Result(..))
import Test.Spec.Runner.Event (Event, Execution(..))
import Test.Spec.Runner.Event as Event
import Test.Spec.Speed (speedOf)
import Test.Spec.Style (styled)
import Test.Spec.Style as Style
import Test.Spec.Summary (successful)
import Test.Spec.Tree (Path, PathItem(..), countTests, discardUnfocused, isAllParallelizable)

foreign import exit :: Int -> Effect Unit

foreign import dateNow :: Effect Int

type Config =
{ slow :: Int
, timeout :: Maybe Int
{ slow :: Milliseconds
, timeout :: Maybe Milliseconds
, exit :: Boolean
}

defaultConfig :: Config
defaultConfig =
{ slow: 75
, timeout: Just 2000
{ slow: Milliseconds 75.0
, timeout: Just $ Milliseconds 2000.0
, exit: true
}

makeTimeout
:: Int
:: Milliseconds
-> Aff Unit
makeTimeout time = do
delay (Milliseconds $ toNumber time)
makeTimeout ms@(Milliseconds ms') = do
delay ms
makeAff \cb -> mempty <$ do
cb <<< Left $ error $ "test timed out after " <> show time <> "ms"
cb <<< Left $ error $ "test timed out after " <> show (Int.round ms') <> "ms"

timeout
:: Int
:: Milliseconds
-> Aff Unit
-> Aff Unit
timeout time t = do
Expand Down Expand Up @@ -115,11 +116,12 @@ _run config specs = execWriterT specs <#> discardUnfocused >>> \tests -> do
(Leaf name (Just (Item item))) -> do
yield $ Event.Test (if isParallelizable then Parallel else Sequential) path name
let example = item.example \a -> a unit
start <- lift $ liftEffect dateNow
start <- lift $ liftEffect now
e <- lift $ attempt case config.timeout of
Just t -> timeout t example
_ -> example
duration <- lift $ (_ - start) <$> liftEffect dateNow
end <- liftEffect now
let duration = Milliseconds $ on (-) (unInstant >>> un Milliseconds) end start
let res = either Failure (const $ Success (speedOf config.slow duration) duration) e
yield $ Event.TestEnd path name res
pure [ Leaf name $ Just res ]
Expand Down
9 changes: 5 additions & 4 deletions src/Test/Spec/Speed.purs
Expand Up @@ -5,6 +5,7 @@ import Prelude
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Generic.Rep.Show (genericShow)
import Data.Time.Duration (Milliseconds(..))
import Test.Spec.Style (Style)
import Test.Spec.Style as Style

Expand All @@ -14,10 +15,10 @@ derive instance genericSpeed :: Generic Speed _
instance showSpeed :: Show Speed where show = genericShow
instance showEq :: Eq Speed where eq = genericEq

speedOf :: Int -> Int -> Speed
speedOf thresh ms | ms > thresh = Slow
speedOf thresh ms | ms > thresh / 2 = Medium
speedOf _ _ = Fast
speedOf :: Milliseconds -> Milliseconds -> Speed
speedOf thresh ms | ms > thresh = Slow
speedOf (Milliseconds thresh) (Milliseconds ms) | ms > thresh / 2.0 = Medium
speedOf _ _ = Fast

toStyle :: Speed -> Style
toStyle Fast = Style.dim
Expand Down

0 comments on commit 00f8589

Please sign in to comment.