From 29f51ee629b80b0701de64bca4406c6affe4a051 Mon Sep 17 00:00:00 2001 From: kamoii <> Date: Thu, 11 Jun 2020 01:29:28 +0900 Subject: [PATCH] Move runWidegeAsAff function to Dischage module --- src/Concur/Core/Discharge.purs | 21 +++++++++++++++++++-- test/Test/Utils.purs | 28 ---------------------------- test/Test/WidgetSpec.purs | 6 +++--- 3 files changed, 22 insertions(+), 33 deletions(-) delete mode 100644 test/Test/Utils.purs diff --git a/src/Concur/Core/Discharge.purs b/src/Concur/Core/Discharge.purs index 4475d82..7149643 100644 --- a/src/Concur/Core/Discharge.purs +++ b/src/Concur/Core/Discharge.purs @@ -3,11 +3,15 @@ module Concur.Core.Discharge where import Prelude import Concur.Core.Types (Widget(..), WidgetStep(..), unWidget) -import Control.Monad.Free (resume, wrap) +import Control.Monad.Free (resume, runFreeM, wrap) +import Control.Monad.Writer (runWriterT, tell) +import Data.Array (singleton) import Data.Either (Either(..)) import Data.Tuple (Tuple(..)) import Effect (Effect) -import Effect.Aff (runAff_) +import Effect.Aff (Aff, runAff_) +import Effect.Aff.Class (liftAff) +import Effect.Class (liftEffect) import Effect.Exception (Error) -- Widget discharge strategies @@ -44,6 +48,19 @@ dischargePartialEffect w = case resume (unWidget w) of dischargePartialEffect (Widget w') Left (WidgetStepView ws) -> pure (Tuple (Widget (wrap (WidgetStepView ws))) ws.view) +-- | Dischage all effects and recieve the result and viewss as Array. +-- | Mainly for testing. +-- | Be carefull that never ending Widget will convert to never ending Aff. +dischargeAll :: forall v a. Widget v a -> Aff { result :: a, views :: Array v } +dischargeAll widget = do + Tuple result views <- runWriterT $ runFreeM interpret (unWidget widget) + pure { result, views } + where + interpret (WidgetStepEff eff) = + liftEffect eff + interpret (WidgetStepView rec) = do + tell $ singleton rec.view + liftAff rec.cont {- -- | Discharge a widget, forces async resolution of the continuation. -- | 1. Runs the Effect action diff --git a/test/Test/Utils.purs b/test/Test/Utils.purs deleted file mode 100644 index 304d99e..0000000 --- a/test/Test/Utils.purs +++ /dev/null @@ -1,28 +0,0 @@ -module Test.Utils where - -import Prelude - -import Concur.Core (Widget) -import Concur.Core.Types (WidgetStep(..), unWidget) -import Control.Monad.Free (runFreeM) -import Control.Monad.Writer.Trans (runWriterT, tell) -import Data.Array (singleton) -import Data.Tuple (Tuple(..)) -import Effect.Aff (Aff) -import Effect.Aff.Class (liftAff) -import Effect.Class (liftEffect) - - --- Evalutates Widget to Aff --- Be carefull that never ending Widget will convert to never ending Aff. -runWidgetAsAff :: forall v a. Widget v a -> Aff { result :: a, views :: Array v } -runWidgetAsAff widget = do - Tuple result views <- runWriterT $ runFreeM interpret (unWidget widget) - pure { result, views } - where - interpret (WidgetStepEff eff) = - liftEffect eff - - interpret (WidgetStepView rec) = do - tell $ singleton rec.view - liftAff rec.cont diff --git a/test/Test/WidgetSpec.purs b/test/Test/WidgetSpec.purs index c859225..e20f0b3 100644 --- a/test/Test/WidgetSpec.purs +++ b/test/Test/WidgetSpec.purs @@ -2,6 +2,7 @@ module Test.WidgetSpec where import Prelude +import Concur.Core.Discharge (dischargeAll) import Concur.Core.Types (affAction) import Control.MultiAlternative (orr) import Data.Time.Duration (Milliseconds(..)) @@ -10,7 +11,6 @@ import Effect.Class (liftEffect) import Effect.Ref as Ref import Test.Spec (Spec, describe, it) import Test.Spec.Assertions (shouldEqual, shouldReturn) -import Test.Utils (runWidgetAsAff) widgetSpec :: Spec Unit widgetSpec = @@ -18,7 +18,7 @@ widgetSpec = describe "orr" do it "should cancel running effects when the widget returns a value" do ref <- liftEffect $ Ref.new "" - { views } <- runWidgetAsAff $ orr + { views } <- dischargeAll $ orr [ affAction "a" do delay (Milliseconds 100.0) liftEffect $ Ref.write "a" ref @@ -33,7 +33,7 @@ widgetSpec = it "should start all the widgets only once" do ref <- liftEffect (Ref.new 0) - { result, views } <- runWidgetAsAff $ orr + { result, views } <- dischargeAll $ orr [ do affAction "a0" $ delay (Milliseconds 100.0) affAction "a1" $ delay (Milliseconds 100.0)