aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/model/JavaTest.kt
blob: 4cad784b11e33b74866cfa1f12c2151c2085c57e (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package model

import org.jetbrains.dokka.base.transformers.documentables.InheritorsInfo
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.sureClassNames
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.model.doc.Param
import org.jetbrains.dokka.model.doc.Text
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import utils.AbstractModelTest
import utils.assertNotNull
import utils.name

class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") {

    @Test
    fun function() {
        inlineModelTest(
            """
            |class Test {
            |    /**
            |     * Summary for Function
            |     * @param name is String parameter
            |     * @param value is int parameter
            |     */
            |    public void fn(String name, int value) {}
            |}
            """
        ) {
            with((this / "java" / "Test").cast<DClass>()) {
                name equals "Test"
                children counts 1
                with((this / "fn").cast<DFunction>()) {
                    name equals "fn"
                    val params = parameters.map { it.documentation.values.first().children.first() as Param }
                    params.mapNotNull { it.firstChildOfTypeOrNull<Text>()?.body } equals listOf("is String parameter", "is int parameter")
                }
            }
        }
    }

    @Test fun allImplementedInterfacesInJava() {
        inlineModelTest(
            """
            |interface Highest { }
            |interface Lower extends Highest { }
            |class Extendable { }
            |class Tested extends Extendable implements Lower { }
        """){
            with((this / "java" / "Tested").cast<DClass>()){
                extra[ImplementedInterfaces]?.interfaces?.entries?.single()?.value?.map { it.sureClassNames }?.sorted() equals listOf("Highest", "Lower").sorted()
            }
        }
    }

    //@Test fun function() {
    //        verifyJavaPackageMember("testdata/java/member.java", defaultModelConfig) { cls ->
    //            assertEquals("Test", cls.name)
    //            assertEquals(NodeKind.Class, cls.kind)
    //            with(cls.members(NodeKind.Function).single()) {
    //                assertEquals("fn", name)
    //                assertEquals("Summary for Function", content.summary.toTestString().trimEnd())
    //                assertEquals(3, content.sections.size)
    //                with(content.sections[0]) {
    //                    assertEquals("Parameters", tag)
    //                    assertEquals("name", subjectName)
    //                    assertEquals("render(Type:String,SUMMARY): is String parameter", toTestString())
    //                }
    //                with(content.sections[1]) {
    //                    assertEquals("Parameters", tag)
    //                    assertEquals("value", subjectName)
    //                    assertEquals("render(Type:Int,SUMMARY): is int parameter", toTestString())
    //                }
    //                assertEquals("Unit", detail(NodeKind.Type).name)
    //                assertTrue(members.none())
    //                assertTrue(links.none())
    //                with(details.first { it.name == "name" }) {
    //                    assertEquals(NodeKind.Parameter, kind)
    //                    assertEquals("String", detail(NodeKind.Type).name)
    //                }
    //                with(details.first { it.name == "value" }) {
    //                    assertEquals(NodeKind.Parameter, kind)
    //                    assertEquals("Int", detail(NodeKind.Type).name)
    //                }
    //            }
    //        }
    //    }

    @Test // todo
    fun memberWithModifiers() {
        inlineModelTest(
            """
            |class Test {
            |    /**
            |     * Summary for Function
            |     * @param name is String parameter
            |     * @param value is int parameter
            |     */
            |    public void fn(String name, int value) {}
            |}
            """
        ) {
            with((this / "java" / "Test" / "fn").cast<DFunction>()) {
                this
            }
        }
    }

    //    @Test fun memberWithModifiers() {
    //        verifyJavaPackageMember("testdata/java/memberWithModifiers.java", defaultModelConfig) { cls ->
    //            val modifiers = cls.details(NodeKind.Modifier).map { it.name }
    //            assertTrue("abstract" in modifiers)
    //            with(cls.members.single { it.name == "fn" }) {
    //                assertEquals("protected", details[0].name)
    //            }
    //            with(cls.members.single { it.name == "openFn" }) {
    //                assertEquals("open", details[1].name)
    //            }
    //        }
    //    }

    @Test
    fun superClass() {
        inlineModelTest(
            """
            |public class Foo extends Exception implements Cloneable {}
            """
        ) {
            with((this / "java" / "Foo").cast<DClass>()) {
                val sups = listOf("Exception", "Cloneable")
                assertTrue(
                    sups.all { s -> supertypes.values.flatten().any { it.classNames == s } })
                "Foo must extend ${sups.joinToString(", ")}"
            }
        }
    }

    @Test
    fun arrayType() {
        inlineModelTest(
            """
            |class Test {
            |    public String[] arrayToString(int[] data) {
            |      return null;
            |    }
            |}
            """
        ) {
            with((this / "java" / "Test").cast<DClass>()) {
                name equals "Test"
                children counts 1

                with((this / "arrayToString").cast<DFunction>()) {
                    name equals "arrayToString"
                    type.name equals "Array"
                    with(parameters.firstOrNull().assertNotNull("parameters")) {
                        name equals "data"
                        type.name equals "Array"
                    }
                }
            }
        }
    }

    @Test
    fun typeParameter() {
        inlineModelTest(
            """
            |class Foo<T extends Comparable<T>> {
            |     public <E> E foo();
            |}
            """
        ) {
            with((this / "java" / "Foo").cast<DClass>()) {
                generics counts 1
            }
        }
    }

    //    @Test fun typeParameter() {
    //        verifyJavaPackageMember("testdata/java/typeParameter.java", defaultModelConfig) { cls ->
    //            val typeParameters = cls.details(NodeKind.TypeParameter)
    //            with(typeParameters.single()) {
    //                assertEquals("T", name)
    //                with(detail(NodeKind.UpperBound)) {
    //                    assertEquals("Comparable", name)
    //                    assertEquals("T", detail(NodeKind.Type).name)
    //                }
    //            }
    //            with(cls.members(NodeKind.Function).single()) {
    //                val methodTypeParameters = details(NodeKind.TypeParameter)
    //                with(methodTypeParameters.single()) {
    //                    assertEquals("E", name)
    //                }
    //            }
    //        }
    //    }

    @Test
    fun constructors() {
        inlineModelTest(
            """
            |class Test {
            |  public Test() {}
            |
            |  public Test(String s) {}
            |}
            """
        ) {
            with((this / "java" / "Test").cast<DClass>()) {
                name equals "Test"

                constructors counts 2
                constructors.find { it.parameters.isNullOrEmpty() }.assertNotNull("Test()")

                with(constructors.find { it.parameters.isNotEmpty() }.assertNotNull("Test(String)")) {
                    parameters.firstOrNull()?.type?.name equals "String"
                }
            }
        }
    }

    @Test
    fun innerClass() {
        inlineModelTest(
            """
            |class InnerClass {
            |    public class D {}
            |}
            """
        ) {
            with((this / "java" / "InnerClass").cast<DClass>()) {
                children counts 1
                with((this / "D").cast<DClass>()) {
                    name equals "D"
                    children counts 0
                }
            }
        }
    }

    @Test
    fun varargs() {
        inlineModelTest(
            """
            |class Foo {
            |     public void bar(String... x);
            |}
            """
        ) {
            with((this / "java" / "Foo").cast<DClass>()) {
                name equals "Foo"
                children counts 1

                with((this / "bar").cast<DFunction>()) {
                    name equals "bar"
                    with(parameters.firstOrNull().assertNotNull("parameter")) {
                        name equals "x"
                        type.name equals "Array"
                    }
                }
            }
        }
    }

    @Test // todo
    fun fields() {
        inlineModelTest(
            """
            |class Test {
            |  public int i;
            |  public static final String s;
            |}
            """
        ) {
            with((this / "java" / "Test").cast<DClass>()) {
                children counts 2

                with((this / "i").cast<DProperty>()) {
                    getter equals null
                    setter equals null
                }

                with((this / "s").cast<DProperty>()) {
                    getter equals null
                    setter equals null
                }
            }
        }
    }

    //    @Test fun fields() {
    //        verifyJavaPackageMember("testdata/java/field.java", defaultModelConfig) { cls ->
    //            val i = cls.members(NodeKind.Property).single { it.name == "i" }
    //            assertEquals("Int", i.detail(NodeKind.Type).name)
    //            assertTrue("var" in i.details(NodeKind.Modifier).map { it.name })
    //
    //            val s = cls.members(NodeKind.Property).single { it.name == "s" }
    //            assertEquals("String", s.detail(NodeKind.Type).name)
    //            assertFalse("var" in s.details(NodeKind.Modifier).map { it.name })
    //            assertTrue("static" in s.details(NodeKind.Modifier).map { it.name })
    //        }
    //    }

    @Test
    fun staticMethod() {
        inlineModelTest(
            """
            |class C {
            |  public static void foo() {}
            |}
            """
        ) {
            with((this / "java" / "C" / "foo").cast<DFunction>()) {
                with(extra[AdditionalModifiers]!!.content.entries.single().value.assertNotNull("AdditionalModifiers")) {
                    this counts 1
                    first() equals ExtraModifiers.JavaOnlyModifiers.Static
                }
            }
        }
    }

    //    @Test fun staticMethod() { todo
    //        verifyJavaPackageMember("testdata/java/staticMethod.java", defaultModelConfig) { cls ->
    //            val m = cls.members(NodeKind.Function).single { it.name == "foo" }
    //            assertTrue("static" in m.details(NodeKind.Modifier).map { it.name })
    //        }
    //    }
    //
    //    /**
    //     *  `@suppress` not supported in Java!
    //     *
    //     *  [Proposed tags](https://www.oracle.com/technetwork/java/javase/documentation/proposed-tags-142378.html)
    //     *  Proposed tag `@exclude` for it, but not supported yet
    //     */
    //    @Ignore("@suppress not supported in Java!") @Test fun suppressTag() {
    //        verifyJavaPackageMember("testdata/java/suppressTag.java", defaultModelConfig) { cls ->
    //            assertEquals(1, cls.members(NodeKind.Function).size)
    //        }
    //    }

    @Test
    fun annotatedAnnotation() {
        inlineModelTest(
            """
            |import java.lang.annotation.*;
            |
            |@Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
            |public @interface Attribute {
            |  String value() default "";
            |}
            """
        ) {
            with((this / "java" / "Attribute").cast<DAnnotation>()) {
                with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) {
                    with(single()) {
                        dri.classNames equals "Target"
                        (params["value"].assertNotNull("value") as ArrayValue).value equals listOf(
                            EnumValue("ElementType.FIELD", DRI("java.lang.annotation", "ElementType")),
                            EnumValue("ElementType.TYPE", DRI("java.lang.annotation", "ElementType")),
                            EnumValue("ElementType.METHOD", DRI("java.lang.annotation", "ElementType"))
                        )
                    }
                }
            }
        }
    }

    //    @Test fun deprecation() {
    //        verifyJavaPackageMember("testdata/java/deprecation.java", defaultModelConfig) { cls ->
    //            val fn = cls.members(NodeKind.Function).single()
    //            assertEquals("This should no longer be used", fn.deprecation!!.content.toTestString())
    //        }
    //    }

    @Test
    fun javaLangObject() {
        inlineModelTest(
            """
            |class Test {
            |  public Object fn() { return null; }
            |}
            """
        ) {
            with((this / "java" / "Test" / "fn").cast<DFunction>()) {
                assertTrue(type is JavaObject)
            }
        }
    }

    //    @Test fun javaLangObject() {
    //        verifyJavaPackageMember("testdata/java/javaLangObject.java", defaultModelConfig) { cls ->
    //            val fn = cls.members(NodeKind.Function).single()
    //            assertEquals("Any", fn.detail(NodeKind.Type).name)
    //        }
    //    }

    @Test
    fun enumValues() {
        inlineModelTest(
            """
            |enum E {
            |  Foo
            |}
            """
        ) {
            with((this / "java" / "E").cast<DEnum>()) {
                name equals "E"
                entries counts 1

                with((this / "Foo").cast<DEnumEntry>()) {
                    name equals "Foo"
                }
            }
        }
    }

    @Test
    fun inheritorLinks() {
        inlineModelTest(
            """
            |public class InheritorLinks {
            |  public static class Foo {}
            |
            |  public static class Bar extends Foo {}
            |}
            """
        ) {
            with((this / "java" / "InheritorLinks").cast<DClass>()) {
                val dri = (this / "Bar").assertNotNull("Foo dri").dri
                with((this / "Foo").cast<DClass>()) {
                    with(extra[InheritorsInfo].assertNotNull("InheritorsInfo")) {
                        with(value.values.flatten().distinct()) {
                            this counts 1
                            first() equals dri
                        }
                    }
                }
            }
        }
    }

    //    todo
    //    @Test fun inheritorLinks() {
    //        verifyJavaPackageMember("testdata/java/InheritorLinks.java", defaultModelConfig) { cls ->
    //            val fooClass = cls.members.single { it.name == "Foo" }
    //            val inheritors = fooClass.references(RefKind.Inheritor)
    //            assertEquals(1, inheritors.size)
    //        }
    //    }
}