1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 08:54:07 +03:00
semantic/test/InterpreterSpec.hs

45 lines
2.0 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
module InterpreterSpec where
2016-06-27 22:28:04 +03:00
import Category
import Data.Functor.Foldable
import Data.Functor.Listable
import Data.RandomWalkSimilarity
import Data.Record
import Data.String
import Diff
2016-10-13 02:19:36 +03:00
import Diffing
import Info
import Interpreter
import Patch
2016-06-27 22:28:04 +03:00
import Prologue
import Syntax
import Term
import Test.Hspec (Spec, describe, it, parallel)
import Test.Hspec.Expectations.Pretty
import Test.Hspec.LeanCheck
spec :: Spec
spec = parallel $ do
describe "interpret" $ do
let decorate = defaultFeatureVectorDecorator (category . headF)
let compare = (==) `on` category . extract
it "returns a replacement when comparing two unicode equivalent terms" $
2017-01-19 23:36:04 +03:00
let termA = cofree $ (StringLiteral .: Nil) :< Leaf ("t\776" :: String)
termB = cofree $ (StringLiteral .: Nil) :< Leaf "\7831" in
2016-10-13 02:19:36 +03:00
stripDiff (diffTerms wrap compare diffCost getLabel (decorate termA) (decorate termB)) `shouldBe` replacing termA termB
prop "produces correct diffs" $
\ a b -> let diff = stripDiff $ diffTerms wrap compare diffCost getLabel (decorate (unListableF a)) (decorate (unListableF b :: SyntaxTerm String '[Category])) in
(beforeTerm diff, afterTerm diff) `shouldBe` (Just (unListableF a), Just (unListableF b))
prop "constructs zero-cost diffs of equal terms" $
\ a -> let term = decorate (unListableF a :: SyntaxTerm String '[Category])
2016-10-13 02:19:36 +03:00
diff = diffTerms wrap compare diffCost getLabel term term in
diffCost diff `shouldBe` 0
it "produces unbiased insertions within branches" $
2017-01-19 23:36:04 +03:00
let term s = decorate (cofree ((StringLiteral .: Nil) :< Indexed [ cofree ((StringLiteral .: Nil) :< Leaf s) ]))
root = cofree . ((pure 0 .: Program .: Nil) :<) . Indexed in
stripDiff (diffTerms wrap compare diffCost getLabel (root [ term "b" ]) (root [ term "a", term "b" ])) `shouldBe` wrap (pure (Program .: Nil) :< Indexed [ inserting (stripTerm (term "a")), cata wrap (fmap pure (stripTerm (term "b"))) ])