2016-06-27 22:28:11 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2017-11-27 22:03:45 +03:00
|
|
|
module Diffing.Interpreter.Spec where
|
2015-12-16 22:18:09 +03:00
|
|
|
|
2017-09-27 19:41:41 +03:00
|
|
|
import Data.Diff
|
2017-01-09 22:20:11 +03:00
|
|
|
import Data.Functor.Listable
|
2016-06-17 20:33:50 +03:00
|
|
|
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
|
2017-11-27 19:45:08 +03:00
|
|
|
import Diffing.Interpreter
|
2018-03-08 04:01:47 +03:00
|
|
|
import qualified Data.Syntax as Syntax
|
2016-09-15 00:45:23 +03:00
|
|
|
import Test.Hspec (Spec, describe, it, parallel)
|
|
|
|
import Test.Hspec.Expectations.Pretty
|
2017-01-09 22:20:11 +03:00
|
|
|
import Test.Hspec.LeanCheck
|
2015-12-16 22:18:09 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
2016-06-27 20:08:12 +03:00
|
|
|
spec = parallel $ do
|
2017-10-23 18:58:31 +03:00
|
|
|
describe "diffTerms" $ do
|
2015-12-16 22:18:09 +03:00
|
|
|
it "returns a replacement when comparing two unicode equivalent terms" $
|
2018-05-02 23:10:56 +03:00
|
|
|
let termA = termIn Nil (injectSum (Syntax.Identifier "t\776"))
|
|
|
|
termB = termIn Nil (injectSum (Syntax.Identifier "\7831")) in
|
2017-09-26 16:48:59 +03:00
|
|
|
diffTerms termA termB `shouldBe` replacing termA (termB :: Term ListableSyntax (Record '[]))
|
2016-06-27 22:28:11 +03:00
|
|
|
|
|
|
|
prop "produces correct diffs" $
|
2017-09-26 16:48:59 +03:00
|
|
|
\ 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)
|
2016-06-30 20:34:31 +03:00
|
|
|
|
2017-10-23 18:58:43 +03:00
|
|
|
prop "produces identity diffs for equal terms " $
|
2017-09-26 16:48:59 +03:00
|
|
|
\ a -> let diff = diffTerms a a :: Diff ListableSyntax (Record '[]) (Record '[]) in
|
2017-10-23 16:37:47 +03:00
|
|
|
length (diffPatches diff) `shouldBe` 0
|
2016-08-11 19:23:34 +03:00
|
|
|
|
2016-08-12 19:16:48 +03:00
|
|
|
it "produces unbiased insertions within branches" $
|
2018-05-02 23:10:56 +03:00
|
|
|
let term s = termIn Nil (injectSum [ termIn Nil (injectSum (Syntax.Identifier s)) ]) :: Term ListableSyntax (Record '[])
|
|
|
|
wrap = termIn Nil . injectSum in
|
|
|
|
diffTerms (wrap [ term "b" ]) (wrap [ term "a", term "b" ]) `shouldBe` merge (Nil, Nil) (injectSum [ inserting (term "a"), merging (term "b") ])
|
2017-09-28 18:24:51 +03:00
|
|
|
|
|
|
|
prop "compares nodes against context" $
|
2018-05-02 23:10:56 +03:00
|
|
|
\ a b -> diffTerms a (termIn Nil (injectSum (Syntax.Context (pure b) a))) `shouldBe` insertF (In Nil (injectSum (Syntax.Context (pure (inserting b)) (merging (a :: Term ListableSyntax (Record '[]))))))
|
2017-10-23 18:58:52 +03:00
|
|
|
|
|
|
|
prop "diffs forward permutations as changes" $
|
2018-05-02 23:10:56 +03:00
|
|
|
\ a -> let wrap = termIn Nil . injectSum
|
2017-10-23 18:58:52 +03:00
|
|
|
b = wrap [a]
|
|
|
|
c = wrap [a, b] in
|
2018-05-02 23:10:56 +03:00
|
|
|
diffTerms (wrap [a, b, c]) (wrap [c, a, b :: Term ListableSyntax (Record '[])]) `shouldBe` merge (Nil, Nil) (injectSum [ inserting c, merging a, merging b, deleting c ])
|
2017-10-23 18:58:52 +03:00
|
|
|
|
|
|
|
prop "diffs backward permutations as changes" $
|
2018-05-02 23:10:56 +03:00
|
|
|
\ a -> let wrap = termIn Nil . injectSum
|
2017-10-23 18:58:52 +03:00
|
|
|
b = wrap [a]
|
|
|
|
c = wrap [a, b] in
|
2018-05-02 23:10:56 +03:00
|
|
|
diffTerms (wrap [a, b, c]) (wrap [b, c, a :: Term ListableSyntax (Record '[])]) `shouldBe` merge (Nil, Nil) (injectSum [ deleting a, merging b, merging c, inserting a ])
|