Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 8a8c2ba

Browse files
author
Patrick Thomson
authored
Merge pull request #222 from github/source-coverage-guarantees
Source coverage “guarantees”
2 parents becf320 + 768f642 commit 8a8c2ba

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

test/Data/Source/Spec.hs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import qualified Data.Text as Text
88
import Test.Hspec
99

1010
import qualified Generators as Gen
11+
import Hedgehog hiding (Range)
1112
import qualified Hedgehog.Gen as Gen
12-
import Hedgehog ((===), label)
1313
import qualified Hedgehog.Range
14-
import Hedgehog hiding (Range)
1514
import qualified Test.Tasty as Tasty
1615
import Test.Tasty.Hedgehog (testProperty)
1716

@@ -25,14 +24,12 @@ prop desc f
2524
testTree :: Tasty.TestTree
2625
testTree = Tasty.testGroup "Data.Source"
2726
[ Tasty.testGroup "sourceLineRanges"
28-
[ testProperty "produces 1 more range than there are newlines" $ property $ do
29-
source <- forAll (Gen.source (Hedgehog.Range.linear 0 100))
30-
label (summarize source)
31-
(length (sourceLineRanges source) === length (Text.splitOn "\r\n" (toText source) >>= Text.splitOn "\r" >>= Text.splitOn "\n"))
32-
33-
, testProperty "produces exhaustive ranges" $ property $ do
34-
source <- forAll (Gen.source (Hedgehog.Range.linear 0 100))
35-
label (summarize source)
27+
[ prop "produces 1 more range than there are newlines" $ \ source -> do
28+
summarize source
29+
length (sourceLineRanges source) === length (Text.splitOn "\r\n" (toText source) >>= Text.splitOn "\r" >>= Text.splitOn "\n")
30+
31+
, prop "produces exhaustive ranges" $ \ source -> do
32+
summarize source
3633
foldMap (`slice` source) (sourceLineRanges source) === source
3734
]
3835

@@ -72,10 +69,12 @@ testTree = Tasty.testGroup "Data.Source"
7269
]
7370

7471
]
75-
where summarize src = case sourceLines src of
76-
[] -> "empty"
77-
[x] -> if nullSource x then "empty" else "single-line"
78-
_ -> "multiple lines"
72+
where summarize src = do
73+
let lines = sourceLines src
74+
-- 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'.
75+
classify "empty" $ nullSource src
76+
classify "single-line" $ length lines == 1
77+
classify "multiple lines" $ length lines > 1
7978

8079
spec :: Spec
8180
spec = do

test/Generators.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ import qualified Data.Source
99
import Data.Functor.Identity
1010

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

0 commit comments

Comments
 (0)