1
1
mirror of https://github.com/github/semantic.git synced 2024-12-20 05:11:44 +03:00
semantic/test/Semantic/CLI/Spec.hs

77 lines
4.0 KiB
Haskell
Raw Normal View History

2019-06-20 00:56:37 +03:00
module Semantic.CLI.Spec (testTree) where
2019-10-01 00:59:23 +03:00
import Control.Carrier.Parse.Simple
import Control.Effect.Reader
import Data.ByteString.Builder
2019-06-12 19:26:37 +03:00
import Semantic.Api hiding (Blob, BlobPair, File)
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
]
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
("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)
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 =
[ ("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"
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"