2016-06-27 20:15:03 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2017-03-01 02:35:43 +03:00
|
|
|
module DiffSpec where
|
2016-06-27 20:12:06 +03:00
|
|
|
|
2016-06-27 20:15:03 +03:00
|
|
|
import Category
|
2017-06-01 18:10:47 +03:00
|
|
|
import Data.Functor.Both
|
2017-09-26 16:56:47 +03:00
|
|
|
import Data.Functor.Foldable (cata)
|
2017-09-09 16:59:03 +03:00
|
|
|
import Data.Functor.Listable ()
|
2017-09-14 02:14:01 +03:00
|
|
|
import Data.Record
|
2017-04-24 22:53:11 +03:00
|
|
|
import RWS
|
2016-06-27 20:12:06 +03:00
|
|
|
import Diff
|
2017-02-08 19:15:37 +03:00
|
|
|
import Info
|
2016-06-27 20:15:03 +03:00
|
|
|
import Interpreter
|
2017-09-14 02:14:01 +03:00
|
|
|
import Syntax
|
2017-01-08 07:17:05 +03:00
|
|
|
import Term
|
2016-06-27 20:12:06 +03:00
|
|
|
import Test.Hspec
|
2017-01-08 07:17:05 +03:00
|
|
|
import Test.Hspec.LeanCheck
|
2016-06-27 20:12:06 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2017-09-09 13:50:29 +03:00
|
|
|
let decorate = defaultFeatureVectorDecorator (category . termAnnotation)
|
2016-06-27 20:18:20 +03:00
|
|
|
prop "equality is reflexive" $
|
2017-09-14 16:41:52 +03:00
|
|
|
\ diff -> diff `shouldBe` (diff :: Diff Syntax (Record '[Category]) (Record '[Category]))
|
2016-06-27 20:18:20 +03:00
|
|
|
|
2016-07-13 00:29:47 +03:00
|
|
|
prop "equal terms produce identity diffs" $
|
2017-09-14 02:14:01 +03:00
|
|
|
\ a -> let term = decorate (a :: Term Syntax (Record '[Category])) in
|
2017-09-14 02:34:27 +03:00
|
|
|
diffCost (diffTerms term term) `shouldBe` 0
|
2016-06-27 20:18:20 +03:00
|
|
|
|
2016-06-27 20:23:43 +03:00
|
|
|
describe "beforeTerm" $ do
|
|
|
|
prop "recovers the before term" $
|
2017-09-14 16:41:52 +03:00
|
|
|
\ a b -> let diff = diffTerms a b :: Diff Syntax (Record '[Category]) (Record '[Category]) in
|
2017-09-09 16:18:08 +03:00
|
|
|
beforeTerm diff `shouldBe` Just a
|
2016-06-27 20:15:03 +03:00
|
|
|
|
2016-06-27 20:24:50 +03:00
|
|
|
describe "afterTerm" $ do
|
|
|
|
prop "recovers the after term" $
|
2017-09-14 16:41:52 +03:00
|
|
|
\ a b -> let diff = diffTerms a b :: Diff Syntax (Record '[Category]) (Record '[Category]) in
|
2017-09-09 16:18:08 +03:00
|
|
|
afterTerm diff `shouldBe` Just b
|
2017-09-26 16:56:47 +03:00
|
|
|
|
|
|
|
prop "forward permutations are changes" $
|
|
|
|
\ a b -> let wrap = termIn (Program :. Nil) . Indexed
|
2017-09-26 16:59:31 +03:00
|
|
|
c = wrap [a, b] in
|
2017-09-26 16:56:47 +03:00
|
|
|
diffTerms (wrap [a, b, c]) (wrap [c, a, b :: Term Syntax (Record '[Category])]) `shouldBe` merge (Program :. Nil, Program :. Nil) (Indexed [ inserting c, mergeTerm a, mergeTerm b, deleting c ])
|
|
|
|
|
|
|
|
prop "backward permutations are changes" $
|
|
|
|
\ a b -> let wrap = termIn (Program :. Nil) . Indexed
|
2017-09-26 16:59:31 +03:00
|
|
|
c = wrap [a, b] in
|
2017-09-26 16:56:47 +03:00
|
|
|
diffTerms (wrap [a, b, c]) (wrap [b, c, a :: Term Syntax (Record '[Category])]) `shouldBe` merge (Program :. Nil, Program :. Nil) (Indexed [ 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)
|