1
1
mirror of https://github.com/github/semantic.git synced 2024-12-29 01:42:43 +03:00

Update the tests.

This commit is contained in:
Rob Rix 2017-04-18 11:06:24 -04:00
parent 4a29eed58e
commit ccad5c275e
2 changed files with 28 additions and 23 deletions

View File

@ -191,6 +191,7 @@ test-suite test
, gitlib
, gitlib-libgit2
, Glob
, haskell-tree-sitter
, hspec >= 2.4.1
, hspec-core
, hspec-expectations-pretty-diff

View File

@ -4,71 +4,72 @@ import Data.ByteString.Char8 (words)
import Data.Syntax.Assignment
import Prologue
import Test.Hspec
import Text.Parser.TreeSitter.Language (Symbol(..), SymbolType(..))
spec :: Spec
spec = do
describe "Applicative" $ do
it "matches in sequence" $
runAssignment ((,) <$> red <*> red) [ast Red "hello" [], ast Red "world" []] `shouldBe` Just ([], (Out "hello", Out "world"))
runAssignment ((,) <$> red <*> red) [ast Red "hello" [], ast Red "world" []] `shouldBe` Result ([], (Out "hello", Out "world"))
describe "Alternative" $ do
it "attempts multiple alternatives" $
runAssignment (green <|> red) [ast Red "hello" []] `shouldBe` Just ([], Out "hello")
runAssignment (green <|> red) [ast Red "hello" []] `shouldBe` Result ([], Out "hello")
it "matches repetitions" $
let w = words "colourless green ideas sleep furiously" in
runAssignment (many red) (flip (ast Red) [] <$> w) `shouldBe` Just ([], Out <$> w)
runAssignment (many red) (flip (ast Red) [] <$> w) `shouldBe` Result ([], Out <$> w)
it "matches one-or-more repetitions against one or more input nodes" $
runAssignment (some red) [ast Red "hello" []] `shouldBe` Just ([], [Out "hello"])
runAssignment (some red) [ast Red "hello" []] `shouldBe` Result ([], [Out "hello"])
describe "rule" $ do
it "matches nodes with the same symbol" $
runAssignment red [ast Red "hello" []] `shouldBe` Just ([], Out "hello")
runAssignment red [ast Red "hello" []] `shouldBe` Result ([], Out "hello")
it "does not advance past the current node" $
fst <$> runAssignment (rule ()) [ Rose (Node () "hi") [] ] `shouldBe` Just [ Rose (Node () "hi") [] ]
fst <$> runAssignment (rule Red) [ Rose (Node Red "hi") [] ] `shouldBe` Result [ Rose (Node Red "hi") [] ]
describe "content" $ do
it "produces the nodes content" $
snd <$> runAssignment content [ Rose (Node () "hi") [] ] `shouldBe` Just "hi"
snd <$> runAssignment content [ Rose (Node Red "hi") [] ] `shouldBe` Result "hi"
it "advances past the current node" $
fst <$> runAssignment content [ Rose (Node () "hi") [] ] `shouldBe` Just []
fst <$> runAssignment content [ Rose (Node Red "hi") [] ] `shouldBe` Result []
describe "children" $ do
it "advances past the current node" $
fst <$> runAssignment (children (pure (Out ""))) [ast Red "a" []] `shouldBe` Just []
fst <$> runAssignment (children (pure (Out ""))) [ast Red "a" []] `shouldBe` Result []
it "matches if its subrule matches" $
() <$ runAssignment (children red) [ast Blue "b" [ast Red "a" []]] `shouldBe` Just ()
() <$ runAssignment (children red) [ast Blue "b" [ast Red "a" []]] `shouldBe` Result ()
it "does not match if its subrule does not match" $
runAssignment (children red) [ast Blue "b" [ast Green "a" []]] `shouldBe` Nothing
runAssignment (children red) [ast Blue "b" [ast Green "a" []]] `shouldBe` Error []
it "matches nested children" $ do
runAssignment
(rule 'A' *> children (rule 'B' *> children (rule 'C' *> content)))
[ ast 'A' "" [ ast 'B' "" [ ast 'C' "1" [] ] ] ]
(rule Red *> children (rule Green *> children (rule Blue *> content)))
[ ast Red "" [ ast Green "" [ ast Blue "1" [] ] ] ]
`shouldBe`
Just ([], "1")
Result ([], "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" [] ]
(many (rule Red *> children (rule Green *> content)
<|> rule Blue *> content))
[ ast Red "" [ ast Green "B" [] ]
, ast Blue "C" [] ]
`shouldBe`
Just ([], ["B", "C"])
Result ([], ["B", "C"])
it "matches multiple nested children" $ do
runAssignment
(rule 'A' *> children (many (rule 'B' *> children (rule 'C' *> content))))
[ ast 'A' "" [ ast 'B' "" [ ast 'C' "1" [] ]
, ast 'B' "" [ ast 'C' "2" [] ] ] ]
(rule Red *> children (many (rule Green *> children (rule Blue *> content))))
[ ast Red "" [ ast Green "" [ ast Blue "1" [] ]
, ast Green "" [ ast Blue "2" [] ] ] ]
`shouldBe`
Just ([], ["1", "2"])
Result ([], ["1", "2"])
ast :: grammar -> ByteString -> [AST grammar] -> AST grammar
ast g s c = Rose (Node g s) c
@ -76,6 +77,9 @@ ast g s c = Rose (Node g s) c
data Grammar = Red | Green | Blue
deriving (Eq, Show)
instance Symbol Grammar where
symbolType _ = Regular
data Out = Out ByteString
deriving (Eq, Show)