2019-06-20 00:56:37 +03:00
|
|
|
module Semantic.CLI.Spec (testTree) where
|
2017-04-11 03:10:34 +03:00
|
|
|
|
2019-10-01 00:59:23 +03:00
|
|
|
import Control.Carrier.Parse.Simple
|
2019-09-30 19:10:59 +03:00
|
|
|
import Control.Effect.Reader
|
2018-06-01 03:20:51 +03:00
|
|
|
import Data.ByteString.Builder
|
2019-06-12 19:26:37 +03:00
|
|
|
import Semantic.Api hiding (Blob, BlobPair, File)
|
2018-06-01 03:20:51 +03:00
|
|
|
import Semantic.Task
|
2019-01-18 02:36:56 +03:00
|
|
|
import Serializing.Format
|
2019-06-12 19:26:37 +03:00
|
|
|
import System.IO.Unsafe
|
2019-09-20 02:15:09 +03:00
|
|
|
import qualified System.Path as Path
|
|
|
|
import System.Path ((</>))
|
|
|
|
import qualified System.Path.Directory as Path
|
2018-03-13 21:10:50 +03:00
|
|
|
|
2019-09-20 19:26:49 +03:00
|
|
|
import SpecHelpers
|
2019-06-12 19:26:37 +03:00
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.Golden
|
2018-03-13 21:10:50 +03:00
|
|
|
|
2019-06-20 00:56:37 +03:00
|
|
|
testTree :: TestTree
|
|
|
|
testTree = testGroup "Semantic.CLI"
|
2019-06-12 19:26:37 +03:00
|
|
|
[ testGroup "parseDiffBuilder" $ fmap testForDiffFixture diffFixtures
|
|
|
|
, testGroup "parseTermBuilder" $ fmap testForParseFixture parseFixtures
|
|
|
|
]
|
2017-04-11 03:10:34 +03:00
|
|
|
|
2019-06-12 19:29:11 +03:00
|
|
|
-- We provide this function to the golden tests so as to have better
|
|
|
|
-- output when diffing JSON outputs. If you're investigating these
|
|
|
|
-- tests and find this output hard to read, install the `jd` CLI tool
|
|
|
|
-- (https://github.com/josephburnett/jd), which will print a detailed
|
|
|
|
-- summary of the differences between these JSON files.
|
2019-06-12 19:26:37 +03:00
|
|
|
renderDiff :: String -> String -> [String]
|
|
|
|
renderDiff ref new = unsafePerformIO $ do
|
2019-09-20 02:15:09 +03:00
|
|
|
useJD <- (Path.hasExtension ".json" (Path.relPath ref) &&) <$> fmap isJust (Path.findExecutable "jd")
|
2019-06-12 19:26:37 +03:00
|
|
|
pure $ if useJD
|
|
|
|
then ["jd", "-set", ref, new]
|
|
|
|
else ["git", "diff", ref, new]
|
|
|
|
{-# NOINLINE renderDiff #-}
|
|
|
|
|
2019-10-18 17:45:59 +03:00
|
|
|
testForDiffFixture :: (String, [BlobPair] -> ParseC TaskC Builder, [(File, File)], Path.RelFile) -> TestTree
|
2019-06-12 19:26:37 +03:00
|
|
|
testForDiffFixture (diffRenderer, runDiff, files, expected) =
|
|
|
|
goldenVsStringDiff
|
2019-06-12 19:29:11 +03:00
|
|
|
("diff fixture renders to " <> diffRenderer <> " " <> show files)
|
2019-06-12 19:26:37 +03:00
|
|
|
renderDiff
|
2019-09-20 02:15:09 +03:00
|
|
|
(Path.toString expected)
|
2019-06-12 19:26:37 +03:00
|
|
|
(fmap toLazyByteString . runTaskOrDie $ readBlobPairs (Right files) >>= runDiff)
|
|
|
|
|
2019-10-01 00:59:23 +03:00
|
|
|
testForParseFixture :: (String, [Blob] -> ParseC TaskC Builder, [File], Path.RelFile) -> TestTree
|
2019-06-12 19:26:37 +03:00
|
|
|
testForParseFixture (format, runParse, files, expected) =
|
|
|
|
goldenVsStringDiff
|
2019-06-20 00:42:53 +03:00
|
|
|
("diff fixture renders to " <> format)
|
2019-06-12 19:26:37 +03:00
|
|
|
renderDiff
|
2019-09-20 02:15:09 +03:00
|
|
|
(Path.toString expected)
|
2019-06-12 19:26:37 +03:00
|
|
|
(fmap toLazyByteString . runTaskOrDie $ readBlobs (FilesFromPaths files) >>= runParse)
|
2017-04-21 23:56:19 +03:00
|
|
|
|
2019-10-01 00:59:23 +03:00
|
|
|
parseFixtures :: [(String, [Blob] -> ParseC TaskC Builder, [File], Path.RelFile)]
|
2017-06-16 19:47:47 +03:00
|
|
|
parseFixtures =
|
2019-09-30 19:10:59 +03:00
|
|
|
[ ("s-expression", run . parseTermBuilder TermSExpression, path, Path.relFile "test/fixtures/ruby/corpus/and-or.parseA.txt")
|
|
|
|
, ("json", run . parseTermBuilder TermJSONTree, path, prefix </> Path.file "parse-tree.json")
|
|
|
|
, ("json", run . parseTermBuilder TermJSONTree, path', prefix </> Path.file "parse-trees.json")
|
|
|
|
, ("json", run . parseTermBuilder TermJSONTree, [], prefix </> Path.file "parse-tree-empty.json")
|
|
|
|
, ("symbols", run . parseSymbolsBuilder Serializing.Format.JSON, path'', prefix </> Path.file "parse-tree.symbols.json")
|
|
|
|
, ("protobuf symbols", run . parseSymbolsBuilder Serializing.Format.Proto, path'', prefix </> Path.file "parse-tree.symbols.protobuf.bin")
|
2017-06-16 19:47:47 +03:00
|
|
|
]
|
2018-06-05 01:26:47 +03:00
|
|
|
where path = [File "test/fixtures/ruby/corpus/and-or.A.rb" Ruby]
|
|
|
|
path' = [File "test/fixtures/ruby/corpus/and-or.A.rb" Ruby, File "test/fixtures/ruby/corpus/and-or.B.rb" Ruby]
|
|
|
|
path'' = [File "test/fixtures/ruby/corpus/method-declaration.A.rb" Ruby]
|
2019-09-20 02:15:09 +03:00
|
|
|
prefix = Path.relDir "test/fixtures/cli"
|
2019-09-30 19:10:59 +03:00
|
|
|
run = runReader (PerLanguageModes ALaCarte)
|
2017-06-16 19:47:47 +03:00
|
|
|
|
2019-10-18 17:45:59 +03:00
|
|
|
diffFixtures :: [(String, [BlobPair] -> ParseC TaskC Builder, [(File, File)], Path.RelFile)]
|
2017-06-16 19:51:42 +03:00
|
|
|
diffFixtures =
|
2019-09-20 02:15:09 +03:00
|
|
|
[ ("json diff", parseDiffBuilder DiffJSONTree, pathMode, prefix </> Path.file "diff-tree.json")
|
|
|
|
, ("s-expression diff", parseDiffBuilder DiffSExpression, pathMode, Path.relFile "test/fixtures/ruby/corpus/method-declaration.diffA-B.txt")
|
2019-10-17 20:47:52 +03:00
|
|
|
, ("toc summaries diff", runReader defaultLanguageModes . diffSummaryBuilder Serializing.Format.JSON, pathMode, prefix </> Path.file "diff-tree.toc.json")
|
|
|
|
, ("protobuf diff", runReader defaultLanguageModes . diffSummaryBuilder Serializing.Format.Proto, pathMode, prefix </> Path.file "diff-tree.toc.protobuf.bin")
|
2017-06-16 19:51:42 +03:00
|
|
|
]
|
2019-10-18 17:45:59 +03:00
|
|
|
where pathMode = [(File "test/fixtures/ruby/corpus/method-declaration.A.rb" Ruby, File "test/fixtures/ruby/corpus/method-declaration.B.rb" Ruby)]
|
2019-09-20 02:15:09 +03:00
|
|
|
prefix = Path.relDir "test/fixtures/cli"
|