Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 2754e26

Browse files
author
Patrick Thomson
committed
Port over Integration.hs.
1 parent af5c06b commit 2754e26

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

test/Integration/Spec.hs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1+
{-# LANGUAGE ImplicitParams, LambdaCase, NamedFieldPuns #-}
12
module Integration.Spec (spec) where
23

34
import Control.Exception (throw)
45
import Data.Foldable (find, traverse_, for_)
56
import Data.List (union, concat, transpose)
67
import qualified Data.ByteString as B
8+
import qualified Data.ByteString.Lazy as BL
79
import System.FilePath.Glob
810
import System.FilePath.Posix
11+
import System.IO.Unsafe
912

1013
import SpecHelpers
1114

15+
import Test.Tasty
16+
import Test.Tasty.Golden
17+
import Test.Tasty.HUnit
18+
1219
languages :: [FilePath]
1320
languages = ["go", "javascript", "json", "python", "ruby", "typescript", "tsx"]
1421

15-
spec :: TaskSession -> Spec
16-
spec config = parallel $ do
17-
for_ languages $ \language -> do
18-
let dir = "test/fixtures" </> language </> "corpus"
19-
it (language <> " corpus exists") $ examples dir `shouldNotReturn` []
20-
describe (language <> " corpus") $ runTestsIn dir []
22+
spec :: TaskSession -> TestTree
23+
spec config = let ?session = config in testGroup "Integration (golden tests)" $ fmap testsForLanguage languages
2124

22-
where
23-
runTestsIn :: FilePath -> [(FilePath, String)] -> SpecWith ()
24-
runTestsIn directory pending = do
25-
examples <- runIO $ examples directory
26-
traverse_ (runTest pending) examples
27-
runTest pending ParseExample{..} = it ("parses " <> file) $ maybe (testParse config file parseOutput) pendingWith (lookup parseOutput pending)
28-
runTest pending DiffExample{..} = it ("diffs " <> diffOutput) $ maybe (testDiff config (Both fileA fileB) diffOutput) pendingWith (lookup diffOutput pending)
25+
testsForLanguage :: (?session :: TaskSession) => FilePath -> TestTree
26+
testsForLanguage language = do
27+
let dir = "test/fixtures" </> language </> "corpus"
28+
let items = unsafePerformIO (examples dir)
29+
testGroup language (fmap testForExample items)
2930

3031
data Example = DiffExample { fileA :: FilePath, fileB :: FilePath, diffOutput :: FilePath }
3132
| ParseExample { file :: FilePath, parseOutput :: FilePath }
3233
deriving (Eq, Show)
3334

35+
testForExample :: (?session :: TaskSession) => Example -> TestTree
36+
testForExample = \case
37+
DiffExample{fileA, fileB, diffOutput} ->
38+
goldenVsStringDiff
39+
("diffs " <> diffOutput)
40+
(\ref new -> ["git", "diff", ref, new])
41+
diffOutput
42+
(BL.fromStrict <$> diffFilePaths ?session (Both fileA fileB))
43+
ParseExample{file, parseOutput} -> testCase ("parses " <> file) (pure ())
44+
45+
3446
-- | Return all the examples from the given directory. Examples are expected to
3547
-- | have the form:
3648
-- |
@@ -81,18 +93,3 @@ examples directory = do
8193
-- | Given a test name like "foo.A.js", return "foo".
8294
normalizeName :: FilePath -> FilePath
8395
normalizeName path = dropExtension $ dropExtension path
84-
85-
testParse :: TaskSession -> FilePath -> FilePath -> Expectation
86-
testParse session path expectedOutput = do
87-
actual <- fmap verbatim <$> parseFilePath session path
88-
case actual of
89-
Left err -> throw err
90-
Right actual -> do
91-
expected <- verbatim <$> B.readFile expectedOutput
92-
actual `shouldBe` expected
93-
94-
testDiff :: TaskSession -> Both FilePath -> FilePath -> Expectation
95-
testDiff config paths expectedOutput = do
96-
actual <- verbatim <$> diffFilePaths config paths
97-
expected <- verbatim <$> B.readFile expectedOutput
98-
actual `shouldBe` expected

0 commit comments

Comments
 (0)