File: RIOSpec.hs

package info (click to toggle)
haskell-rio 0.1.22.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 500 kB
  • sloc: haskell: 4,858; makefile: 3
file content (54 lines) | stat: -rw-r--r-- 1,483 bytes parent folder | download | duplicates (3)
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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ViewPatterns #-}
module RIO.Prelude.RIOSpec (spec) where

import RIO
import RIO.State
import RIO.Writer
import Test.Hspec

spec :: Spec
spec = do
  describe "RIO writer instance" $ do
    it "tell works" $ do
     ref <- newSomeRef (mempty :: Text)
     runRIO ref $ do
       tell "hello\n"
       tell "world\n"
     contents <- readSomeRef ref
     contents `shouldBe` "hello\nworld\n"

    it "listen works" $ do
      ref <- newSomeRef (mempty :: Text)
      ((), str) <- runRIO ref $ listen $ do
        tell "hello\n"
        tell "world\n"
      contents <- readSomeRef ref
      contents `shouldBe` ""
      str `shouldBe` "hello\nworld\n"

    it "pass works" $ do
      ref <- newSomeRef (mempty :: Text)
      () <- runRIO ref $ pass $ do
        tell "hello\n"
        tell "world\n"
        return ((), \a -> a <> "!")
      contents <- readSomeRef ref
      contents `shouldBe` "hello\nworld\n!"

  describe "RIO state instance" $ do
    it "get works" $ do
      ref <- newSomeRef (mempty :: Text)
      result <- runRIO ref $ do
        put "hello world"
        x <- get
        return x
      result `shouldBe` "hello world"

    it "state works" $ do
      ref <- newSomeRef (mempty :: Text)
      _newRef <- newSomeRef ("Hello World!" :: Text)
      () <- runRIO ref $ state (\_ -> ((), "Hello World!"))
      contents <- readSomeRef ref
      contents `shouldBe` "Hello World!"