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

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

import model.{
  Val,
  Object => EObject,
  CaseClass,
  Entity,
  Members,
  SuperTypes,
  Modifiers,
  TypeParams,
  Constructors => EConstructors,
  Class,
  Companion,
  ReturnValue,
  ImplicitlyAddedEntity,
  TypeAlias,
  Trait,
  Package,
  Def,
  NonEntity,
  ParamList
}
import model.references._
import model.internal.{ParamListImpl}
import model.comment.Comment
import dotty.tools.dotc.core.Symbols.NoSymbol
import java.util.{Optional => JOptional, Map => JMap, List => JList}

class JavaConverterTest {
  import model.JavaConverters._
  import scala.collection.JavaConverters._

  @Test def entityConversions = {
    val paramList = new ParamListImpl(new NamedReference("x", new TypeReference("Int", new NoLink("title", "target"), List())) :: Nil, false)
    val df = new Def {
      def symbol = NoSymbol
      def name = "test"
      def path = "path" :: "to" :: "def" :: Nil
      def comment = None
      def annotations = List("test")
      def parent = NonEntity
      def modifiers = "private" :: Nil
      def typeParams = "String" :: "String" :: Nil
      def implicitlyAddedFrom = Some(
          new TypeReference("String", new NoLink("title", "target"), List()))
      def returnValue = new TypeReference("String", new NoLink("title", "target"), List())
      def paramLists = List(paramList)
    }
    assertSerializedCorrectly(df, df.asJava())
    val trt = new Trait {
      def symbol = NoSymbol
      def name = "someTrait"
      def path = "path" :: "to" :: "trait" :: Nil
      def comment = None
      def annotations = List("test")
      def parent = NonEntity
      def modifiers = "protected" :: Nil
      def typeParams = "String" :: "String" :: Nil
      def superTypes = new NoLink("title", "query") :: Nil
      def members = df :: Nil
      def traitParams = List(paramList)
      def companionPath = "path" :: "to" :: "companion" :: Nil
      def companionPath_=(xs: List[String]) = Unit
    }
    assertSerializedCorrectly(trt, trt.asJava())
    val cls = new Class {
      def symbol = NoSymbol
      def name = "test"
      def path = "path" :: "to" :: "test" :: Nil
      def comment = None
      def annotations = List("test")
      def parent = NonEntity
      def modifiers = "private" :: Nil
      def typeParams = "String" :: "String" :: Nil
      def superTypes = new NoLink("title", "query") :: Nil
      def members = Nil
      def companionPath = "path" :: "to" :: "companion" :: Nil
      def companionPath_=(xs: List[String]) = Unit
      def constructors = List(List(paramList))
    }
    assertSerializedCorrectly(cls, cls.asJava())
    val caseClass = new CaseClass {
      def symbol = NoSymbol
      def name = "test"
      def path = "path" :: "to" :: "test" :: Nil
      def comment = None
      def annotations = List("test")
      def parent = NonEntity
      def modifiers = "private" :: Nil
      def typeParams = "String" :: "String" :: Nil
      def constructors = List(List(paramList))
      def superTypes = new NoLink("title", "query") :: Nil
      def members = Nil
      def companionPath = "path" :: "to" :: "companion" :: Nil
      def companionPath_=(xs: List[String]) = Unit
    }
    assertSerializedCorrectly(caseClass, caseClass.asJava())
    val obj = new EObject {
      def symbol = NoSymbol
      def name = "someObject"
      def path = "path" :: "to" :: "object" :: Nil
      def comment = None
      def annotations = List("test")
      def parent = NonEntity
      def modifiers = "protected" :: Nil
      def typeParams = "String" :: "String" :: Nil
      def superTypes = new NoLink("title", "query") :: Nil
      def members = df :: Nil
      def companionPath = "path" :: "to" :: "companion" :: Nil
      def companionPath_=(xs: List[String]) = Unit
    }
    assertSerializedCorrectly(obj, obj.asJava())
    val typeAlias = new TypeAlias {
      def symbol = NoSymbol
      def name = "typeAlias"
      def path = "path" :: "to" :: "typeAlias" :: Nil
      def comment = None
      def annotations = List("test")
      def parent = NonEntity
      def modifiers = "private" :: Nil
      def typeParams = "String" :: "String" :: Nil
      def alias = Some(new TypeReference("String", new NoLink("title", "target"), List()))
    }
    assertSerializedCorrectly(typeAlias, typeAlias.asJava())
    val vl = new Val {
      val kind = "val"
      def symbol = NoSymbol
      def name = "val"
      def path = "path" :: "to" :: "val" :: Nil
      def comment = None
      def annotations = List("test")
      def parent = NonEntity
      def modifiers = "private" :: Nil
      def returnValue = new TypeReference("String", new NoLink("title", "target"), List())
      def implicitlyAddedFrom = Some(
        new TypeReference("String", new NoLink("title", "target"), List()))
    }
    assertSerializedCorrectly(vl, vl.asJava())
    val pkg = new Package {
      def symbol = NoSymbol
      def name = "test"
      def path = "path" :: "to" :: "test" :: Nil
      def comment = None
      def annotations = List("test")
      def parent = NonEntity
      def members = trt :: typeAlias :: Nil
      def superTypes = new NoLink("title", "query") :: Nil
    }
    assertSerializedCorrectly(pkg, pkg.asJava())
  }

