@@ -4,33 +4,52 @@ import Control.Monad (when)
44import qualified Data.ByteString as B
55import Data.ByteString.Builder
66import Data.Foldable (for_ )
7- import Semantic.Api hiding (File , Blob , BlobPair )
7+ import Semantic.Api hiding (Blob , BlobPair , File )
88import Semantic.CLI
99import Semantic.IO
1010import Semantic.Task
1111import Serializing.Format
12+ import System.Directory
13+ import System.IO.Unsafe
14+ import System.Process
1215
1316import SpecHelpers
17+ import Test.Tasty
18+ import Test.Tasty.Golden
1419
20+ spec :: TestTree
21+ spec = testGroup " Semantic.CLI"
22+ [ testGroup " parseDiffBuilder" $ fmap testForDiffFixture diffFixtures
23+ , testGroup " parseTermBuilder" $ fmap testForParseFixture parseFixtures
24+ ]
25+
26+ -- If you're investigating these tests and find this output hard to read
27+ -- the `jd` CLI tool (https://github.com/josephburnett/jd) will print
28+ -- a detailed summary of the differences between these JSON files.
29+ renderDiff :: String -> String -> [String ]
30+ renderDiff ref new = unsafePerformIO $ do
31+ useJD <- (isExtensionOf " .json" ref && ) <$> fmap isJust (findExecutable " jd" )
32+ pure $ if useJD
33+ then [" jd" , " -set" , ref, new]
34+ else [" git" , " diff" , ref, new]
35+ {-# NOINLINE renderDiff #-}
36+
37+
38+ -- PT TODO: reduce duplication
39+
40+ testForDiffFixture (diffRenderer, runDiff, files, expected) =
41+ goldenVsStringDiff
42+ (" renders to " <> diffRenderer)
43+ renderDiff
44+ expected
45+ (fmap toLazyByteString . runTaskOrDie $ readBlobPairs (Right files) >>= runDiff)
1546
16- spec :: Spec
17- spec = parallel $ do
18- describe " parseDiffBuilder" $
19- for_ diffFixtures $ \ (diffRenderer, runDiff, files, expected) ->
20- it (" renders to " <> diffRenderer <> " with files " <> show files) $ do
21- output <- runTaskOrDie $ readBlobPairs (Right files) >>= runDiff
22- runBuilder output `shouldBe'` expected
23-
24- describe " parseTermBuilder" $
25- for_ parseFixtures $ \ (format, runParse, files, expected) ->
26- it (" renders to " <> format <> " with files " <> show files) $ do
27- output <- runTaskOrDie $ readBlobs (FilesFromPaths files) >>= runParse
28- runBuilder output `shouldBe'` expected
29- where
30- shouldBe' actual' expectedFile = do
31- let actual = verbatim actual'
32- expected <- verbatim <$> B. readFile expectedFile
33- actual `shouldBe` expected
47+ testForParseFixture (format, runParse, files, expected) =
48+ goldenVsStringDiff
49+ (" renders to " <> format)
50+ renderDiff
51+ expected
52+ (fmap toLazyByteString . runTaskOrDie $ readBlobs (FilesFromPaths files) >>= runParse)
3453
3554parseFixtures :: [(String , [Blob ] -> TaskEff Builder , [File ], FilePath )]
3655parseFixtures =
0 commit comments