packages feed

tilia-0.2.0.1: tests/Tilia/Fixity/InterfaceSpec.hs

{-# LANGUAGE OverloadedStrings #-}

-- | Reading what @ghc --show-iface -dppr-debug@ prints.
--
-- The samples below are cut from real output rather than invented, since
-- the whole risk here is in the format: this is a pretty-printer's idea of
-- an interface, not a documented one.
module Tilia.Fixity.InterfaceSpec (spec) where

import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Data.Text (Text)
import Test.Hspec

import Tilia.Fixity
import Tilia.Fixity.HiFile (HiExport (..), HiFile (..), HiName (..))
import Tilia.Fixity.Interface

spec :: Spec
spec = do
  describe "what a module exports" $ do
    it "reads each name with the namespace it is in" $
      exportsOf
        "exports:\n  Control.Applicative.optional{v r1}\n  GHC.Types.Type{(w) tc 32Q}\n"
        `shouldBe` Just
          [ Avail (HiName "Control.Applicative" InTerms (OpName "optional")),
            Avail (HiName "GHC.Types" InTypes (OpName "Type"))
          ]

    it "reads a type with its members, itself first" $
      exportsOf
        "exports:\n  GHC.Generics.:*:{tc 34A}{GHC.Generics.:*:{d r2}}\n"
        `shouldBe` Just
          [ AvailTC
              (inGenerics InTypes ":*:")
              [inGenerics InTypes ":*:", inGenerics InTerms ":*:"]
          ]

    it "reads a type a bar says is exported without itself" $
      exportsOf "exports:\n  GHC.Base.Functor{tc 2a}|{GHC.Base.<${v rq}}\n"
        `shouldBe` Just
          [ AvailTC
              (HiName "GHC.Base" InTypes (OpName "Functor"))
              [HiName "GHC.Base" InTerms (OpName "<$")]
          ]

    it "reads members made of dots and bars" $
      exportsOf
        "exports:\n  GHC.Internal.Bits.Bits{tc rb}{GHC.Internal.Bits..&.{v rc} GHC.Internal.Bits..|.{v rd}}\n"
        `shouldBe` Just
          [ AvailTC
              (inBits InTypes "Bits")
              [inBits InTypes "Bits", inBits InTerms ".&.", inBits InTerms ".|."]
          ]

    it "reads a record field as GHC 9.8 prints it" $
      exportsOf
        "exports:\n  M.T{tc r2}{M.+++{fld r3} M.T{d r4} M.foo{fld r5}}\n"
        `shouldBe` Just
          [ AvailTC
              (inM InTypes "T")
              [inM InTypes "T", inM InTerms "+++", inM InTerms "T", inM InTerms "foo"]
          ]

    it "reads a record field as GHC 9.6 prints it, by its label" $
      exportsOf
        "exports:\n  M.T{tc r2}{M.T{d r3} +++{M.$sel:+++:T{v r4}}+dup+sel foo{M.$sel:foo:T{v r5}}+dup+sel}\n  GHC.Generics.M1{tc 34w}{GHC.Generics.M1{d ru} unM1{GHC.Generics.unM1{v rv}}-dup+sel}\n"
        `shouldBe` Just
          [ AvailTC
              (inM InTypes "T")
              [inM InTypes "T", inM InTerms "T", inM InTerms "+++", inM InTerms "foo"],
            AvailTC
              (inGenerics InTypes "M1")
              [ inGenerics InTypes "M1",
                inGenerics InTerms "M1",
                inGenerics InTerms "unM1"
              ]
          ]

    it "passes over built-in syntax, which it prints bare" $
      exportsOf "exports:\n  []\n  GHC.Types.~{(w) tc 31i}\n"
        `shouldBe` Just
          [Avail Unneeded, Avail (HiName "GHC.Types" InTypes (OpName "~"))]

    it "reads a tuple with the commas in its name" $
      exportsOf
        "exports:\n  (,){GHC.Internal.Tuple.(,){(w) d 55_6}}\n  GHC.Internal.Tuple.getSolo{v r1}\n"
        `shouldBe` Just
          [ AvailTC
              Unneeded
              [Unneeded, HiName "GHC.Internal.Tuple" InTerms (OpName "(,)")],
            Avail (HiName "GHC.Internal.Tuple" InTerms (OpName "getSolo"))
          ]

    it "reads nothing where one entry cannot be read" $ do
      exportsOf "exports:\n  M.f{v r1}\n  M.g{zz r2}\n" `shouldBe` Nothing
      exportsOf "exports:\n  M.f{v r1}\n  g{v r2}\n" `shouldBe` Nothing

  describe "what a module declares" $ do
    it "reads a fixity line wrapped across lines, by namespace" $
      fixitiesOf
        "fixities infixr -1 ->{tc}, infixr 5 :{d}, infix 4 ~{tc},\n\
        \         infix 4 ~~{tc}\n"
        `shouldBe` [ (InTypes, OpName "->", Fixity RightAssoc (-1)),
                     (InTerms, OpName ":", Fixity RightAssoc 5),
                     (InTypes, OpName "~", Fixity NoAssoc 4),
                     (InTypes, OpName "~~", Fixity NoAssoc 4)
                   ]

    it "keeps a type and a constructor of one name apart" $
      fixitiesOf
        "fixities infixr 6 :*:{d}, infixr 6 :*:{tc}, infixr 5 :+:{tc}\n"
        `shouldBe` [ (InTerms, OpName ":*:", Fixity RightAssoc 6),
                     (InTypes, OpName ":*:", Fixity RightAssoc 6),
                     (InTypes, OpName ":+:", Fixity RightAssoc 5)
                   ]

    it "passes over an entry it cannot read, and keeps the rest" $
      fixitiesOf
        "fixities infixl notadigit ?{v}, infixl 9 !{v}, infixl 9 ?, infixl\n"
        `shouldBe` [(InTerms, OpName "!", Fixity LeftAssoc 9)]

  describe "what it brings in, read off ghc --show-iface" $ do
    it "is every name it exports, by namespace" $
      fmap
        (fmap certainNames . interfaceExports)
        ( interfaceOf
            "Control.Applicative"
            "exports:\n  GHC.Base.Alternative{tc rj}{GHC.Base.<|>{v rk} GHC.Base.empty{v rl}}\n  GHC.Base.Functor{tc 2a}|{GHC.Base.<${v rq}}\n"
        )
        `shouldBe` Just
          ( Just
              ( Set.fromList
                  [ (InTypes, OpName "Alternative"),
                    (InTerms, OpName "<|>"),
                    (InTerms, OpName "empty"),
                    (InTerms, OpName "<$")
                  ]
              )
          )

    it "passes on what other modules declare" $
      fmap
        interfaceReexports
        ( interfaceOf
            "Control.Applicative"
            "exports:\n  Control.Applicative.optional{v r1}\n  Data.Functor.<$>{v r3}\n"
        )
        `shouldBe` Just [("Data.Functor", InTerms, OpName "<$>")]

    it "declares the fixities of what it exports itself" $
      fmap
        (Map.toList . interfaceDeclares)
        ( interfaceOf
            "M"
            "exports:\n  M.<+>{v r1}\n  M.T{tc r2}{M.T{d r3}}\nfixities infixl 6 <+>{v}, infixr 2 <~>{v}\n"
        )
        `shouldBe` Just [((InTerms, OpName "<+>"), Fixity LeftAssoc 6)]

  describe "what a decoded interface brings in" $ do
    it "is every name it exports, by namespace, and the members of each type" $
      fmap
        interfaceExports
        ( fromHiFile
            "M"
            ( HiFile
                "M"
                [ AvailTC (inM InTypes "T") [inM InTypes "T", inM InTerms "C"],
                  Avail (inM InTerms "f")
                ]
                []
            )
        )
        `shouldBe` Right
          ( Just
              ( Certain
                  ( Set.fromList
                      [ (InTypes, OpName "T"),
                        (InTerms, OpName "C"),
                        (InTerms, OpName "f")
                      ]
                  )
                  ( Map.fromList
                      [(OpName "T", Set.fromList [(InTerms, OpName "C")])]
                  )
              )
          )

    it "gives a type exported without its members none" $
      fmap
        interfaceMembers
        ( fromHiFile
            "M"
            (HiFile "M" [AvailTC (inM InTypes "T") [inM InTypes "T"]] [])
        )
        `shouldBe` Right (Map.fromList [(OpName "T", Set.empty)])

    it "does not take a type for exported where only its field is" $
      fmap
        (fmap certainNames . interfaceExports)
        ( fromHiFile
            "M"
            (HiFile "M" [AvailTC (inM InTypes "T") [inM InTerms "field"]] [])
        )
        `shouldBe` Right (Just (Set.fromList [(InTerms, OpName "field")]))

    it "passes on the members of a type exported without itself" $
      fmap
        interfaceReexports
        ( fromHiFile
            "M"
            (HiFile "M" [AvailTC (inN InTypes ":<:") [inN InTerms "inj"]] [])
        )
        `shouldBe` Right [("N", InTerms, OpName "inj")]

  describe "a fixity for a name the module does not export" $ do
    it "is left out of a decoded interface" $
      fmap
        (Map.toList . interfaceDeclares)
        ( fromHiFile
            "M"
            ( HiFile
                "M"
                [Avail (inM InTerms "<+>")]
                [ (InTerms, OpName "<+>", Fixity LeftAssoc 3),
                  (InTerms, OpName "<~>", Fixity RightAssoc 2)
                ]
            )
        )
        `shouldBe` Right [((InTerms, OpName "<+>"), Fixity LeftAssoc 3)]

    it "is left out of a decoded interface that passes on another spelled the same" $
      fmap
        (Map.toList . interfaceDeclares)
        ( fromHiFile
            "M"
            ( HiFile
                "M"
                [Avail (inN InTerms "<+>")]
                [(InTerms, OpName "<+>", Fixity LeftAssoc 1)]
            )
        )
        `shouldBe` Right []

  describe "sections it has no use for" $
    it "is not confused by the rest of the file" $ do
      let out =
            "Magic: Wanted 33214052,\n\
            \       got    33214052\n\
            \Version: Wanted 9067,\n\
            \         got    9067\n\
            \interface Data.Bits 9067\n\
            \  interface hash: efd3cbc01244a91d72b3a670a9191f4b\n\
            \exports:\n\
            \  Data.Bits.!<<.{v r1}\n\
            \direct module dependencies:\n\
            \fixities infixl 8 !<<.{v}\n\
            \instance GHC.Base.Alternative{tc rj} [Control.Applicative.ZipList{tc rd}]\n\
            \trusted: none\n"
      fmap (Map.toList . interfaceDeclares) (parseInterface "Data.Bits" out)
        `shouldBe` Just [((InTerms, OpName "!<<."), Fixity LeftAssoc 8)]

  describe "output it will not read" $ do
    it "refuses what does not name a module at all" $
      parseInterface "M" "some future rendering we do not recognise\n"
        `shouldBe` Nothing

    it "refuses an interface for a different module" $
      parseInterface
        "Data.Map.Strict"
        (header "Data.Map.Lazy" <> "fixities infixl 9 !{v}\n")
        `shouldBe` Nothing

    it "reads one that names the module asked for" $
      fmap
        (Map.toList . interfaceDeclares)
        (interfaceOf "M" "exports:\n  M.!{v r1}\nfixities infixl 9 !{v}\n")
        `shouldBe` Just [((InTerms, OpName "!"), Fixity LeftAssoc 9)]

header :: Text -> Text
header modName = "interface " <> modName <> " 9067\n"

-- | What an interface of this shape exports, as decoding it would give it.
exportsOf :: Text -> Maybe [HiExport]
exportsOf = fmap hiExports . shownHiFile . (header "M" <>)

-- | The fixities an interface of this shape declares, as decoding it would
-- give them.
fixitiesOf :: Text -> [(Namespace, OpName, Fixity)]
fixitiesOf = maybe [] hiFixities . shownHiFile . (header "M" <>)

-- | What an interface of this shape says, for the module named.
interfaceOf :: Text -> Text -> Maybe Interface
interfaceOf modName = parseInterface modName . (header modName <>)

-- | A name the module @M@ declares.
inM :: Namespace -> Text -> HiName
inM namespace = HiName "M" namespace . OpName

-- | A name another module, N, declares.
inN :: Namespace -> Text -> HiName
inN namespace = HiName "N" namespace . OpName

-- | A name @GHC.Generics@ declares.
inGenerics :: Namespace -> Text -> HiName
inGenerics namespace = HiName "GHC.Generics" namespace . OpName

-- | A name @GHC.Internal.Bits@ declares.
inBits :: Namespace -> Text -> HiName
inBits namespace = HiName "GHC.Internal.Bits" namespace . OpName