aboutsummaryrefslogtreecommitdiff
path: root/compiler/test/dotty/tools/dotc/parsing/DocstringTests.scala
blob: 930ec117a6b8b93f64786c4180a4a222d47ba0e9 (plain) (blame)
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package dotty.tools
package dotc
package parsing

import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.ast.Trees._

import org.junit.Assert._
import org.junit.Test

class DocstringTests extends DocstringTest {

  @Test def noComment = {
    import dotty.tools.dotc.ast.untpd._
    val source = "class Class"

    checkFrontend(source) {
      case PackageDef(_, Seq(c: TypeDef)) =>
        assert(c.rawComment.map(_.raw) == None, "Should not have a comment, mainly used for exhaustive tests")
    }
  }

  @Test def singleClassInPackage = {
    val source =
      """
      |package a
      |
      |/** Hello world! */
      |class Class(val x: String)
      """.stripMargin

    checkFrontend(source) {
      case PackageDef(_, Seq(t @ TypeDef(name, _))) if name.toString == "Class" =>
        checkDocString(t.rawComment.map(_.raw), "/** Hello world! */")
    }
  }

  @Test def multipleOpenedOnSingleClassInPackage = {
    val source =
      """
      |package a
      |
      |/** Hello /* multiple open */ world! */
      |class Class(val x: String)
      """.stripMargin

    checkFrontend(source) {
      case PackageDef(_, Seq(t @ TypeDef(name, _))) if name.toString == "Class" =>
        checkDocString(t.rawComment.map(_.raw), "/** Hello /* multiple open */ world! */")
    }
  }
  @Test def multipleClassesInPackage = {
    val source =
      """
      |package a
      |
      |/** Class1 docstring */
      |class Class1(val x: String)
      |
      |/** Class2 docstring */
      |class Class2(val x: String)
      """.stripMargin

    checkCompile("frontend", source) { (_, ctx) =>
      ctx.compilationUnit.untpdTree match {
        case PackageDef(_, Seq(c1 @ TypeDef(_,_), c2 @ TypeDef(_,_))) => {
          checkDocString(c1.rawComment.map(_.raw), "/** Class1 docstring */")
          checkDocString(c2.rawComment.map(_.raw), "/** Class2 docstring */")
        }
      }
    }
  }

  @Test def singleCaseClassWithoutPackage = {
    val source =
      """
      |/** Class without package */
      |case class Class(val x: Int)
      """.stripMargin

    checkFrontend(source) {
      case PackageDef(_, Seq(t @ TypeDef(_,_))) => checkDocString(t.rawComment.map(_.raw), "/** Class without package */")
    }
  }

  @Test def SingleTraitWihoutPackage = {
    val source = "/** Trait docstring */\ntrait Trait"

    checkFrontend(source) {
      case PackageDef(_, Seq(t @ TypeDef(_,_))) => checkDocString(t.rawComment.map(_.raw), "/** Trait docstring */")
    }
  }

  @Test def multipleTraitsWithoutPackage = {
    val source =
      """
      |/** Trait1 docstring */
      |trait Trait1
      |
      |/** Trait2 docstring */
      |trait Trait2
      """.stripMargin

    checkFrontend(source) {
      case PackageDef(_, Seq(t1 @ TypeDef(_,_), t2 @ TypeDef(_,_))) => {
        checkDocString(t1.rawComment.map(_.raw), "/** Trait1 docstring */")
        checkDocString(t2.rawComment.map(_.raw), "/** Trait2 docstring */")
      }
    }
  }

  @Test def multipleMixedEntitiesWithPackage = {
    val source =
      """
      |/** Trait1 docstring */
      |trait Trait1
      |
      |/** Class2 docstring */
      |class Class2(val x: Int)
      |
      |/** CaseClass3 docstring */
      |case class CaseClass3()
      |
      |case class NoComment()
      |
      |/** AbstractClass4 docstring */
      |abstract class AbstractClass4(val x: Int)
      """.stripMargin

    checkFrontend(source) {
      case PackageDef(_, Seq(t1 @ TypeDef(_,_), c2 @ TypeDef(_,_), cc3 @ TypeDef(_,_), _, ac4 @ TypeDef(_,_))) => {
        checkDocString(t1.rawComment.map(_.raw), "/** Trait1 docstring */")
        checkDocString(c2.rawComment.map(_.raw), "/** Class2 docstring */")
        checkDocString(cc3.rawComment.map(_.raw), "/** CaseClass3 docstring */")
        checkDocString(ac4.rawComment.map(_.raw), "/** AbstractClass4 docstring */")
      }
    }
  }

