Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Tests.Undo #1083

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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