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

70 lines
3.3 KiB
Haskell
Raw Normal View History

module Semantic.CLI.Spec (spec) 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.Directory
import System.IO.Unsafe
2018-03-13 21:10:50 +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-12 19:26:37 +03:00
spec :: TestTree
spec = testGroup "Semantic.CLI"
[ 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
useJD <- (isExtensionOf ".json" ref &&) <$> fmap isJust (findExecutable "jd")
pure $ if useJD
then ["jd", "-set", ref, new]
else ["git", "diff", ref, new]
{-# NOINLINE renderDiff #-}
testForDiffFixture :: (String, [BlobPair] -> TaskEff Builder, [Both File], FilePath) -> 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
expected
(fmap toLazyByteString . runTaskOrDie $ readBlobPairs (Right files) >>= runDiff)
testForParseFixture :: (String, [Blob] -> TaskEff Builder, [File], FilePath) -> 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
expected
(fmap toLazyByteString . runTaskOrDie $ readBlobs (FilesFromPaths files) >>= runParse)
parseFixtures :: [(String, [Blob] -> TaskEff Builder, [File], FilePath)]
2017-06-16 19:47:47 +03:00
parseFixtures =
[ ("s-expression", parseTermBuilder TermSExpression, path, "test/fixtures/ruby/corpus/and-or.parseA.txt")
, ("json", parseTermBuilder TermJSONTree, path, prefix </> "parse-tree.json")
, ("json", parseTermBuilder TermJSONTree, path', prefix </> "parse-trees.json")
, ("json", parseTermBuilder TermJSONTree, [], prefix </> "parse-tree-empty.json")
2019-04-12 22:50:30 +03:00
, ("symbols", parseSymbolsBuilder Serializing.Format.JSON, path'', prefix </> "parse-tree.symbols.json")
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]
prefix = "test/fixtures/cli"
2017-06-16 19:47:47 +03:00
diffFixtures :: [(String, [BlobPair] -> TaskEff Builder, [Both File], FilePath)]
2017-06-16 19:51:42 +03:00
diffFixtures =
[ ("json diff", parseDiffBuilder DiffJSONTree, pathMode, prefix </> "diff-tree.json")
, ("s-expression diff", parseDiffBuilder DiffSExpression, pathMode, "test/fixtures/ruby/corpus/method-declaration.diffA-B.txt")
, ("toc summaries diff", diffSummaryBuilder Serializing.Format.JSON, pathMode, prefix </> "diff-tree.toc.json")
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)]
prefix = "test/fixtures/cli"