1
1
mirror of https://github.com/github/semantic.git synced 2024-12-27 17:05:33 +03:00
semantic/test/Diffing/Algorithm/RWS/Spec.hs

44 lines
2.0 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.Bifunctor
2017-09-27 19:41:41 +03:00
import Data.Diff
import Data.Functor.Listable (ListableSyntax)
import Data.Record
2018-05-02 19:00:15 +03:00
import Data.Sum
import qualified Data.Syntax as Syntax
2017-09-27 19:37:37 +03:00
import Data.Term
import Data.These
import Diffing.Algorithm
2017-11-27 19:51:39 +03:00
import Diffing.Algorithm.RWS
2018-07-20 17:04:34 +03:00
import Diffing.Interpreter.Spec (afterTerm, beforeTerm)
import Test.Hspec.LeanCheck
2018-06-29 23:17:27 +03:00
import SpecHelpers
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" $
2018-05-16 23:15:06 +03:00
\ (term, p, q) -> pqGramDecorator (positively p) (positively q) (term :: Term ListableSyntax (Record '[])) `shouldSatisfy` all ((== positively p) . length . stem . rhead)
prop "produces grams with bases of the specified width" $
2018-05-16 23:15:06 +03:00
\ (term, p, q) -> pqGramDecorator (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 '[])])
2018-05-17 01:25:02 +03:00
wrap = termIn Nil . inject
diff = merge (Nil, Nil) (inject (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" $
2018-05-17 01:25:02 +03:00
let (a, b) = (decorate (termIn Nil (inject [ termIn Nil (inject (Syntax.Identifier "a")) ])), decorate (termIn Nil (inject [ termIn Nil (inject (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 ]
2018-05-16 23:21:02 +03:00
where decorate = defaultFeatureVectorDecorator
diffThese = these deleting inserting replacing