  def assertEach[E, C[E] <: Seq[E]](expected: C[E], serialized: Any)(pairwiseAssertion: (E, Any) => Unit): Unit = {
    val s = serialized.asInstanceOf[JList[_]]
    val actual = s.asScala.toList
    assertEquals(expected.length, actual.length)
    for ((exp, act) <- expected zip actual) {
      pairwiseAssertion(exp, act)
    }
  }
  def assertSameSeq[T, C[T] <: Seq[T]](expected: C[T], serialized: Any): Unit = {
    val actual = serialized.asInstanceOf[java.util.List[T]].asScala.toList
    assertEquals(expected, actual);
  }
  def assertSerializedCorrectly(expected: ParamList, serialized: Any): Unit = {
    val actual = serialized.asInstanceOf[JMap[String, _]]
    assertEach(expected.list, actual.get("list")) {(exp, act) =>
      assertSerializedCorrectly(exp, act)
    }
    assertEquals(expected.isImplicit, actual.get("isImplicit"))
  }
  def assertSerializedCorrectly(expected: Reference, serialized: Any): Unit = {
    val actual = serialized.asInstanceOf[JMap[String, _]]
    expected match {
      case TypeReference(title, tpeLink, paramLinks) =>
        assertEquals(title, actual.get("title"))
        assertSerializedCorrectly(tpeLink, actual.get("tpeLink"))
        assertEach(paramLinks, actual.get("paramLinks")) { (exp, act) =>
          assertSerializedCorrectly(exp, act)
        }
      case OrTypeReference(left, right) =>
        assertSerializedCorrectly(left, actual.get("left"))
        assertSerializedCorrectly(right, actual.get("right"))
      case AndTypeReference(left, right) =>
        assertSerializedCorrectly(left, actual.get("left"))
        assertSerializedCorrectly(right, actual.get("right"))
      case FunctionReference(args, returnValue) =>
        assertEach(args, actual.get("args")) { (exp, act) =>
          assertSerializedCorrectly(exp, act)
        }
        assertSerializedCorrectly(returnValue, actual.get("returnValue"))
      case TupleReference(args) =>
        assertEach(args, actual.get("args")) { (exp, act) =>
          assertSerializedCorrectly(exp, act)
        }
      case BoundsReference(low, high) =>
        assertSerializedCorrectly(low, actual.get("low"))
        assertSerializedCorrectly(high, actual.get("high"))
      case NamedReference(title, ref, isByName, isRepeated) =>
        assertEquals(title, actual.get("title"))
        assertSerializedCorrectly(ref, actual.get("ref"))
        assertEquals(isByName, actual.get("isByName"))
        assertEquals(isRepeated, actual.get("isRepeated"))
      case ConstantReference(title) =>
        assertEquals(title, actual.get("title"))
      case EmptyReference =>
    }
  }
  def assertSerializedCorrectly(expected: MaterializableLink, serialized: Any): Unit = {
    val actual = serialized.asInstanceOf[JMap[String, _]]
    expected match {
      case UnsetLink(title, query) =>
        assertEquals(title, actual.get("title"))
        assertEquals(query, actual.get("query"))
      case MaterializedLink(title, target) =>
        assertEquals(title, actual.get("title"))
        assertEquals(target, actual.get("target"))
      case NoLink(title, target) =>
        assertEquals(title, actual.get("title"))
        assertEquals(target, actual.get("target"))
    }
  }
  def assertSerializedCorrectly(expected: Entity, serialized: Any): Unit = {
    val actual = serialized.asInstanceOf[JMap[String, _]]
    assertEquals(expected.name, actual.get("name"))
    assertEquals(expected.kind, actual.get("kind"))
    assertSameSeq(expected.annotations, actual.get("annotations"))
    assertSameSeq(expected.path, actual.get("path"))
    assertEquals(expected.hasShortenedDocstring, actual.get("hasShortenedDocstring"))
    // Only test if a comment is present
    expected.comment match {
      case Some(c) => assertNotEquals(null, actual.get("comment"))
      case _ =>
    }
    expected match {
      case e: Members => {
        assertEach(e.members, actual.get("members")) { (exp, act) =>
          assertSerializedCorrectly(exp, act)
        }
      }
      case _ =>
    }
    expected match {
      case e: SuperTypes => {
        assertEach(e.superTypes, actual.get("superTypes")) { (exp, act) =>
          assertSerializedCorrectly(exp, act)
        }
      }
      case _ =>
    }
    expected match {
      case e: Modifiers => {
        assertSameSeq(e.modifiers, actual.get("modifiers"))
        assertEquals(e.isPrivate, actual.get("isPrivate"))
        assertEquals(e.isProtected, actual.get("isProtected"))
      }
      case _ =>
    }
    expected match {
      case e: TypeParams => {
        assertSameSeq(e.typeParams, actual.get("typeParams"))
      }
      case _ =>
    }
    expected match {
      case e: EConstructors => {
        // constructors is of type List[List[ParamList]], so we need to apply assertEach twice
        assertEach(e.constructors, actual.get("constructors")) { (exp, act) =>
          assertEach(exp, act) { (exp, act) =>
            assertSerializedCorrectly(exp, act)
          }
        }
      }
      case _ =>
    }
    expected match {
      case e: Companion => {
        assertSameSeq(e.companionPath, actual.get("companionPath"))
      }
      case _ =>
    }
    expected match {
      case e: ReturnValue => {
        assertSerializedCorrectly(e.returnValue, actual.get("returnValue"))
      }
      case _ =>
    }
    expected match {
      case e: ImplicitlyAddedEntity => {
        e.implicitlyAddedFrom.map(assertSerializedCorrectly(_, actual.get("implicitlyAddedFrom")))
      }
      case _ =>
    }
    expected match {
      case e: TypeAlias => {
        e.alias.map(assertSerializedCorrectly(_, actual.get("alias")))
      }
      case _ =>
    }
    expected match {
      case e: Def => {
        assertEach(e.paramLists, actual.get("paramLists")) { (exp, act) =>
          assertSerializedCorrectly(exp, act)
        }
      }
      case _ =>
    }
    expected match {
      case e: Trait => {
        assertEach(e.traitParams, actual.get("traitParams")) { (exp, act) =>
          assertSerializedCorrectly(exp, act)
        }
      }
      case _ =>
    }
  }
}