2016-02-28 22:05:19 +03:00
|
|
|
module AlignmentSpec where
|
2015-12-17 21:08:47 +03:00
|
|
|
|
2016-02-28 22:01:56 +03:00
|
|
|
import Alignment
|
2016-04-26 22:44:11 +03:00
|
|
|
import ArbitraryTerm ()
|
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-05-14 01:25:42 +03:00
|
|
|
import Data.Align hiding (align)
|
2016-03-22 02:08:02 +03:00
|
|
|
import Data.Bifunctor.Join
|
2016-04-25 19:54:51 +03:00
|
|
|
import Data.Foldable (toList)
|
2016-02-29 18:30:33 +03:00
|
|
|
import Data.Functor.Both as Both
|
2016-04-15 02:34:59 +03:00
|
|
|
import Data.Functor.Identity
|
2016-04-25 21:51:25 +03:00
|
|
|
import Data.Maybe (catMaybes, fromMaybe)
|
2016-04-26 21:54:24 +03:00
|
|
|
import Data.Monoid
|
2016-04-26 21:53:50 +03:00
|
|
|
import Data.Text.Arbitrary ()
|
2016-04-15 05:00:00 +03:00
|
|
|
import Data.These
|
2015-12-18 23:03:54 +03:00
|
|
|
import Diff
|
2016-03-31 00:33:07 +03:00
|
|
|
import Info
|
2016-04-15 02:40:36 +03:00
|
|
|
import Patch
|
2016-03-01 03:39:04 +03:00
|
|
|
import Prelude hiding (fst, snd)
|
2016-04-26 21:54:24 +03:00
|
|
|
import qualified Prelude
|
2015-12-18 23:03:54 +03:00
|
|
|
import Range
|
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
|
2016-04-26 21:53:50 +03:00
|
|
|
import Test.Hspec
|
2016-04-26 22:44:11 +03:00
|
|
|
import Test.Hspec.QuickCheck
|
2015-12-17 21:08:47 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
2016-01-05 18:38:51 +03:00
|
|
|
spec = parallel $ do
|
2016-04-28 00:44:15 +03:00
|
|
|
describe "alignChildrenInRanges" $ do
|
2016-03-24 20:54:11 +03:00
|
|
|
it "produces symmetrical context" $
|
2016-04-28 00:44:15 +03:00
|
|
|
alignChildrenInRanges getRange (Join (These [Range 0 2, Range 2 4] [Range 0 2, Range 2 4])) ([] :: [Identity [Join These (SplitDiff String Info)]]) `shouldBe`
|
2016-04-22 19:18:40 +03:00
|
|
|
[ Join (These (Range 0 2, [])
|
2016-03-24 20:53:58 +03:00
|
|
|
(Range 0 2, []))
|
|
|
|
, Join (These (Range 2 4, [])
|
|
|
|
(Range 2 4, []))
|
|
|
|
]
|
|
|
|
|
2016-04-05 01:19:58 +03:00
|
|
|
it "produces asymmetrical context" $
|
2016-04-28 00:44:15 +03:00
|
|
|
alignChildrenInRanges getRange (Join (These [Range 0 2, Range 2 4] [Range 0 1])) ([] :: [Identity [Join These (SplitDiff String Info)]]) `shouldBe`
|
2016-04-22 19:18:40 +03:00
|
|
|
[ Join (These (Range 0 2, [])
|
2016-03-24 20:56:07 +03:00
|
|
|
(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-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "[ foo ]") (Source.fromList "[ foo ]") in
|
|
|
|
align sources (pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ]) `shouldBe` PrettyDiff sources
|
2016-03-24 20:05:44 +03:00
|
|
|
[ 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-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "[\nfoo\n]") (Source.fromList "[\nfoo\n]") in
|
|
|
|
align sources (pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ]) `shouldBe` PrettyDiff sources
|
2016-03-24 20:05:52 +03:00
|
|
|
[ 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-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "[ foo ]") (Source.fromList "[\nfoo\n]") in
|
|
|
|
align sources (pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ]) `shouldBe` PrettyDiff sources
|
2016-05-13 17:51:52 +03:00
|
|
|
[ Join (That (info 0 2 `branch` []))
|
|
|
|
, Join (These (info 0 7 `branch` [ info 2 5 `leaf` "foo" ])
|
|
|
|
(info 2 6 `branch` [ info 2 5 `leaf` "foo" ]))
|
2016-03-22 07:17:20 +03:00
|
|
|
, 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-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "[ foo ]\nbar\n") (Source.fromList "[\nfoo\n]\nbar\n") in
|
|
|
|
align sources (pure (info 0 12) `branch` [ pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ], pure (info 8 11) `leaf` "bar" ]) `shouldBe` PrettyDiff sources
|
2016-05-13 17:51:52 +03:00
|
|
|
[ Join (That (info 0 2 `branch` [ info 0 2 `branch` [] ]))
|
|
|
|
, Join (These (info 0 8 `branch` [ info 0 7 `branch` [ info 2 5 `leaf` "foo" ] ])
|
|
|
|
(info 2 6 `branch` [ info 2 6 `branch` [ info 2 5 `leaf` "foo" ] ]))
|
2016-03-22 07:17:20 +03:00
|
|
|
, 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-05-14 01:27:38 +03:00
|
|
|
let sources = pure (Source.fromList "[ foo, bar ]") in
|
|
|
|
align sources (pure (info 0 12) `branch` [ pure (info 2 5) `leaf` "foo", pure (info 7 10) `leaf` "bar" ]) `shouldBe` PrettyDiff sources
|
2016-04-05 18:37:40 +03:00
|
|
|
[ 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
|
|
|
|
2016-04-15 02:40:36 +03:00
|
|
|
it "aligns insertions" $
|
2016-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "a") (Source.fromList "a\nb") in
|
|
|
|
align sources (both (info 0 1) (info 0 3) `branch` [ pure (info 0 1) `leaf` "a", Pure (Insert (info 2 3 :< Leaf "b")) ]) `shouldBe` PrettyDiff sources
|
2016-04-15 02:40:36 +03:00
|
|
|
[ Join (These (info 0 1 `branch` [ info 0 1 `leaf` "a" ])
|
|
|
|
(info 0 2 `branch` [ info 0 1 `leaf` "a" ]))
|
2016-05-16 17:41:10 +03:00
|
|
|
, Join (That (info 2 3 `branch` [ Pure (SplitInsert (info 2 3 :< Leaf "b")) ]))
|
2016-04-15 02:40:36 +03:00
|
|
|
]
|
|
|
|
|
2016-05-16 17:54:56 +03:00
|
|
|
it "aligns total insertions" $
|
|
|
|
let sources = both (Source.fromList "") (Source.fromList "a") in
|
|
|
|
align sources (Pure (Insert (info 0 1 :< Leaf "a"))) `shouldBe` PrettyDiff sources
|
|
|
|
[ Join (That (Pure (SplitInsert (info 0 1 :< Leaf "a")))) ]
|
|
|
|
|
2016-05-16 19:37:13 +03:00
|
|
|
it "aligns insertions into empty branches" $
|
2016-05-19 00:22:16 +03:00
|
|
|
let sources = both (Source.fromList "[ ]") (Source.fromList "[a]") in
|
|
|
|
align sources (pure (info 0 3) `branch` [ Pure (Insert (info 1 2 :< Leaf "a")) ]) `shouldBe` PrettyDiff sources
|
|
|
|
[ Join (These (info 0 3 `branch` [])
|
|
|
|
(info 0 3 `branch` [ Pure (SplitInsert (info 1 2 :< Leaf "a")) ])) ]
|
2016-05-16 19:37:13 +03:00
|
|
|
|
2016-05-19 07:13:26 +03:00
|
|
|
it "aligns symmetrically following insertions" $
|
2016-05-14 01:25:42 +03:00
|
|
|
let sources = both (Source.fromList "a\nc") (Source.fromList "a\nb\nc") in
|
|
|
|
align sources (both (info 0 3) (info 0 5) `branch` [ pure (info 0 1) `leaf` "a", Pure (Insert (info 2 3 :< Leaf "b")), both (info 2 3) (info 4 5) `leaf` "c" ])
|
2016-04-25 20:37:19 +03:00
|
|
|
`shouldBe` PrettyDiff sources
|
2016-04-15 23:10:09 +03:00
|
|
|
[ Join (These (info 0 2 `branch` [ info 0 1 `leaf` "a" ])
|
|
|
|
(info 0 2 `branch` [ info 0 1 `leaf` "a" ]))
|
2016-05-17 22:04:09 +03:00
|
|
|
, Join (That (info 2 4 `branch` [ Pure (SplitInsert (info 2 3 :< Leaf "b")) ]))
|
2016-04-15 23:10:09 +03:00
|
|
|
, Join (These (info 2 3 `branch` [ info 2 3 `leaf` "c" ])
|
|
|
|
(info 4 5 `branch` [ info 4 5 `leaf` "c" ]))
|
|
|
|
]
|
|
|
|
|
2016-05-19 07:19:08 +03:00
|
|
|
it "symmetrical nodes force the alignment of asymmetrical nodes on both sides" $
|
|
|
|
let sources = both (Source.fromList "[ a, b ]") (Source.fromList "[ b, c ]") in
|
|
|
|
align sources (pure (info 0 8) `branch` [ Pure (Delete (info 2 3 :< Leaf "a")), both (info 5 6) (info 2 3) `leaf` "b", Pure (Insert (info 5 6 :< Leaf "c")) ]) `shouldBe` PrettyDiff sources
|
|
|
|
[ Join (These (info 0 8 `branch` [ Pure (SplitDelete (info 2 3 :< Leaf "a")), info 5 6 `leaf` "b" ])
|
|
|
|
(info 0 8 `branch` [ info 2 3 `leaf` "b", Pure (SplitInsert (info 5 6 :< Leaf "c")) ])) ]
|
|
|
|
|
2016-05-19 16:01:15 +03:00
|
|
|
it "when one of two symmetrical nodes must be split, splits the latter" $
|
|
|
|
let sources = both (Source.fromList "[ a, b ]") (Source.fromList "[ a\n, b\n]") in
|
|
|
|
align sources (both (info 0 8) (info 0 9) `branch` [ pure (info 2 3) `leaf` "a", both (info 5 6) (info 6 7) `leaf` "b" ]) `shouldBe` PrettyDiff sources
|
|
|
|
[ Join (These (info 0 8 `branch` [ info 2 3 `leaf` "a", info 5 6 `leaf` "b" ])
|
|
|
|
(info 0 4 `branch` [ info 2 3 `leaf` "a" ]))
|
|
|
|
, Join (That (info 4 8 `branch` [ info 6 7 `leaf` "b" ]))
|
|
|
|
, Join (That (info 8 9 `branch` []))
|
|
|
|
]
|
|
|
|
|
2016-04-26 22:44:11 +03:00
|
|
|
describe "numberedRows" $
|
|
|
|
prop "counts only non-empty values" $
|
|
|
|
\ xs -> counts (numberedRows (xs :: [Join These Char])) `shouldBe` length . catMaybes <$> Join (unalign (runJoin <$> xs))
|
|
|
|
|
2016-04-26 21:54:24 +03:00
|
|
|
counts :: [Join These (Int, a)] -> Both Int
|
|
|
|
counts numbered = fromMaybe 0 . getLast . mconcat . fmap Last <$> Join (unalign (runJoin . fmap Prelude.fst <$> numbered))
|
|
|
|
|
2016-05-14 01:25:42 +03:00
|
|
|
align :: Both (Source.Source Char) -> Diff String Info -> PrettyDiff
|
|
|
|
align sources = PrettyDiff sources . alignDiff sources
|
|
|
|
|
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
|
2016-04-14 19:57:45 +03:00
|
|
|
info = ((\ r -> Info r mempty 0) .) . Range
|
2016-04-25 19:37:56 +03:00
|
|
|
|
2016-04-25 20:00:18 +03:00
|
|
|
data PrettyDiff = PrettyDiff { unPrettySources :: Both (Source.Source Char), unPrettyLines :: [Join These (SplitDiff String Info)] }
|
2016-04-25 21:59:20 +03:00
|
|
|
deriving Eq
|
2016-04-25 20:00:26 +03:00
|
|
|
|
2016-04-25 19:54:51 +03:00
|
|
|
instance Show PrettyDiff where
|
2016-05-14 01:44:27 +03:00
|
|
|
show (PrettyDiff sources lines) = showLine (maximum (0 : (maximum . fmap length <$> shownLines))) <$> shownLines >>= ('\n':)
|
2016-05-14 01:38:19 +03:00
|
|
|
where shownLines = catMaybes $ toBoth <$> lines
|
2016-04-25 22:08:20 +03:00
|
|
|
showLine n line = uncurry ((++) . (++ " | ")) (fromThese (replicate n ' ') (replicate n ' ') (runJoin (pad n <$> line)))
|
2016-04-25 22:00:46 +03:00
|
|
|
showDiff diff = filter (/= '\n') . toList . Source.slice (getRange diff)
|
2016-04-25 22:03:49 +03:00
|
|
|
pad n string = showString (take n string) (replicate (max 0 (n - length string)) ' ')
|
2016-04-25 21:43:07 +03:00
|
|
|
toBoth them = showDiff <$> them `applyThese` modifyJoin (uncurry These) sources
|