-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClassDeclarationTreeTest.java
More file actions
98 lines (86 loc) · 4.04 KB
/
ClassDeclarationTreeTest.java
File metadata and controls
98 lines (86 loc) · 4.04 KB
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
/*
* SonarSource Scala
* Copyright (C) 2018-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonarsource.scala.converter;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.sonarsource.slang.api.Annotation;
import org.sonarsource.slang.api.ClassDeclarationTree;
import org.sonarsource.slang.api.Tree;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonarsource.slang.testing.TreeAssert.assertTree;
class ClassDeclarationTreeTest extends AbstractScalaConverterTest {
@Test
void empty_class() {
Tree tree = scalaStatement("class Foo {}");
assertTree(tree).isEquivalentTo(scalaStatement("class Foo {}"));
assertTree(tree).isNotEquivalentTo(scalaStatement("class FooBar {}"));
assertTree(tree).isNotEquivalentTo(scalaStatement("class Foo { def bar() {}}"));
ClassDeclarationTree classTree = (ClassDeclarationTree) tree;
assertThat(classTree.identifier().name()).isEqualTo("Foo");
}
@Test
void class_with_method() {
Tree tree = scalaStatement("class Foo { def bar() {} }");
assertTree(tree).isEquivalentTo(scalaStatement("class Foo { def bar() {} }"));
assertTree(tree).isNotEquivalentTo(scalaStatement("class Foo { def foo() {} }"));
ClassDeclarationTree classTree = (ClassDeclarationTree) tree;
assertThat(classTree.identifier().name()).isEqualTo("Foo");
}
@Test
void class_with_annotation() {
Tree tree = scalaStatement("@my.test.MyAnnotation(\"something\")\n" +
"@MyAnnotation2\n" +
"class A {}");
List<Annotation> annotations = tree.metaData().annotations();
assertThat(annotations).hasSize(2);
Annotation firstAnnotation = annotations.get(0);
assertThat(firstAnnotation.shortName()).isEqualTo("MyAnnotation");
assertThat(firstAnnotation.argumentsText()).containsExactly("\"something\"");
Annotation secondAnnotation = annotations.get(1);
assertThat(secondAnnotation.shortName()).isEqualTo("MyAnnotation2");
assertThat(secondAnnotation.argumentsText()).isEmpty();
}
@Test
void testClassWithComplexAnnotation() {
Tree tree = scalaStatement("@my.test.MyAnnotation(value = \"something\", \"somethingElse\", otherValue = Array(\"a\", \"b\"))\n" +
"class A {}");
List<Annotation> annotations = tree.metaData().annotations();
assertThat(annotations).hasSize(1);
Annotation firstAnnotation = annotations.get(0);
assertThat(firstAnnotation.shortName()).isEqualTo("MyAnnotation");
assertThat(firstAnnotation.argumentsText()).containsExactly("value = \"something\"", "\"somethingElse\"", "otherValue = Array(\"a\", \"b\")");
}
@Test
void testClassWithAnnotatedMember() {
Tree tree = scalaStatement("class A {\n" +
"@MyAnnotation\n" +
"def f(@MyAnnotation i: Int) = { }" +
"}\n");
assertThat(tree.metaData().annotations()).isEmpty();
List<Tree> annotatedDescendants = tree.descendants().filter(d -> !d.metaData().annotations().isEmpty()).collect(Collectors.toList());
// FunctionTree and ParameterTree + two annotations that are mapped to native trees.
assertThat(annotatedDescendants).hasSize(4);
annotatedDescendants.forEach(descendant -> {
List<Annotation> annotations = descendant.metaData().annotations();
assertThat(annotations).hasSize(1);
Annotation annotation = annotations.get(0);
assertThat(annotation.shortName()).isEqualTo("MyAnnotation");
assertThat(annotation.argumentsText()).isEmpty();
});
}
}