forked from smallhadroncollider/taskell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds redo functionality with
r
(#51)
- Loading branch information
1 parent
3b3ba8d
commit c3a4094
Showing
13 changed files
with
201 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
|
||
module Events.State.History | ||
( undo | ||
, redo | ||
, store | ||
) where | ||
|
||
import ClassyPrelude | ||
|
||
import Control.Lens (Lens', (&), (.~), (^.)) | ||
|
||
import Events.State.Types (History, future, past, present) | ||
|
||
λstack :: Lens' (History a) [a] -> History a -> [a] | ||
λstack fn history = history ^. present : (history ^. fn) | ||
|
||
store :: History a -> History a | ||
store history = history & past .~ λstack past history & future .~ empty | ||
|
||
undo :: History a -> History a | ||
undo history = | ||
case history ^. past of | ||
[] -> history | ||
(moment:xs) -> history & present .~ moment & past .~ xs & future .~ λstack future history | ||
|
||
redo :: History a -> History a | ||
redo history = | ||
case history ^. future of | ||
[] -> history | ||
(moment:xs) -> history & present .~ moment & future .~ xs & past .~ λstack past history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Events.State.HistoryTest | ||
( test_history | ||
) where | ||
|
||
import ClassyPrelude | ||
|
||
import Test.Tasty | ||
import Test.Tasty.HUnit | ||
|
||
import Events.State.History | ||
import Events.State.Types | ||
|
||
make :: [Integer] -> Integer -> [Integer] -> History Integer | ||
make = History | ||
|
||
testHistory :: History Integer | ||
testHistory = make empty 0 empty | ||
|
||
-- tests | ||
test_history :: TestTree | ||
test_history = | ||
testGroup | ||
"Events.State.History" | ||
[ testGroup | ||
"undo" | ||
[ testCase "empty" (assertEqual "Nothing changes" testHistory (undo testHistory)) | ||
, testCase | ||
"one undo" | ||
(assertEqual "Goes back" (make empty 0 [1]) (undo $ make [0] 1 empty)) | ||
, testCase | ||
"two undo" | ||
(assertEqual | ||
"Goes back" | ||
(make empty 0 [1, 2]) | ||
(undo . undo $ make [1, 0] 2 empty)) | ||
, testCase | ||
"three undo" | ||
(assertEqual | ||
"Goes back" | ||
(make empty 0 [1, 2, 3]) | ||
(undo . undo . undo $ make [2, 1, 0] 3 empty)) | ||
] | ||
, testGroup | ||
"redo" | ||
[ testCase "empty" (assertEqual "Nothing changes" testHistory (redo testHistory)) | ||
, testCase | ||
"one redo" | ||
(assertEqual "Goes forward" (make [0] 1 empty) (redo $ make empty 0 [1])) | ||
, testCase | ||
"two redo" | ||
(assertEqual | ||
"Goes forward" | ||
(make [1, 0] 2 empty) | ||
(redo . redo $ make empty 0 [1, 2])) | ||
, testCase | ||
"three redo" | ||
(assertEqual | ||
"Goes forward" | ||
(make [2, 1, 0] 3 empty) | ||
(redo . redo . redo $ make empty 0 [1, 2, 3])) | ||
] | ||
, testGroup | ||
"mix" | ||
[ testCase | ||
"empty" | ||
(assertEqual | ||
"Nothing changes" | ||
testHistory | ||
(undo . redo . undo . redo . undo $ testHistory)) | ||
, testCase | ||
"redo undo" | ||
(assertEqual | ||
"Nothing changes" | ||
(make [4, 3, 2, 1, 0] 5 [6, 7, 8, 9, 10]) | ||
(undo . redo $ make [4, 3, 2, 1, 0] 5 [6, 7, 8, 9, 10])) | ||
, testCase | ||
"undo redo" | ||
(assertEqual | ||
"Nothing changes" | ||
(make [4, 3, 2, 1, 0] 5 [6, 7, 8, 9, 10]) | ||
(redo . undo $ make [4, 3, 2, 1, 0] 5 [6, 7, 8, 9, 10])) | ||
] | ||
, testGroup | ||
"store" | ||
[ testCase | ||
"empty" | ||
(assertEqual "Stores current value" (make [0] 0 empty) (store testHistory)) | ||
, testCase | ||
"undo store redo" | ||
(assertEqual | ||
"Clears redo" | ||
(make [4, 3, 2, 1, 0] 4 empty) | ||
(redo . store . undo $ make [4, 3, 2, 1, 0] 5 [6, 7, 8, 9, 10])) | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters