1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 14:21:31 +03:00
semantic/test/SpecHelpers.hs

75 lines
2.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds, GADTs, ScopedTypeVariables, TypeFamilies, TypeOperators, TypeApplications #-}
module SpecHelpers (
module X
, diffFilePaths
, parseFilePath
2017-12-10 19:46:17 +03:00
, readFilePair
2018-02-07 23:20:27 +03:00
, readFileVerbatim
, verbatim
, Verbatim(..)
, ) where
import Data.Abstract.Address as X
2018-03-16 01:09:28 +03:00
import Data.Abstract.FreeVariables as X hiding (dropExtension)
2018-03-15 03:42:59 +03:00
import Data.Abstract.Heap as X
import Data.Abstract.ModuleTable as X
2018-03-13 20:59:20 +03:00
import Data.Blob as X
import Data.Functor.Listable as X
import Data.Language as X
2018-03-13 20:32:25 +03:00
import Data.Output as X
import Data.Range as X
2018-03-13 20:59:20 +03:00
import Data.Record as X
import Data.Source as X
2018-03-13 20:59:20 +03:00
import Data.Span as X
import Data.Term as X
import Parsing.Parser as X
2018-03-13 20:32:25 +03:00
import Rendering.Renderer as X
import Semantic as X
import Semantic.Task as X
import Semantic.Util as X
2018-03-13 20:59:20 +03:00
import System.FilePath as X
import Data.ByteString as X (ByteString)
2018-03-13 21:04:58 +03:00
import Data.Functor.Both as X (Both, runBothWith, both)
import Data.Maybe as X
import Data.Monoid as X (Monoid(..), First(..), Last(..))
import Data.Semigroup as X (Semigroup(..))
2018-03-13 21:10:50 +03:00
import Test.Hspec as X (Spec, SpecWith, context, describe, it, xit, parallel, pendingWith, around, runIO)
import Test.Hspec.Expectations.Pretty as X
2018-03-13 20:59:20 +03:00
import Test.Hspec.LeanCheck as X
import Test.LeanCheck as X
2018-03-13 20:32:25 +03:00
import qualified Data.ByteString as B
import qualified Semantic.IO as IO
-- | Returns an s-expression formatted diff for the specified FilePath pair.
diffFilePaths :: Both FilePath -> IO ByteString
2017-12-10 19:46:17 +03:00
diffFilePaths paths = readFilePair paths >>= runTask . diffBlobPair SExpressionDiffRenderer
-- | Returns an s-expression parse tree for the specified FilePath.
parseFilePath :: FilePath -> IO ByteString
parseFilePath path = IO.readFile path (IO.languageForFilePath path) >>= pure . fromJust >>= runTask . parseBlob SExpressionTermRenderer
2017-12-11 19:27:13 +03:00
-- | Read two files to a BlobPair.
readFilePair :: Both FilePath -> IO BlobPair
readFilePair paths = let paths' = fmap (\p -> (p, IO.languageForFilePath p)) paths in
2017-12-11 19:27:13 +03:00
runBothWith IO.readFilePair paths'
2017-05-11 16:28:52 +03:00
2018-02-07 23:20:27 +03:00
readFileVerbatim :: FilePath -> IO Verbatim
readFileVerbatim = fmap verbatim . B.readFile
2018-02-07 23:20:27 +03:00
newtype Verbatim = Verbatim ByteString
2018-02-07 23:20:27 +03:00
deriving (Eq)
instance Show Verbatim where
show (Verbatim x) = show x
verbatim :: ByteString -> Verbatim
2018-02-07 23:20:27 +03:00
verbatim = Verbatim . stripWhitespace
where
stripWhitespace :: ByteString -> ByteString
2018-02-07 23:20:27 +03:00
stripWhitespace = B.foldl' go B.empty
where go acc x | x `B.elem` " \t\n" = acc
| otherwise = B.snoc acc x