2017-03-01 02:35:43 +03:00
|
|
|
module SourceSpec where
|
2016-09-13 18:35:10 +03:00
|
|
|
|
2017-02-13 19:58:30 +03:00
|
|
|
import qualified Data.Text as Text
|
2017-02-16 19:27:50 +03:00
|
|
|
import Prologue hiding (list)
|
2016-09-13 18:39:27 +03:00
|
|
|
import Range
|
2016-09-13 18:35:10 +03:00
|
|
|
import Source
|
2016-09-13 19:09:18 +03:00
|
|
|
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
|
2016-09-13 18:39:27 +03:00
|
|
|
spec = parallel $ do
|
|
|
|
describe "actualLineRanges" $ do
|
|
|
|
prop "produces 1 more range than there are newlines" $
|
2017-05-19 17:30:58 +03:00
|
|
|
\ source -> Prologue.length (actualLineRanges source) `shouldBe` succ (Text.count "\n" (toText source))
|
2016-09-13 18:56:43 +03:00
|
|
|
|
|
|
|
prop "produces exhaustive ranges" $
|
2017-05-19 17:30:58 +03:00
|
|
|
\ source -> foldMap (`slice` source) (actualLineRanges source) `shouldBe` source
|
2016-09-13 18:59:46 +03:00
|
|
|
|
2016-09-13 19:09:18 +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
|
2017-05-19 17:45:51 +03:00
|
|
|
spans = zipWith (\ i Range {..} -> SourceSpan (SourcePos i 1) (SourcePos i (succ (end - start)))) [1..] ranges
|
2017-05-19 17:30:58 +03:00
|
|
|
ranges = actualLineRanges source in
|
2016-09-13 20:14:02 +03:00
|
|
|
sourceSpanToRange source <$> spans `shouldBe` ranges
|
2016-09-13 21:27:05 +03:00
|
|
|
|
2017-05-19 17:59:33 +03:00
|
|
|
prop "computes multi-line ranges" $
|
|
|
|
\ source ->
|
2016-09-13 21:27:52 +03:00
|
|
|
sourceSpanToRange source (totalSpan source) `shouldBe` totalRange source
|
|
|
|
|
2017-05-19 17:59:33 +03:00
|
|
|
prop "computes sub-line ranges" $
|
|
|
|
\ s -> let source = "*" <> s <> "*" in
|
2016-09-13 22:04:17 +03:00
|
|
|
sourceSpanToRange source (insetSpan (totalSpan source)) `shouldBe` insetRange (totalRange source)
|
|
|
|
|
2017-05-19 18:08:22 +03:00
|
|
|
prop "inverse of rangeToSourceSpan" $
|
2017-05-19 18:41:16 +03:00
|
|
|
\ a b -> let s = a <> "\n" <> b in sourceSpanToRange s (totalSpan s) `shouldBe` totalRange s
|
2017-05-19 17:52:12 +03:00
|
|
|
|
|
|
|
describe "rangeToSourceSpan" $ do
|
2017-05-19 18:08:22 +03:00
|
|
|
prop "inverse of sourceSpanToRange" $
|
2017-05-19 18:41:16 +03:00
|
|
|
\ a b -> let s = a <> "\n" <> b in rangeToSourceSpan s (totalRange s) `shouldBe` totalSpan s
|
2017-05-19 17:52:12 +03:00
|
|
|
|
2016-09-13 21:27:29 +03:00
|
|
|
describe "totalSpan" $ do
|
|
|
|
prop "covers single lines" $
|
2017-05-19 17:45:51 +03:00
|
|
|
\ n -> totalSpan (fromText (Text.replicate n "*")) `shouldBe` SourceSpan (SourcePos 1 1) (SourcePos 1 (max 1 (succ n)))
|
2016-09-13 21:27:29 +03:00
|
|
|
|
2016-09-13 21:27:36 +03:00
|
|
|
prop "covers multiple lines" $
|
2017-05-19 17:45:51 +03:00
|
|
|
\ n -> totalSpan (fromText (Text.intersperse '\n' (Text.replicate n "*"))) `shouldBe` SourceSpan (SourcePos 1 1) (SourcePos (max 1 n) (if n > 0 then 2 else 1))
|
2016-09-13 21:27:36 +03:00
|
|
|
|
2017-02-16 19:27:50 +03:00
|
|
|
prop "preserves characters" . forAll (toTiers (list +| [chr 0xa0..chr 0x24f])) $
|
2017-02-16 21:08:23 +03:00
|
|
|
\ c -> Text.unpack (toText (fromText (Text.singleton c))) `shouldBe` [c]
|
2017-02-16 19:27:50 +03:00
|
|
|
|
2017-02-16 19:53:49 +03:00
|
|
|
prop "preserves strings" $
|
|
|
|
\ s -> fromText (toText s) `shouldBe` s
|
|
|
|
|
2016-09-14 09:41:28 +03:00
|
|
|
|
2016-09-13 22:03:58 +03:00
|
|
|
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)
|