Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions test/Data/Source/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import qualified Data.Text as Text
import Test.Hspec

import qualified Generators as Gen
import Hedgehog hiding (Range)
import qualified Hedgehog.Gen as Gen
import Hedgehog ((===), label)
import qualified Hedgehog.Range
import Hedgehog hiding (Range)
import qualified Test.Tasty as Tasty
import Test.Tasty.Hedgehog (testProperty)

Expand All @@ -25,14 +24,12 @@ prop desc f
testTree :: Tasty.TestTree
testTree = Tasty.testGroup "Data.Source"
[ Tasty.testGroup "sourceLineRanges"
[ testProperty "produces 1 more range than there are newlines" $ property $ do
source <- forAll (Gen.source (Hedgehog.Range.linear 0 100))
label (summarize source)
(length (sourceLineRanges source) === length (Text.splitOn "\r\n" (toText source) >>= Text.splitOn "\r" >>= Text.splitOn "\n"))

, testProperty "produces exhaustive ranges" $ property $ do
source <- forAll (Gen.source (Hedgehog.Range.linear 0 100))
label (summarize source)
[ prop "produces 1 more range than there are newlines" $ \ source -> do
summarize source
length (sourceLineRanges source) === length (Text.splitOn "\r\n" (toText source) >>= Text.splitOn "\r" >>= Text.splitOn "\n")

, prop "produces exhaustive ranges" $ \ source -> do
summarize source
foldMap (`slice` source) (sourceLineRanges source) === source
]

Expand Down Expand Up @@ -72,10 +69,12 @@ testTree = Tasty.testGroup "Data.Source"
]

]
where summarize src = case sourceLines src of
[] -> "empty"
[x] -> if nullSource x then "empty" else "single-line"
_ -> "multiple lines"
where summarize src = do
let lines = sourceLines src
-- FIXME: this should be using cover (reverted in 1b427b995), but that leads to flaky tests: hedgehog’s 'cover' implementation fails tests instead of warning, and currently has no equivalent to 'checkCoverage'.
classify "empty" $ nullSource src
classify "single-line" $ length lines == 1
classify "multiple lines" $ length lines > 1

spec :: Spec
spec = do
Expand Down
4 changes: 3 additions & 1 deletion test/Generators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ import qualified Data.Source
import Data.Functor.Identity

source :: (GenBase m ~ Identity, MonadGen m) => Hedgehog.Range Int -> m Data.Source.Source
source r = Data.Source.fromUTF8 <$> Gen.utf8 r (Gen.frequency [ (1, pure '\r'), (1, pure '\n'), (20, Gen.unicode) ])
source r = Gen.frequency [ (1, empty), (20, nonEmpty) ]
where empty = pure mempty
nonEmpty = Data.Source.fromUTF8 <$> Gen.utf8 r (Gen.frequency [ (1, pure '\r'), (1, pure '\n'), (20, Gen.unicode) ])