1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 14:54:16 +03:00
semantic/test/Rewriting/Go/Spec.hs
Patrick Thomson 8ae90e53c5 Quiet debug spew in specs.
This console barf makes reading though backscrolls unpleasant. Using
Shelly rather than an abomination of a `system` call and adding a
helper function to parse files quietly improved the situation greatly.

This also contains changes to Util that make the file significantly
easier to navigate, thanks to the power of `PartialTyepSignatures`.

Fixes #140.
2019-06-19 12:09:37 -04:00

41 lines
1.3 KiB
Haskell

{-# LANGUAGE TypeOperators #-}
module Rewriting.Go.Spec (spec) where
import Control.Rewriting
import Data.List
import Data.Sum
import qualified Data.Syntax.Literal as Lit
import qualified Data.Syntax.Statement as Stmt
import Data.Text (Text)
import SpecHelpers
-- This gets the Text contents of all integers
integerMatcher :: (Lit.Integer :< fs) => Rewrite (Term (Sum fs) ann) Text
integerMatcher = enter Lit.integerContent
-- This matches all for-loops with its index variable new variable bound to 0,
-- e.g. `for i := 0; i < 10; i++`
loopMatcher :: ( Stmt.For :< fs
, Stmt.Assignment :< fs
, Lit.Integer :< fs)
=> Rule (Term (Sum fs) ann)
loopMatcher = target <* go where
go = enter Stmt.forBefore
>>> enter Stmt.assignmentValue
>>> enter Lit.integerContent
>>> ensure (== "0")
spec :: Spec
spec = describe "recursively" $ do
it "extracts integers" $ do
parsed <- parseFileQuiet goParser "test/fixtures/go/matching/integers.go"
let matched = recursively integerMatcher parsed
sort matched `shouldBe` ["1", "2", "3"]
it "counts for loops" $ do
parsed <- parseFileQuiet goParser "test/fixtures/go/matching/for.go"
let matched = recursively @[] @(Term _ _) loopMatcher parsed
length matched `shouldBe` 2