1
1
mirror of https://github.com/github/semantic.git synced 2024-11-29 11:02:26 +03:00
semantic/test/Semantic/CLI/Spec.hs

74 lines
3.8 KiB
Haskell
Raw Normal View History

2019-06-20 00:56:37 +03:00
module Semantic.CLI.Spec (testTree) where
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 02:15:09 +03:00
import SpecHelpers hiding ((</>))
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-09-20 02:15:09 +03:00
testForDiffFixture :: (String, [BlobPair] -> TaskEff Builder, [Both 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-09-20 02:15:09 +03:00
testForParseFixture :: (String, [Blob] -> TaskEff 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-09-20 02:15:09 +03:00
parseFixtures :: [(String, [Blob] -> TaskEff Builder, [File], Path.RelFile)]
2017-06-16 19:47:47 +03:00
parseFixtures =
2019-09-20 02:15:09 +03:00
[ ("s-expression", parseTermBuilder TermSExpression, path, Path.relFile "test/fixtures/ruby/corpus/and-or.parseA.txt")
, ("json", parseTermBuilder TermJSONTree, path, prefix </> Path.file "parse-tree.json")
, ("json", parseTermBuilder TermJSONTree, path', prefix </> Path.file "parse-trees.json")
, ("json", parseTermBuilder TermJSONTree, [], prefix </> Path.file "parse-tree-empty.json")
, ("symbols", parseSymbolsBuilder Serializing.Format.JSON, path'', prefix </> Path.file "parse-tree.symbols.json")
, ("protobuf symbols", 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"
2017-06-16 19:47:47 +03:00
2019-09-20 02:15:09 +03:00
diffFixtures :: [(String, [BlobPair] -> TaskEff Builder, [Both 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")
, ("toc summaries diff", diffSummaryBuilder Serializing.Format.JSON, pathMode, prefix </> Path.file "diff-tree.toc.json")
, ("protobuf diff", diffSummaryBuilder Serializing.Format.Proto, pathMode, prefix </> Path.file "diff-tree.toc.protobuf.bin")
2017-06-16 19:51:42 +03:00
]
2019-01-10 23:53:15 +03:00
where pathMode = [Both (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"