2018-03-09 20:07:34 +03:00
|
|
|
module Semantic.CLI.Spec (spec) where
|
2017-04-11 03:10:34 +03:00
|
|
|
|
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.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
|
|
|
|
]
|
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
|
|
|
|
useJD <- (isExtensionOf ".json" ref &&) <$> fmap isJust (findExecutable "jd")
|
|
|
|
pure $ if useJD
|
|
|
|
then ["jd", "-set", ref, new]
|
|
|
|
else ["git", "diff", ref, new]
|
|
|
|
{-# NOINLINE renderDiff #-}
|
|
|
|
|
2019-06-15 07:53:02 +03:00
|
|
|
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)
|
|
|
|
|
2019-06-15 07:53:02 +03:00
|
|
|
testForParseFixture :: (String, [Blob] -> TaskEff Builder, [File], FilePath) -> 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
|
|
|
|
expected
|
|
|
|
(fmap toLazyByteString . runTaskOrDie $ readBlobs (FilesFromPaths files) >>= runParse)
|
2017-04-21 23:56:19 +03:00
|
|
|
|
2018-06-01 03:20:51 +03:00
|
|
|
parseFixtures :: [(String, [Blob] -> TaskEff Builder, [File], FilePath)]
|
2017-06-16 19:47:47 +03:00
|
|
|
parseFixtures =
|
2019-01-19 02:54:55 +03:00
|
|
|
[ ("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]
|
2018-06-01 03:20:51 +03:00
|
|
|
prefix = "test/fixtures/cli"
|
2017-06-16 19:47:47 +03:00
|
|
|
|
2018-06-01 03:20:51 +03:00
|
|
|
diffFixtures :: [(String, [BlobPair] -> TaskEff Builder, [Both File], FilePath)]
|
2017-06-16 19:51:42 +03:00
|
|
|
diffFixtures =
|
2019-01-22 21:42:06 +03:00
|
|
|
[ ("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)]
|
2018-06-01 03:20:51 +03:00
|
|
|
prefix = "test/fixtures/cli"
|