  @Test def nestedClass = {
    val source =
      """
      |/** Outer docstring */
      |class Outer {
      |  /** Inner docstring */
      |  class Inner(val x: Int)
      |}
      """.stripMargin

    checkFrontend(source) {
      case PackageDef(_, Seq(outer @ TypeDef(_, tpl @ Template(_,_,_,_)))) => {
        checkDocString(outer.rawComment.map(_.raw), "/** Outer docstring */")
        tpl.body match {
          case (inner @ TypeDef(_,_)) :: _ => checkDocString(inner.rawComment.map(_.raw), "/** Inner docstring */")
          case _ => assert(false, "Couldn't find inner class")
        }
      }
    }
  }

  @Test def nestedClassThenOuter = {
    val source =
      """
      |/** Outer1 docstring */
      |class Outer1 {
      |  /** Inner docstring */
      |  class Inner(val x: Int)
      |}
      |
      |/** Outer2 docstring */
      |class Outer2
      """.stripMargin

    checkFrontend(source) {
      case PackageDef(_, Seq(o1 @ TypeDef(_, tpl @ Template(_,_,_,_)), o2 @ TypeDef(_,_))) => {
        checkDocString(o1.rawComment.map(_.raw), "/** Outer1 docstring */")
        checkDocString(o2.rawComment.map(_.raw), "/** Outer2 docstring */")
        tpl.body match {
          case (inner @ TypeDef(_,_)) :: _ => checkDocString(inner.rawComment.map(_.raw), "/** Inner docstring */")
          case _ => assert(false, "Couldn't find inner class")
        }
      }
    }
  }

  @Test def objects = {
    val source =
      """
      |package p
      |
      |/** Object1 docstring */
      |object Object1
      |
      |/** Object2 docstring */
      |object Object2
      """.stripMargin

    checkFrontend(source) {
      case p @ PackageDef(_, Seq(o1: MemberDef[Untyped], o2: MemberDef[Untyped])) => {
        assertEquals(o1.name.toString, "Object1")
        checkDocString(o1.rawComment.map(_.raw), "/** Object1 docstring */")
        assertEquals(o2.name.toString, "Object2")
        checkDocString(o2.rawComment.map(_.raw), "/** Object2 docstring */")
      }
    }
  }

