2015-12-17 21:08:47 +03:00
|
|
|
module SplitSpec where
|
|
|
|
|
|
|
|
import Test.Hspec
|
|
|
|
import Split
|
|
|
|
import qualified Data.Set as Set
|
|
|
|
import Diff
|
|
|
|
import Range
|
|
|
|
import Test.Hspec.QuickCheck
|
|
|
|
import Test.QuickCheck hiding (Fixed)
|
|
|
|
import Control.Comonad.Cofree
|
|
|
|
import Control.Monad.Free hiding (unfold)
|
2015-12-21 21:00:46 +03:00
|
|
|
import qualified Data.Maybe as Maybe
|
2015-12-24 07:44:02 +03:00
|
|
|
import Source hiding ((++))
|
2015-12-23 01:22:07 +03:00
|
|
|
import Line
|
2015-12-23 01:24:58 +03:00
|
|
|
import Row
|
2015-12-17 21:08:47 +03:00
|
|
|
import Patch
|
|
|
|
import Syntax
|
2015-12-22 21:04:07 +03:00
|
|
|
import ArbitraryTerm
|
2015-12-17 21:08:47 +03:00
|
|
|
|
2015-12-29 22:42:35 +03:00
|
|
|
instance (Arbitrary a, Arbitrary b) => Arbitrary (Row a b) where
|
2015-12-17 21:08:47 +03:00
|
|
|
arbitrary = oneof [
|
|
|
|
Row <$> arbitrary <*> arbitrary ]
|
|
|
|
|
2015-12-21 17:58:20 +03:00
|
|
|
instance Arbitrary a => Arbitrary (Line a) where
|
2015-12-17 21:08:47 +03:00
|
|
|
arbitrary = oneof [
|
2015-12-24 18:44:40 +03:00
|
|
|
makeLine <$> arbitrary,
|
2015-12-17 21:08:47 +03:00
|
|
|
const EmptyLine <$> (arbitrary :: Gen ()) ]
|
|
|
|
|
2015-12-24 06:18:01 +03:00
|
|
|
instance Arbitrary a => Arbitrary (Source a) where
|
2015-12-24 07:37:51 +03:00
|
|
|
arbitrary = fromList <$> arbitrary
|
2015-12-24 06:18:01 +03:00
|
|
|
|
2015-12-29 18:21:09 +03:00
|
|
|
arbitraryLeaf :: Gen (Source Char, Info, Syntax String f)
|
2015-12-22 21:38:44 +03:00
|
|
|
arbitraryLeaf = toTuple <$> arbitrary
|
2015-12-29 18:21:09 +03:00
|
|
|
where toTuple source = (source, Info (Range 0 $ length source) mempty, Leaf (toString source))
|
2015-12-22 21:16:03 +03:00
|
|
|
|
2015-12-17 21:08:47 +03:00
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
2015-12-22 20:39:54 +03:00
|
|
|
describe "splitAnnotatedByLines" $ do
|
2015-12-22 21:04:07 +03:00
|
|
|
prop "outputs one row for single-line unchanged leaves" $
|
2015-12-22 22:44:57 +03:00
|
|
|
forAll (arbitraryLeaf `suchThat` isOnSingleLine) $
|
2015-12-22 21:29:36 +03:00
|
|
|
\ (source, info@(Info range categories), syntax) -> splitAnnotatedByLines (source, source) (range, range) (categories, categories) syntax `shouldBe` [
|
2015-12-29 18:21:09 +03:00
|
|
|
Row (makeLine [ Free $ Annotated info $ Leaf (toString source) ]) (makeLine [ Free $ Annotated info $ Leaf (toString source) ]) ]
|
2015-12-22 21:04:07 +03:00
|
|
|
|
2015-12-22 23:38:18 +03:00
|
|
|
prop "outputs one row for single-line empty unchanged indexed nodes" $
|
2015-12-24 07:36:49 +03:00
|
|
|
forAll (arbitrary `suchThat` (\ a -> filter (/= '\n') (toList a) == toList a)) $
|
2015-12-24 06:18:10 +03:00
|
|
|
\ source -> splitAnnotatedByLines (source, source) (getTotalRange source, getTotalRange source) (mempty, mempty) (Indexed [] :: Syntax String (Diff String Info)) `shouldBe` [
|
2015-12-24 18:44:40 +03:00
|
|
|
Row (makeLine [ Free $ Annotated (Info (getTotalRange source) mempty) $ Indexed [] ]) (makeLine [ Free $ Annotated (Info (getTotalRange source) mempty) $ Indexed [] ]) ]
|
2015-12-22 23:38:18 +03:00
|
|
|
|
2015-12-22 23:48:37 +03:00
|
|
|
prop "preserves line counts in equal sources" $
|
2015-12-22 23:55:59 +03:00
|
|
|
\ source ->
|
2015-12-24 07:26:37 +03:00
|
|
|
length (splitAnnotatedByLines (source, source) (getTotalRange source, getTotalRange source) (mempty, mempty) (Indexed . fst $ foldl combineIntoLeaves ([], 0) source)) `shouldBe` length (filter (== '\n') $ toList 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" $
|
|
|
|
\ sourceA sourceB ->
|
2015-12-24 07:26:37 +03:00
|
|
|
length (splitAnnotatedByLines (sourceA, sourceB) (getTotalRange sourceA, getTotalRange sourceB) (mempty, mempty) (Indexed $ zipWith (leafWithRangesInSources sourceA sourceB) (actualLineRanges (getTotalRange sourceA) sourceA) (actualLineRanges (getTotalRange sourceB) sourceB))) `shouldBe` max (length (filter (== '\n') $ toList sourceA) + 1) (length (filter (== '\n') $ toList sourceB) + 1)
|
2015-12-23 00:10:05 +03:00
|
|
|
|
2015-12-21 20:57:50 +03:00
|
|
|
describe "adjoinRowsBy" $ do
|
2015-12-17 21:08:47 +03:00
|
|
|
prop "is identity on top of no rows" $
|
2015-12-23 01:06:51 +03:00
|
|
|
\ a -> adjoinRowsBy openMaybe openMaybe [] a == [ a ]
|
2015-12-17 21:08:47 +03:00
|
|
|
|
2015-12-18 21:04:52 +03:00
|
|
|
prop "appends onto open rows" $
|
2015-12-23 01:06:45 +03:00
|
|
|
forAll ((arbitrary `suchThat` isOpenBy openMaybe) >>= \ a -> (,) a <$> (arbitrary `suchThat` isOpenBy openMaybe)) $
|
2015-12-24 18:44:40 +03:00
|
|
|
\ (a@(Row a1 b1), b@(Row a2 b2)) ->
|
|
|
|
adjoinRowsBy openMaybe openMaybe [ a ] b `shouldBe` [ Row (makeLine $ unLine a1 ++ unLine a2) (makeLine $ unLine b1 ++ unLine b2) ]
|
2015-12-18 20:11:01 +03:00
|
|
|
|
2015-12-18 21:19:40 +03:00
|
|
|
prop "does not append onto closed rows" $
|
2015-12-23 01:05:02 +03:00
|
|
|
forAll ((arbitrary `suchThat` isClosedBy openMaybe) >>= \ a -> (,) a <$> (arbitrary `suchThat` isClosedBy openMaybe)) $
|
|
|
|
\ (a, b) -> adjoinRowsBy openMaybe openMaybe [ a ] b `shouldBe` [ b, a ]
|
2015-12-18 21:19:40 +03:00
|
|
|
|
2015-12-18 21:27:03 +03:00
|
|
|
prop "does not promote elements through empty lines onto closed lines" $
|
2015-12-23 01:05:35 +03:00
|
|
|
forAll ((arbitrary `suchThat` isClosedBy openMaybe) >>= \ a -> (,) a <$> (arbitrary `suchThat` isClosedBy openMaybe)) $
|
|
|
|
\ (a, b) -> adjoinRowsBy openMaybe openMaybe [ Row EmptyLine EmptyLine, a ] b `shouldBe` [ b, Row EmptyLine EmptyLine, a ]
|
2015-12-18 21:27:03 +03:00
|
|
|
|
2015-12-18 21:58:02 +03:00
|
|
|
prop "promotes elements through empty lines onto open lines" $
|
2015-12-23 01:06:09 +03:00
|
|
|
forAll ((arbitrary `suchThat` isOpenBy openMaybe) >>= \ a -> (,) a <$> (arbitrary `suchThat` isOpenBy openMaybe)) $
|
|
|
|
\ (a, b) -> adjoinRowsBy openMaybe openMaybe [ Row EmptyLine EmptyLine, a ] b `shouldBe` Row EmptyLine EmptyLine : adjoinRowsBy openMaybe openMaybe [ a ] b
|
2015-12-18 21:58:02 +03:00
|
|
|
|
2015-12-22 03:54:40 +03:00
|
|
|
describe "splitTermByLines" $ do
|
2015-12-23 00:34:32 +03:00
|
|
|
prop "preserves line count" $
|
2015-12-24 06:18:10 +03:00
|
|
|
\ source -> let range = getTotalRange source in
|
2015-12-29 18:21:09 +03:00
|
|
|
splitTermByLines (Info range mempty :< Leaf (toString source)) source `shouldBe` (makeLine . (:[]) . (:< Leaf (toString source)) . (`Info` mempty) <$> actualLineRanges range source, range)
|
2015-12-22 03:54:40 +03:00
|
|
|
|
2015-12-21 18:01:00 +03:00
|
|
|
describe "openLineBy" $ do
|
2015-12-22 04:23:56 +03:00
|
|
|
it "produces the earliest non-empty line in a list, if open" $
|
2015-12-24 07:37:51 +03:00
|
|
|
openLineBy (openTerm $ fromList "\n ") [
|
2015-12-24 18:44:40 +03:00
|
|
|
makeLine [ Info (Range 1 2) mempty :< Leaf "" ],
|
|
|
|
makeLine [ Info (Range 0 1) mempty :< Leaf "" ]
|
|
|
|
] `shouldBe` (Just $ makeLine [ Info (Range 1 2) mempty :< Leaf "" ])
|
2015-12-22 04:04:05 +03:00
|
|
|
|
2015-12-22 04:23:56 +03:00
|
|
|
it "returns Nothing if the earliest non-empty line is closed" $
|
2015-12-24 07:37:51 +03:00
|
|
|
openLineBy (openTerm $ fromList "\n") [
|
2015-12-24 18:44:40 +03:00
|
|
|
makeLine [ Info (Range 0 1) mempty :< Leaf "" ]
|
2015-12-22 04:04:05 +03:00
|
|
|
] `shouldBe` Nothing
|
|
|
|
|
2015-12-22 04:06:19 +03:00
|
|
|
describe "openTerm" $ do
|
2015-12-22 04:08:05 +03:00
|
|
|
it "returns Just the term if its substring does not end with a newline" $
|
2015-12-24 07:37:51 +03:00
|
|
|
let term = Info (Range 0 2) mempty :< Leaf "" in openTerm (fromList " ") term `shouldBe` Just term
|
2015-12-22 04:08:05 +03:00
|
|
|
|
2015-12-22 04:07:53 +03:00
|
|
|
it "returns Nothing for terms whose substring ends with a newline" $
|
2015-12-24 07:37:51 +03:00
|
|
|
openTerm (fromList " \n") (Info (Range 0 2) mempty :< Leaf "") `shouldBe` Nothing
|
2015-12-22 04:06:19 +03:00
|
|
|
|
2015-12-17 21:08:47 +03:00
|
|
|
where
|
2015-12-23 00:55:18 +03:00
|
|
|
isOpenBy f (Row a b) = Maybe.isJust (openLineBy f [ a ]) && Maybe.isJust (openLineBy f [ b ])
|
2015-12-23 00:55:13 +03:00
|
|
|
isClosedBy f (Row a@(Line _) b@(Line _)) = Maybe.isNothing (openLineBy f [ a ]) && Maybe.isNothing (openLineBy f [ b ])
|
|
|
|
isClosedBy _ (Row _ _) = False
|
2015-12-22 22:44:49 +03:00
|
|
|
|
2015-12-24 07:36:49 +03:00
|
|
|
isOnSingleLine (a, _, _) = filter (/= '\n') (toList a) == toList a
|
2015-12-22 23:48:37 +03:00
|
|
|
|
2015-12-24 07:36:49 +03:00
|
|
|
getTotalRange (Source vector) = Range 0 $ length vector
|
2015-12-24 06:20:35 +03:00
|
|
|
|
2015-12-22 23:48:37 +03:00
|
|
|
combineIntoLeaves (leaves, start) char = (leaves ++ [ Free $ Annotated (Info (Range start $ start + 1) mempty, Info (Range start $ start + 1) mempty) (Leaf [ char ]) ], start + 1)
|
2015-12-23 00:26:07 +03:00
|
|
|
|
2015-12-24 07:26:37 +03:00
|
|
|
leafWithRangesInSources sourceA sourceB rangeA rangeB = Free $ Annotated (Info rangeA mempty, Info rangeB mempty) (Leaf $ toList sourceA ++ toList sourceB)
|
2015-12-23 00:52:20 +03:00
|
|
|
|
2015-12-23 01:08:02 +03:00
|
|
|
openMaybe :: Maybe Bool -> Maybe (Maybe Bool)
|
2015-12-23 01:02:34 +03:00
|
|
|
openMaybe (Just a) = Just (Just a)
|
|
|
|
openMaybe Nothing = Nothing
|