2016-09-13 18:35:10 +03:00
|
|
|
module Source.Spec where
|
|
|
|
|
2016-09-14 09:44:26 +03:00
|
|
|
import qualified Prelude
|
2016-09-13 18:35:10 +03:00
|
|
|
import Prologue
|
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
|
2016-09-13 18:39:27 +03:00
|
|
|
import Test.Hspec.QuickCheck
|
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" $
|
|
|
|
\ s -> length (actualLineRanges (totalRange s) (fromList s)) `shouldBe` succ (length (filter (== '\n') s))
|
2016-09-13 18:56:43 +03:00
|
|
|
|
|
|
|
prop "produces exhaustive ranges" $
|
|
|
|
\ s -> let source = fromList s in
|
|
|
|
foldMap (`slice` source) (actualLineRanges (totalRange s) source) `shouldBe` source
|
2016-09-13 18:59:46 +03:00
|
|
|
|
2016-09-13 19:09:18 +03:00
|
|
|
describe "sourceSpanToRange" $ do
|
2016-09-13 21:27:15 +03:00
|
|
|
prop "computes single-line ranges" $
|
2016-09-13 19:37:59 +03:00
|
|
|
\ s -> let source = fromList s
|
2016-10-11 22:12:48 +03:00
|
|
|
spans = zipWith (\ i Range {..} -> SourceSpan (SourcePos i 0) (SourcePos i (end - start))) [0..] ranges
|
2016-09-13 19:37:59 +03:00
|
|
|
ranges = actualLineRanges (totalRange source) source in
|
2016-09-13 20:14:02 +03:00
|
|
|
sourceSpanToRange source <$> spans `shouldBe` ranges
|
2016-09-13 21:27:05 +03:00
|
|
|
|
2016-09-13 21:27:52 +03:00
|
|
|
prop "computes multi-line ranges" $
|
2016-09-13 22:10:49 +03:00
|
|
|
\ s -> let source = fromList s in
|
2016-09-13 21:27:52 +03:00
|
|
|
sourceSpanToRange source (totalSpan source) `shouldBe` totalRange source
|
|
|
|
|
2016-09-13 22:04:17 +03:00
|
|
|
prop "computes sub-line ranges" $
|
|
|
|
\ s -> let source = fromList ('*' : s <> "*") in
|
|
|
|
sourceSpanToRange source (insetSpan (totalSpan source)) `shouldBe` insetRange (totalRange source)
|
|
|
|
|
2016-09-13 21:27:29 +03:00
|
|
|
describe "totalSpan" $ do
|
|
|
|
prop "covers single lines" $
|
2016-10-11 22:12:48 +03:00
|
|
|
\ n -> totalSpan (fromList (replicate n '*')) `shouldBe` SourceSpan (SourcePos 0 0) (SourcePos 0 (max 0 n))
|
2016-09-13 21:27:29 +03:00
|
|
|
|
2016-09-13 21:27:36 +03:00
|
|
|
prop "covers multiple lines" $
|
2016-10-11 22:12:48 +03:00
|
|
|
\ n -> totalSpan (fromList (intersperse '\n' (replicate n '*'))) `shouldBe` SourceSpan (SourcePos 0 0) (SourcePos (max 0 (pred n)) (if n > 0 then 1 else 0))
|
2016-09-13 21:27:36 +03:00
|
|
|
|
2016-09-14 09:41:28 +03:00
|
|
|
totalSpan :: Source Char -> SourceSpan
|
2016-10-11 22:12:48 +03:00
|
|
|
totalSpan source = SourceSpan (SourcePos 0 0) (SourcePos (pred (length ranges)) (end lastRange - start lastRange))
|
2016-09-14 09:41:28 +03:00
|
|
|
where ranges = actualLineRanges (totalRange source) source
|
|
|
|
lastRange = Prelude.last ranges
|
|
|
|
|
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)
|