1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 06:41:45 +03:00
semantic/test/Data/Syntax/Assignment/Spec.hs

82 lines
2.7 KiB
Haskell
Raw Normal View History

2017-04-07 19:21:45 +03:00
module Data.Syntax.Assignment.Spec where
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
import Prologue
import Test.Hspec
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" $
runAssignment ((,) <$> red <*> red) [ast Red "hello" [], ast Red "world" []] `shouldBe` Just ([], (Out "hello", Out "world"))
2017-04-07 21:47:23 +03:00
describe "Alternative" $ do
it "attempts multiple alternatives" $
runAssignment (green <|> red) [ast Red "hello" []] `shouldBe` Just ([], Out "hello")
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
runAssignment (many red) (flip (ast Red) [] <$> w) `shouldBe` Just ([], 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" $
runAssignment (some red) [ast Red "hello" []] `shouldBe` Just ([], [Out "hello"])
2017-04-07 16:48:21 +03:00
2017-04-07 21:50:57 +03:00
describe "rule" $ do
it "matches nodes with the same symbol" $
runAssignment red [ast Red "hello" []] `shouldBe` Just ([], Out "hello")
it "does not advance past the current node" $
fst <$> runAssignment (rule ()) [ Rose (Node () "hi") [] ] `shouldBe` Just [ Rose (Node () "hi") [] ]
describe "content" $ do
it "produces the nodes content" $
snd <$> runAssignment content [ Rose (Node () "hi") [] ] `shouldBe` Just "hi"
it "advances past the current node" $
fst <$> runAssignment content [ Rose (Node () "hi") [] ] `shouldBe` Just []
describe "children" $ do
it "advances past the current node" $
fst <$> runAssignment (children (pure (Out ""))) [ast Red "a" []] `shouldBe` Just []
it "matches if its subrule matches" $
() <$ runAssignment (children red) [ast Blue "b" [ast Red "a" []]] `shouldBe` Just ()
it "does not match if its subrule does not match" $
runAssignment (children red) [ast Blue "b" [ast Green "a" []]] `shouldBe` Nothing
2017-04-10 17:35:39 +03:00
it "matches nested children" $ do
runAssignment
(rule 'A' *> children (rule 'B' *> children (rule 'C' *> content)))
[ ast 'A' "" [ ast 'B' "" [ ast 'C' "1" [] ] ] ]
`shouldBe`
Just ([], "1")
it "continues after children" $ do
runAssignment
(many (rule 'A' *> children (rule 'B' *> content)
<|> rule 'C' *> content))
[ ast 'A' "" [ ast 'B' "B" [] ]
, ast 'C' "C" [] ]
`shouldBe`
Just ([], ["B", "C"])
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)
data Out = Out ByteString
deriving (Eq, Show)
red :: Assignment Grammar Out
2017-04-07 21:37:22 +03:00
red = Out <$ rule Red <*> content
2017-04-07 19:36:14 +03:00
green :: Assignment Grammar Out
2017-04-07 21:37:22 +03:00
green = Out <$ rule Green <*> content
2017-04-07 19:36:14 +03:00
blue :: Assignment Grammar Out
2017-04-07 21:37:22 +03:00
blue = Out <$ rule Blue <*> content