aboutsummaryrefslogtreecommitdiff
path: root/specs/Specs
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2024-12-26 20:52:51 +0530
committerAkshay Nair <phenax5@gmail.com>2024-12-26 21:13:57 +0530
commit7dfa0f2866ea6d3441c6c343d841e969aa2ea77d (patch)
tree0a503ad21d7efa4a8d9c91e319804c23a1e9f567 /specs/Specs
parent6ad789149036a9e97a9c66860828892efa432bd4 (diff)
downloadchelleport-7dfa0f2866ea6d3441c6c343d841e969aa2ea77d.tar.gz
chelleport-7dfa0f2866ea6d3441c6c343d841e969aa2ea77d.zip
Adds mock return typing
Diffstat (limited to 'specs/Specs')
-rw-r--r--specs/Specs/AppStateSpec.hs94
-rw-r--r--specs/Specs/ViewSpec.hs48
2 files changed, 90 insertions, 52 deletions
diff --git a/specs/Specs/AppStateSpec.hs b/specs/Specs/AppStateSpec.hs
index 7eeb082..245ee9c 100644
--- a/specs/Specs/AppStateSpec.hs
+++ b/specs/Specs/AppStateSpec.hs
@@ -35,7 +35,7 @@ test = do
it "hides window, triggers mouse click and shows the window again" $ do
(_, mock) <- runWithMocks $ update currentState $ ChainMouseClick LeftClick
- calls mock `shouldBe` [Mock_hideWindow, Mock_clickMouseButton LeftClick, Mock_showWindow]
+ mock `shouldContainCalls` [Mock_hideWindow, Mock_clickMouseButton LeftClick, Mock_showWindow]
it "continues with action ResetKeys without updating state" $ do
((nextState, action), _) <- runWithMocks $ update currentState $ ChainMouseClick LeftClick
@@ -51,23 +51,23 @@ test = do
it "clicks multiple times" $ do
(_, mock) <- runWithMocks $ update currentState $ ChainMouseClick LeftClick
- calls mock
- `shouldBe` [ Mock_hideWindow,
- Mock_clickMouseButton LeftClick,
- Mock_clickMouseButton LeftClick,
- Mock_clickMouseButton LeftClick,
- Mock_showWindow
- ]
+ mock
+ `shouldContainCalls` [ Mock_hideWindow,
+ Mock_clickMouseButton LeftClick,
+ Mock_clickMouseButton LeftClick,
+ Mock_clickMouseButton LeftClick,
+ Mock_showWindow
+ ]
context "when repetition is 0" $ do
let currentState = defaultState {stateRepetition = 0}
it "clicks just once" $ do
(_, mock) <- runWithMocks $ update currentState $ ChainMouseClick LeftClick
- calls mock `shouldBe` [Mock_hideWindow, Mock_clickMouseButton LeftClick, Mock_showWindow]
+ mock `shouldContainCalls` [Mock_hideWindow, Mock_clickMouseButton LeftClick, Mock_showWindow]
context "with action IncrementHighlightIndex" $ do
- let currentState = defaultState
+ -- let currentState = defaultState
it "todo: implement" $ do
1 `shouldBe` 1
@@ -76,7 +76,9 @@ test = do
let currentState = defaultState
it "continues with MoveMousePosition" $ do
- ((_, action), _) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5)
+ ((_, action), _) <- runWithMocks $ do
+ Mock_getMousePointerPosition `mockReturns` (42, 42)
+ update currentState $ IncrementMouseCursor (10, -5)
action `shouldBe` Just (MoveMousePosition (52, 37))
it "does update state" $ do
@@ -87,14 +89,18 @@ test = do
let currentState = defaultState {stateRepetition = 5}
it "multiplies increment" $ do
- ((_, action), _) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5)
+ ((_, action), _) <- runWithMocks $ do
+ Mock_getMousePointerPosition `mockReturns` (42, 42)
+ update currentState $ IncrementMouseCursor (10, -5)
action `shouldBe` Just (MoveMousePosition (92, 17))
context "when repetition is 0" $ do
let currentState = defaultState {stateRepetition = 0}
it "increments just once" $ do
- ((_, action), _) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5)
+ ((_, action), _) <- runWithMocks $ do
+ Mock_getMousePointerPosition `mockReturns` (42, 42)
+ update currentState $ IncrementMouseCursor (10, -5)
action `shouldBe` Just (MoveMousePosition (52, 37))
context "with action MouseDragEnd" $ do
@@ -102,7 +108,7 @@ test = do
it "hides window, stops dragging and shows the window again" $ do
(_, mock) <- runWithMocks $ update currentState MouseDragEnd
- calls mock `shouldContain` [Mock_hideWindow, Mock_releaseMouseButton, Mock_showWindow]
+ mock `shouldContainCalls` [Mock_hideWindow, Mock_releaseMouseButton, Mock_showWindow]
it "does not continue or update state" $ do
(result, _) <- runWithMocks $ update currentState MouseDragStart
@@ -113,7 +119,7 @@ test = do
it "hides window, starts dragging and shows the window again" $ do
(_, mock) <- runWithMocks $ update currentState MouseDragStart
- calls mock `shouldContain` [Mock_hideWindow, Mock_pressMouseButton, Mock_showWindow]
+ mock `shouldContainCalls` [Mock_hideWindow, Mock_pressMouseButton, Mock_showWindow]
it "does not continue or update state" $ do
(result, _) <- runWithMocks $ update currentState MouseDragStart
@@ -172,7 +178,10 @@ test = do
nextState `shouldBe` currentState {stateKeySequence = "DEF", stateIsMatched = True}
it "continues with MoveMousePosition action at center of matched cell" $ do
- ((_, action), _) <- runWithMocks $ update currentState $ HandleKeyInput SDL.KeycodeF
+ ((_, action), _) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ Mock_windowPosition `mockReturns` mockWindowPosition
+ update currentState $ HandleKeyInput SDL.KeycodeF
action `shouldBe` Just (MoveMousePosition (1640, 370))
context "with action MoveMouseInDirection" $ do
@@ -180,19 +189,27 @@ test = do
context "when direction is up" $ do
it "continues to increment movement" $ do
- ((_, action), _) <- runWithMocks $ update currentState $ MoveMouseInDirection DirUp
+ ((_, action), _) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ update currentState $ MoveMouseInDirection DirUp
action `shouldBe` Just (IncrementMouseCursor (0, -33))
context "when direction is down" $ do
it "continues to increment movement" $ do
- ((_, action), _) <- runWithMocks $ update currentState $ MoveMouseInDirection DirDown
+ ((_, action), _) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ update currentState $ MoveMouseInDirection DirDown
action `shouldBe` Just (IncrementMouseCursor (0, 33))
context "when direction is left" $ do
it "continues to increment movement" $ do
- ((_, action), _) <- runWithMocks $ update currentState $ MoveMouseInDirection DirLeft
+ ((_, action), _) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ update currentState $ MoveMouseInDirection DirLeft
action `shouldBe` Just (IncrementMouseCursor (-60, 0))
context "when direction is right" $ do
it "continues to increment movement" $ do
- ((_, action), _) <- runWithMocks $ update currentState $ MoveMouseInDirection DirRight
+ ((_, action), _) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ update currentState $ MoveMouseInDirection DirRight
action `shouldBe` Just (IncrementMouseCursor (60, 0))
context "with action MoveMousePosition" $ do
@@ -225,13 +242,28 @@ test = do
context "when mode is ModeSearch" $ do
it "captures screenshot for word search" $ do
- ((_, _), mock) <- runWithMocks $ update currentState $ SetMode defaultSearchMode
+ ((_, _), mock) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ Mock_windowPosition `mockReturns` mockWindowPosition
+ update currentState $ SetMode defaultSearchMode
mock `shouldHaveCalled` Mock_captureScreenshot (mockWindowOffsetX, mockWindowOffsetY) (mockWindowWidth, mockWindowHeight)
it "updates mode in state with ocr words" $ do
- ((nextState, _), _) <- runWithMocks $ update currentState $ SetMode defaultSearchMode
let matchWord = OCRMatch {matchStartX = 40, matchStartY = 5, matchEndX = 100, matchEndY = 20, matchText = "Wow"}
- nextState `shouldBe` currentState {stateMode = defaultSearchMode {searchWords = [matchWord], searchFilteredWords = [matchWord]}}
+ ((nextState, _), _) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ Mock_windowPosition `mockReturns` mockWindowPosition
+ Mock_captureScreenshot mockWindowPosition mockWindowSize `mockReturns` "mock-filename"
+ Mock_getWordsInImage "mock-filename" `mockReturns` [matchWord]
+ update currentState $ SetMode defaultSearchMode
+ nextState
+ `shouldBe` currentState
+ { stateMode =
+ defaultSearchMode
+ { searchWords = [matchWord],
+ searchFilteredWords = [matchWord]
+ }
+ }
context "with action ShutdownApp" $ do
let currentState = defaultState
@@ -249,7 +281,7 @@ test = do
it "hides window and triggers mouse click" $ do
(_, mock) <- runWithMocks $ update currentState $ TriggerMouseClick LeftClick
- calls mock `shouldContain` [Mock_hideWindow, Mock_clickMouseButton LeftClick]
+ mock `shouldContainCalls` [Mock_hideWindow, Mock_clickMouseButton LeftClick]
it "continues with action ShutdownApp without updating state" $ do
((nextState, action), _) <- runWithMocks $ update currentState $ TriggerMouseClick LeftClick
@@ -265,19 +297,19 @@ test = do
it "clicks multiple times" $ do
(_, mock) <- runWithMocks $ update currentState $ TriggerMouseClick LeftClick
- calls mock
- `shouldBe` [ Mock_hideWindow,
- Mock_clickMouseButton LeftClick,
- Mock_clickMouseButton LeftClick,
- Mock_clickMouseButton LeftClick
- ]
+ mock
+ `shouldContainCalls` [ Mock_hideWindow,
+ Mock_clickMouseButton LeftClick,
+ Mock_clickMouseButton LeftClick,
+ Mock_clickMouseButton LeftClick
+ ]
context "when repetition is 0" $ do
let currentState = defaultState {stateRepetition = 0}
it "clicks just once" $ do
(_, mock) <- runWithMocks $ update currentState $ TriggerMouseClick LeftClick
- calls mock `shouldBe` [Mock_hideWindow, Mock_clickMouseButton LeftClick]
+ mock `shouldContainCalls` [Mock_hideWindow, Mock_clickMouseButton LeftClick]
context "with action UpdateRepetition" $ do
let currentState = defaultState
diff --git a/specs/Specs/ViewSpec.hs b/specs/Specs/ViewSpec.hs
index 1e34811..79c8173 100644
--- a/specs/Specs/ViewSpec.hs
+++ b/specs/Specs/ViewSpec.hs
@@ -9,46 +9,52 @@ import TestUtils
test :: SpecWith ()
test = do
let defaultState = defaultAppState {stateGrid = [["ABC", "DEF"], ["DJK", "JKL"]]}
- let drawTextCalls = filter (\case Mock_drawText {} -> True; _ -> False) . calls
describe "#render" $ do
context "when key sequence is empty" $ do
let currentState = defaultState {stateKeySequence = ""}
it "draws matching text labels" $ do
- (_, mock) <- runWithMocks $ render currentState
- drawTextCalls mock
- `shouldBe` [ Mock_drawText (460, 10) colorWhite FontLG "ABC",
- Mock_drawText (1420, 10) colorWhite FontLG "DEF",
- Mock_drawText (460, 550) colorWhite FontLG "DJK",
- Mock_drawText (1420, 550) colorWhite FontLG "JKL"
- ]
+ (_, mock) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ render currentState
+ mock `shouldHaveCalled` Mock_drawText (460, 10) colorWhite FontLG "ABC"
+ mock `shouldHaveCalled` Mock_drawText (1420, 10) colorWhite FontLG "DEF"
+ mock `shouldHaveCalled` Mock_drawText (460, 550) colorWhite FontLG "DJK"
+ mock `shouldHaveCalled` Mock_drawText (1420, 550) colorWhite FontLG "JKL"
context "when there is a partial match" $ do
let currentState = defaultState {stateKeySequence = "D"}
it "draws matching text labels" $ do
- (_, mock) <- runWithMocks $ render currentState
- drawTextCalls mock
- `shouldBe` [ Mock_drawText (1420, 10) colorLightGray FontLG "D",
- Mock_drawText (1430, 10) colorAccent FontLG "EF",
- Mock_drawText (460, 550) colorLightGray FontLG "D",
- Mock_drawText (470, 550) colorAccent FontLG "JK"
- ]
+ (_, mock) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ Mock_drawText (1420, 10) colorLightGray FontLG "D" `mockReturns` (10, 0)
+ Mock_drawText (460, 550) colorLightGray FontLG "D" `mockReturns` (10, 0)
+ render currentState
+ mock `shouldHaveCalled` Mock_drawText (1420, 10) colorLightGray FontLG "D"
+ mock `shouldHaveCalled` Mock_drawText (1430, 10) colorAccent FontLG "EF"
+ mock `shouldHaveCalled` Mock_drawText (460, 550) colorLightGray FontLG "D"
+ mock `shouldHaveCalled` Mock_drawText (470, 550) colorAccent FontLG "JK"
context "when key sequence is complete match" $ do
let currentState = defaultState {stateKeySequence = "DEF"}
it "draws only the matching label" $ do
- (_, mock) <- runWithMocks $ render currentState
- drawTextCalls mock `shouldBe` [Mock_drawText (1420, 10) colorLightGray FontLG "DEF"]
+ (_, mock) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ render currentState
+ mock `shouldHaveCalled` Mock_drawText (1420, 10) colorLightGray FontLG "DEF"
describe "#renderKeySequence" $ do
context "when there is a partial match" $ do
it "draws the matched section and highlights the remaining characters" $ do
- (_, mock) <- runWithMocks $ renderKeySequence "ABC" "ABCDE" (0, 0)
- calls mock
- `shouldBe` [Mock_drawText (0, 0) colorLightGray FontLG "ABC", Mock_drawText (3 * 10, 0) colorAccent FontLG "DE"]
+ (_, mock) <- runWithMocks $ do
+ Mock_windowSize `mockReturns` mockWindowSize
+ Mock_drawText (0, 0) colorLightGray FontLG "ABC" `mockReturns` (30, 0)
+ renderKeySequence "ABC" "ABCDE" (0, 0)
+ mock `shouldHaveCalled` Mock_drawText (0, 0) colorLightGray FontLG "ABC"
+ mock `shouldHaveCalled` Mock_drawText (30, 0) colorAccent FontLG "DE"
it "return true as the text is visible" $ do
(isVisible, _) <- runWithMocks $ renderKeySequence "ABC" "ABCDE" (0, 0)
@@ -57,7 +63,7 @@ test = do
context "when there is no input key sequence" $ do
it "draws text as a single chunk" $ do
(_, mock) <- runWithMocks $ renderKeySequence "" "ABCD" (0, 0)
- calls mock `shouldBe` [Mock_drawText (0, 0) colorWhite FontLG "ABCD"]
+ mock `shouldHaveCalled` Mock_drawText (0, 0) colorWhite FontLG "ABCD"
it "return true as the text is visible" $ do
(isVisible, _) <- runWithMocks $ renderKeySequence "" "ABCD" (0, 0)