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

Commit 78dad09

Browse files
author
Patrick Thomson
authored
Merge pull request #152 from github/remove-orphan-bytestring-listable
Remove orphan ByteString Listable instance.
2 parents 1227403 + 50424cf commit 78dad09

File tree

6 files changed

+83
-67
lines changed

6 files changed

+83
-67
lines changed

semantic-core/semantic-core.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ test-suite spec
7171
other-modules: Generators
7272
build-depends: base
7373
, semantic-core
74-
, hedgehog >= 0.6 && <1
74+
, hedgehog ^>= 1
7575
, tasty >= 1.2 && <2
76-
, tasty-hedgehog >= 0.2 && <1
76+
, tasty-hedgehog ^>= 1.0.0.1
7777
, tasty-hunit >= 0.10 && <1
7878
, trifecta
7979
hs-source-dirs: test

semantic.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,17 @@ test-suite test
381381
, Tags.Spec
382382
, SpecHelpers
383383
, Test.Hspec.LeanCheck
384+
, Generators
384385
build-depends: semantic
385386
, tree-sitter-json
386387
, Glob ^>= 0.10.0
388+
, hedgehog ^>= 1
387389
, hspec >= 2.6 && <3
388390
, hspec-core >= 2.6 && <3
389391
, hspec-expectations ^>= 0.8.2
390392
, tasty ^>= 1.2.3
391393
, tasty-golden ^>= 2.3.2
394+
, tasty-hedgehog ^>= 1.0.0.1
392395
, tasty-hspec ^>= 1.1.5.1
393396
, HUnit ^>= 1.6.0.0
394397
, leancheck >= 0.8 && <1

test/Data/Functor/Listable.hs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -566,20 +566,3 @@ instance Listable Pos where
566566

