aboutsummaryrefslogtreecommitdiff
path: root/specs/Specs/AppEventSpec.hs
blob: 9ac9d4c6741ff8c6d968f8be74c43458c36ba23d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module Specs.AppEventSpec where

import Chelleport (eventHandler)
import Chelleport.Types
import qualified SDL
import SDL.Internal.Numbered (FromNumber (fromNumber))
import Test.Hspec
import Unsafe.Coerce (unsafeCoerce)

test :: SpecWith ()
test = do
  describe "#eventHandler" $ do
    let mkEvent payload = SDL.Event {SDL.eventTimestamp = 0, SDL.eventPayload = payload}
    let mkKeyboardEvent key motion =
          mkEvent $
            SDL.KeyboardEvent $
              SDL.KeyboardEventData
                { SDL.keyboardEventWindow = unsafeCoerce (0 :: Integer),
                  SDL.keyboardEventRepeat = False,
                  SDL.keyboardEventKeysym =
                    SDL.Keysym
                      { SDL.keysymScancode = SDL.Scancode0,
                        SDL.keysymModifier = fromNumber 0,
                        SDL.keysymKeycode = key
                      },
                  SDL.keyboardEventKeyMotion = motion
                }

    context "when window quit event is triggered" $ do
      it "shuts down app" $ do
        let action = eventHandler $ mkEvent SDL.QuitEvent
        action `shouldBe` Just ShutdownApp

    context "when q key is pressed" $ do
      it "shuts down app" $ do
        let action = eventHandler $ mkKeyboardEvent SDL.KeycodeQ SDL.Pressed
        action `shouldBe` Just ShutdownApp

    context "when escape key is pressed" $ do
      it "shuts down app" $ do
        let action = eventHandler $ mkKeyboardEvent SDL.KeycodeEscape SDL.Pressed
        action `shouldBe` Just ShutdownApp

    context "when space key is pressed" $ do
      it "triggers left mouse button click" $ do
        let action = eventHandler $ mkKeyboardEvent SDL.KeycodeSpace SDL.Pressed
        action `shouldBe` Just TriggerLeftClick

    context "when tab key is pressed" $ do
      it "resets key state" $ do
        let action = eventHandler $ mkKeyboardEvent SDL.KeycodeTab SDL.Pressed
        action `shouldBe` Just ResetKeys

    context "when an alphanumeric key (excluding Q) is pressed" $ do
      it "calls key input handler" $ do
        eventHandler (mkKeyboardEvent SDL.KeycodeA SDL.Pressed) `shouldBe` Just (HandleKeyInput SDL.KeycodeA)
        eventHandler (mkKeyboardEvent SDL.KeycodeB SDL.Pressed) `shouldBe` Just (HandleKeyInput SDL.KeycodeB)
        eventHandler (mkKeyboardEvent SDL.Keycode9 SDL.Pressed) `shouldBe` Just (HandleKeyInput SDL.Keycode9)

    context "when shift key is pressed" $ do
      it "enables shift" $ do
        let action = eventHandler $ mkKeyboardEvent SDL.KeycodeRShift SDL.Pressed
        action `shouldBe` Just (UpdateShiftState True)

    context "when shift key is released" $ do
      it "disabled shift" $ do
        let action = eventHandler $ mkKeyboardEvent SDL.KeycodeRShift SDL.Released
        action `shouldBe` Just (UpdateShiftState False)