1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00
semantic/test/Diffing/Interpreter/Spec.hs

84 lines
3.9 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
2017-11-27 22:03:45 +03:00
module Diffing.Interpreter.Spec where
2018-07-20 16:56:33 +03:00
import Control.Applicative ((<|>))
2017-09-27 19:41:41 +03:00
import Data.Diff
2018-07-20 16:56:33 +03:00
import Data.Foldable (asum)
2018-07-20 17:04:34 +03:00
import Data.Functor.Foldable (cata)
import Data.Functor.Listable
2018-06-04 19:18:02 +03:00
import Data.Maybe
2018-07-20 16:53:52 +03:00
import Data.Mergeable
2018-07-20 17:04:34 +03:00
import Data.Patch (after, before)
import Data.Record
2018-05-02 19:00:15 +03:00
import Data.Sum
2017-09-27 19:37:37 +03:00
import Data.Term
import Data.These
import Diffing.Interpreter
import qualified Data.Syntax as Syntax
import Test.Hspec (Spec, describe, it, parallel)
import Test.Hspec.Expectations.Pretty
import Test.Hspec.LeanCheck
2018-06-04 19:18:02 +03:00
import Test.LeanCheck.Core
2018-06-29 23:17:27 +03:00
import SpecHelpers
spec :: Spec
spec = parallel $ do
2017-10-23 18:58:31 +03:00
describe "diffTerms" $ do
it "returns a replacement when comparing two unicode equivalent terms" $
2018-05-17 01:25:02 +03:00
let termA = termIn Nil (inject (Syntax.Identifier "t\776"))
termB = termIn Nil (inject (Syntax.Identifier "\7831")) in
diffTerms termA termB `shouldBe` replacing termA (termB :: Term ListableSyntax (Record '[]))
prop "produces correct diffs" $
\ a b -> let diff = diffTerms a b :: Diff ListableSyntax (Record '[]) (Record '[]) in
2017-09-14 02:34:27 +03:00
(beforeTerm diff, afterTerm diff) `shouldBe` (Just a, Just b)
2017-10-23 18:58:43 +03:00
prop "produces identity diffs for equal terms " $
\ a -> let diff = diffTerms a a :: Diff ListableSyntax (Record '[]) (Record '[]) in
length (diffPatches diff) `shouldBe` 0
it "produces unbiased insertions within branches" $
2018-05-17 01:25:02 +03:00
let term s = termIn Nil (inject [ termIn Nil (inject (Syntax.Identifier s)) ]) :: Term ListableSyntax (Record '[])
wrap = termIn Nil . inject in
diffTerms (wrap [ term "b" ]) (wrap [ term "a", term "b" ]) `shouldBe` merge (Nil, Nil) (inject [ inserting (term "a"), merging (term "b") ])
2018-06-04 19:18:02 +03:00
let noContext :: Term ListableSyntax a -> Bool
noContext = isNothing . project @Syntax.Context . termOut
prop "compares nodes against context" . forAll (filterT (noContext . fst) tiers) $
\ (a, b) -> diffTerms a (termIn Nil (inject (Syntax.Context (pure b) a))) `shouldBe` insertF (In Nil (inject (Syntax.Context (pure (inserting b)) (merging (a :: Term ListableSyntax (Record '[]))))))
prop "diffs forward permutations as changes" $
2018-05-17 01:25:02 +03:00
\ a -> let wrap = termIn Nil . inject
b = wrap [a]
c = wrap [a, b] in
2018-05-17 01:25:02 +03:00
diffTerms (wrap [a, b, c]) (wrap [c, a, b :: Term ListableSyntax (Record '[])]) `shouldBe` merge (Nil, Nil) (inject [ inserting c, merging a, merging b, deleting c ])
prop "diffs backward permutations as changes" $
2018-05-17 01:25:02 +03:00
\ a -> let wrap = termIn Nil . inject
b = wrap [a]
c = wrap [a, b] in
2018-05-17 01:25:02 +03:00
diffTerms (wrap [a, b, c]) (wrap [b, c, a :: Term ListableSyntax (Record '[])]) `shouldBe` merge (Nil, Nil) (inject [ deleting a, merging b, merging c, inserting a ])
describe "diffTermPair" $ do
prop "produces an Insert when the first term is missing" $ do
\ after -> let diff = diffTermPair (That after) :: Diff ListableSyntax (Record '[]) (Record '[]) in
diff `shouldBe` inserting after
prop "produces a Delete when the second term is missing" $ do
\ before -> let diff = diffTermPair (This before) :: Diff ListableSyntax (Record '[]) (Record '[]) in
diff `shouldBe` deleting before
-- | Recover the before state of a diff.
beforeTerm :: (Foldable syntax, Mergeable syntax) => Diff syntax ann1 ann2 -> Maybe (Term syntax ann1)
beforeTerm = cata $ \ diff -> case diff of
Patch patch -> (before patch >>= \ (In a l) -> termIn a <$> sequenceAlt l) <|> (after patch >>= asum)
Merge (In (a, _) l) -> termIn a <$> sequenceAlt l
-- | Recover the after state of a diff.
afterTerm :: (Foldable syntax, Mergeable syntax) => Diff syntax ann1 ann2 -> Maybe (Term syntax ann2)
afterTerm = cata $ \ diff -> case diff of
Patch patch -> (after patch >>= \ (In b r) -> termIn b <$> sequenceAlt r) <|> (before patch >>= asum)
Merge (In (_, b) r) -> termIn b <$> sequenceAlt r