1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 17:04:47 +03:00

add Data/Abstract/Name/Spec

This commit is contained in:
Patrick Thomson 2018-10-31 13:19:43 -04:00
parent b0e6190dbf
commit d473b9e1af
4 changed files with 15 additions and 5 deletions

View File

@ -333,6 +333,7 @@ test-suite test
, Control.Abstract.Evaluator.Spec , Control.Abstract.Evaluator.Spec
, Control.Rewriting.Spec , Control.Rewriting.Spec
, Data.Abstract.Path.Spec , Data.Abstract.Path.Spec
, Data.Abstract.Name.Spec
, Data.Diff.Spec , Data.Diff.Spec
, Data.Functor.Classes.Generic.Spec , Data.Functor.Classes.Generic.Spec
, Data.Functor.Listable , Data.Functor.Listable

View File

@ -48,17 +48,13 @@ nameI :: Int -> Name
nameI = I nameI = I
-- | Extract a human-readable 'Text' from a 'Name'. -- | Extract a human-readable 'Text' from a 'Name'.
-- Sample outputs can be found in @Data.Abstract.Name.Spec@.
formatName :: Name -> Text formatName :: Name -> Text
formatName (Name name) = name formatName (Name name) = name
formatName (I i) = Text.pack $ '_' : (alphabet !! a) : replicate n 'ʹ' formatName (I i) = Text.pack $ '_' : (alphabet !! a) : replicate n 'ʹ'
where alphabet = ['a'..'z'] where alphabet = ['a'..'z']
(n, a) = i `divMod` length alphabet (n, a) = i `divMod` length alphabet
-- $
-- >>> I 0
-- "_a"
-- >>> I 26
-- "_aʹ"
instance Show Name where instance Show Name where
showsPrec _ = prettyShowString . Text.unpack . formatName showsPrec _ = prettyShowString . Text.unpack . formatName
where prettyShowString str = showChar '"' . foldr ((.) . prettyChar) id str . showChar '"' where prettyShowString str = showChar '"' . foldr ((.) . prettyChar) id str . showChar '"'

View File

@ -0,0 +1,11 @@
module Data.Abstract.Name.Spec where
import SpecHelpers
import Data.Abstract.Name
spec :: Spec
spec = describe "Data.Abstract.Name" $
it "should format anonymous names correctly" $ do
show (nameI 0) `shouldBe` "\"_a\""
show (nameI 26) `shouldBe` "\"_aʹ\""

View File

@ -9,6 +9,7 @@ import qualified Assigning.Assignment.Spec
import qualified Control.Abstract.Evaluator.Spec import qualified Control.Abstract.Evaluator.Spec
import qualified Control.Rewriting.Spec import qualified Control.Rewriting.Spec
import qualified Data.Diff.Spec import qualified Data.Diff.Spec
import qualified Data.Abstract.Name.Spec
import qualified Data.Abstract.Path.Spec import qualified Data.Abstract.Path.Spec
import qualified Data.Functor.Classes.Generic.Spec import qualified Data.Functor.Classes.Generic.Spec
import qualified Data.Graph.Spec import qualified Data.Graph.Spec
@ -52,6 +53,7 @@ main = do
describe "Data.Diff" Data.Diff.Spec.spec describe "Data.Diff" Data.Diff.Spec.spec
describe "Data.Graph" Data.Graph.Spec.spec describe "Data.Graph" Data.Graph.Spec.spec
describe "Data.Abstract.Path" Data.Abstract.Path.Spec.spec describe "Data.Abstract.Path" Data.Abstract.Path.Spec.spec
describe "Data.Abstract.Name" Data.Abstract.Name.Spec.spec
describe "Data.Functor.Classes.Generic" Data.Functor.Classes.Generic.Spec.spec describe "Data.Functor.Classes.Generic" Data.Functor.Classes.Generic.Spec.spec
describe "Data.Range" Data.Range.Spec.spec describe "Data.Range" Data.Range.Spec.spec
describe "Data.Scientific" Data.Scientific.Spec.spec describe "Data.Scientific" Data.Scientific.Spec.spec