From 698497b1a05a5b2d5665b0f4ffd491146e4f436a Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Aug 2019 16:22:38 -0400 Subject: [PATCH 1/8] Tidy up the sourceLineRanges tests using prop. Co-Authored-By: Patrick Thomson --- test/Data/Source/Spec.hs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/Data/Source/Spec.hs b/test/Data/Source/Spec.hs index 27711cd83c..745f7c3ac9 100644 --- a/test/Data/Source/Spec.hs +++ b/test/Data/Source/Spec.hs @@ -25,13 +25,11 @@ 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)) + [ prop "produces 1 more range than there are newlines" $ \ source -> do label (summarize source) - (length (sourceLineRanges source) === length (Text.splitOn "\r\n" (toText source) >>= Text.splitOn "\r" >>= Text.splitOn "\n")) + 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)) + , prop "produces exhaustive ranges" $ \ source -> do label (summarize source) foldMap (`slice` source) (sourceLineRanges source) === source ] From 3beda88f325c12589bc74783742d12a4dec771b8 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Aug 2019 16:23:35 -0400 Subject: [PATCH 2/8] Move the label call into summarize. Co-Authored-By: Patrick Thomson --- test/Data/Source/Spec.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Data/Source/Spec.hs b/test/Data/Source/Spec.hs index 745f7c3ac9..b78c3a3e38 100644 --- a/test/Data/Source/Spec.hs +++ b/test/Data/Source/Spec.hs @@ -26,11 +26,11 @@ testTree :: Tasty.TestTree testTree = Tasty.testGroup "Data.Source" [ Tasty.testGroup "sourceLineRanges" [ prop "produces 1 more range than there are newlines" $ \ source -> do - label (summarize source) + summarize source length (sourceLineRanges source) === length (Text.splitOn "\r\n" (toText source) >>= Text.splitOn "\r" >>= Text.splitOn "\n") , prop "produces exhaustive ranges" $ \ source -> do - label (summarize source) + summarize source foldMap (`slice` source) (sourceLineRanges source) === source ] @@ -70,7 +70,7 @@ testTree = Tasty.testGroup "Data.Source" ] ] - where summarize src = case sourceLines src of + where summarize src = label $ case sourceLines src of [] -> "empty" [x] -> if nullSource x then "empty" else "single-line" _ -> "multiple lines" From 0af81b1fef82913e69543f058db2af15e3c7635c Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Aug 2019 16:29:01 -0400 Subject: [PATCH 3/8] Label using classify. Co-Authored-By: Patrick Thomson --- test/Data/Source/Spec.hs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/Data/Source/Spec.hs b/test/Data/Source/Spec.hs index b78c3a3e38..2cebfda8a2 100644 --- a/test/Data/Source/Spec.hs +++ b/test/Data/Source/Spec.hs @@ -9,7 +9,7 @@ import Test.Hspec import qualified Generators as Gen import qualified Hedgehog.Gen as Gen -import Hedgehog ((===), label) +import Hedgehog ((===)) import qualified Hedgehog.Range import Hedgehog hiding (Range) import qualified Test.Tasty as Tasty @@ -70,10 +70,11 @@ testTree = Tasty.testGroup "Data.Source" ] ] - where summarize src = label $ case sourceLines src of - [] -> "empty" - [x] -> if nullSource x then "empty" else "single-line" - _ -> "multiple lines" + where summarize src = do + let lines = sourceLines src + classify "empty" $ nullSource src + classify "single-line" $ length lines == 1 + classify "multiple lines" $ length lines > 1 spec :: Spec spec = do From b658804193bc4f0ba19f58bce605d9d8eb663def Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Aug 2019 16:38:27 -0400 Subject: [PATCH 4/8] Test the coverage of source values. Co-Authored-By: Patrick Thomson --- test/Data/Source/Spec.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Data/Source/Spec.hs b/test/Data/Source/Spec.hs index 2cebfda8a2..6ccd3dd2c0 100644 --- a/test/Data/Source/Spec.hs +++ b/test/Data/Source/Spec.hs @@ -72,9 +72,9 @@ testTree = Tasty.testGroup "Data.Source" ] where summarize src = do let lines = sourceLines src - classify "empty" $ nullSource src - classify "single-line" $ length lines == 1 - classify "multiple lines" $ length lines > 1 + cover 5 "empty" $ nullSource src + cover 20 "single-line" $ length lines == 1 + cover 50 "multiple lines" $ length lines > 1 spec :: Spec spec = do From 7251c1c4b4be367117ae1e2d7e2c9c1bf817d40d Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Aug 2019 16:38:41 -0400 Subject: [PATCH 5/8] Generate empty source values 20% of the time. Co-Authored-By: Patrick Thomson --- test/Generators.hs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/Generators.hs b/test/Generators.hs index d71112c279..55161319db 100644 --- a/test/Generators.hs +++ b/test/Generators.hs @@ -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) ]) From 1b427b995b2ddf4e42adccce7cdac51ca418db75 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Aug 2019 16:46:33 -0400 Subject: [PATCH 6/8] Revert "Test the coverage of source values." This reverts commit b658804193bc4f0ba19f58bce605d9d8eb663def. --- test/Data/Source/Spec.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Data/Source/Spec.hs b/test/Data/Source/Spec.hs index 6ccd3dd2c0..2cebfda8a2 100644 --- a/test/Data/Source/Spec.hs +++ b/test/Data/Source/Spec.hs @@ -72,9 +72,9 @@ testTree = Tasty.testGroup "Data.Source" ] where summarize src = do let lines = sourceLines src - cover 5 "empty" $ nullSource src - cover 20 "single-line" $ length lines == 1 - cover 50 "multiple lines" $ length lines > 1 + classify "empty" $ nullSource src + classify "single-line" $ length lines == 1 + classify "multiple lines" $ length lines > 1 spec :: Spec spec = do From 668efbbd4c2e7a752bb8e326dc53fa6d9bbd9457 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Aug 2019 16:49:36 -0400 Subject: [PATCH 7/8] Tidy up the imports. Co-Authored-By: Patrick Thomson --- test/Data/Source/Spec.hs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Data/Source/Spec.hs b/test/Data/Source/Spec.hs index 2cebfda8a2..5a2b90788c 100644 --- a/test/Data/Source/Spec.hs +++ b/test/Data/Source/Spec.hs @@ -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 ((===)) import qualified Hedgehog.Range -import Hedgehog hiding (Range) import qualified Test.Tasty as Tasty import Test.Tasty.Hedgehog (testProperty) From 3106bd2dddbb50186d76719297b2ff9f2e3fe31a Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Aug 2019 16:50:45 -0400 Subject: [PATCH 8/8] Note a problem with coverage testing. Co-Authored-By: Patrick Thomson --- test/Data/Source/Spec.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Data/Source/Spec.hs b/test/Data/Source/Spec.hs index 5a2b90788c..30ff909af6 100644 --- a/test/Data/Source/Spec.hs +++ b/test/Data/Source/Spec.hs @@ -71,6 +71,7 @@ testTree = Tasty.testGroup "Data.Source" ] 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