2016-02-28 22:05:19 +03:00
|
|
|
module AlignmentSpec where
|
2015-12-17 21:08:47 +03:00
|
|
|
|
|
|
|
import Test.Hspec
|
|
|
|
import Test.Hspec.QuickCheck
|
|
|
|
import Test.QuickCheck hiding (Fixed)
|
2015-12-18 23:03:54 +03:00
|
|
|
import Data.Text.Arbitrary ()
|
|
|
|
|
2016-02-28 22:01:56 +03:00
|
|
|
import Alignment
|
2016-03-15 18:17:47 +03:00
|
|
|
import ArbitraryTerm (arbitraryLeaf)
|
2016-03-03 21:41:56 +03:00
|
|
|
import Control.Arrow
|
2015-12-17 21:08:47 +03:00
|
|
|
import Control.Comonad.Cofree
|
2016-04-01 17:13:22 +03:00
|
|
|
import Control.Monad.Free
|
2016-03-15 05:58:07 +03:00
|
|
|
import Data.Adjoined
|
2016-03-22 02:08:02 +03:00
|
|
|
import Data.Bifunctor.Join
|
|
|
|
import Data.Bifunctor.These
|
2016-02-29 18:30:33 +03:00
|
|
|
import Data.Functor.Both as Both
|
2015-12-18 23:03:54 +03:00
|
|
|
import Diff
|
2016-03-31 00:33:07 +03:00
|
|
|
import Info
|
2015-12-30 18:26:40 +03:00
|
|
|
import Data.Functor.Identity
|
2015-12-23 01:22:07 +03:00
|
|
|
import Line
|
2016-03-01 03:39:04 +03:00
|
|
|
import Prelude hiding (fst, snd)
|
|
|
|
import qualified Prelude
|
2015-12-18 23:03:54 +03:00
|
|
|
import Range
|
2016-03-15 05:58:07 +03:00
|
|
|
import Source hiding ((++), fromList)
|
2016-03-03 23:40:41 +03:00
|
|
|
import qualified Source
|
2016-03-05 03:53:47 +03:00
|
|
|
import SplitDiff
|
2015-12-17 21:08:47 +03:00
|
|
|
import Syntax
|
|
|
|
|
|
|
|
spec :: Spec
|
2016-01-05 18:38:51 +03:00
|
|
|
spec = parallel $ do
|
2016-03-05 03:53:28 +03:00
|
|
|
describe "splitDiffByLines" $ do
|
2015-12-22 23:48:37 +03:00
|
|
|
prop "preserves line counts in equal sources" $
|
2015-12-22 23:55:59 +03:00
|
|
|
\ source ->
|
2016-03-12 02:35:31 +03:00
|
|
|
length (splitDiffByLines (pure source) (Free $ Annotated (pure $ Info (totalRange source) mempty) (Indexed . Prelude.fst $ foldl combineIntoLeaves ([], 0) source))) `shouldBe` length (filter (== '\n') $ toString source) + 1
|
2015-12-22 23:48:37 +03:00
|
|
|
|
2015-12-23 00:10:05 +03:00
|
|
|
prop "produces the maximum line count in inequal sources" $
|
2016-02-29 05:43:47 +03:00
|
|
|
\ sources ->
|
2016-03-12 02:35:31 +03:00
|
|
|
length (splitDiffByLines sources (Free $ Annotated ((`Info` mempty) . totalRange <$> sources) (Indexed $ leafWithRangesInSources sources <$> runBothWith (zipWith both) (actualLineRanges <$> (totalRange <$> sources) <*> sources)))) `shouldBe` runBothWith max ((+ 1) . length . filter (== '\n') . toString <$> sources)
|
2015-12-23 00:10:05 +03:00
|
|
|
|
2016-03-03 19:39:23 +03:00
|
|
|
describe "splitAbstractedTerm" $ do
|
2015-12-23 00:34:32 +03:00
|
|
|
prop "preserves line count" $
|
2016-03-09 09:44:36 +03:00
|
|
|
\ source -> let range = totalRange source in
|
2016-04-05 01:25:44 +03:00
|
|
|
splitAbstractedTerm (:<) (Identity source) (Identity (Info range mempty)) (Leaf source) `shouldBe` (Identity . lineMap (fmap ((:< Leaf source) . (`Info` mempty) &&& id)) <$> linesInRangeOfSource range source)
|
2015-12-22 03:54:40 +03:00
|
|
|
|
2016-03-09 17:32:51 +03:00
|
|
|
let makeTerm = ((Free .) . Annotated) :: 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-03-15 05:58:07 +03:00
|
|
|
\ (source, info@(Info range categories), syntax) -> splitAbstractedTerm makeTerm (pure source) (pure $ Info range categories) syntax `shouldBe` fromList [
|
2016-03-09 17:32:51 +03:00
|
|
|
both (pure (makeTerm info $ Leaf source, Range 0 (length source))) (pure (makeTerm info $ Leaf source, Range 0 (length source))) ]
|
|
|
|
|
|
|
|
prop "outputs one row for single-line empty unchanged indexed nodes" $
|
2016-03-12 02:35:31 +03:00
|
|
|
forAll (arbitrary `suchThat` (\ a -> filter (/= '\n') (toString a) == toString a)) $
|
2016-03-15 05:58:07 +03:00
|
|
|
\ source -> splitAbstractedTerm makeTerm (pure source) (pure $ Info (totalRange source) mempty) (Indexed []) `shouldBe` fromList [
|
2016-03-09 17:32:51 +03:00
|
|
|
both (pure (makeTerm (Info (totalRange source) mempty) $ Indexed [], Range 0 (length source))) (pure (makeTerm (Info (totalRange source) mempty) $ Indexed [], Range 0 (length source))) ]
|
|
|
|
|
2016-03-24 20:53:58 +03:00
|
|
|
describe "groupChildrenByLine" $ do
|
2016-03-24 20:54:11 +03:00
|
|
|
it "produces symmetrical context" $
|
2016-03-24 20:53:58 +03:00
|
|
|
groupChildrenByLine (Join (These [Range 0 2, Range 2 4] [Range 0 2, Range 2 4])) [] `shouldBe`
|
|
|
|
[ Join (These (Range 0 2, [] :: [SplitDiff String Info])
|
|
|
|
(Range 0 2, []))
|
|
|
|
, Join (These (Range 2 4, [])
|
|
|
|
(Range 2 4, []))
|
|
|
|
]
|
|
|
|
|
2016-04-05 01:19:58 +03:00
|
|
|
it "produces asymmetrical context" $
|
2016-03-24 20:56:07 +03:00
|
|
|
groupChildrenByLine (Join (These [Range 0 2, Range 2 4] [Range 0 1])) [] `shouldBe`
|
|
|
|
[ Join (These (Range 0 2, [] :: [SplitDiff String Info])
|
|
|
|
(Range 0 1, []))
|
|
|
|
, Join (This (Range 2 4, []))
|
|
|
|
]
|
|
|
|
|
2016-03-22 02:08:02 +03:00
|
|
|
describe "alignDiff" $ do
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns identical branches on a single line" $
|
2016-03-24 20:05:44 +03:00
|
|
|
alignDiff (both (Source.fromList "[ foo ]") (Source.fromList "[ foo ]")) (pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ]) `shouldBe`
|
|
|
|
[ Join (These (info 0 7 `branch` [ info 2 5 `leaf` "foo" ])
|
|
|
|
(info 0 7 `branch` [ info 2 5 `leaf` "foo" ])) ]
|
|
|
|
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns identical branches spanning multiple lines" $
|
2016-03-24 20:05:52 +03:00
|
|
|
alignDiff (both (Source.fromList "[\nfoo\n]") (Source.fromList "[\nfoo\n]")) (pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ]) `shouldBe`
|
|
|
|
[ Join (These (info 0 2 `branch` [])
|
|
|
|
(info 0 2 `branch` []))
|
|
|
|
, Join (These (info 2 6 `branch` [ info 2 5 `leaf` "foo" ])
|
|
|
|
(info 2 6 `branch` [ info 2 5 `leaf` "foo" ]))
|
|
|
|
, Join (These (info 6 7 `branch` [])
|
|
|
|
(info 6 7 `branch` []))
|
|
|
|
]
|
|
|
|
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns reformatted branches" $
|
2016-03-22 02:08:02 +03:00
|
|
|
alignDiff (both (Source.fromList "[ foo ]") (Source.fromList "[\nfoo\n]")) (pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ]) `shouldBe`
|
2016-03-22 07:17:20 +03:00
|
|
|
[ Join (These (info 0 7 `branch` [ info 2 5 `leaf` "foo" ])
|
|
|
|
(info 0 2 `branch` []))
|
|
|
|
, Join (That (info 2 6 `branch` [ info 2 5 `leaf` "foo" ]))
|
|
|
|
, Join (That (info 6 7 `branch` []))
|
2016-03-22 02:08:02 +03:00
|
|
|
]
|
|
|
|
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns nodes following reformatted branches" $
|
2016-03-22 02:08:02 +03:00
|
|
|
alignDiff (both (Source.fromList "[ foo ]\nbar\n") (Source.fromList "[\nfoo\n]\nbar\n")) (pure (info 0 12) `branch` [ pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ], pure (info 8 11) `leaf` "bar" ]) `shouldBe`
|
2016-03-22 07:17:20 +03:00
|
|
|
[ Join (These (info 0 8 `branch` [ info 0 7 `branch` [ info 2 5 `leaf` "foo" ] ])
|
|
|
|
(info 0 2 `branch` [ info 0 2 `branch` [] ]))
|
|
|
|
, Join (That (info 2 6 `branch` [ info 2 6 `branch` [ info 2 5 `leaf` "foo" ] ]))
|
|
|
|
, Join (That (info 6 8 `branch` [ info 6 7 `branch` [] ]))
|
|
|
|
, Join (These (info 8 12 `branch` [ info 8 11 `leaf` "bar" ])
|
|
|
|
(info 8 12 `branch` [ info 8 11 `leaf` "bar" ]))
|
|
|
|
, Join (These (info 12 12 `branch` [])
|
|
|
|
(info 12 12 `branch` []))
|
2016-03-22 02:08:02 +03:00
|
|
|
]
|
|
|
|
|
2016-04-05 01:26:10 +03:00
|
|
|
it "aligns identical branches with multiple children on the same line" $
|
2016-04-05 18:37:40 +03:00
|
|
|
alignDiff (pure (Source.fromList "[ foo, bar ]")) (pure (info 0 12) `branch` [ pure (info 2 5) `leaf` "foo", pure (info 7 10) `leaf` "bar" ]) `shouldBe`
|
|
|
|
[ Join (runBothWith These (pure (info 0 12 `branch` [ info 2 5 `leaf` "foo", info 7 10 `leaf` "bar" ])) ) ]
|
2016-04-05 01:26:10 +03:00
|
|
|
|
2015-12-17 21:08:47 +03:00
|
|
|
where
|
2016-03-12 02:35:31 +03:00
|
|
|
isOnSingleLine (a, _, _) = filter (/= '\n') (toString a) == toString a
|
2015-12-22 23:48:37 +03:00
|
|
|
|
2016-03-08 00:33:30 +03:00
|
|
|
combineIntoLeaves (leaves, start) char = (leaves ++ [ Free $ Annotated (Info <$> pure (Range start $ start + 1) <*> mempty) (Leaf [ char ]) ], start + 1)
|
2015-12-23 00:26:07 +03:00
|
|
|
|
2016-03-12 02:35:31 +03:00
|
|
|
leafWithRangesInSources sources ranges = Free $ Annotated (Info <$> ranges <*> pure mempty) (Leaf $ runBothWith (++) (toString <$> sources))
|
2015-12-23 00:52:20 +03:00
|
|
|
|
2016-04-01 17:15:48 +03:00
|
|
|
branch :: annotation -> [Free (Annotated String annotation) patch] -> Free (Annotated String annotation) patch
|
|
|
|
branch annotation = Free . Annotated annotation . Indexed
|
2016-03-22 01:54:34 +03:00
|
|
|
|
2016-04-01 17:15:48 +03:00
|
|
|
leaf :: annotation -> String -> Free (Annotated String annotation) patch
|
|
|
|
leaf info = Free . Annotated info . Leaf
|
2016-03-22 01:54:27 +03:00
|
|
|
|
2016-04-01 17:15:48 +03:00
|
|
|
info :: Int -> Int -> Info
|
|
|
|
info = ((`Info` mempty) .) . Range
|