2017-04-07 19:21:45 +03:00
|
|
|
module Data.Syntax.Assignment.Spec where
|
2017-04-06 17:09:12 +03:00
|
|
|
|
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
|
|
|
|
|
|
|
|
spec :: Spec
|
2017-04-06 20:36:54 +03:00
|
|
|
spec = do
|
|
|
|
describe "stepAssignment" $ do
|
2017-04-07 16:45:23 +03:00
|
|
|
it "matches nodes" $
|
2017-04-07 19:36:14 +03:00
|
|
|
stepAssignment red [ast Red "hello" []] `shouldBe` Just ([], Out "hello")
|
2017-04-06 21:25:21 +03:00
|
|
|
|
2017-04-07 16:45:23 +03:00
|
|
|
it "attempts multiple alternatives" $
|
2017-04-07 19:36:14 +03:00
|
|
|
stepAssignment (green <|> red) [ast Red "hello" []] `shouldBe` Just ([], Out "hello")
|
2017-04-06 21:36:19 +03:00
|
|
|
|
2017-04-07 16:45:23 +03:00
|
|
|
it "matches in sequence" $
|
2017-04-07 19:36:14 +03:00
|
|
|
stepAssignment ((,) <$> red <*> red) [ast Red "hello" [], ast Red "world" []] `shouldBe` Just ([], (Out "hello", Out "world"))
|
2017-04-07 16:14:03 +03:00
|
|
|
|
2017-04-07 16:45:23 +03:00
|
|
|
it "matches repetitions" $
|
2017-04-07 19:36:14 +03:00
|
|
|
stepAssignment (many red) [ast Red "colourless" [], ast Red "green" [], ast Red "ideas" [], ast Red "sleep" [], ast Red "furiously" []] `shouldBe` Just ([], [Out "colourless", Out "green", Out "ideas", Out "sleep", Out "furiously"])
|
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-07 19:36:14 +03:00
|
|
|
stepAssignment (some red) [ast Red "hello" []] `shouldBe` Just ([], [Out "hello"])
|
2017-04-07 16:48:21 +03:00
|
|
|
|
2017-04-07 16:16:57 +03:00
|
|
|
ast :: Grammar -> ByteString -> [AST Grammar] -> AST Grammar
|
|
|
|
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
|
|
|
|
red = rule Red (Out <$> content)
|
|
|
|
|
|
|
|
green :: Assignment Grammar Out
|
|
|
|
green = rule Green (Out <$> content)
|
|
|
|
|
|
|
|
blue :: Assignment Grammar Out
|
|
|
|
blue = rule Blue (Out <$> content)
|