567567
instance Listable Span where
568568
tiers = cons2 Span
569-
570-
instance Listable Blob where
571-
tiers = cons4 makeBlob
572-
573-
instance Listable BlobPair where
574-
tiers = liftTiers tiers
575-
576-
instance Listable Source where
577-
tiers = fromUTF8 `mapT` tiers
578-
579-
instance Listable ByteString where
580-
tiers = (T.encodeUtf8 . T.pack) `mapT` strings
581-
where strings = foldr ((\\//) . listsOf . toTiers) []
582-
[ ['a'..'z'] <> ['A'..'Z'] <> ['0'..'9']
583-
, [' '..'/'] <> [':'..'@'] <> ['['..'`'] <> ['{'..'~']
584-
, [chr 0x00..chr 0x1f] <> [chr 127] -- Control characters.
585-
, [chr 0xa0..chr 0x24f] ] -- Non-ASCII.

test/Data/Source/Spec.hs

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,77 @@
1-
module Data.Source.Spec (spec) where
1+
{-# LANGUAGE NamedFieldPuns #-}
2+
module Data.Source.Spec (spec, testTree) where
23

3-
import Data.Char (chr)
4-
import Data.Functor.Listable
54
import Data.Range
65
import Data.Source
76
import Data.Span
87
import qualified Data.Text as Text
8+
99
import Test.Hspec
10-
import Test.Hspec.LeanCheck
11-
import Test.LeanCheck
10+
11+
import qualified Generators as Gen
12+
import qualified Hedgehog.Gen as Gen
13+
import Hedgehog ((===))
14+
import qualified Hedgehog.Range
15+
import Hedgehog hiding (Range)
16+
import qualified Test.Tasty as Tasty
17+
import Test.Tasty.Hedgehog (testProperty)
18+
19+
prop :: HasCallStack => String -> (Source -> PropertyT IO ()) -> Tasty.TestTree
20+
prop desc f
21+
= testProperty desc
22+
. property
23+
$ forAll (Gen.source (Hedgehog.Range.linear 0 100))
24+
>>= f
25+
26+
testTree :: Tasty.TestTree
27+
testTree = Tasty.testGroup "Data.Source"
28+
[ Tasty.testGroup "sourceLineRanges"
29+
[ prop "produces 1 more range than there are newlines" $
30+
\ source -> length (sourceLineRanges source) === succ (Text.count "\n" (toText source))
31+
32+
, prop "produces exhaustive ranges" $
33+
\ source -> foldMap (`slice` source) (sourceLineRanges source) === source
34+
]
35+
36+
, Tasty.testGroup "spanToRange"
37+
[ prop "computes single-line ranges" $ \ source -> do
38+
let ranges = sourceLineRanges source
39+
let spans = zipWith (\ i Range {..} -> Span (Pos i 1) (Pos i (succ (end - start)))) [1..] ranges
40+
fmap (spanToRange source) spans === ranges
41+
42+
, prop "computes multi-line ranges" $
43+
\ source ->
44+
spanToRange source (totalSpan source) === totalRange source
45+
46+
, prop "computes sub-line ranges" $
47+
\ s -> let source = "*" <> s <> "*" in
48+
spanToRange source (insetSpan (totalSpan source)) === insetRange (totalRange source)
49+
50+
, testProperty "inverse of rangeToSpan" . property $ do
51+
a <- forAll . Gen.source $ Hedgehog.Range.linear 0 100
52+
b <- forAll . Gen.source $ Hedgehog.Range.linear 0 100
53+
let s = a <> "\n" <> b in spanToRange s (totalSpan s) === totalRange s
54+
]
55+
56+
, testProperty "rangeToSpan inverse of spanToRange" . property $ do
57+
a <- forAll . Gen.source $ Hedgehog.Range.linear 0 100
58+
b <- forAll . Gen.source $ Hedgehog.Range.linear 0 100
59+
let s = a <> "\n" <> b in rangeToSpan s (totalRange s) === totalSpan s
60+
61+
, Tasty.testGroup "totalSpan"
62+
[ testProperty "covers single lines" . property $ do
63+
n <- forAll $ Gen.int (Hedgehog.Range.linear 0 100)
64+
totalSpan (fromText (Text.replicate n "*")) === Span (Pos 1 1) (Pos 1 (max 1 (succ n)))
65+
66+
, testProperty "covers multiple lines" . property $ do
67+
n <- forAll $ Gen.int (Hedgehog.Range.linear 0 100)
68+
totalSpan (fromText (Text.intersperse '\n' (Text.replicate n "*"))) === Span (Pos 1 1) (Pos (max 1 n) (if n > 0 then 2 else 1))
69+
]
70+
71+
]
1272

1373
spec :: Spec
1474
spec = parallel $ do
15-
describe "sourceLineRanges" $ do
16-
prop "produces 1 more range than there are newlines" $
17-
\ source -> length (sourceLineRanges source) `shouldBe` succ (Text.count "\n" (toText source))
18-
19-
prop "produces exhaustive ranges" $
20-
\ source -> foldMap (`slice` source) (sourceLineRanges source) `shouldBe` source
21-
22-
describe "spanToRange" $ do
23-
prop "computes single-line ranges" $
24-
\ s -> let source = fromUTF8 s
25-
spans = zipWith (\ i Range {..} -> Span (Pos i 1) (Pos i (succ (end - start)))) [1..] ranges
26-
ranges = sourceLineRanges source in
27-
spanToRange source <$> spans `shouldBe` ranges
28-
29-
prop "computes multi-line ranges" $
30-
\ source ->
31-
spanToRange source (totalSpan source) `shouldBe` totalRange source
32-
33-
prop "computes sub-line ranges" $
34-
\ s -> let source = "*" <> s <> "*" in
35-
spanToRange source (insetSpan (totalSpan source)) `shouldBe` insetRange (totalRange source)
36-
37-
prop "inverse of rangeToSpan" $
38-
\ a b -> let s = a <> "\n" <> b in spanToRange s (totalSpan s) `shouldBe` totalRange s
39-
40-
describe "rangeToSpan" $ do
41-
prop "inverse of spanToRange" $
42-
\ a b -> let s = a <> "\n" <> b in rangeToSpan s (totalRange s) `shouldBe` totalSpan s
43-
44-
describe "totalSpan" $ do
45-
prop "covers single lines" $
46-
\ n -> totalSpan (fromText (Text.replicate n "*")) `shouldBe` Span (Pos 1 1) (Pos 1 (max 1 (succ n)))
47-
48-
prop "covers multiple lines" $
49-
\ 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))
50-
5175
describe "newlineIndices" $ do
5276
it "finds \\n" $
5377
let source = "a\nb" in
@@ -62,13 +86,6 @@ spec = parallel $ do
6286
let source = "hi\r}\r}\n xxx \r a" in
6387
newlineIndices source `shouldBe` [2, 4, 6, 12]
6488

65-
prop "preserves characters" . forAll (toTiers (list +| [chr 0xa0..chr 0x24f])) $
66-
\ c -> Text.unpack (toText (fromText (Text.singleton c))) `shouldBe` [c]
67-
68-
prop "preserves strings" $
69-
\ s -> fromText (toText s) `shouldBe` s
70-
71-
7289
insetSpan :: Span -> Span
7390
insetSpan sourceSpan = sourceSpan { spanStart = (spanStart sourceSpan) { posColumn = succ (posColumn (spanStart sourceSpan)) }
7491
, spanEnd = (spanEnd sourceSpan) { posColumn = pred (posColumn (spanEnd sourceSpan)) } }

test/Generators.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{-# LANGUAGE TypeFamilies #-}
2+
module Generators
3+
( source
4+
) where
5+
6+
import Hedgehog
7+
import qualified Hedgehog.Gen as Gen
8+
import qualified Data.Source
9+
import Data.Functor.Identity
10+
11+
source :: (GenBase m ~ Identity, MonadGen m) => Hedgehog.Range Int -> m Data.Source.Source
12+
source r = Data.Source.fromUTF8 <$> Gen.utf8 r Gen.unicode

test/Spec.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ tests :: (?session :: TaskSession) => [TestTree]
4646
tests =
4747
[ Integration.Spec.spec
4848
, Semantic.CLI.Spec.spec
49+
, Data.Source.Spec.testTree
4950
]
5051

5152
-- We can't bring this out of the IO monad until we divest

0 commit comments

Comments
 (0)