From 1d9436bb142f2d0b32b5b3c036fd80e15a3348c6 Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Tue, 28 Feb 2017 15:00:56 -0800 Subject: [PATCH] Collapse all these tests into DiffCommandSpec --- semantic-diff.cabal | 1 - test/DiffCommandSpec.hs | 72 +++++++++++++++++++++++++++++- test/SemanticDiffSpec.hs | 95 ---------------------------------------- test/Spec.hs | 2 - 4 files changed, 71 insertions(+), 99 deletions(-) delete mode 100644 test/SemanticDiffSpec.hs diff --git a/semantic-diff.cabal b/semantic-diff.cabal index 46e859765..a00a13347 100644 --- a/semantic-diff.cabal +++ b/semantic-diff.cabal @@ -151,7 +151,6 @@ test-suite test , TOCSpec , IntegrationSpec , DiffCommandSpec - , SemanticDiffSpec , Test.Hspec.LeanCheck build-depends: aeson , array diff --git a/test/DiffCommandSpec.hs b/test/DiffCommandSpec.hs index 713819fe6..07dcd774a 100644 --- a/test/DiffCommandSpec.hs +++ b/test/DiffCommandSpec.hs @@ -1,12 +1,22 @@ module DiffCommandSpec where -import Prologue +import Data.Aeson +import Data.Aeson.Types +import Data.Maybe import Data.Functor.Both +import Prelude +import Prologue (($), fmap, (.), pure, for, panic) import Test.Hspec hiding (shouldBe, shouldNotBe, shouldThrow, errorCall) import Test.Hspec.Expectations.Pretty import Test.Hspec.LeanCheck +import Data.Text.Lazy.Encoding as E +import Data.Text.Lazy as T +import Data.Map +import qualified Data.Vector as V import Arguments import DiffCommand +import Renderer +import qualified Git.Types as Git spec :: Spec spec = parallel $ do @@ -20,3 +30,63 @@ spec = parallel $ do \format -> do output <- diff $ args "test/fixtures/git/examples/all-languages.git" "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe" ["methods.rb"] format output `shouldNotBe` "" + + describe "fetchDiffs" $ do + it "generates diff summaries for two shas" $ do + (errors, summaries) <- fetchDiffsOutput summaryText $ args "test/fixtures/git/examples/all-languages.git" "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe" ["methods.rb"] Renderer.Summary + errors `shouldBe` Just (fromList []) + summaries `shouldBe` Just (fromList [("methods.rb", ["Added the 'foo()' method"])]) + + it "generates toc summaries for two shas" $ do + (errors, summaries) <- fetchDiffsOutput termText $ args "test/fixtures/git/examples/all-languages.git" "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe" ["methods.rb"] Renderer.TOC + errors `shouldBe` Just (fromList []) + summaries `shouldBe` Just (fromList [("methods.rb", ["foo"])]) + + it "generates toc summaries for two shas inferring paths" $ do + (errors, summaries) <- fetchDiffsOutput termText $ args "test/fixtures/git/examples/all-languages.git" "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe" [] Renderer.TOC + errors `shouldBe` Just (fromList []) + summaries `shouldBe` Just (fromList [("methods.rb", ["foo"])]) + + it "errors with bad shas" $ + fetchDiffsOutput summaryText (args "test/fixtures/git/examples/all-languages.git" "dead" "beef" ["methods.rb"] Renderer.Summary) + `shouldThrow` (== Git.BackendError "Could not lookup dead: Object not found - no match for prefix (dead000000000000000000000000000000000000)") + + it "errors with bad repo path" $ + fetchDiffsOutput summaryText (args "test/fixtures/git/examples/not-a-repo.git" "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe" ["methods.rb"] Renderer.Summary) + `shouldThrow` errorCall "Could not open repository \"test/fixtures/git/examples/not-a-repo.git\"" + +fetchDiffsOutput :: (Object -> Text) -> Arguments -> IO (Maybe (Map Text Value), Maybe (Map Text [Text])) +fetchDiffsOutput f arguments = do + diffs <- fetchDiffs arguments + let json = fromJust . decode . E.encodeUtf8 $ T.fromChunks [concatOutputs diffs] + pure (errors json, summaries f json) + +-- Diff Summaries payloads look like this: +-- { +-- "changes": { "methods.rb": [{ "span":{"insert":{"start":[1,1],"end":[2,4]}}, "summary":"Added the 'foo()' method" }] }, +-- "errors":{} +-- } +-- TOC Summaries payloads look like this: +-- { +-- "changes": { "methods.rb": [{ "span":{"start":[1,1],"end":[2,4]}, "category":"Method", "term":"foo", "changeType":"added" }] +-- }, +-- "errors":{} +-- } +summaries :: (Object -> Text) -> Object -> Maybe (Map Text [Text]) +summaries f = parseMaybe $ \o -> do + changes <- o .: "changes" :: Parser (Map Text (V.Vector Object)) + xs <- for (toList changes) $ \(path, s) -> do + let ys = fmap f s + pure (path, V.toList ys) + pure $ fromList xs + +summaryText :: Object -> Text +summaryText o = fromMaybe (panic "key 'summary' not found") $ + parseMaybe (.: "summary") o + +termText :: Object -> Text +termText o = fromMaybe (panic "key 'term' not found") $ + parseMaybe (.: "term") o + +errors :: Object -> Maybe (Map Text Value) +errors = parseMaybe (.: "errors") diff --git a/test/SemanticDiffSpec.hs b/test/SemanticDiffSpec.hs deleted file mode 100644 index 8cd0f952b..000000000 --- a/test/SemanticDiffSpec.hs +++ /dev/null @@ -1,95 +0,0 @@ -module SemanticDiffSpec where - -import Data.Aeson -import Data.Aeson.Types -import Data.Maybe -import Prelude -import Prologue (($), fmap, (.), pure, for, panic) -import Test.Hspec hiding (shouldBe, shouldThrow, errorCall) -import Test.Hspec.Expectations.Pretty -import Data.Text.Lazy.Encoding as E -import Data.Text.Lazy as T -import Data.Map -import qualified Data.Vector as V -import Arguments -import DiffCommand -import Renderer -import qualified Git.Types as Git - -spec :: Spec -spec = parallel $ do - describe "fetchDiffs" $ do - it "generates diff summaries for two shas" $ do - (errors, summaries) <- fetchDiffsOutput summaryText $ args "test/fixtures/git/examples/all-languages.git" "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe" ["methods.rb"] Renderer.Summary - errors `shouldBe` Just (fromList []) - summaries `shouldBe` Just (fromList [("methods.rb", ["Added the 'foo()' method"])]) - - it "generates toc summaries for two shas" $ do - (errors, summaries) <- fetchDiffsOutput termText $ args "test/fixtures/git/examples/all-languages.git" "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe" ["methods.rb"] Renderer.TOC - errors `shouldBe` Just (fromList []) - summaries `shouldBe` Just (fromList [("methods.rb", ["foo"])]) - - it "generates toc summaries for two shas inferring paths" $ do - (errors, summaries) <- fetchDiffsOutput termText $ args "test/fixtures/git/examples/all-languages.git" "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe" [] Renderer.TOC - errors `shouldBe` Just (fromList []) - summaries `shouldBe` Just (fromList [("methods.rb", ["foo"])]) - - it "errors with bad shas" $ - fetchDiffsOutput summaryText (args "test/fixtures/git/examples/all-languages.git" "dead" "beef" ["methods.rb"] Renderer.Summary) - `shouldThrow` (== Git.BackendError "Could not lookup dead: Object not found - no match for prefix (dead000000000000000000000000000000000000)") - - it "errors with bad repo path" $ - fetchDiffsOutput summaryText (args "test/fixtures/git/examples/not-a-repo.git" "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe" ["methods.rb"] Renderer.Summary) - `shouldThrow` errorCall "Could not open repository \"test/fixtures/git/examples/not-a-repo.git\"" - -fetchDiffsOutput :: (Object -> Text) -> Arguments -> IO (Maybe (Map Text Value), Maybe (Map Text [Text])) -fetchDiffsOutput f arguments = do - diffs <- fetchDiffs arguments - let json = fromJust . decode . E.encodeUtf8 $ T.fromChunks [concatOutputs diffs] - pure (errors json, summaries f json) - --- Diff Summaries payloads look like this: --- { --- "changes": { --- "methods.rb": [ --- { --- "span":{"insert":{"start":[1,1],"end":[2,4]}}, --- "summary":"Added the 'foo()' method" --- } --- ] --- }, --- "errors":{} --- } --- --- TOC Summaries payloads look like this: --- { --- "changes": { --- "methods.rb": [ --- { --- "span":{"start":[1,1],"end":[2,4]}, --- "category":"Method", --- "term":"foo", --- "changeType":"added" --- } --- ] --- }, --- "errors":{} --- } -summaries :: (Object -> Text) -> Object -> Maybe (Map Text [Text]) -summaries f = parseMaybe $ \o -> do - changes <- o .: "changes" :: Parser (Map Text (V.Vector Object)) - xs <- for (toList changes) $ \(path, s) -> do - let ys = fmap f s - pure (path, V.toList ys) - pure $ fromList xs - -summaryText :: Object -> Text -summaryText o = fromMaybe (panic "key 'summary' not found") $ - parseMaybe (.: "summary") o - -termText :: Object -> Text -termText o = fromMaybe (panic "key 'term' not found") $ - parseMaybe (.: "term") o - -errors :: Object -> Maybe (Map Text Value) -errors = parseMaybe (.: "errors") diff --git a/test/Spec.hs b/test/Spec.hs index 8f178ce45..d2d4562e9 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -12,7 +12,6 @@ import qualified RangeSpec import qualified Source.Spec import qualified TermSpec import qualified TOCSpec -import qualified SemanticDiffSpec import qualified DiffCommandSpec import qualified IntegrationSpec import Test.Hspec @@ -31,5 +30,4 @@ main = hspec . parallel $ do describe "Term" TermSpec.spec describe "TOC" TOCSpec.spec describe "DiffCommand" DiffCommandSpec.spec - describe "SemanticDiff" SemanticDiffSpec.spec describe "Integration" IntegrationSpec.spec