-
Notifications
You must be signed in to change notification settings - Fork 10
/
ApidocDirectiveSpec.scala
349 lines (314 loc) · 18.5 KB
/
ApidocDirectiveSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright 2018 Lightbend Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lightbend.paradox.apidoc
import java.io.IOException
import com.lightbend.paradox.ParadoxException
import com.lightbend.paradox.markdown.{MarkdownTestkit, Writer}
import io.github.classgraph.ScanResult
import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpecLike
class ApidocDirectiveSpec extends MarkdownTestkit with Matchers with AnyFlatSpecLike {
val rootPackage = "akka"
val allClasses = Vector(
"akka.actor.ActorRef",
"akka.actor.typed.ActorRef",
"akka.cluster.client.ClusterClient",
"akka.cluster.client.ClusterClient$",
"akka.cluster.ddata.Replicator",
"akka.cluster.ddata.Replicator$",
"akka.cluster.ddata.typed.scaladsl.Replicator",
"akka.cluster.ddata.typed.scaladsl.Replicator$",
"akka.cluster.ddata.typed.javadsl.Replicator",
"akka.cluster.ddata.typed.javadsl.Replicator$",
"akka.dispatch.Envelope",
"akka.http.javadsl.model.sse.ServerSentEvent",
"akka.http.javadsl.marshalling.Marshaller",
"akka.http.javadsl.marshalling.Marshaller$",
"akka.http.scaladsl.marshalling.Marshaller",
"akka.http.scaladsl.marshalling.Marshaller$",
"akka.stream.javadsl.Source",
"akka.stream.javadsl.Source$",
"akka.stream.scaladsl.Source",
"akka.stream.scaladsl.Source$",
"akka.stream.javadsl.Flow",
"akka.stream.javadsl.Flow$",
"akka.stream.scaladsl.Flow",
"akka.stream.scaladsl.Flow$",
"akka.stream.scaladsl.JavaFlowSupport$",
"akka.stream.javadsl.JavaFlowSupport",
"akka.kafka.Metadata",
"akka.kafka.Metadata$",
"akka.kafka.javadsl.Producer",
"akka.kafka.javadsl.Producer$",
"akka.kafka.scaladsl.Producer",
"akka.kafka.scaladsl.Producer$",
"akka.kafka.scaladsl.Consumer$Control",
"akka.kafka.javadsl.Consumer$Control",
"akka.kafka.scaladsl.Consumer$Control$$anonfun$drainAndShutdown$2",
"akka.actor.typed.receptionist.Receptionist$Command"
)
override val markdownWriter = new Writer(
linkRenderer = Writer.defaultLinks,
verbatimSerializers = Writer.defaultVerbatims,
serializerPlugins = Writer.defaultPlugins(
Writer.defaultDirectives ++ Seq((ctx: Writer.Context) =>
new ApidocDirective(null: ScanResult, allClasses, ctx) {
override def containsOnlyStaticForwarders(classname: String): Boolean =
"akka.kafka.Metadata" == classname ||
"akka.kafka.scaladsl.Producer" == classname
}
)
)
)
implicit val context = writerContextWithProperties(
"javadoc.link_style" -> "frames",
"scaladoc.akka.base_url" -> "https://doc.akka.io/api/akka/2.5",
"scaladoc.akka.http.base_url" -> "https://doc.akka.io/api/akka-http/current",
"javadoc.akka.base_url" -> "https://doc.akka.io/japi/akka/2.5",
"javadoc.akka.http.base_url" -> "https://doc.akka.io/japi/akka-http/current",
"scaladoc.akka.kafka.base_url" -> "https://doc.akka.io/api/alpakka-kafka/current",
"javadoc.akka.kafka.base_url" -> ""
)
"Apidoc directive" should "generate markdown correctly when there is only one match" in {
markdown("@apidoc[Envelope]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/dispatch/Envelope.html" title="akka.dispatch.Envelope"><code>Envelope</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/dispatch/Envelope.html" title="akka.dispatch.Envelope"><code>Envelope</code></a></span>
|</p>""".stripMargin
)
}
it should "throw an exception when there is no match" in {
val thrown = the[ParadoxException] thrownBy markdown("@apidoc[ThereIsNoSuchClass]")
thrown.getMessage shouldEqual
"No matches found for apidoc query [ThereIsNoSuchClass]"
}
it should "fail when pointing to a static-forwarders-only class" in {
val thrown = the[ParadoxException] thrownBy markdown("@apidoc[Metadata]")
thrown.getMessage shouldEqual
"Class `akka.kafka.Metadata` matches @apidoc[Metadata], but is empty, did you intend to link to the object?"
}
it should "fail when pointing to a static-forwarders-only class (in scaladsl/javadsl)" in {
val thrown = the[ParadoxException] thrownBy markdown("@apidoc[Producer]")
thrown.getMessage shouldEqual
"Class `akka.kafka.scaladsl.Producer` matches @apidoc[Producer], but is empty, did you intend to link to the object?"
}
it should "fail when pointing to a static-forwarders-only class (with pattern)" in {
val thrown = the[ParadoxException] thrownBy markdown("@apidoc[akka.kafka.(java|scala)dsl.Producer]")
thrown.getMessage shouldEqual
"Class `akka.kafka.scaladsl.Producer` matches @apidoc[akka.kafka.(java|scala)dsl.Producer], but is empty, did you intend to link to the object?"
}
it should "generate markdown correctly when 2 matches found and their package names include javadsl/scaladsl" in {
markdown("@apidoc[Flow]") shouldEqual
html(
"""<p><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/Flow.html" title="akka.stream.javadsl.Flow"><code>Flow</code></a></span><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/Flow.html" title="akka.stream.scaladsl.Flow"><code>Flow</code></a></span>
|</p>""".stripMargin
)
}
it should "allow linking to a typed class that is also present in classic" in {
markdown("@apidoc[typed.*.Replicator$]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/cluster/ddata/typed/scaladsl/Replicator$.html" title="akka.cluster.ddata.typed.scaladsl.Replicator"><code>Replicator</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/cluster/ddata/typed/javadsl/Replicator.html" title="akka.cluster.ddata.typed.javadsl.Replicator"><code>Replicator</code></a></span>
|</p>""".stripMargin
)
}
it should "allow linking to a classic class that is also present in typed" in {
markdown("@apidoc[ddata.Replicator$]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/cluster/ddata/Replicator$.html" title="akka.cluster.ddata.Replicator"><code>Replicator</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/cluster/ddata/Replicator.html" title="akka.cluster.ddata.Replicator"><code>Replicator</code></a></span>
|</p>""".stripMargin
)
}
it should "throw an exception when two matches found but javadsl/scaladsl is not in their packages" in {
val thrown = the[ParadoxException] thrownBy markdown("@apidoc[ActorRef]")
thrown.getMessage shouldEqual
"2 matches found for ActorRef, but not javadsl/scaladsl: akka.actor.ActorRef, akka.actor.typed.ActorRef. You may want to use the fully qualified class name as @apidoc[fqcn] instead of @apidoc[ActorRef]. For examples see https://github.com/lightbend/sbt-paradox-apidoc#examples"
}
it should "generate markdown correctly when fully qualified class name (fqcn) is specified as @apidoc[fqcn]" in {
markdown("@apidoc[akka.actor.ActorRef]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/actor/ActorRef.html" title="akka.actor.ActorRef"><code>ActorRef</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/actor/ActorRef.html" title="akka.actor.ActorRef"><code>ActorRef</code></a></span>
|</p>""".stripMargin
)
}
it should "find a class by partial fqcn" in {
markdown("@apidoc[actor.typed.ActorRef]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/actor/typed/ActorRef.html" title="akka.actor.typed.ActorRef"><code>ActorRef</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/actor/typed/ActorRef.html" title="akka.actor.typed.ActorRef"><code>ActorRef</code></a></span>
|</p>""".stripMargin
)
}
it should "generate markdown correctly for a companion object" in {
markdown("@apidoc[ClusterClient$]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/cluster/client/ClusterClient$.html" title="akka.cluster.client.ClusterClient"><code>ClusterClient</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/cluster/client/ClusterClient.html" title="akka.cluster.client.ClusterClient"><code>ClusterClient</code></a></span>
|</p>""".stripMargin
)
}
it should "generate markdown correctly for scaladsl `object` and javadsl `class`" in {
markdown("@apidoc[JavaFlowSupport]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/JavaFlowSupport$.html" title="akka.stream.scaladsl.JavaFlowSupport"><code>JavaFlowSupport</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/JavaFlowSupport.html" title="akka.stream.javadsl.JavaFlowSupport"><code>JavaFlowSupport</code></a></span>
|</p>""".stripMargin
)
}
it should "generate markdown correctly for type parameter and wildcard" in {
markdown("@apidoc[Source[ServerSentEvent, \\_]]") shouldEqual
html(
"""<p><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/Source.html" title="akka.stream.javadsl.Source"><code>Source<ServerSentEvent, ?></code></a></span><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/Source.html" title="akka.stream.scaladsl.Source"><code>Source[ServerSentEvent, _]</code></a></span>
|</p>""".stripMargin
)
}
it should "generate markdown correctly for type parameters with concrete names" in {
markdown("@apidoc[Flow[Message, Message, Mat]]") shouldEqual
html(
"""<p><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/Flow.html" title="akka.stream.javadsl.Flow"><code>Flow<Message, Message, Mat></code></a></span><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/Flow.html" title="akka.stream.scaladsl.Flow"><code>Flow[Message, Message, Mat]</code></a></span>
|</p>""".stripMargin
)
}
it should "generate markdown correctly for nested type parameters" in {
markdown("@apidoc[Marshaller[Try[A], B]]") shouldEqual
html(
"""<p><span class="group-java">
|<a href="https://doc.akka.io/japi/akka-http/current/?akka/http/javadsl/marshalling/Marshaller.html" title="akka.http.javadsl.marshalling.Marshaller"><code>Marshaller<Try<A>, B></code></a></span><span class="group-scala">
|<a href="https://doc.akka.io/api/akka-http/current/akka/http/scaladsl/marshalling/Marshaller.html" title="akka.http.scaladsl.marshalling.Marshaller"><code>Marshaller[Try[A], B]</code></a></span>
|</p>""".stripMargin
)
}
"Anchor attributes" should "be used" in {
markdown("""The @apidoc[Flow] { scala="#method():Unit" java="#method()" } thingie""") shouldEqual
html(
"""<p>The <span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/Flow.html#method()" title="akka.stream.javadsl.Flow"><code>Flow</code></a></span><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/Flow.html#method():Unit" title="akka.stream.scaladsl.Flow"><code>Flow</code></a></span>
|thingie</p>""".stripMargin
)
}
"Inner classes" should "be linked (only scaladoc)" in {
markdown("@apidoc[Consumer.Control]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/alpakka-kafka/current/akka/kafka/scaladsl/Consumer$$Control.html" title="akka.kafka.scaladsl.Consumer.Control"><code>Consumer.Control</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/api/alpakka-kafka/current/akka/kafka/javadsl/Consumer$$Control.html" title="akka.kafka.javadsl.Consumer.Control"><code>Consumer.Control</code></a></span>
|</p>""".stripMargin
)
}
it should "be linked with a label and generics (only scaladoc)" in {
markdown("@apidoc[Consumer.Control[T]](Consumer.Control)") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/alpakka-kafka/current/akka/kafka/scaladsl/Consumer$$Control.html" title="akka.kafka.scaladsl.Consumer.Control"><code>Consumer.Control[T]</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/api/alpakka-kafka/current/akka/kafka/javadsl/Consumer$$Control.html" title="akka.kafka.javadsl.Consumer.Control"><code>Consumer.Control<T></code></a></span>
|</p>""".stripMargin
)
}
it should "be linked with a regex" in {
markdown("@apidoc[akka.kafka.(scaladsl|javadsl).Consumer.Control]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/alpakka-kafka/current/akka/kafka/scaladsl/Consumer$$Control.html" title="akka.kafka.scaladsl.Consumer.Control"><code>Consumer.Control</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/api/alpakka-kafka/current/akka/kafka/javadsl/Consumer$$Control.html" title="akka.kafka.javadsl.Consumer.Control"><code>Consumer.Control</code></a></span>
|</p>""".stripMargin
)
}
it should "be linked with a regex and label" in {
markdown("@apidoc[Consumer.Control](akka.kafka.(scaladsl|javadsl).Consumer.Control)") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/alpakka-kafka/current/akka/kafka/scaladsl/Consumer$$Control.html" title="akka.kafka.scaladsl.Consumer.Control"><code>Consumer.Control</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/api/alpakka-kafka/current/akka/kafka/javadsl/Consumer$$Control.html" title="akka.kafka.javadsl.Consumer.Control"><code>Consumer.Control</code></a></span>
|</p>""".stripMargin
)
}
it should "generate links to inner classes" in {
markdown("@apidoc[Receptionist.Command]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/actor/typed/receptionist/Receptionist$$Command.html" title="akka.actor.typed.receptionist.Receptionist.Command"><code>Receptionist.Command</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/actor/typed/receptionist/Receptionist.Command.html" title="akka.actor.typed.receptionist.Receptionist.Command"><code>Receptionist.Command</code></a></span>
|</p>""".stripMargin
)
}
"Directive with label and source" should "use the source as class pattern" in {
markdown("The @apidoc[TheClass.method](Flow) { .scaladoc a=1 } thingie") shouldEqual
html(
"""<p>The <span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/Flow.html" title="akka.stream.javadsl.Flow"><code>TheClass.method</code></a></span><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/Flow.html" title="akka.stream.scaladsl.Flow"><code>TheClass.method</code></a></span>
|thingie</p>""".stripMargin
)
}
it should "adapt generics notation from the label" in {
markdown("The @apidoc[TheClass[File].method[String]](Flow) { .scaladoc a=1 } thingie") shouldEqual
html(
"""<p>The <span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/Flow.html" title="akka.stream.javadsl.Flow"><code>TheClass<File>.method<String></code></a></span><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/Flow.html" title="akka.stream.scaladsl.Flow"><code>TheClass[File].method[String]</code></a></span>
|thingie</p>""".stripMargin
)
}
it should "use anchors" in {
markdown(
"""The @apidoc[TheClass[File].method[String]](Flow) { scala="#method():Unit" java="#method()" } thingie"""
) shouldEqual
html(
"""<p>The <span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/Flow.html#method()" title="akka.stream.javadsl.Flow"><code>TheClass<File>.method<String></code></a></span><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/Flow.html#method():Unit" title="akka.stream.scaladsl.Flow"><code>TheClass[File].method[String]</code></a></span>
|thingie</p>""".stripMargin
)
}
it should "use anchors for methods with scala bounded types" in {
markdown(
"""The @apidoc[label](Flow) { scala="#method%5BT%3C:Q[T]](Flow=%3EUnit):Unit" java="#method()" } thingie"""
) shouldEqual
html(
"""<p>The <span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/Flow.html#method()" title="akka.stream.javadsl.Flow"><code>label</code></a></span><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/Flow.html#method[T%3C:Q[T]](Flow=%3EUnit):Unit" title="akka.stream.scaladsl.Flow"><code>label</code></a></span>
|thingie</p>""".stripMargin
)
}
it should "catch exception on malformed URIs and make suggestions" in {
try
markdown(
"""The @apidoc[label](Flow) { scala="#method[ T <: Q[T] ](Flow => Unit):Unit" java="#method()" } thingie"""
)
catch {
case t @ ParadoxException(error) =>
error.msg should include("template resulted in an invalid URL")
error.msg should include("method%5B T %3C: Q%5BT] ](Flow =%3E Unit):Unit")
}
}
}