1{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE ViewPatterns #-}
3{-# LANGUAGE DataKinds #-}
10import Data.Version (Version(..))
13import Data.Maybe (fromMaybe)
14import Data.List ((\\))
18import qualified Language.PureScript as P
19import qualified Language.PureScript.Docs as Docs
20import Language.PureScript.Docs.AsMarkdown (codeToString)
21import qualified Language.PureScript.Publish as Publish
22import qualified Language.PureScript.Publish.ErrorsWarnings as Publish
26publishOpts :: Publish.PublishOptions
27publishOpts = Publish.defaultPublishOptions
28 { Publish.publishGetVersion = return testVersion
29 , Publish.publishWorkingTreeDirty = return ()
31 where testVersion = ("v999.0.0", Version [999,0,0] [])
34main = pushd "examples/docs" $ do
35 res <- Publish.preparePackage publishOpts
37 Left e -> Publish.printErrorToStdout e >> exitFailure
38 Right Docs.Package{..} ->
39 forM_ testCases $ \(P.moduleNameFromString -> mn, pragmas) ->
40 let mdl = takeJust ("module not found in docs: " ++ P.runModuleName mn)
41 (find ((==) mn . Docs.modName) pkgModules)
42 in forM_ pragmas (`runAssertionIO` mdl)
45takeJust :: String -> Maybe a -> a
46takeJust msg = fromMaybe (error msg)
49 -- | Assert that a particular declaration is documented with the given
51 = ShouldBeDocumented P.ModuleName String [String]
52 -- | Assert that a particular declaration is not documented
53 | ShouldNotBeDocumented P.ModuleName String
54 -- | Assert that a particular declaration exists, but without a particular
56 | ChildShouldNotBeDocumented P.ModuleName String String
57 -- | Assert that a particular declaration has a particular type class
59 | ShouldBeConstrained P.ModuleName String String
60 -- | Assert that a particular value declaration exists, and its type
61 -- satisfies the given predicate.
62 | ValueShouldHaveTypeSignature P.ModuleName String (ShowFn (P.Type -> Bool))
63 -- | Assert that a particular type alias exists, and its corresponding
64 -- type, when rendered, matches a given string exactly
65 -- fields: module, type synonym name, expected type
66 | TypeSynonymShouldRenderAs P.ModuleName String String
69newtype ShowFn a = ShowFn a
71instance Show (ShowFn a) where
75 -- | A declaration was not documented, but should have been
76 = NotDocumented P.ModuleName String
77 -- | A child declaration was not documented, but should have been
78 | ChildrenNotDocumented P.ModuleName String [String]
79 -- | A declaration was documented, but should not have been
80 | Documented P.ModuleName String
81 -- | A child declaration was documented, but should not have been
82 | ChildDocumented P.ModuleName String String
83 -- | A constraint was missing.
84 | ConstraintMissing P.ModuleName String String
85 -- | A declaration had the wrong "type" (ie, value, type, type class)
86 -- Fields: declaration title, expected "type", actual "type".
87 | WrongDeclarationType P.ModuleName String String String
88 -- | A value declaration had the wrong type (in the sense of "type
89 -- checking"), eg, because the inferred type was used when the explicit type
91 -- Fields: module name, declaration name, actual type.
92 | ValueDeclarationWrongType P.ModuleName String P.Type
93 -- | A Type synonym has been rendered in an unexpected format
94 -- Fields: module name, declaration name, expected rendering, actual rendering
95 | TypeSynonymMismatch P.ModuleName String String String
100 | Fail AssertionFailure
103runAssertion :: Assertion -> Docs.Module -> AssertionResult
104runAssertion assertion Docs.Module{..} =
106 ShouldBeDocumented mn decl children ->
107 case findChildren decl (declarationsFor mn) of
109 Fail (NotDocumented mn decl)
110 Just actualChildren ->
111 case children \\ actualChildren of
113 cs -> Fail (ChildrenNotDocumented mn decl cs)
115 ShouldNotBeDocumented mn decl ->
116 case findChildren decl (declarationsFor mn) of
118 Fail (Documented mn decl)
122 ChildShouldNotBeDocumented mn decl child ->
123 case findChildren decl (declarationsFor mn) of
125 if child `elem` children
126 then Fail (ChildDocumented mn decl child)
129 Fail (NotDocumented mn decl)
131 ShouldBeConstrained mn decl tyClass ->
132 case find ((==) decl . Docs.declTitle) (declarationsFor mn) of
134 Fail (NotDocumented mn decl)
135 Just Docs.Declaration{..} ->
137 Docs.ValueDeclaration ty ->
138 if checkConstrained ty tyClass
140 else Fail (ConstraintMissing mn decl tyClass)
142 Fail (WrongDeclarationType mn decl "value"
143 (Docs.declInfoToString declInfo))
145 ValueShouldHaveTypeSignature mn decl (ShowFn tyPredicate) ->
146 case find ((==) decl . Docs.declTitle) (declarationsFor mn) of
148 Fail (NotDocumented mn decl)
149 Just Docs.Declaration{..} ->
151 Docs.ValueDeclaration ty ->
155 (ValueDeclarationWrongType mn decl ty)
157 Fail (WrongDeclarationType mn decl "value"
158 (Docs.declInfoToString declInfo))
160 TypeSynonymShouldRenderAs mn decl expected ->
161 case find ((==) decl . Docs.declTitle) (declarationsFor mn) of
163 Fail (NotDocumented mn decl)
164 Just Docs.Declaration{..} ->
166 Docs.TypeSynonymDeclaration [] ty ->
167 let actual = codeToString (Docs.renderType ty) in
168 if actual == expected
170 else Fail (TypeSynonymMismatch mn decl expected actual)
172 Fail (WrongDeclarationType mn decl "synonym"
173 (Docs.declInfoToString declInfo))
179 else fromMaybe [] (lookup mn modReExports)
182 fmap childrenTitles . find ((==) title . Docs.declTitle)
184 childrenTitles = map Docs.cdeclTitle . Docs.declChildren
186checkConstrained :: P.Type -> String -> Bool
187checkConstrained ty tyClass =
188 -- Note that we don't recurse on ConstrainedType if none of the constraints
189 -- match; this is by design, as constraints should be moved to the front
192 P.ConstrainedType cs _ | any (matches tyClass) cs ->
195 checkConstrained ty' tyClass
200 (==) className . P.runProperName . P.disqualify . P.constraintClass
202runAssertionIO :: Assertion -> Docs.Module -> IO ()
203runAssertionIO assertion mdl = do
204 putStrLn ("In " ++ P.runModuleName (Docs.modName mdl) ++ ": " ++ show assertion)
205 case runAssertion assertion mdl of
208 putStrLn ("Failed: " <> show reason)
211testCases :: [(String, [Assertion])]
214 [ -- From dependencies
215 ShouldBeDocumented (n "Prelude") "Unit" []
216 , ShouldNotBeDocumented (n "Prelude") "unit"
219 , ShouldBeDocumented (n "Example2") "one" []
220 , ShouldNotBeDocumented (n "Example2") "two"
223 [ ShouldBeDocumented (n "Example2") "one" []
224 , ShouldBeDocumented (n "Example2") "two" []
228 [ ShouldBeDocumented (n "UTF8") "thing" []
232 [ ShouldBeDocumented (n "Transitive2") "transitive3" []
236 [ ShouldBeDocumented (n "Prelude") "Boolean2" ["True"]
237 , ChildShouldNotBeDocumented (n "Prelude") "Boolean2" "False"
241 [ ShouldBeDocumented (n "Prelude") "Unit" []
242 , ShouldBeDocumented (n "DuplicateNames") "unit" []
243 , ShouldNotBeDocumented (n "Prelude") "unit"
247 [ ShouldBeDocumented (n "MultiVirtual1") "foo" []
248 , ShouldBeDocumented (n "MultiVirtual2") "bar" []
249 , ShouldBeDocumented (n "MultiVirtual2") "baz" []
253 [ ShouldBeDocumented (n "Clash1") "value" []
254 , ShouldBeDocumented (n "Clash1") "Type" []
255 , ShouldBeDocumented (n "Clash1") "TypeClass" ["typeClassMember"]
258 , ("SolitaryTypeClassMember",
259 [ ShouldBeDocumented (n "SomeTypeClass") "member" []
260 , ShouldNotBeDocumented (n "SomeTypeClass") "SomeClass"
261 , ShouldBeConstrained (n "SomeTypeClass") "member" "SomeClass"
264 , ("ReExportedTypeClass",
265 [ ShouldBeDocumented (n "SomeTypeClass") "SomeClass" ["member"]
268 , ("TypeClassWithoutMembers",
269 [ ShouldBeDocumented (n "TypeClassWithoutMembersIntermediate") "SomeClass" []
270 , ChildShouldNotBeDocumented (n "TypeClassWithoutMembersIntermediate") "SomeClass" "member"
274 [ ShouldBeDocumented (n "NewOperators2") "(>>>)" []
277 , ("ExplicitTypeSignatures",
278 [ ValueShouldHaveTypeSignature (n "ExplicitTypeSignatures") "explicit" (ShowFn (hasTypeVar "something"))
279 , ValueShouldHaveTypeSignature (n "ExplicitTypeSignatures") "anInt" (ShowFn (P.tyInt ==))
280 , ValueShouldHaveTypeSignature (n "ExplicitTypeSignatures") "aNumber" (ShowFn (P.tyNumber ==))
283 , ("ConstrainedArgument",
284 [ TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithoutArgs" "forall a. (Partial => a) -> a"
285 , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithArgs" "forall a. (Foo a => a) -> a"
286 , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "MultiWithoutArgs" "forall a. ((Partial, Partial) => a) -> a"
287 , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "MultiWithArgs" "forall a b. ((Foo a, Foo b) => a) -> a"
291 [ ValueShouldHaveTypeSignature (n "TypeOpAliases") "test1" (renderedType "forall a b. a ~> b")
292 , ValueShouldHaveTypeSignature (n "TypeOpAliases") "test2" (renderedType "forall a b c. a ~> b ~> c")
293 , ValueShouldHaveTypeSignature (n "TypeOpAliases") "test3" (renderedType "forall a b c d. a ~> (b ~> c) ~> d")
294 , ValueShouldHaveTypeSignature (n "TypeOpAliases") "test4" (renderedType "forall a b c d. ((a ~> b) ~> c) ~> d")
295 , ValueShouldHaveTypeSignature (n "TypeOpAliases") "third" (renderedType "forall a b c. a × b × c -> c")
300 n = P.moduleNameFromString
303 getAny . P.everythingOnTypes (<>) (Any . isVar varName)
305 isVar varName (P.TypeVar name) | varName == name = True
308 renderedType expected =
309 ShowFn $ \ty -> codeToString (Docs.renderType ty) == expected
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list