1
1
mirror of https://github.com/github/semantic.git synced 2024-12-26 08:25:19 +03:00
semantic/test/AlignmentSpec.hs

69 lines
3.5 KiB
Haskell
Raw Normal View History

2016-02-28 22:05:19 +03:00
module AlignmentSpec where
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck hiding (Fixed)
import Data.Text.Arbitrary ()
import Alignment
import ArbitraryTerm (arbitraryLeaf)
import Control.Arrow
2016-05-04 22:15:25 +03:00
import Control.Comonad.Trans.Cofree
import Control.Monad.Trans.Free hiding (unfold)
import Data.Adjoined
2016-03-03 19:39:23 +03:00
import Data.Copointed
import Data.Functor.Both as Both
import Diff
2016-03-31 00:33:07 +03:00
import Info
import qualified Data.Maybe as Maybe
2015-12-30 18:26:40 +03:00
import Data.Functor.Identity
2015-12-23 01:22:07 +03:00
import Line
2016-03-03 22:59:33 +03:00
import Patch
2016-03-01 03:39:04 +03:00
import Prelude hiding (fst, snd)
import qualified Prelude
import Range
import Source hiding ((++), fromList)
import qualified Source
import SplitDiff
import Syntax
spec :: Spec
spec = parallel $ do
describe "splitDiffByLines" $ do
prop "preserves line counts in equal sources" $
2015-12-22 23:55:59 +03:00
\ source ->
2016-05-04 22:15:25 +03:00
length (splitDiffByLines (pure source) (free . Free $ (pure $ Info (totalRange source) mempty 1) :< (Indexed . Prelude.fst $ foldl combineIntoLeaves ([], 0) source))) `shouldBe` length (filter (== '\n') $ toString source) + 1
prop "produces the maximum line count in inequal sources" $
2016-04-12 12:00:39 +03:00
\ sources -> let ranges = actualLineRanges <$> (totalRange <$> sources) <*> sources in
2016-05-04 22:15:25 +03:00
length (splitDiffByLines sources (free . Free $ ((\ s -> Info (totalRange s) mempty 0) <$> sources) :< (Indexed $ leafWithRangesInSources sources <$> runBothWith (zipWith both) ranges))) `shouldBe` runBothWith max ((+ 1) . length . filter (== '\n') . toString <$> sources)
2016-03-03 19:39:23 +03:00
describe "splitAbstractedTerm" $ do
prop "preserves line count" $
\ source -> let range = totalRange source in
2016-05-04 22:15:25 +03:00
splitAbstractedTerm ((cofree .) . (:<)) (Identity source) (Identity (Info range mempty 0) :< Leaf source) `shouldBe` (Identity . lineMap (fmap (cofree . (:< Leaf source) . (\ r -> Info r mempty 0) &&& id)) <$> linesInRangeOfSource range source)
2016-05-04 22:15:25 +03:00
let makeTerm = ((free .) . (Free .) . (:<)) :: Info -> Syntax (Source Char) (SplitDiff (Source Char) Info) -> SplitDiff (Source Char) Info
prop "outputs one row for single-line unchanged leaves" $
forAll (arbitraryLeaf `suchThat` isOnSingleLine) $
2016-05-04 22:15:25 +03:00
\ (source, (Info range categories _), syntax) -> splitAbstractedTerm makeTerm (pure source) (pure (Info range categories 0) :< syntax) `shouldBe` fromList [
2016-04-12 12:00:39 +03:00
both (pure (makeTerm (Info range categories 0) $ Leaf source, Range 0 (length source))) (pure (makeTerm (Info range categories 0) $ Leaf source, Range 0 (length source))) ]
prop "outputs one row for single-line empty unchanged indexed nodes" $
forAll (arbitrary `suchThat` (\ a -> filter (/= '\n') (toString a) == toString a)) $
2016-05-04 22:15:25 +03:00
\ source -> splitAbstractedTerm makeTerm (pure source) (pure (Info (totalRange source) mempty 0) :< Indexed []) `shouldBe` fromList [
2016-04-12 12:00:39 +03:00
both (pure (makeTerm (Info (totalRange source) mempty 0) $ Indexed [], Range 0 (length source))) (pure (makeTerm (Info (totalRange source) mempty 0) $ Indexed [], Range 0 (length source))) ]
where
isOnSingleLine (a, _, _) = filter (/= '\n') (toString a) == toString a
2016-05-04 22:15:25 +03:00
combineIntoLeaves (leaves, start) char = (leaves ++ [ free . Free $ (Info <$> pure (Range start $ start + 1) <*> mempty <*> pure 1) :< Leaf [ char ] ], start + 1)
2016-05-04 22:15:25 +03:00
leafWithRangesInSources sources ranges = free . Free $ (Info <$> ranges <*> pure mempty <*> pure 1) :< (Leaf $ runBothWith (++) (toString <$> sources))
2015-12-23 00:52:20 +03:00
2016-04-12 11:54:32 +03:00
leafWithRangeInSource source range = Info range mempty 1 :< Leaf source
patchWithBoth (Insert ()) = Insert . snd
patchWithBoth (Delete ()) = Delete . fst
patchWithBoth (Replace () ()) = runBothWith Replace