1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 05:41:54 +03:00
semantic/test/Data/Source/Spec.hs

78 lines
2.9 KiB
Haskell
Raw Normal View History

module Data.Source.Spec (spec) where
2016-09-13 18:35:10 +03:00
2017-07-28 21:37:02 +03:00
import Data.Char (chr)
import Data.Functor.Listable
2017-06-24 16:46:28 +03:00
import Data.Range
import Data.Source
2017-06-24 16:46:28 +03:00
import Data.Span
import qualified Data.Text as Text
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
2017-06-24 18:13:22 +03:00
describe "sourceLineRanges" $ do
prop "produces 1 more range than there are newlines" $
2017-06-24 18:13:22 +03:00
\ source -> length (sourceLineRanges source) `shouldBe` succ (Text.count "\n" (toText source))
prop "produces exhaustive ranges" $
2017-06-24 18:13:22 +03:00
\ source -> foldMap (`slice` source) (sourceLineRanges source) `shouldBe` source
2016-09-13 18:59:46 +03:00
describe "spanToRange" $ do
prop "computes single-line ranges" $
2018-06-04 17:18:32 +03:00
\ s -> let source = fromUTF8 s
spans = zipWith (\ i Range {..} -> Span (Pos i 1) (Pos i (succ (end - start)))) [1..] ranges
2017-06-24 18:13:22 +03:00
ranges = sourceLineRanges source in
spanToRange source <$> spans `shouldBe` ranges
2017-05-19 17:59:33 +03:00
prop "computes multi-line ranges" $
\ source ->
spanToRange source (totalSpan source) `shouldBe` totalRange source
2017-05-19 17:59:33 +03:00
prop "computes sub-line ranges" $
\ s -> let source = "*" <> s <> "*" in
spanToRange source (insetSpan (totalSpan source)) `shouldBe` insetRange (totalRange source)
prop "inverse of rangeToSpan" $
\ a b -> let s = a <> "\n" <> b in spanToRange s (totalSpan s) `shouldBe` totalRange s
describe "rangeToSpan" $ do
prop "inverse of spanToRange" $
\ a b -> let s = a <> "\n" <> b in rangeToSpan s (totalRange s) `shouldBe` totalSpan s
describe "totalSpan" $ do
prop "covers single lines" $
\ n -> totalSpan (fromText (Text.replicate n "*")) `shouldBe` Span (Pos 1 1) (Pos 1 (max 1 (succ n)))
prop "covers multiple lines" $
\ n -> totalSpan (fromText (Text.intersperse '\n' (Text.replicate n "*"))) `shouldBe` Span (Pos 1 1) (Pos (max 1 n) (if n > 0 then 2 else 1))
describe "newlineIndices" $ do
it "finds \\n" $
let source = "a\nb" in
newlineIndices source `shouldBe` [1]
it "finds \\r" $
let source = "a\rb" in
newlineIndices source `shouldBe` [1]
it "finds \\r\\n" $
let source = "a\r\nb" in
newlineIndices source `shouldBe` [2]
it "finds intermixed line endings" $
let source = "hi\r}\r}\n xxx \r a" in
newlineIndices source `shouldBe` [2, 4, 6, 12]
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
insetSpan :: Span -> Span
insetSpan sourceSpan = sourceSpan { spanStart = (spanStart sourceSpan) { posColumn = succ (posColumn (spanStart sourceSpan)) }
, spanEnd = (spanEnd sourceSpan) { posColumn = pred (posColumn (spanEnd sourceSpan)) } }
insetRange :: Range -> Range
insetRange Range {..} = Range (succ start) (pred end)