This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 459
Remove orphan ByteString Listable instance. #152
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e1d94f0
Remove Listable instance for Source.
02b36d2
Port Leancheck+Hspec properties to Tasty.Hedgehog.
30fd1b4
Merge branch 'master' into remove-orphan-bytestring-listable
robrix 50424cf
Merge branch 'master' into remove-orphan-bytestring-listable
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,77 @@ | ||
| module Data.Source.Spec (spec) where | ||
| {-# LANGUAGE NamedFieldPuns #-} | ||
| module Data.Source.Spec (spec, testTree) where | ||
|
|
||
| import Data.Char (chr) | ||
| import Data.Functor.Listable | ||
| import Data.Range | ||
| import Data.Source | ||
| import Data.Span | ||
| import qualified Data.Text as Text | ||
|
|
||
| import Test.Hspec | ||
| import Test.Hspec.LeanCheck | ||
| import Test.LeanCheck | ||
|
|
||
| import qualified Generators as Gen | ||
| 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) | ||
|
|
||
| prop :: HasCallStack => String -> (Source -> PropertyT IO ()) -> Tasty.TestTree | ||
| prop desc f | ||
| = testProperty desc | ||
| . property | ||
| $ forAll (Gen.source (Hedgehog.Range.linear 0 100)) | ||
| >>= f | ||
|
|
||
| testTree :: Tasty.TestTree | ||
| testTree = Tasty.testGroup "Data.Source" | ||
| [ Tasty.testGroup "sourceLineRanges" | ||
| [ prop "produces 1 more range than there are newlines" $ | ||
| \ source -> length (sourceLineRanges source) === succ (Text.count "\n" (toText source)) | ||
|
|
||
| , prop "produces exhaustive ranges" $ | ||
| \ source -> foldMap (`slice` source) (sourceLineRanges source) === source | ||
| ] | ||
|
|
||
| , Tasty.testGroup "spanToRange" | ||
| [ prop "computes single-line ranges" $ \ source -> do | ||
| let ranges = sourceLineRanges source | ||
| let spans = zipWith (\ i Range {..} -> Span (Pos i 1) (Pos i (succ (end - start)))) [1..] ranges | ||
| fmap (spanToRange source) spans === ranges | ||
|
|
||
| , prop "computes multi-line ranges" $ | ||
| \ source -> | ||
| spanToRange source (totalSpan source) === totalRange source | ||
|
|
||
| , prop "computes sub-line ranges" $ | ||
| \ s -> let source = "*" <> s <> "*" in | ||
| spanToRange source (insetSpan (totalSpan source)) === insetRange (totalRange source) | ||
|
|
||
| , testProperty "inverse of rangeToSpan" . property $ do | ||
| a <- forAll . Gen.source $ Hedgehog.Range.linear 0 100 | ||
| b <- forAll . Gen.source $ Hedgehog.Range.linear 0 100 | ||
| let s = a <> "\n" <> b in spanToRange s (totalSpan s) === totalRange s | ||
| ] | ||
|
|
||
| , testProperty "rangeToSpan inverse of spanToRange" . property $ do | ||
| a <- forAll . Gen.source $ Hedgehog.Range.linear 0 100 | ||
| b <- forAll . Gen.source $ Hedgehog.Range.linear 0 100 | ||
| let s = a <> "\n" <> b in rangeToSpan s (totalRange s) === totalSpan s | ||
|
|
||
| , Tasty.testGroup "totalSpan" | ||
| [ testProperty "covers single lines" . property $ do | ||
| n <- forAll $ Gen.int (Hedgehog.Range.linear 0 100) | ||
| totalSpan (fromText (Text.replicate n "*")) === Span (Pos 1 1) (Pos 1 (max 1 (succ n))) | ||
|
|
||
| , testProperty "covers multiple lines" . property $ do | ||
| n <- forAll $ Gen.int (Hedgehog.Range.linear 0 100) | ||
| totalSpan (fromText (Text.intersperse '\n' (Text.replicate n "*"))) === Span (Pos 1 1) (Pos (max 1 n) (if n > 0 then 2 else 1)) | ||
| ] | ||
|
|
||
| ] | ||
|
|
||
| spec :: Spec | ||
| spec = parallel $ do | ||
| describe "sourceLineRanges" $ do | ||
| prop "produces 1 more range than there are newlines" $ | ||
| \ source -> length (sourceLineRanges source) `shouldBe` succ (Text.count "\n" (toText source)) | ||
|
|
||
| prop "produces exhaustive ranges" $ | ||
| \ source -> foldMap (`slice` source) (sourceLineRanges source) `shouldBe` source | ||
|
|
||
| describe "spanToRange" $ do | ||
| prop "computes single-line ranges" $ | ||
| \ s -> let source = fromUTF8 s | ||
| spans = zipWith (\ i Range {..} -> Span (Pos i 1) (Pos i (succ (end - start)))) [1..] ranges | ||
| ranges = sourceLineRanges source in | ||
| spanToRange source <$> spans `shouldBe` ranges | ||
|
|
||
| prop "computes multi-line ranges" $ | ||
| \ source -> | ||
| spanToRange source (totalSpan source) `shouldBe` totalRange source | ||
|
|
||
| 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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In keeping with my philosophy of “do as little as possible so I don’t end up rewriting the whole test suite in an act of Pyrrhic futility”, I’ve left the tests that didn’t use LeanCheck as specs with |
||
| it "finds \\n" $ | ||
| let source = "a\nb" in | ||
|
|
@@ -62,13 +86,6 @@ spec = parallel $ do | |
| 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)) } } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| {-# LANGUAGE TypeFamilies #-} | ||
| module Generators | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figure we’re gonna have a lot of generators, many of which may depend on each other. |
||
| ( source | ||
| ) where | ||
|
|
||
| import Hedgehog | ||
| import qualified Hedgehog.Gen as Gen | ||
| import qualified Data.Source | ||
| import Data.Functor.Identity | ||
|
|
||
| source :: (GenBase m ~ Identity, MonadGen m) => Hedgehog.Range Int -> m Data.Source.Source | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The GenBase constraint is a weird artifact of the Hedgehog API. |
||
| source r = Data.Source.fromUTF8 <$> Gen.utf8 r Gen.unicode | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrote this little helper to save keystrokes and modify as few of the specs as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really nice ✨