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

220 lines
9.5 KiB
Haskell
Raw Normal View History

2017-04-21 23:12:56 +03:00
{-# LANGUAGE DataKinds #-}
2017-04-07 19:21:45 +03:00
module Data.Syntax.Assignment.Spec where
2017-04-21 23:12:56 +03:00
import Data.ByteString.Char8 as B (words, length)
2017-06-25 00:30:57 +03:00
import Data.Source
2017-04-07 19:21:45 +03:00
import Data.Syntax.Assignment
2017-04-21 23:12:56 +03:00
import Info
import Prologue
import Test.Hspec
2017-04-18 18:06:24 +03:00
import Text.Parser.TreeSitter.Language (Symbol(..), SymbolType(..))
spec :: Spec
2017-04-06 20:36:54 +03:00
spec = do
2017-05-12 19:45:07 +03:00
describe "Applicative" $
2017-04-07 16:45:23 +03:00
it "matches in sequence" $
2017-07-18 00:34:28 +03:00
runAssignment headF ((,) <$> red <*> red) (makeState "helloworld" [node Red 0 5 [], node Red 5 10 []])
`shouldBe`
Right ((Out "hello", Out "world"), AssignmentState 10 (Info.Pos 1 11) Nothing "helloworld" [])
2017-04-07 21:47:23 +03:00
describe "Alternative" $ do
it "attempts multiple alternatives" $
2017-07-18 00:34:28 +03:00
runAssignment headF (green <|> red) (makeState "hello" [node Red 0 5 []])
`shouldBe`
Right (Out "hello", AssignmentState 5 (Info.Pos 1 6) Nothing "hello" [])
2017-04-07 21:47:23 +03:00
2017-04-07 16:45:23 +03:00
it "matches repetitions" $
2017-04-21 23:12:56 +03:00
let s = "colourless green ideas sleep furiously"
w = words s
(_, nodes) = foldl (\ (i, prev) word -> (i + B.length word + 1, prev <> [node Red i (i + B.length word) []])) (0, []) w in
2017-07-18 00:34:28 +03:00
runAssignment headF (many red) (makeState (fromBytes s) nodes)
`shouldBe`
Right (Out <$> w, AssignmentState (B.length s)
(Info.Pos 1 (succ (B.length s)))
(Just (Error (Info.Pos 1 39) (UnexpectedEndOfInput [Red])))
(fromBytes s)
[])
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-07-18 00:34:28 +03:00
runAssignment headF (some red) (makeState "hello" [node Red 0 5 []])
`shouldBe`
Right ([Out "hello"], AssignmentState 5
(Info.Pos 1 6)
(Just (Error (Info.Pos 1 6) (UnexpectedEndOfInput [Red])))
"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-07-18 00:34:28 +03:00
fst <$> runAssignment headF red (makeState "hello" [node Red 0 5 []]) `shouldBe` Right (Out "hello")
2017-04-07 21:50:57 +03:00
it "does not advance past the current node" $
let initialState = makeState "hi" [ node Red 0 2 [] ] in
2017-07-18 00:34:28 +03:00
snd <$> runAssignment headF (symbol Red) initialState `shouldBe` Right initialState
2017-07-20 00:02:07 +03:00
describe "without catchError" $ do
it "assignment returns UnexpectedSymbol" $
runAssignment headF
red
(makeState "A" [node Green 0 1 []])
`shouldBe`
Left (Error (Info.Pos 1 1) (UnexpectedSymbol [Red] Green))
it "assignment returns UnexpectedEndOfInput" $
runAssignment headF
(symbol Green *> children (some red))
(makeState "A" [node Green 0 1 []])
`shouldBe`
Left (Error (Info.Pos 1 1) (UnexpectedEndOfInput [Red]))
describe "catchError" $ do
it "handler that always matches" $
runAssignment headF
(red `catchError` (\ _ -> OutError <$ location <*> source))
(makeState "A" [node Green 0 1 []])
`shouldBe`
Right (OutError "A", AssignmentState 1 (Info.Pos 1 2) Nothing "A" [])
it "handler that matches" $
runAssignment headF
(red `catchError` const green)
(makeState "A" [node Green 0 1 []])
`shouldBe`
Right (Out "A", AssignmentState 1 (Info.Pos 1 2) Nothing "A" [])
it "handler that doesn't match produces error" $
runAssignment headF
(red `catchError` const blue)
(makeState "A" [node Green 0 1 []])
`shouldBe`
Left (Error (Info.Pos 1 1) (UnexpectedSymbol [Blue] Green))
describe "in many" $ do
let err = Just (Error (Info.Pos 1 3) (UnexpectedEndOfInput []))
it "handler that always matches" $
runAssignment headF
(symbol Palatte *> children (
many (red `catchError` (\ _ -> OutError <$ location <*> source))
))
(makeState "PG" [node Palatte 0 1 [node Green 1 2 []]])
`shouldBe`
Right ([OutError "G"], AssignmentState 1 (Info.Pos 1 2) err "PG" [])
let err = Just (Error (Info.Pos 1 3) (UnexpectedEndOfInput [Green]))
it "handler that matches" $
runAssignment headF
(symbol Palatte *> children ( many (red `catchError` const green) ))
(makeState "PG" [node Palatte 0 1 [node Green 1 2 []]])
`shouldBe`
Right ([Out "G"], AssignmentState 1 (Info.Pos 1 2) err "PG" [])
it "handler that doesn't match produces error" $
runAssignment headF
(symbol Palatte *> children ( many (red `catchError` const blue) ))
(makeState "PG" [node Palatte 0 1 [node Green 1 2 []]])
`shouldBe`
Left (Error (Info.Pos 1 2) (UnexpectedSymbol [Blue] Green))
it "handler that always matches with apply consumes and then errors" $
runAssignment headF
(symbol Palatte *> children (
(,) <$> many (red `catchError` (\ _ -> OutError <$ location <*> source)) <*> green
))
(makeState "PG" [node Palatte 0 1 [node Green 1 2 []]])
`shouldBe`
Left (Error (Info.Pos 1 3) (UnexpectedEndOfInput [Green]))
let err = Just (Error (Info.Pos 1 2) (UnexpectedSymbol [Blue] Green))
it "handler that doesn't match with apply" $
runAssignment headF
(symbol Palatte *> children (
2017-07-20 00:20:03 +03:00
(,) <$> many (red `catchError` const blue) <*> green
2017-07-20 00:02:07 +03:00
))
(makeState "PG" [node Palatte 0 1 [node Green 1 2 []]])
`shouldBe`
Right (([], Out "G"), AssignmentState 1 (Info.Pos 1 2) err "PG" [])
describe "many" $ do
it "should not infinite loop if nothing matches" $
runAssignment headF
(symbol Palatte *> children ( many (pure (Out "always")) ))
(makeState "A" [node Palatte 0 1 [node Green 1 2 []]])
`shouldBe`
Left (Error (Info.Pos 1 1) (UnexpectedSymbol [Red] Green))
2017-04-19 23:00:44 +03:00
describe "source" $ do
it "produces the nodes source" $
2017-07-18 00:34:28 +03:00
assignBy headF source "hi" (node Red 0 2 []) `shouldBe` Right "hi"
it "advances past the current node" $
2017-07-18 00:34:28 +03:00
snd <$> runAssignment headF source (makeState "hi" [ node Red 0 2 [] ]) `shouldBe` Right (AssignmentState 2 (Info.Pos 1 3) Nothing "hi" [])
describe "children" $ do
it "advances past the current node" $
2017-07-18 00:34:28 +03:00
snd <$> runAssignment headF (children (pure (Out ""))) (makeState "a" [node Red 0 1 []]) `shouldBe` Right (AssignmentState 1 (Info.Pos 1 2) Nothing "a" [])
it "matches if its subrule matches" $
2017-07-18 00:34:28 +03:00
() <$ runAssignment headF (children red) (makeState "a" [node Blue 0 1 [node Red 0 1 []]]) `shouldBe` Right ()
it "does not match if its subrule does not match" $
2017-07-18 00:34:28 +03:00
runAssignment headF (children red) (makeState "a" [node Blue 0 1 [node Green 0 1 []]]) `shouldBe` Left (Error (Info.Pos 1 1) (UnexpectedSymbol [Red] Green))
2017-05-12 19:45:07 +03:00
it "matches nested children" $
2017-06-08 03:17:14 +03:00
runAssignment headF
2017-04-19 23:00:44 +03:00
(symbol Red *> children (symbol Green *> children (symbol Blue *> source)))
(makeState "1" [ node Red 0 1 [ node Green 0 1 [ node Blue 0 1 [] ] ] ])
2017-04-10 17:35:39 +03:00
`shouldBe`
2017-07-18 00:34:28 +03:00
Right ("1", AssignmentState 1 (Info.Pos 1 2) Nothing "1" [])
2017-04-10 17:35:39 +03:00
2017-05-12 19:45:07 +03:00
it "continues after children" $
2017-07-18 00:34:28 +03:00
runAssignment headF
2017-04-19 23:00:44 +03:00
(many (symbol Red *> children (symbol Green *> source)
<|> symbol Blue *> source))
(makeState "BC" [ node Red 0 1 [ node Green 0 1 [] ]
2017-07-18 00:34:28 +03:00
, node Blue 1 2 [] ])
`shouldBe`
2017-07-18 00:34:28 +03:00
Right (["B", "C"], AssignmentState 2 (Info.Pos 1 3) (Just (Error (Info.Pos 1 3) (UnexpectedEndOfInput [Red, Blue]))) "BC" [])
2017-05-12 19:45:07 +03:00
it "matches multiple nested children" $
2017-06-08 03:17:14 +03:00
runAssignment headF
2017-04-19 23:00:44 +03:00
(symbol Red *> children (many (symbol Green *> children (symbol Blue *> source))))
(makeState "12" [ node Red 0 2 [ node Green 0 1 [ node Blue 0 1 [] ]
, node Green 1 2 [ node Blue 1 2 [] ] ] ])
`shouldBe`
2017-07-18 00:34:28 +03:00
Right (["1", "2"], AssignmentState 2 (Info.Pos 1 3) (Just (Error (Info.Pos 1 3) (UnexpectedEndOfInput [Green]))) "12" [])
describe "runAssignment" $ do
2017-05-03 18:23:31 +03:00
it "drops anonymous nodes before matching symbols" $
2017-07-18 00:34:28 +03:00
runAssignment headF red (makeState "magenta red" [node Magenta 0 7 [], node Red 8 11 []]) `shouldBe` Right (Out "red", AssignmentState 11 (Info.Pos 1 12) Nothing "magenta red" [])
it "does not drop anonymous nodes after matching" $
2017-07-18 00:34:28 +03:00
runAssignment headF red (makeState "red magenta" [node Red 0 3 [], node Magenta 4 11 []]) `shouldBe` Right (Out "red", AssignmentState 3 (Info.Pos 1 4) Nothing "red magenta" [node Magenta 4 11 []])
2017-05-03 17:56:01 +03:00
it "does not drop anonymous nodes when requested" $
2017-07-18 00:34:28 +03:00
runAssignment headF ((,) <$> magenta <*> red) (makeState "magenta red" [node Magenta 0 7 [], node Red 8 11 []]) `shouldBe` Right ((Out "magenta", Out "red"), AssignmentState 11 (Info.Pos 1 12) Nothing "magenta red" [])
node :: symbol -> Int -> Int -> [AST symbol] -> AST symbol
2017-07-18 00:34:28 +03:00
node symbol start end children = cofree $ Node symbol (Range start end) (Info.Span (Info.Pos 1 (succ start)) (Info.Pos 1 (succ end))) :< children
2017-07-20 00:02:07 +03:00
data Grammar = Palatte | Red | Green | Blue | Magenta
deriving (Enum, Eq, Show)
2017-04-07 19:36:14 +03:00
2017-04-18 18:06:24 +03:00
instance Symbol Grammar where
symbolType Magenta = Anonymous
2017-04-18 18:06:24 +03:00
symbolType _ = Regular
2017-07-20 00:02:07 +03:00
data Out = Out ByteString | OutError ByteString
2017-04-07 19:36:14 +03:00
deriving (Eq, Show)
2017-06-08 03:17:14 +03:00
red :: Assignment (AST Grammar) Grammar Out
2017-04-19 23:00:44 +03:00
red = Out <$ symbol Red <*> source
2017-04-07 19:36:14 +03:00
2017-06-08 03:17:14 +03:00
green :: Assignment (AST Grammar) Grammar Out
2017-04-19 23:00:44 +03:00
green = Out <$ symbol Green <*> source
2017-04-07 19:36:14 +03:00
2017-06-08 03:17:14 +03:00
blue :: Assignment (AST Grammar) Grammar Out
2017-04-19 23:00:44 +03:00
blue = Out <$ symbol Blue <*> source
2017-06-08 03:17:14 +03:00
magenta :: Assignment (AST Grammar) Grammar Out
magenta = Out <$ symbol Magenta <*> source