1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 14:21:31 +03:00
semantic/test/DiffSpec.hs

67 lines
2.8 KiB
Haskell

{-# LANGUAGE DataKinds #-}
module DiffSpec where
import Category
import Data.Functor.Both
import Data.Functor.Foldable (cata)
import Data.Functor.Listable (ListableSyntax)
import Data.Record
import Data.Union
import Diff
import Interpreter
import RWS
import Term
import Test.Hspec
import Test.Hspec.LeanCheck
spec :: Spec
spec = parallel $ do
prop "equality is reflexive" $
\ diff -> diff `shouldBe` (diff :: Diff ListableSyntax (Record '[]) (Record '[]))
prop "equal terms produce identity diffs" $
\ term -> diffCost (diffTerms term (term :: Term ListableSyntax (Record '[]))) `shouldBe` 0
describe "beforeTerm" $ do
prop "recovers the before term" $
\ a b -> let diff = diffTerms a b :: Diff ListableSyntax (Record '[]) (Record '[]) in
beforeTerm diff `shouldBe` Just a
prop "recovers the before term from forward permutations" $
\ a b -> let wrap = termIn Nil . inj
c = wrap [a] in
beforeTerm (diffTerms (wrap [a, b, c]) (wrap [c, a, b :: Term ListableSyntax (Record '[])])) `shouldBe` Just (wrap [a, b, c])
prop "recovers the before term from backward permutations" $
\ a b -> let wrap = termIn Nil . inj
c = wrap [a] in
beforeTerm (diffTerms (wrap [a, b, c]) (wrap [b, c, a :: Term ListableSyntax (Record '[])])) `shouldBe` Just (wrap [a, b, c])
describe "afterTerm" $ do
prop "recovers the after term" $
\ a b -> let diff = diffTerms a b :: Diff ListableSyntax (Record '[]) (Record '[]) in
afterTerm diff `shouldBe` Just b
prop "recovers the after term from forward permutations" $
\ a b -> let wrap = termIn Nil . inj
c = wrap [a] in
afterTerm (diffTerms (wrap [a, b, c]) (wrap [c, a, b :: Term ListableSyntax (Record '[])])) `shouldBe` Just (wrap [c, a, b])
prop "recovers the after term from backward permutations" $
\ a b -> let wrap = termIn Nil . inj
c = wrap [a] in
afterTerm (diffTerms (wrap [a, b, c]) (wrap [b, c, a :: Term ListableSyntax (Record '[])])) `shouldBe` Just (wrap [b, c, a])
prop "forward permutations are changes" $
\ a b -> let wrap = termIn Nil . inj
c = wrap [a] in
diffTerms (wrap [a, b, c]) (wrap [c, a, b :: Term ListableSyntax (Record '[])]) `shouldBe` merge (Nil, Nil) (inj [ inserting c, mergeTerm a, mergeTerm b, deleting c ])
prop "backward permutations are changes" $
\ a b -> let wrap = termIn Nil . inj
c = wrap [a] in
diffTerms (wrap [a, b, c]) (wrap [b, c, a :: Term ListableSyntax (Record '[])]) `shouldBe` merge (Nil, Nil) (inj [ deleting a, mergeTerm b, mergeTerm c, inserting a ])
mergeTerm :: Functor syntax => Term syntax ann -> Diff syntax ann ann
mergeTerm = cata (\ (In ann syntax) -> merge (ann, ann) syntax)