File: TestText.hs

package info (click to toggle)
haskell-replace-megaparsec 1.5.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 120 kB
  • sloc: haskell: 554; makefile: 3
file content (105 lines) | stat: -rw-r--r-- 4,067 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE BlockArguments #-}

module TestText ( tests ) where

import Replace.Megaparsec
import Text.Megaparsec
import Text.Megaparsec.Char
import Data.Void
import qualified Data.Text as T
import Data.Bifunctor (second)
import Test.Hspec (describe, shouldBe, it, SpecWith)

type Parser = Parsec Void T.Text

findAllCap' :: MonadParsec e s m => m a -> m [Either (Tokens s) (Tokens s, a)]
findAllCap' sep = sepCap (match sep)

findAll' :: MonadParsec e s f => f b -> f [Either (Tokens s) (Tokens s)]
findAll' sep = (fmap.fmap) (second fst) $ sepCap (match sep)

tests :: SpecWith ()
tests = describe "input Text" do
    runParserTest "findAll upperChar"
        (findAllCap' (upperChar :: Parser Char))
        ("aBcD" :: T.Text)
        [Left "a", Right ("B", 'B'), Left "c", Right ("D", 'D')]
    -- check that sepCap can progress even when parser consumes nothing
    -- and succeeds.
    runParserTest "zero-consumption parser"
        (sepCap (many (upperChar :: Parser Char)))
        ("aBcD" :: T.Text)
        [Left "a", Right "B", Left "c", Right "D"]
    runParserTest "scinum"
        (sepCap scinum)
        ("1E3")
        ([Right (1,3)])
    runParserTest "getOffset"
        (sepCap offsetA)
        ("xxAxx")
        ([Left "xx", Right 2, Left "xx"])
    runParserTest "monad fail"
        (sepCap (fail "" :: Parser ()))
        ("xxx")
        ([Left "xxx"])
#if MIN_VERSION_GLASGOW_HASKELL(8,6,0,0)
    runParserTest "read fail"
        (sepCap (return (read "a" :: Int) :: Parser Int))
        ("a")
        ([Left "a"])
#endif
    runParserTest "findAll astral"
        (findAll' (takeWhileP Nothing (=='𝅘𝅥𝅯') :: Parser T.Text))
        ("𝄞𝅘𝅥𝅘𝅥𝅘𝅥𝅘𝅥𝅘𝅥𝅯𝅘𝅥𝅯𝅘𝅥𝅯𝅘𝅥𝅯𝅘𝅥𝅘𝅥𝅘𝅥𝅘𝅥" :: T.Text)
        [Left "𝄞𝅘𝅥𝅘𝅥𝅘𝅥𝅘𝅥", Right "𝅘𝅥𝅯𝅘𝅥𝅯𝅘𝅥𝅯𝅘𝅥𝅯", Left "𝅘𝅥𝅘𝅥𝅘𝅥𝅘𝅥"]
    runParserTest "empty input" (sepCap (fail "" :: Parser ())) "" []
    streamEditTest "x to o"
        (string "x" :: Parser T.Text) (const "o")
        "x x x" "o o o"
    streamEditTest "x to o inner"
        (string "x" :: Parser T.Text) (const "o")
        " x x x " " o o o "
    streamEditTest "ordering"
        (string "456" :: Parser T.Text) (const "ABC")
        "123456789" "123ABC789"
    streamEditTest "empty input" (match (fail "" :: Parser ())) fst "" ""
    breakCapTest "basic" (upperChar :: Parser Char) "aAa" (Just ("a", 'A', "a"))
    breakCapTest "first" (upperChar :: Parser Char) "Aa" (Just ("", 'A', "a"))
    breakCapTest "last" (upperChar :: Parser Char) "aA" (Just ("a", 'A', ""))
    breakCapTest "fail" (upperChar :: Parser Char) "aaa" Nothing
    breakCapTest "match" (match (upperChar :: Parser Char)) "aAa" (Just ("a", ("A",'A'), "a"))
    breakCapTest "zero-width" (lookAhead (upperChar :: Parser Char)) "aAa" (Just ("a", 'A', "Aa"))
    breakCapTest "empty input" (upperChar :: Parser Char) "" Nothing
    breakCapTest "empty input zero-width" (return () :: Parser ()) "" (Just ("", (), ""))
    -- I was unable to write a failing test for
    -- https://github.com/jamesdbrock/replace-megaparsec/issues/33
    -- but adding this "sep backtrack" test anyway
    splitCapTest "sep backtrack" (match $ between (string "{{") (string "}}") (T.pack <$> many (alphaNumChar <|> spaceChar) :: Parser T.Text)) "{{foo.}}" [Left "{{foo.}}"]
  where
    runParserTest nam p input expected = it nam $ shouldBe
        (runParser p "" input) (Right expected)

    streamEditTest nam sep editor input expected = it nam $ shouldBe
        (streamEdit sep editor input) expected

    breakCapTest nam sep input expected = it nam $ shouldBe
        (breakCap sep input) expected

    splitCapTest nam sep input expected = it nam $ shouldBe
        (splitCap sep input) expected

    scinum :: Parser (Double, Integer)
    scinum = do
        m <- some digitChar
        _ <- chunk "E"
        e <- some digitChar
        return (read m, read e)


    offsetA :: Parser Int
    offsetA = getOffset <* chunk "A"