Debian Codegraph @

tools/protologtool/tests/com/android/protolog/tool/CodeUtilsTest.kt

raw
1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.protolog.tool
18
19 import com.github.javaparser.StaticJavaParser
20 import com.github.javaparser.ast.expr.BinaryExpr
21 import com.github.javaparser.ast.expr.StringLiteralExpr
22 import org.junit.Assert.assertEquals
23 import org.junit.Assert.assertFalse
24 import org.junit.Assert.assertTrue
25 import org.junit.Test
26
27 class CodeUtilsTest {
28 @Test
29 fun hash() {
30 assertEquals(-1259556708, CodeUtils.hash("Test.java:50", "test",
31 LogLevel.DEBUG, LogGroup("test", true, true, "TAG")))
32 }
33
34 @Test
35 fun hash_changeLocation() {
36 assertEquals(15793504, CodeUtils.hash("Test.java:10", "test2",
37 LogLevel.DEBUG, LogGroup("test", true, true, "TAG")))
38 }
39
40 @Test
41 fun hash_changeLevel() {
42 assertEquals(-731772463, CodeUtils.hash("Test.java:50", "test",
43 LogLevel.ERROR, LogGroup("test", true, true, "TAG")))
44 }
45
46 @Test
47 fun hash_changeMessage() {
48 assertEquals(-2026343204, CodeUtils.hash("Test.java:50", "test2",
49 LogLevel.DEBUG, LogGroup("test", true, true, "TAG")))
50 }
51
52 @Test
53 fun hash_changeGroup() {
54 assertEquals(1607870166, CodeUtils.hash("Test.java:50", "test2",
55 LogLevel.DEBUG, LogGroup("test2", true, true, "TAG")))
56 }
57
58 @Test(expected = IllegalImportException::class)
59 fun checkWildcardStaticImported_true() {
60 val code = """package org.example.test;
61 import static org.example.Test.*;
62 """
63 CodeUtils.checkWildcardStaticImported(
64 StaticJavaParser.parse(code), "org.example.Test", "")
65 }
66
67 @Test
68 fun checkWildcardStaticImported_notStatic() {
69 val code = """package org.example.test;
70 import org.example.Test.*;
71 """
72 CodeUtils.checkWildcardStaticImported(
73 StaticJavaParser.parse(code), "org.example.Test", "")
74 }
75
76 @Test
77 fun checkWildcardStaticImported_differentClass() {
78 val code = """package org.example.test;
79 import static org.example.Test2.*;
80 """
81 CodeUtils.checkWildcardStaticImported(
82 StaticJavaParser.parse(code), "org.example.Test", "")
83 }
84
85 @Test
86 fun checkWildcardStaticImported_notWildcard() {
87 val code = """package org.example.test;
88 import org.example.Test.test;
89 """
90 CodeUtils.checkWildcardStaticImported(
91 StaticJavaParser.parse(code), "org.example.Test", "")
92 }
93
94 @Test
95 fun isClassImportedOrSamePackage_imported() {
96 val code = """package org.example.test;
97 import org.example.Test;
98 """
99 assertTrue(CodeUtils.isClassImportedOrSamePackage(
100 StaticJavaParser.parse(code), "org.example.Test"))
101 }
102
103 @Test
104 fun isClassImportedOrSamePackage_samePackage() {
105 val code = """package org.example.test;
106 """
107 assertTrue(CodeUtils.isClassImportedOrSamePackage(
108 StaticJavaParser.parse(code), "org.example.test.Test"))
109 }
110
111 @Test
112 fun isClassImportedOrSamePackage_false() {
113 val code = """package org.example.test;
114 import org.example.Test;
115 """
116 assertFalse(CodeUtils.isClassImportedOrSamePackage(
117 StaticJavaParser.parse(code), "org.example.Test2"))
118 }
119
120 @Test
121 fun staticallyImportedMethods_ab() {
122 val code = """
123 import static org.example.Test.a;
124 import static org.example.Test.b;
125 """
126 val imported = CodeUtils.staticallyImportedMethods(StaticJavaParser.parse(code),
127 "org.example.Test")
128 assertTrue(imported.containsAll(listOf("a", "b")))
129 assertEquals(2, imported.size)
130 }
131
132 @Test
133 fun staticallyImportedMethods_differentClass() {
134 val code = """
135 import static org.example.Test.a;
136 import static org.example.Test2.b;
137 """
138 val imported = CodeUtils.staticallyImportedMethods(StaticJavaParser.parse(code),
139 "org.example.Test")
140 assertTrue(imported.containsAll(listOf("a")))
141 assertEquals(1, imported.size)
142 }
143
144 @Test
145 fun staticallyImportedMethods_notStatic() {
146 val code = """
147 import static org.example.Test.a;
148 import org.example.Test.b;
149 """
150 val imported = CodeUtils.staticallyImportedMethods(StaticJavaParser.parse(code),
151 "org.example.Test")
152 assertTrue(imported.containsAll(listOf("a")))
153 assertEquals(1, imported.size)
154 }
155
156 @Test
157 fun concatMultilineString_single() {
158 val str = StringLiteralExpr("test")
159 val out = CodeUtils.concatMultilineString(str, ParsingContext())
160 assertEquals("test", out)
161 }
162
163 @Test
164 fun concatMultilineString_double() {
165 val str = """
166 "test" + "abc"
167 """
168 val code = StaticJavaParser.parseExpression<BinaryExpr>(str)
169 val out = CodeUtils.concatMultilineString(code, ParsingContext())
170 assertEquals("testabc", out)
171 }
172
173 @Test
174 fun concatMultilineString_multiple() {
175 val str = """
176 "test" + "abc" + "1234" + "test"
177 """
178 val code = StaticJavaParser.parseExpression<BinaryExpr>(str)
179 val out = CodeUtils.concatMultilineString(code, ParsingContext())
180 assertEquals("testabc1234test", out)
181 }
182 }