  @Test def objectsNestedClass = {
    val source =
      """
      |package p
      |
      |/** Object1 docstring */
      |object Object1
      |
      |/** Object2 docstring */
      |object Object2 {
      |  class A1
      |  /** Inner docstring */
      |  class Inner
      |}
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case p @ PackageDef(_, Seq(o1: ModuleDef, o2: ModuleDef)) => {
        assert(o1.name.toString == "Object1")
        checkDocString(o1.rawComment.map(_.raw), "/** Object1 docstring */")
        assert(o2.name.toString == "Object2")
        checkDocString(o2.rawComment.map(_.raw), "/** Object2 docstring */")

        o2.impl.body match {
          case _ :: (inner @ TypeDef(_,_)) :: _ => checkDocString(inner.rawComment.map(_.raw), "/** Inner docstring */")
          case _ => assert(false, "Couldn't find inner class")
        }
      }
    }
  }

  @Test def packageObject = {
    val source =
      """
      |/** Package object docstring */
      |package object foo {
      |  /** Boo docstring */
      |  case class Boo()
      |
      |  /** Trait docstring */
      |  trait Trait
      |
      |  /** InnerObject docstring */
      |  object InnerObject {
      |    /** InnerClass docstring */
      |    class InnerClass
      |  }
      |}
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case PackageDef(_, Seq(p: ModuleDef)) => {
        checkDocString(p.rawComment.map(_.raw), "/** Package object docstring */")

        p.impl.body match {
          case (b: TypeDef) :: (t: TypeDef) :: (o: ModuleDef) :: Nil => {
            checkDocString(b.rawComment.map(_.raw), "/** Boo docstring */")
            checkDocString(t.rawComment.map(_.raw), "/** Trait docstring */")
            checkDocString(o.rawComment.map(_.raw), "/** InnerObject docstring */")
            checkDocString(o.impl.body.head.asInstanceOf[TypeDef].rawComment.map(_.raw), "/** InnerClass docstring */")
          }
          case _ => assert(false, "Incorrect structure inside package object")
        }
      }
    }
  }

  @Test def multipleDocStringsBeforeEntity = {
    val source =
      """
      |/** First comment */
      |/** Second comment */
      |/** Real comment */
      |class Class
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case PackageDef(_, Seq(c: TypeDef)) =>
        checkDocString(c.rawComment.map(_.raw), "/** Real comment */")
    }
  }

  @Test def multipleDocStringsBeforeAndAfter = {
    val source =
      """
      |/** First comment */
      |/** Second comment */
      |/** Real comment */
      |class Class
      |/** Following comment 1 */
      |/** Following comment 2 */
      |/** Following comment 3 */
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case PackageDef(_, Seq(c: TypeDef)) =>
        checkDocString(c.rawComment.map(_.raw), "/** Real comment */")
    }
  }

  @Test def valuesWithDocString = {
    val source =
      """
      |object Object {
      |  /** val1 */
      |  val val1 = 1
      |
      |  /** val2 */
      |  val val2: Int = 2
      |  /** bogus docstring */
      |
      |  /** bogus docstring */
      |  /** val3 */
      |  val val3: List[Int] = 1 :: 2 :: 3 :: Nil
      |}
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case PackageDef(_, Seq(o: ModuleDef)) => {
        o.impl.body match {
          case (v1: MemberDef) :: (v2: MemberDef) :: (v3: MemberDef) :: Nil => {
            checkDocString(v1.rawComment.map(_.raw), "/** val1 */")
            checkDocString(v2.rawComment.map(_.raw), "/** val2 */")
            checkDocString(v3.rawComment.map(_.raw), "/** val3 */")
          }
          case _ => assert(false, "Incorrect structure inside object")
        }
      }
    }
  }

  @Test def varsWithDocString = {
    val source =
      """
      |object Object {
      |  /** var1 */
      |  var var1 = 1
      |
      |  /** var2 */
      |  var var2: Int = 2
      |  /** bogus docstring */
      |
      |  /** bogus docstring */
      |  /** var3 */
      |  var var3: List[Int] = 1 :: 2 :: 3 :: Nil
      |}
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case PackageDef(_, Seq(o: ModuleDef)) => {
        o.impl.body match {
          case (v1: MemberDef) :: (v2: MemberDef) :: (v3: MemberDef) :: Nil => {
            checkDocString(v1.rawComment.map(_.raw), "/** var1 */")
            checkDocString(v2.rawComment.map(_.raw), "/** var2 */")
            checkDocString(v3.rawComment.map(_.raw), "/** var3 */")
          }
          case _ => assert(false, "Incorrect structure inside object")
        }
      }
    }
  }

  @Test def defsWithDocString = {
    val source =
      """
      |object Object {
      |  /** def1 */
      |  def def1 = 1
      |
      |  /** def2 */
      |  def def2: Int = 2
      |  /** bogus docstring */
      |
      |  /** bogus docstring */
      |  /** def3 */
      |  def def3: List[Int] = 1 :: 2 :: 3 :: Nil
      |}
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case PackageDef(_, Seq(o: ModuleDef)) => {
        o.impl.body match {
          case (v1: MemberDef) :: (v2: MemberDef) :: (v3: MemberDef) :: Nil => {
            checkDocString(v1.rawComment.map(_.raw), "/** def1 */")
            checkDocString(v2.rawComment.map(_.raw), "/** def2 */")
            checkDocString(v3.rawComment.map(_.raw), "/** def3 */")
          }
          case _ => assert(false, "Incorrect structure inside object")
        }
      }
    }
  }

  @Test def typesWithDocString = {
    val source =
      """
      |object Object {
      |  /** type1 */
      |  type T1 = Int
      |
      |  /** type2 */
      |  type T2 = String
      |  /** bogus docstring */
      |
      |  /** bogus docstring */
      |  /** type3 */
      |  type T3 = T2
      |}
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case PackageDef(_, Seq(o: ModuleDef)) => {
        o.impl.body match {
          case (v1: MemberDef) :: (v2: MemberDef) :: (v3: MemberDef) :: Nil => {
            checkDocString(v1.rawComment.map(_.raw), "/** type1 */")
            checkDocString(v2.rawComment.map(_.raw), "/** type2 */")
            checkDocString(v3.rawComment.map(_.raw), "/** type3 */")
          }
          case _ => assert(false, "Incorrect structure inside object")
        }
      }
    }
  }

  @Test def defInnerClass = {
    val source =
      """
      |object Foo {
      |  def foo() = {
      |    /** Innermost */
      |    class Innermost
      |  }
      |}
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case PackageDef(_, Seq(o: ModuleDef)) =>
        o.impl.body match {
          case (foo: MemberDef) :: Nil =>
            expectNoDocString(foo.rawComment.map(_.raw))
          case _ => assert(false, "Incorrect structure inside object")
        }
    }
  }

  @Test def withExtends = {
    val source =
      """
      |trait Trait1
      |/** Class1 */
      |class Class1 extends Trait1
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case p @ PackageDef(_, Seq(_, c: TypeDef)) =>
        checkDocString(c.rawComment.map(_.raw), "/** Class1 */")
    }
  }

  @Test def withAnnotation = {
    val source =
      """
      |/** Class1 */
      |@SerialVersionUID(1)
      |class Class1
      """.stripMargin

    import dotty.tools.dotc.ast.untpd._
    checkFrontend(source) {
      case p @ PackageDef(_, Seq(c: TypeDef)) =>
        checkDocString(c.rawComment.map(_.raw), "/** Class1 */")
    }
  }
} /* End class */