1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 18:23:44 +03:00
semantic/test/AlignmentSpec.hs

123 lines
5.9 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
2016-03-01 03:39:04 +03:00
import ArbitraryTerm ()
import Control.Comonad.Cofree
import Control.Monad.Free hiding (unfold)
import Data.Functor.Both as Both
import Diff
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-01 03:39:04 +03:00
import Prelude hiding (fst, snd)
import qualified Prelude
2015-12-23 01:24:58 +03:00
import Row
import Range
2016-03-01 03:39:04 +03:00
import Source hiding ((++))
import Syntax
2016-02-29 05:43:47 +03:00
instance Arbitrary a => Arbitrary (Both a) where
arbitrary = pure (curry Both) <*> arbitrary <*> arbitrary
instance Arbitrary a => Arbitrary (Row a) where
2016-02-29 17:22:52 +03:00
arbitrary = Row <$> arbitrary
instance Arbitrary a => Arbitrary (Line a) where
arbitrary = oneof [
makeLine <$> arbitrary,
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-24 06:27:48 +03:00
arbitraryLeaf :: Gen (Source Char, Info, Syntax (Source Char) f)
2015-12-22 21:38:44 +03:00
arbitraryLeaf = toTuple <$> arbitrary
where toTuple string = (string, Info (Range 0 $ length string) mempty, Leaf string)
spec :: Spec
spec = parallel $ do
describe "splitAnnotatedByLines" $ do
prop "outputs one row for single-line unchanged leaves" $
2015-12-22 22:44:57 +03:00
forAll (arbitraryLeaf `suchThat` isOnSingleLine) $
2016-02-29 06:10:02 +03:00
\ (source, info@(Info range categories), syntax) -> splitAnnotatedByLines (pure source) (pure range) (pure categories) syntax `shouldBe` [
2016-02-29 17:22:52 +03:00
makeRow (makeLine [ Free $ Annotated info $ Leaf source ]) (makeLine [ Free $ Annotated info $ Leaf source ]) ]
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)) $
2016-02-29 06:10:02 +03:00
\ source -> splitAnnotatedByLines (pure source) (pure (getTotalRange source)) (pure mempty) (Indexed [] :: Syntax String (Diff String Info)) `shouldBe` [
2016-02-29 17:22:52 +03:00
makeRow (makeLine [ Free $ Annotated (Info (getTotalRange source) mempty) $ Indexed [] ]) (makeLine [ Free $ Annotated (Info (getTotalRange source) mempty) $ Indexed [] ]) ]
prop "preserves line counts in equal sources" $
2015-12-22 23:55:59 +03:00
\ source ->
2016-03-01 03:39:04 +03:00
length (splitAnnotatedByLines (pure source) (pure (getTotalRange source)) (pure mempty) (Indexed . Prelude.fst $ foldl combineIntoLeaves ([], 0) source)) `shouldBe` length (filter (== '\n') $ toList source) + 1
prop "produces the maximum line count in inequal sources" $
2016-02-29 05:43:47 +03:00
\ sources ->
length (splitAnnotatedByLines sources (getTotalRange <$> sources) (pure mempty) (Indexed $ leafWithRangesInSources sources <$> Both.zip (actualLineRanges <$> (getTotalRange <$> sources) <*> sources))) `shouldBe` uncurry max (runBoth ((+ 1) . length . filter (== '\n') . toList <$> sources))
2015-12-21 20:57:50 +03:00
describe "adjoinRowsBy" $ do
prop "is identity on top of no rows" $
\ a -> adjoinRowsBy (pure openMaybe) [] a == [ a ]
2015-12-18 21:04:52 +03:00
prop "appends onto open rows" $
forAll ((arbitrary `suchThat` isOpenBy openMaybe) >>= \ a -> (,) a <$> (arbitrary `suchThat` isOpenBy openMaybe)) $
\ (a, b) -> adjoinRowsBy (pure openMaybe) [ a ] b `shouldBe` [ Row (mappend <$> unRow a <*> unRow b) ]
prop "does not append onto closed rows" $
forAll ((arbitrary `suchThat` isClosedBy openMaybe) >>= \ a -> (,) a <$> (arbitrary `suchThat` isClosedBy openMaybe)) $
\ (a, b) -> adjoinRowsBy (pure openMaybe) [ a ] b `shouldBe` [ b, a ]
prop "does not promote elements through empty lines onto closed lines" $
forAll ((arbitrary `suchThat` isClosedBy openMaybe) >>= \ a -> (,) a <$> (arbitrary `suchThat` isClosedBy openMaybe)) $
2016-02-29 17:22:52 +03:00
\ (a, b) -> adjoinRowsBy (pure openMaybe) [ makeRow EmptyLine EmptyLine, a ] b `shouldBe` [ b, makeRow EmptyLine EmptyLine, a ]
2015-12-18 21:58:02 +03:00
prop "promotes elements through empty lines onto open lines" $
forAll ((arbitrary `suchThat` isOpenBy openMaybe) >>= \ a -> (,) a <$> (arbitrary `suchThat` isOpenBy openMaybe)) $
2016-02-29 17:22:52 +03:00
\ (a, b) -> adjoinRowsBy (pure openMaybe) [ makeRow EmptyLine EmptyLine, a ] b `shouldBe` makeRow EmptyLine EmptyLine : adjoinRowsBy (pure openMaybe) [ a ] b
2015-12-18 21:58:02 +03:00
describe "splitTermByLines" $ do
prop "preserves line count" $
2015-12-24 06:18:10 +03:00
\ source -> let range = getTotalRange source in
splitTermByLines (Info range mempty :< Leaf source) source `shouldBe` (pure . (:< Leaf source) . (`Info` mempty) <$> actualLineRanges range source, range)
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-30 18:31:30 +03:00
openLineBy openMaybe [
pure (Just True),
pure (Just False)
] `shouldBe` (Just $ pure $ Just True)
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-30 18:31:30 +03:00
openLineBy openMaybe [
2015-12-30 18:33:49 +03:00
pure Nothing, pure (Just True)
] `shouldBe` Nothing
describe "openTerm" $ do
it "returns Just the term if its substring does not end with a newline" $
2015-12-30 18:26:40 +03:00
let term = Info (Range 0 2) mempty :< Leaf "" in openTerm (fromList " ") (Identity term) `shouldBe` Just (Identity term)
2015-12-22 04:07:53 +03:00
it "returns Nothing for terms whose substring ends with a newline" $
2015-12-30 18:26:40 +03:00
openTerm (fromList " \n") (Identity $ Info (Range 0 2) mempty :< Leaf "") `shouldBe` Nothing
where
2016-02-29 17:22:52 +03:00
isOpenBy f (Row lines) = and (Maybe.isJust . openLineBy f . pure <$> lines)
isClosedBy f (Row lines@(Both (Line _, Line _))) = and (Maybe.isNothing . openLineBy f . pure <$> lines)
isClosedBy _ _ = False
2015-12-24 07:36:49 +03:00
isOnSingleLine (a, _, _) = filter (/= '\n') (toList a) == toList a
2015-12-24 07:36:49 +03:00
getTotalRange (Source vector) = Range 0 $ length vector
combineIntoLeaves (leaves, start) char = (leaves ++ [ Free $ Annotated (Info <$> (pure (Range start $ start + 1)) <*> mempty) (Leaf [ char ]) ], start + 1)
2016-03-01 03:39:04 +03:00
leafWithRangesInSources sources ranges = Free $ Annotated (Info <$> ranges <*> pure mempty) (Leaf $ toList (fst sources) ++ toList (snd sources))
2015-12-23 00:52:20 +03:00
2015-12-23 01:08:02 +03:00
openMaybe :: Maybe Bool -> Maybe (Maybe Bool)
openMaybe (Just a) = Just (Just a)
2015-12-30 18:28:16 +03:00
openMaybe Nothing = Nothing