2016-06-27 22:28:11 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2015-12-16 22:18:09 +03:00
|
|
|
module InterpreterSpec where
|
|
|
|
|
2016-06-27 22:28:04 +03:00
|
|
|
import Category
|
2016-08-11 21:32:10 +03:00
|
|
|
import Data.Align.Generic
|
2016-08-10 16:26:58 +03:00
|
|
|
import Data.RandomWalkSimilarity
|
2016-06-17 20:33:50 +03:00
|
|
|
import Data.Record
|
2016-08-10 16:26:58 +03:00
|
|
|
import Diff
|
|
|
|
import Info
|
2016-06-27 20:08:12 +03:00
|
|
|
import Interpreter
|
2015-12-16 22:18:09 +03:00
|
|
|
import Patch
|
2016-06-27 22:28:04 +03:00
|
|
|
import Prologue
|
|
|
|
import Syntax
|
2016-06-27 22:28:11 +03:00
|
|
|
import Term.Arbitrary
|
2015-12-16 22:18:09 +03:00
|
|
|
import Test.Hspec
|
2016-06-27 22:28:11 +03:00
|
|
|
import Test.Hspec.QuickCheck
|
2016-08-11 20:33:33 +03:00
|
|
|
import Test.QuickCheck
|
2015-12-16 22:18:09 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
2016-06-27 20:08:12 +03:00
|
|
|
spec = parallel $ do
|
2016-06-27 20:03:58 +03:00
|
|
|
describe "interpret" $ do
|
2016-08-10 16:26:58 +03:00
|
|
|
let decorate = featureVectorDecorator (category . headF) 2 2 15
|
2016-08-11 19:32:44 +03:00
|
|
|
let compare = ((==) `on` category . extract)
|
2015-12-16 22:18:09 +03:00
|
|
|
it "returns a replacement when comparing two unicode equivalent terms" $
|
2016-08-11 19:48:41 +03:00
|
|
|
let termA = cofree $ (StringLiteral .: RNil) :< Leaf ("t\776" :: Text)
|
|
|
|
termB = cofree $ (StringLiteral .: RNil) :< Leaf "\7831" in
|
2016-08-11 21:42:17 +03:00
|
|
|
stripDiff (diffTerms wrap compare diffCost (decorate termA) (decorate termB)) `shouldBe` replacing termA termB
|
2016-06-27 22:28:11 +03:00
|
|
|
|
|
|
|
prop "produces correct diffs" $
|
2016-08-11 19:48:27 +03:00
|
|
|
\ a b -> let diff = stripDiff $ diffTerms wrap compare diffCost (decorate (toTerm a)) (decorate (toTerm (b :: ArbitraryTerm Text (Record '[Category])))) in
|
|
|
|
(beforeTerm diff, afterTerm diff) `shouldBe` (Just (toTerm a), Just (toTerm b))
|
2016-06-30 20:34:31 +03:00
|
|
|
|
|
|
|
prop "constructs zero-cost diffs of equal terms" $
|
2016-08-10 16:26:58 +03:00
|
|
|
\ a -> let term = decorate (toTerm (a :: ArbitraryTerm Text (Record '[Category])))
|
2016-08-11 19:32:44 +03:00
|
|
|
diff = diffTerms wrap compare diffCost term term in
|
2016-06-30 20:34:31 +03:00
|
|
|
diffCost diff `shouldBe` 0
|
2016-08-11 19:23:34 +03:00
|
|
|
|
2016-08-11 19:34:32 +03:00
|
|
|
let reverse' diff = case runFree diff of
|
|
|
|
Free (h :< Indexed children) -> wrap (h :< Indexed (reverse children))
|
|
|
|
Free c -> wrap c
|
|
|
|
Pure a -> pure a
|
2016-08-11 20:00:54 +03:00
|
|
|
let toTerm' c (ArbitraryTerm r f) = decorate (toTerm (ArbitraryTerm (setCategory r c) f))
|
2016-08-11 20:05:36 +03:00
|
|
|
let root = cofree . ((pure 0 .: Program .: RNil) :<) . Indexed
|
2016-08-11 21:32:10 +03:00
|
|
|
prop "produces unbiased deletions" . forAll (arbitrary `suchThat` \ (a, b, _) -> isNothing ((galign `on` syntax) a b)) $
|
2016-08-11 20:33:33 +03:00
|
|
|
\ (a, b, c) -> let (a', b') = (toTerm' c a, toTerm' c (b :: ArbitraryTerm Text (Record '[Category]))) in
|
2016-08-11 19:45:40 +03:00
|
|
|
stripDiff (diffTerms wrap compare diffCost (root [ a', b' ]) (root [ a' ])) `shouldBe` reverse' (stripDiff (diffTerms wrap compare diffCost (root [ b', a' ]) (root [ a' ])))
|
2016-08-11 20:03:22 +03:00
|
|
|
|
2016-08-11 21:32:10 +03:00
|
|
|
prop "produces unbiased insertions" . forAll (arbitrary `suchThat` \ (a, b, _) -> isNothing ((galign `on` syntax) a b)) $
|
2016-08-11 20:33:33 +03:00
|
|
|
\ (a, b, c) -> let (a', b') = (toTerm' c a, toTerm' c (b :: ArbitraryTerm Text (Record '[Category]))) in
|
2016-08-11 20:03:22 +03:00
|
|
|
stripDiff (diffTerms wrap compare diffCost (root [ a' ]) (root [ a', b' ])) `shouldBe` reverse' (stripDiff (diffTerms wrap compare diffCost (root [ a' ]) (root [ b', a' ])))
|