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

43 lines
1.9 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.RandomWalkSimilarity
import Data.Record
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.Arbitrary
import Test.Hspec (Spec, describe, it, parallel)
import Test.Hspec.Expectations.Pretty
import Test.Hspec.QuickCheck
spec :: Spec
spec = parallel $ do
describe "interpret" $ do
let decorate = defaultFeatureVectorDecorator (category . headF)
2016-08-11 19:32:44 +03:00
let compare = ((==) `on` category . extract)
it "returns a replacement when comparing two unicode equivalent terms" $
let termA = cofree $ (StringLiteral .: RNil) :< Leaf ("t\776" :: Text)
termB = cofree $ (StringLiteral .: RNil) :< 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" $
2016-10-13 02:19:36 +03:00
\ a b -> let diff = stripDiff $ diffTerms wrap compare diffCost getLabel (decorate (toTerm a)) (decorate (toTerm (b :: ArbitraryTerm Text (Record '[Category])))) in
(beforeTerm diff, afterTerm diff) `shouldBe` (Just (toTerm a), Just (toTerm b))
prop "constructs zero-cost diffs of equal terms" $
\ a -> let term = decorate (toTerm (a :: ArbitraryTerm Text (Record '[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" $
let term s = decorate (cofree ((StringLiteral .: RNil) :< Indexed [ cofree ((StringLiteral .: RNil) :< Leaf s) ]))
root = cofree . ((pure 0 .: Program .: RNil) :<) . Indexed in
2016-10-13 02:19:36 +03:00
stripDiff (diffTerms wrap compare diffCost getLabel (root [ term "b" ]) (root [ term "a", term "b" ])) `shouldBe` wrap (pure (Program .: RNil) :< Indexed [ inserting (stripTerm (term "a")), cata wrap (fmap pure (stripTerm (term "b"))) ])