1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 22:31:36 +03:00
semantic/test/SourceSpec.hs

61 lines
2.7 KiB
Haskell
Raw Normal View History

2017-03-01 02:35:43 +03:00
module SourceSpec where
2016-09-13 18:35:10 +03:00
import qualified Data.Text as Text
import qualified Prelude
import Prologue hiding (list)
import Range
2016-09-13 18:35:10 +03:00
import Source
import SourceSpan
2016-09-13 18:35:10 +03:00
import Test.Hspec
2017-02-13 18:55:57 +03:00
import Test.Hspec.LeanCheck
2017-02-14 23:42:14 +03:00
import Test.LeanCheck
2016-09-13 18:35:10 +03:00
spec :: Spec
spec = parallel $ do
describe "actualLineRanges" $ do
prop "produces 1 more range than there are newlines" $
2017-02-14 23:42:14 +03:00
\ source -> Prologue.length (actualLineRanges (totalRange source) source) `shouldBe` succ (Text.count "\n" (toText source))
prop "produces exhaustive ranges" $
\ source -> foldMap (`slice` source) (actualLineRanges (totalRange source) source) `shouldBe` source
2016-09-13 18:59:46 +03:00
describe "sourceSpanToRange" $ do
2017-02-14 23:42:14 +03:00
prop "computes single-line ranges" . forAll (unListableByteString `mapT` tiers) $
\ s -> let source = Source s
2016-10-11 22:12:48 +03:00
spans = zipWith (\ i Range {..} -> SourceSpan (SourcePos i 0) (SourcePos i (end - start))) [0..] ranges
ranges = actualLineRanges (totalRange source) source in
2016-09-13 20:14:02 +03:00
sourceSpanToRange source <$> spans `shouldBe` ranges
2017-02-14 23:42:14 +03:00
prop "computes multi-line ranges" . forAll (unListableByteString `mapT` tiers) $
\ s -> let source = Source s in
sourceSpanToRange source (totalSpan source) `shouldBe` totalRange source
2017-02-14 23:42:14 +03:00
prop "computes sub-line ranges" . forAll (unListableByteString `mapT` tiers) $
\ s -> let source = Source ("*" <> s <> "*") in
sourceSpanToRange source (insetSpan (totalSpan source)) `shouldBe` insetRange (totalRange source)
describe "totalSpan" $ do
prop "covers single lines" $
2017-02-14 23:42:14 +03:00
\ n -> totalSpan (fromText (Text.replicate n "*")) `shouldBe` SourceSpan (SourcePos 0 0) (SourcePos 0 (max 0 n))
prop "covers multiple lines" $
2017-02-14 23:42:14 +03:00
\ n -> totalSpan (fromText (Text.intersperse '\n' (Text.replicate n "*"))) `shouldBe` SourceSpan (SourcePos 0 0) (SourcePos (max 0 (pred n)) (if n > 0 then 1 else 0))
prop "preserves characters" . forAll (toTiers (list +| [chr 0xa0..chr 0x24f])) $
\ c -> Text.unpack (toText (fromText (Text.singleton c))) `shouldBe` [c]
prop "preserves strings" $
\ s -> fromText (toText s) `shouldBe` s
totalSpan :: Source -> SourceSpan
totalSpan source = SourceSpan (SourcePos 0 0) (SourcePos (pred (Prologue.length ranges)) (end lastRange - start lastRange))
where ranges = actualLineRanges (totalRange source) source
lastRange = Prelude.last ranges
insetSpan :: SourceSpan -> SourceSpan
insetSpan sourceSpan = sourceSpan { spanStart = (spanStart sourceSpan) { column = succ (column (spanStart sourceSpan)) }
, spanEnd = (spanEnd sourceSpan) { column = pred (column (spanEnd sourceSpan)) } }
insetRange :: Range -> Range
insetRange Range {..} = Range (succ start) (pred end)