2017-04-19 23:25:46 +03:00
|
|
|
{-# LANGUAGE DataKinds, GADTs, ScopedTypeVariables, TypeFamilies, TypeOperators #-}
|
|
|
|
module SpecHelpers
|
2017-04-20 02:33:27 +03:00
|
|
|
( diffFilePaths
|
|
|
|
, parseFilePath
|
2017-05-10 01:49:38 +03:00
|
|
|
, readFile
|
|
|
|
, languageForFilePath
|
2017-05-11 16:28:52 +03:00
|
|
|
, unListableDiff
|
2017-04-19 23:25:46 +03:00
|
|
|
) where
|
2017-04-19 19:12:19 +03:00
|
|
|
|
|
|
|
import qualified Data.ByteString as B
|
2017-04-19 23:25:46 +03:00
|
|
|
import Data.Functor.Both
|
2017-05-11 16:28:52 +03:00
|
|
|
import Data.Functor.Listable
|
2017-04-19 19:12:19 +03:00
|
|
|
import qualified Data.Text.ICU.Convert as Convert
|
|
|
|
import qualified Data.Text.ICU.Detect as Detect
|
2017-05-11 16:28:52 +03:00
|
|
|
import Diff
|
2017-04-19 23:25:46 +03:00
|
|
|
import Language
|
2017-05-11 16:28:52 +03:00
|
|
|
import Patch
|
2017-04-21 23:56:19 +03:00
|
|
|
import Prologue hiding (readFile)
|
|
|
|
import Renderer
|
2017-04-20 02:33:27 +03:00
|
|
|
import Semantic
|
2017-04-21 23:56:19 +03:00
|
|
|
import Source
|
2017-04-19 19:45:08 +03:00
|
|
|
import System.FilePath
|
2017-05-11 16:28:52 +03:00
|
|
|
import Term
|
2017-04-19 19:12:19 +03:00
|
|
|
|
2017-04-19 23:25:46 +03:00
|
|
|
-- | Returns an s-expression formatted diff for the specified FilePath pair.
|
2017-04-20 02:33:27 +03:00
|
|
|
diffFilePaths :: Both FilePath -> IO ByteString
|
|
|
|
diffFilePaths paths = do
|
2017-04-21 23:56:19 +03:00
|
|
|
blobs <- pure <$> traverse readFile paths
|
2017-05-10 22:41:17 +03:00
|
|
|
diffBlobPairs (const identity) (SExpressionDiffRenderer TreeOnly) blobs
|
2017-04-19 23:25:46 +03:00
|
|
|
|
|
|
|
-- | Returns an s-expression parse tree for the specified FilePath.
|
2017-04-20 02:33:27 +03:00
|
|
|
parseFilePath :: FilePath -> IO ByteString
|
|
|
|
parseFilePath path = do
|
2017-05-10 01:49:38 +03:00
|
|
|
blob <- readFile path
|
|
|
|
parseBlobs (SExpressionParseTreeRenderer TreeOnly) [blob]
|
2017-04-21 23:56:19 +03:00
|
|
|
|
|
|
|
-- | Read a file to a SourceBlob.
|
|
|
|
--
|
|
|
|
-- NB: This is intentionally duplicated from Command.Files because eventually
|
2017-04-24 19:17:18 +03:00
|
|
|
-- we want to be able to test a core Semantic library that has no knowledge of
|
2017-04-21 23:56:19 +03:00
|
|
|
-- the filesystem or Git. The tests, however, will still leverage reading files.
|
|
|
|
readFile :: FilePath -> IO SourceBlob
|
|
|
|
readFile path = do
|
|
|
|
source <- (Just <$> readFileToUnicode path) `catch` (const (pure Nothing) :: IOException -> IO (Maybe Source))
|
2017-05-10 01:49:38 +03:00
|
|
|
pure $ fromMaybe (emptySourceBlob path) (sourceBlob path (languageForFilePath path) <$> source)
|
|
|
|
where
|
|
|
|
-- | Read a file, convert it's contents unicode and return it wrapped in Source.
|
|
|
|
readFileToUnicode :: FilePath -> IO Source
|
|
|
|
readFileToUnicode path = B.readFile path >>= transcode
|
|
|
|
where
|
|
|
|
transcode :: B.ByteString -> IO Source
|
|
|
|
transcode text = fromText <$> do
|
|
|
|
match <- Detect.detectCharset text
|
|
|
|
converter <- Convert.open match Nothing
|
|
|
|
pure $ Convert.toUnicode converter text
|
2017-05-11 16:28:52 +03:00
|
|
|
|
2017-05-10 01:49:38 +03:00
|
|
|
-- | Returns a Maybe Language based on the FilePath's extension.
|
|
|
|
languageForFilePath :: FilePath -> Maybe Language
|
|
|
|
languageForFilePath = languageForType . toS . takeExtension
|
2017-05-11 16:28:52 +03:00
|
|
|
|
|
|
|
-- | Extract a 'Diff' from a 'ListableF' enumerated by a property test.
|
|
|
|
unListableDiff :: Functor f => ListableF (Free (TermF f (ListableF (Join (,)) annotation))) (Patch (ListableF (Term f) annotation)) -> Diff f annotation
|
|
|
|
unListableDiff diff = hoistFree (first unListableF) $ fmap unListableF <$> unListableF diff
|