Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81248 / clex / test / Clex / LexSpec.hs
blob: c5fec81f1e27e307f984408a71870fe6ed47b4dd (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
module Clex.LexSpec
    ( spec
    ) where

import qualified Data.ByteString       as BS
import           Data.ByteString       (ByteString)
import           Data.ByteString.Char8 (pack)

import           Clex.Types
import           Clex.Lex   (lexC)

import           Test.QuickCheck
import           Test.Hspec
import           Test.Hspec.QuickCheck
import           Clex.TypesSpec        ()

spec :: Spec
spec = describe "lexing" $ do
    prop "tokensToBS after lexC is id" $
        \x -> let x' = unValidC x in
            case lexC x' of
            (Left _) -> False
            (Right x'') -> x' == tokensToBS x''
    prop "lexing after tokensToBS is id" $
        \x -> let x' = unTokenC x in
            case lexC $ tokensToBS x' of
            (Left _) -> False
            (Right x'') -> x' == x''

newtype TokenC = TokenC {unTokenC :: [Token]}
    deriving (Eq, Show)

newtype ValidC = ValidC {unValidC :: ByteString}
    deriving (Eq, Show)

instance Arbitrary TokenC where
    arbitrary = TokenC . packWhitespaceTokens . concat <$>
        listOf1 (do
            x <- arbitrary
            whitespace <- whitespaceGen
            pure [x, whitespace])
        where
            whitespaceGen = Whitespace . pack <$> do listOf1 $ elements ['\t', ' ', '\n']

instance Arbitrary ValidC where
    arbitrary = ValidC . BS.concat . map tokenToBS <$> listOf1 (frequency
        [ (30, arbitrary)
        , (60, whitespaceGen)
        ])
        where
            whitespaceGen = Whitespace . pack <$> do listOf1 $ elements ['\t', ' ', '\n']

packWhitespaceTokens :: [Token] -> [Token]
packWhitespaceTokens []  = []
packWhitespaceTokens [x] = [x]
packWhitespaceTokens (Whitespace x : Whitespace y : xs)
    = packWhitespaceTokens $ Whitespace (x <> y) : xs
packWhitespaceTokens (x:xs) = x : packWhitespaceTokens xs