Skip to content

Commit

Permalink
refactor: Tests.Undo (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
brprice authored Jun 26, 2023
2 parents 42c210b + 9670300 commit b662e66
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions primer/test/Tests/Undo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import Test.Tasty.HUnit (
assertFailure,
(@?=),
)
import Tests.Action.Prog (runAppTestM)
import Tests.Action.Prog (AppTestM, runAppTestM)
import Prelude (error)

mainModuleName :: App.Prog -> ModuleName
Expand All @@ -62,6 +62,14 @@ undoFailed e = assertFailure $ "Expected successful undo, but got " <> show e
redoFailed :: ProgError -> Assertion
redoFailed e = assertFailure $ "Expected successful redo, but got " <> show e

expectSuccess :: (Either ProgError a, b) -> IO (a, b)
expectSuccess = \case
(Left e, _) -> assertFailure $ "action unexpectedly failed: " <> show e
(Right x, y) -> pure (x, y)

run :: App -> AppTestM a -> IO (Either ProgError a, App)
run app = runAppTestM (appIdCounter app) app

compareAfterUndo :: App.Prog -> App.Prog -> App.Log -> Assertion
compareAfterUndo orig undone expectedRedoLog =
(orig & #redoLog .~ expectedRedoLog) @?= undone
Expand All @@ -76,7 +84,7 @@ unit_undo_available =
scope = mainModuleName $ appProg app
action = handleEditRequest [CreateDef scope $ Just "newDef"]
in do
(result, newApp) <- runAppTestM (appIdCounter app) app action
(result, newApp) <- run app action
case result of
Left e -> actionFailed e
Right _ -> assertBool "Expected undo to be available" $ undoAvailable $ getProg newApp
Expand All @@ -89,8 +97,8 @@ unit_undo_test1 =
edit = handleEditRequest action
undo = handleMutationRequest App.Undo
in do
(_, newApp) <- runAppTestM (appIdCounter originalApp) originalApp edit
(result, undoneApp) <- runAppTestM (appIdCounter newApp) newApp undo
(_, newApp) <- expectSuccess =<< run originalApp edit
(result, undoneApp) <- run newApp undo
case result of
Left e -> undoFailed e
Right _ -> do
Expand All @@ -107,9 +115,9 @@ unit_undo_test2 =
edit = handleEditRequest action
undo = handleMutationRequest App.Undo
in do
(_, newApp1) <- runAppTestM (appIdCounter originalApp) originalApp edit
(_, undoneApp) <- runAppTestM (appIdCounter newApp1) newApp1 undo
(result, newApp2) <- runAppTestM (appIdCounter undoneApp) undoneApp edit
(_, newApp1) <- expectSuccess =<< run originalApp edit
(_, undoneApp) <- expectSuccess =<< run newApp1 undo
(result, newApp2) <- run undoneApp edit
case result of
Left e -> undoFailed e
Right _ -> do
Expand All @@ -128,7 +136,7 @@ unit_no_redo_available_after_edit =
scope = mainModuleName $ appProg app
action = handleEditRequest [CreateDef scope $ Just "newDef"]
in do
(result, newApp) <- runAppTestM (appIdCounter app) app action
(result, newApp) <- run app action
case result of
Left e -> actionFailed e
Right _ -> assertBool "Expected no redo available" $ not $ redoAvailable $ getProg newApp
Expand All @@ -142,9 +150,9 @@ unit_redo_test1 =
undo = handleMutationRequest App.Undo
redo = handleMutationRequest App.Redo
in do
(_, newApp) <- runAppTestM (appIdCounter originalApp) originalApp edit
(_, undoneApp) <- runAppTestM (appIdCounter newApp) newApp undo
(result, redoneApp) <- runAppTestM (appIdCounter undoneApp) undoneApp redo
(_, newApp) <- expectSuccess =<< run originalApp edit
(_, undoneApp) <- expectSuccess =<< run newApp undo
(result, redoneApp) <- run undoneApp redo
case result of
Left e -> redoFailed e
Right _ -> do
Expand Down

0 comments on commit b662e66

Please sign in to comment.