2017-04-07 19:21:45 +03:00
|
|
|
|
module Data.Syntax.Assignment.Spec where
|
2017-04-06 17:09:12 +03:00
|
|
|
|
|
2017-04-10 16:01:33 +03:00
|
|
|
|
import Data.ByteString.Char8 (words)
|
2017-04-07 19:21:45 +03:00
|
|
|
|
import Data.Syntax.Assignment
|
2017-04-06 17:09:12 +03:00
|
|
|
|
import Prologue
|
|
|
|
|
import Test.Hspec
|
2017-04-18 18:06:24 +03:00
|
|
|
|
import Text.Parser.TreeSitter.Language (Symbol(..), SymbolType(..))
|
2017-04-06 17:09:12 +03:00
|
|
|
|
|
|
|
|
|
spec :: Spec
|
2017-04-06 20:36:54 +03:00
|
|
|
|
spec = do
|
2017-04-07 21:47:31 +03:00
|
|
|
|
describe "Applicative" $ do
|
2017-04-07 16:45:23 +03:00
|
|
|
|
it "matches in sequence" $
|
2017-04-18 18:06:24 +03:00
|
|
|
|
runAssignment ((,) <$> red <*> red) [ast Red "hello" [], ast Red "world" []] `shouldBe` Result ([], (Out "hello", Out "world"))
|
2017-04-07 16:14:03 +03:00
|
|
|
|
|
2017-04-07 21:47:23 +03:00
|
|
|
|
describe "Alternative" $ do
|
|
|
|
|
it "attempts multiple alternatives" $
|
2017-04-18 18:06:24 +03:00
|
|
|
|
runAssignment (green <|> red) [ast Red "hello" []] `shouldBe` Result ([], Out "hello")
|
2017-04-07 21:47:23 +03:00
|
|
|
|
|
2017-04-07 16:45:23 +03:00
|
|
|
|
it "matches repetitions" $
|
2017-04-10 16:01:33 +03:00
|
|
|
|
let w = words "colourless green ideas sleep furiously" in
|
2017-04-18 18:06:24 +03:00
|
|
|
|
runAssignment (many red) (flip (ast Red) [] <$> w) `shouldBe` Result ([], Out <$> w)
|
2017-04-07 16:44:13 +03:00
|
|
|
|
|
2017-04-07 16:48:21 +03:00
|
|
|
|
it "matches one-or-more repetitions against one or more input nodes" $
|
2017-04-18 18:06:24 +03:00
|
|
|
|
runAssignment (some red) [ast Red "hello" []] `shouldBe` Result ([], [Out "hello"])
|
2017-04-07 16:48:21 +03:00
|
|
|
|
|
2017-04-19 20:11:09 +03:00
|
|
|
|
describe "symbol" $ do
|
2017-04-07 21:50:57 +03:00
|
|
|
|
it "matches nodes with the same symbol" $
|
2017-04-18 18:06:24 +03:00
|
|
|
|
runAssignment red [ast Red "hello" []] `shouldBe` Result ([], Out "hello")
|
2017-04-07 21:50:57 +03:00
|
|
|
|
|
2017-04-07 21:59:13 +03:00
|
|
|
|
it "does not advance past the current node" $
|
2017-04-19 20:11:09 +03:00
|
|
|
|
fst <$> runAssignment (symbol Red) [ Rose (Node Red "hi") [] ] `shouldBe` Result [ Rose (Node Red "hi") [] ]
|
2017-04-07 21:59:13 +03:00
|
|
|
|
|
2017-04-19 23:00:44 +03:00
|
|
|
|
describe "source" $ do
|
|
|
|
|
it "produces the node’s source" $
|
|
|
|
|
snd <$> runAssignment source [ Rose (Node Red "hi") [] ] `shouldBe` Result "hi"
|
2017-04-07 21:57:00 +03:00
|
|
|
|
|
2017-04-07 21:57:44 +03:00
|
|
|
|
it "advances past the current node" $
|
2017-04-19 23:00:44 +03:00
|
|
|
|
fst <$> runAssignment source [ Rose (Node Red "hi") [] ] `shouldBe` Result []
|
2017-04-07 21:57:44 +03:00
|
|
|
|
|
2017-04-07 21:39:13 +03:00
|
|
|
|
describe "children" $ do
|
|
|
|
|
it "advances past the current node" $
|
2017-04-18 18:06:24 +03:00
|
|
|
|
fst <$> runAssignment (children (pure (Out ""))) [ast Red "a" []] `shouldBe` Result []
|
2017-04-07 21:39:13 +03:00
|
|
|
|
|
2017-04-07 21:42:25 +03:00
|
|
|
|
it "matches if its subrule matches" $
|
2017-04-18 18:06:24 +03:00
|
|
|
|
() <$ runAssignment (children red) [ast Blue "b" [ast Red "a" []]] `shouldBe` Result ()
|
2017-04-07 21:42:25 +03:00
|
|
|
|
|
2017-04-07 21:43:53 +03:00
|
|
|
|
it "does not match if its subrule does not match" $
|
2017-04-18 18:06:24 +03:00
|
|
|
|
runAssignment (children red) [ast Blue "b" [ast Green "a" []]] `shouldBe` Error []
|
2017-04-07 21:43:53 +03:00
|
|
|
|
|
2017-04-10 17:35:39 +03:00
|
|
|
|
it "matches nested children" $ do
|
|
|
|
|
runAssignment
|
2017-04-19 23:00:44 +03:00
|
|
|
|
(symbol Red *> children (symbol Green *> children (symbol Blue *> source)))
|
2017-04-18 18:06:24 +03:00
|
|
|
|
[ ast Red "" [ ast Green "" [ ast Blue "1" [] ] ] ]
|
2017-04-10 17:35:39 +03:00
|
|
|
|
`shouldBe`
|
2017-04-18 18:06:24 +03:00
|
|
|
|
Result ([], "1")
|
2017-04-10 17:35:39 +03:00
|
|
|
|
|
2017-04-10 17:52:12 +03:00
|
|
|
|
it "continues after children" $ do
|
|
|
|
|
runAssignment
|
2017-04-19 23:00:44 +03:00
|
|
|
|
(many (symbol Red *> children (symbol Green *> source)
|
|
|
|
|
<|> symbol Blue *> source))
|
2017-04-18 18:06:24 +03:00
|
|
|
|
[ ast Red "" [ ast Green "B" [] ]
|
|
|
|
|
, ast Blue "C" [] ]
|
2017-04-10 17:52:12 +03:00
|
|
|
|
`shouldBe`
|
2017-04-18 18:06:24 +03:00
|
|
|
|
Result ([], ["B", "C"])
|
2017-04-10 17:52:12 +03:00
|
|
|
|
|
2017-04-10 18:24:30 +03:00
|
|
|
|
it "matches multiple nested children" $ do
|
|
|
|
|
runAssignment
|
2017-04-19 23:00:44 +03:00
|
|
|
|
(symbol Red *> children (many (symbol Green *> children (symbol Blue *> source))))
|
2017-04-18 18:06:24 +03:00
|
|
|
|
[ ast Red "" [ ast Green "" [ ast Blue "1" [] ]
|
|
|
|
|
, ast Green "" [ ast Blue "2" [] ] ] ]
|
2017-04-10 18:24:30 +03:00
|
|
|
|
`shouldBe`
|
2017-04-18 18:06:24 +03:00
|
|
|
|
Result ([], ["1", "2"])
|
2017-04-10 18:24:30 +03:00
|
|
|
|
|
2017-04-08 04:26:02 +03:00
|
|
|
|
ast :: grammar -> ByteString -> [AST grammar] -> AST grammar
|
2017-04-07 16:16:57 +03:00
|
|
|
|
ast g s c = Rose (Node g s) c
|
2017-04-07 19:36:14 +03:00
|
|
|
|
|
|
|
|
|
data Grammar = Red | Green | Blue
|
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
2017-04-18 18:06:24 +03:00
|
|
|
|
instance Symbol Grammar where
|
|
|
|
|
symbolType _ = Regular
|
|
|
|
|
|
2017-04-07 19:36:14 +03:00
|
|
|
|
data Out = Out ByteString
|
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
|
|
red :: Assignment Grammar Out
|
2017-04-19 23:00:44 +03:00
|
|
|
|
red = Out <$ symbol Red <*> source
|
2017-04-07 19:36:14 +03:00
|
|
|
|
|
|
|
|
|
green :: Assignment Grammar Out
|
2017-04-19 23:00:44 +03:00
|
|
|
|
green = Out <$ symbol Green <*> source
|
2017-04-07 19:36:14 +03:00
|
|
|
|
|
|
|
|
|
blue :: Assignment Grammar Out
|
2017-04-19 23:00:44 +03:00
|
|
|
|
blue = Out <$ symbol Blue <*> source
|