aboutsummaryrefslogtreecommitdiff
path: root/doc-tool/test/UsecaseTest.scala
blob: 4eb072b98ff8fe8137c1120010e9d7cbafb301ae (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
package dotty.tools
package dottydoc

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

import dotc.util.SourceFile
import model._
import model.internal._
import model.references._
import util.syntax._

class UsecaseTest extends DottyDocTest {
  @Test def simpleUsecase = {
    val source = new SourceFile(
      "DefWithUseCase.scala",
      """
      |package scala
      |
      |trait Test[A] {
      |  /** Definition with a "disturbing" signature
      |   *
      |   *  @usecase def foo: A
      |   */
      |  def foo[B]: A => B
      |}
      """.stripMargin
    )

    checkSources(source :: Nil) { packages =>
      packages("scala") match {
        case PackageImpl(_, _, _, List(trt: Trait), _, _, _, _) =>
          val List(foo: Def) = trt.members

          assert(foo.comment.isDefined, "Lost comment in transformations")

          val returnValue = foo.returnValue match {
            case ref: TypeReference => ref.title
            case _ =>
              assert(
                false,
                "Incorrect return value after usecase transformation"
              )
              ""
          }

          assert(
            foo.typeParams.isEmpty,
            "Type parameters were not stripped by usecase"
          )
          assert(returnValue == "A", "Incorrect return type after usecase")

          assert(foo.name == "foo", s"Incorrect name after transform: ${foo.name}")
      }
    }
  }

  @Test def simpleUsecaseAddedArg = {
    val source = new SourceFile(
      "DefWithUseCase.scala",
      """
      |package scala
      |
      |trait Test[A] {
      |  /** Definition with a "disturbing" signature
      |   *
      |   *  @usecase def foo(a: A): A
      |   */
      |  def foo[B]: A => B
      |}
      """.stripMargin
    )

    checkSources(source :: Nil) { packages =>
      packages("scala") match {
        case PackageImpl(_, _, _, List(trt: Trait), _, _, _, _) =>
          val List(foo: Def) = trt.members

          val returnValue = foo.returnValue match {
            case ref: TypeReference => ref.title
            case _ =>
              assert(
                false,
                "Incorrect return value after usecase transformation"
              )
              ""
          }

          assert(
            foo.typeParams.isEmpty,
            "Type parameters were not stripped by usecase"
          )
          assert(returnValue == "A", "Incorrect return type after usecase")
          assert(
            foo.paramLists.head.list.head.title == "a",
            "Incorrect parameter to function after usecase transformation"
          )
          assert(foo.name == "foo", s"Incorrect name after transform: ${foo.name}")
      }
    }
  }

  @Test def simpleTparamUsecase = {
    val source = new SourceFile(
      "DefWithUseCase.scala",
      """
      |package scala
      |
      |trait Test[A] {
      |  /** Definition with a "disturbing" signature
      |   *
      |   *  @usecase def foo[C]: A
      |   */
      |  def foo[B]: A => B
      |}
      """.stripMargin
    )

    checkSources(source :: Nil) { packages =>
      packages("scala") match {
        case PackageImpl(_, _, _, List(trt: Trait), _, _, _, _) =>
          val List(foo: Def) = trt.members

          val returnValue = foo.returnValue match {
            case ref: TypeReference => ref.title
            case _ =>
              assert(
                false,
                "Incorrect return value after usecase transformation"
              )
              ""
          }

          assert(
            foo.typeParams.nonEmpty,
            "Type parameters were incorrectly stripped by usecase"
          )

          assert(foo.typeParams.head == "C", "Incorrectly switched tparam")
          assert(returnValue == "A", "Incorrect return type after usecase")

          assert(foo.name == "foo", s"Incorrect name after transform: ${foo.name}")
      }
    }
  }

  @Test def expandColl = {
    val source = new SourceFile(
      "ExpandColl.scala",
      """
      |package scala
      |
      |/** The trait $Coll
      | *
      | *  @define Coll Iterable
      | */
      |trait Iterable[A] {
      |  /** Definition with a "disturbing" signature
      |   *
      |   *  @usecase def map[B](f: A => B): $Coll[B]
      |   */
      |  def map[B, M[B]](f: A => B): M[B] = ???
      |}
      """.stripMargin
    )

    checkSources(source :: Nil) { packages =>
      packages("scala") match {
      case PackageImpl(_, _, _, List(trt: Trait), _, _, _, _) =>
        val List(map: Def) = trt.members

        val returnValue = map.returnValue match {
          case ref: TypeReference => ref.title
          case _ =>
            assert(
              false,
              "Incorrect return value after usecase transformation"
            )
            ""
        }

        assert(
          returnValue == "Iterable",
          "Incorrect return type after usecase transformation"
        )
      }
    }
  }

  @Test def checkStripping = {
    val source = new SourceFile(
      "CheckStripping.scala",
      """
      |package scala
      |
      |/** The trait $Coll
      | *
      | *  @define Coll Iterable
      | */
      |trait Iterable[A] {
      |  /** Definition with a "disturbing" signature
      |   *
      |   *  @usecase def map[B](f: A => B): $Coll[B]
      |   */
      |  def map[B, M[B]](f: A => B): M[B] = ???
      |}
      """.stripMargin
    )

    checkSources(source :: Nil) { packages =>
      packages("scala") match {
      case PackageImpl(_, _, _, List(trt: Trait), _, _, _, _) =>
        val List(map: Def) = trt.members
        assert(map.comment.isDefined, "Lost comment in transformations")

        val docstr = ctx.docbase.docstring(map.symbol).get.raw
        assert(
          !docstr.contains("@usecase"),
          s"Comment should not contain usecase after stripping, but was:\n$docstr"
        )
      }
    }
  }

  @Test def checkIterator =
    checkFiles("../scala-scala/src/library/scala/collection/Iterator.scala" :: Nil) { _ =>
      // success if typer throws no errors! :)
    }

  @Test def checkIterableLike =
    checkFiles("../scala-scala/src/library/scala/collection/IterableLike.scala" :: Nil) { _ =>
      // success if typer throws no errors! :)
    }
}