1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 14:54:16 +03:00
semantic/test/Diffing/Algorithm/RWS/Spec.hs

45 lines
2.1 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
2017-11-27 21:48:43 +03:00
module Diffing.Algorithm.RWS.Spec where
2017-11-27 22:32:13 +03:00
import Analysis.Decorator
import Data.Array.IArray
import Data.Bifunctor
2017-09-27 19:41:41 +03:00
import Data.Diff
import Data.Functor.Listable (ListableSyntax)
import Data.Record
import qualified Data.Syntax as Syntax
2017-09-27 19:37:37 +03:00
import Data.Term
import Data.These
import Data.Union
import Diffing.Algorithm
2017-11-27 19:51:39 +03:00
import Diffing.Algorithm.RWS
import Diffing.Interpreter
import Test.Hspec
import Test.Hspec.LeanCheck
spec :: Spec
spec = parallel $ do
2016-08-10 18:32:42 +03:00
let positively = succ . abs
2016-08-09 23:23:38 +03:00
describe "pqGramDecorator" $ do
prop "produces grams with stems of the specified length" $
\ (term, p, q) -> pqGramDecorator constructorNameAndConstantFields (positively p) (positively q) (term :: Term ListableSyntax (Record '[])) `shouldSatisfy` all ((== positively p) . length . stem . rhead)
prop "produces grams with bases of the specified width" $
\ (term, p, q) -> pqGramDecorator constructorNameAndConstantFields (positively p) (positively q) (term :: Term ListableSyntax (Record '[])) `shouldSatisfy` all ((== positively q) . length . base . rhead)
2016-06-27 22:37:32 +03:00
describe "rws" $ do
prop "produces correct diffs" $
\ (as, bs) -> let tas = decorate <$> (as :: [Term ListableSyntax (Record '[])])
tbs = decorate <$> (bs :: [Term ListableSyntax (Record '[])])
wrap = termIn Nil . inj
diff = merge (Nil, Nil) (inj (stripDiff . diffThese <$> rws comparableTerms (equalTerms comparableTerms) tas tbs)) in
(beforeTerm diff, afterTerm diff) `shouldBe` (Just (wrap (stripTerm <$> tas)), Just (wrap (stripTerm <$> tbs)))
2016-08-11 18:53:51 +03:00
it "produces unbiased insertions within branches" $
let (a, b) = (decorate (termIn Nil (inj [ termIn Nil (inj (Syntax.Identifier "a")) ])), decorate (termIn Nil (inj [ termIn Nil (inj (Syntax.Identifier "b")) ]))) in
fmap (bimap stripTerm stripTerm) (rws comparableTerms (equalTerms comparableTerms) [ b ] [ a, b ]) `shouldBe` fmap (bimap stripTerm stripTerm) [ That a, These b b ]
where decorate = defaultFeatureVectorDecorator constructorNameAndConstantFields
diffThese = these deleting inserting replacing