@@ -1,5 +1,6 @@
1{-# LANGUAGE RecordWildCards #-}
1{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE ViewPatterns #-}
2{-# LANGUAGE ViewPatterns #-}
3+{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE DataKinds #-}
4{-# LANGUAGE DataKinds #-}
4{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE OverloadedStrings #-}
@@ -44,11 +45,12 @@ main = pushd "examples/docs" $ do
44 res <- Publish.preparePackage publishOpts
45 res <- Publish.preparePackage publishOpts
45 case res of
46 case res of
46 Left e -> Publish.printErrorToStdout e >> exitFailure
47 Left e -> Publish.printErrorToStdout e >> exitFailure
47− Right Docs.Package{..} ->
48+ Right pkg@Docs.Package{..} ->
48 forM_ testCases $ \(P.moduleNameFromString -> mn, pragmas) ->
49 forM_ testCases $ \(P.moduleNameFromString -> mn, pragmas) ->
49 let mdl = takeJust ("module not found in docs: " ++ T.unpack (P.runModuleName mn))
50 let mdl = takeJust ("module not found in docs: " ++ T.unpack (P.runModuleName mn))
50 (find ((==) mn . Docs.modName) pkgModules)
51 (find ((==) mn . Docs.modName) pkgModules)
52+ linksCtx = Docs.getLinksContext pkg
51− in forM_ pragmas (`runAssertionIO` mdl)
53+ in forM_ pragmas (\a -> runAssertionIO a linksCtx mdl)
54takeJust :: String -> Maybe a -> a
56takeJust :: String -> Maybe a -> a
@@ -82,6 +84,11 @@ data Assertion
82 -- | Assert that there should be some declarations re-exported from a
84 -- | Assert that there should be some declarations re-exported from a
83 -- particular module in a particular package.
85 -- particular module in a particular package.
84 | ShouldHaveReExport (Docs.InPackage P.ModuleName)
86 | ShouldHaveReExport (Docs.InPackage P.ModuleName)
87+ -- | Assert that a link to some specific declaration exists within the
88+ -- rendered code for a declaration. Fields are: local module, local
89+ -- declaration title, title of linked declaration, namespace of linked
90+ -- declaration, destination of link.
91+ | ShouldHaveLink P.ModuleName Text Text Docs.Namespace Docs.LinkLocation
85 deriving (Show)
92 deriving (Show)
87newtype ShowFn a = ShowFn a
94newtype ShowFn a = ShowFn a
@@ -119,6 +126,17 @@ data AssertionFailure
119 -- | A module was missing re-exports from a particular module.
126 -- | A module was missing re-exports from a particular module.
120 -- Fields: module name, expected re-export, actual re-exports.
127 -- Fields: module name, expected re-export, actual re-exports.
121 | ReExportMissing P.ModuleName (Docs.InPackage P.ModuleName) [Docs.InPackage P.ModuleName]
128 | ReExportMissing P.ModuleName (Docs.InPackage P.ModuleName) [Docs.InPackage P.ModuleName]
129+ -- | Expected to find some other declaration mentioned in this declaration's
130+ -- rendered code, but did not find anything.
131+ -- Fields: module name, declaration title, title of declaration which was
132+ -- expected but not found in.
133+ | LinkedDeclarationMissing P.ModuleName Text Text
134+ -- | Expected one link location for a declaration mentioned in some other
135+ -- declaration's rendered code, but found a different one. Fields: module
136+ -- name, title of the local declaration which links to some other
137+ -- declaration, title of the linked declaration, expected location, actual
139+ | BadLinkLocation P.ModuleName Text Text Docs.LinkLocation Docs.LinkLocation
122 deriving (Show)
140 deriving (Show)
124data AssertionResult
142data AssertionResult
@@ -126,8 +144,8 @@ data AssertionResult
126 | Fail AssertionFailure
144 | Fail AssertionFailure
127 deriving (Show)
145 deriving (Show)
129−runAssertion :: Assertion -> Docs.Module -> AssertionResult
147+runAssertion :: Assertion -> Docs.LinksContext -> Docs.Module -> AssertionResult
130−runAssertion assertion Docs.Module{..} =
148+runAssertion assertion linksCtx Docs.Module{..} =
131 case assertion of
149 case assertion of
132 ShouldBeDocumented mn decl children ->
150 ShouldBeDocumented mn decl children ->
133 case findChildren decl (declarationsFor mn) of
151 case findChildren decl (declarationsFor mn) of
@@ -214,6 +232,19 @@ runAssertion assertion Docs.Module{..} =
214 then Pass
232 then Pass
215 else Fail (ReExportMissing modName reExp reExps)
233 else Fail (ReExportMissing modName reExp reExps)
235+ ShouldHaveLink mn decl destTitle destNs expectedLoc ->
236+ findDecl mn decl $ \decl' ->
238+ rendered = Docs.renderDeclaration decl'
240+ case extract rendered destNs destTitle of
241+ Just (Docs.linkLocation -> actualLoc) ->
242+ if expectedLoc == actualLoc
244+ else Fail (BadLinkLocation mn decl destTitle expectedLoc actualLoc)
246+ Fail (LinkedDeclarationMissing mn decl destTitle)
218 declarationsFor mn =
249 declarationsFor mn =
219 if mn == modName
250 if mn == modName
@@ -232,6 +263,17 @@ runAssertion assertion Docs.Module{..} =
233 childrenTitles = map Docs.cdeclTitle . Docs.declChildren
264 childrenTitles = map Docs.cdeclTitle . Docs.declChildren
266+ extract :: Docs.RenderedCode -> Docs.Namespace -> Text -> Maybe Docs.DocLink
267+ extract rc ns title = getFirst (Docs.outputWith (First . go) rc) >>= getLink
270+ Docs.getLink linksCtx (P.moduleNameFromString "$DocsTest") ns title
272+ Docs.Symbol ns' title' (Docs.Link containingMod)
273+ | ns' == ns && title' == title -> Just containingMod
235checkConstrained :: P.Type -> Text -> Bool
277checkConstrained :: P.Type -> Text -> Bool
236checkConstrained ty tyClass =
278checkConstrained ty tyClass =
237 -- Note that we don't recurse on ConstrainedType if none of the constraints
279 -- Note that we don't recurse on ConstrainedType if none of the constraints
@@ -248,10 +290,10 @@ checkConstrained ty tyClass =
248 matches className =
290 matches className =
249 (==) className . P.runProperName . P.disqualify . P.constraintClass
291 (==) className . P.runProperName . P.disqualify . P.constraintClass
251−runAssertionIO :: Assertion -> Docs.Module -> IO ()
293+runAssertionIO :: Assertion -> Docs.LinksContext -> Docs.Module -> IO ()
252−runAssertionIO assertion mdl = do
294+runAssertionIO assertion linksCtx mdl = do
253 putStrLn ("In " ++ T.unpack (P.runModuleName (Docs.modName mdl)) ++ ": " ++ show assertion)
295 putStrLn ("In " ++ T.unpack (P.runModuleName (Docs.modName mdl)) ++ ": " ++ show assertion)
254− case runAssertion assertion mdl of
296+ case runAssertion assertion linksCtx mdl of
255 Pass -> pure ()
297 Pass -> pure ()
256 Fail reason -> do
298 Fail reason -> do
257 putStrLn ("Failed: " <> show reason)
299 putStrLn ("Failed: " <> show reason)
@@ -276,6 +318,8 @@ testCases =
276 , ("Example2",
318 , ("Example2",
277 [ ShouldBeDocumented (n "Example2") "one" []
319 [ ShouldBeDocumented (n "Example2") "one" []
278 , ShouldBeDocumented (n "Example2") "two" []
320 , ShouldBeDocumented (n "Example2") "two" []
322+ , ShouldHaveLink (n "Example2") "one" "Int" Docs.TypeLevel (Docs.BuiltinModule (n "Prim"))
281 , ("UTF8",
325 , ("UTF8",
@@ -359,7 +403,7 @@ testCases =
362− n = P.moduleNameFromString . T.pack
406+ n = P.moduleNameFromString
363 pkg str = let Right p = parsePackageName str in p
407 pkg str = let Right p = parsePackageName str in p
365 hasTypeVar varName =
409 hasTypeVar varName =