Skip to content

Commit 5353fdd

Browse files
committed
(WIP) add unit tests
1 parent a1b4f51 commit 5353fdd

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2025 Mountainminds GmbH & Co. KG and Contributors
3+
* This program and the accompanying materials are made available under
4+
* the terms of the Eclipse Public License 2.0 which is available at
5+
* http://www.eclipse.org/legal/epl-2.0
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Evgeny Mandrikov - initial API and implementation
11+
*
12+
*******************************************************************************/
13+
package org.jacoco.core.internal.analysis.filter;
14+
15+
import org.junit.Test;
16+
import org.objectweb.asm.Label;
17+
import org.objectweb.asm.Opcodes;
18+
import org.objectweb.asm.tree.MethodNode;
19+
20+
/**
21+
* Unit test for {@link KotlinSerializableFilter}.
22+
*/
23+
public class KotlinSerializableFilterTest extends FilterTestBase {
24+
25+
private final IFilter filter = new KotlinSerializableFilter();
26+
27+
/**
28+
* <pre>
29+
* &#064;kotlinx.serialization.Serializable
30+
* data class Example(val data: String)
31+
* </pre>
32+
*/
33+
@Test
34+
public void should_filter_synthetic_writeSelf_method() {
35+
final MethodNode m = new MethodNode(
36+
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC
37+
| Opcodes.ACC_SYNTHETIC,
38+
"write$Self$module_name",
39+
"(Lpkg$Example;Lkotlinx/serialization/encoding/CompositeEncoder;Lkotlinx/serialization/descriptors/SerialDescriptor;)V",
40+
null, null);
41+
m.visitInsn(Opcodes.NOP);
42+
43+
filter.filter(m, context, output);
44+
45+
assertMethodIgnored(m);
46+
}
47+
48+
/**
49+
* <pre>
50+
* &#064;kotlinx.serialization.Serializable
51+
* data class Example(val data: String)
52+
* </pre>
53+
*/
54+
@Test
55+
public void should_filter_synthetic_constructor() {
56+
final MethodNode m = new MethodNode(
57+
Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, "<init>",
58+
"(ILjava/lang/String;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V",
59+
null, null);
60+
m.visitInsn(Opcodes.NOP);
61+
62+
filter.filter(m, context, output);
63+
64+
assertMethodIgnored(m);
65+
}
66+
67+
/**
68+
* <pre>
69+
* &#064;kotlinx.serialization.Serializable // line 1
70+
* data class Example(val data: String)
71+
* </pre>
72+
*/
73+
@Test
74+
public void should_filter_generated_serializer_method() {
75+
context.className = "Example$Companion";
76+
77+
final MethodNode initMethod = new MethodNode(Opcodes.ACC_PRIVATE,
78+
"<init>", "()V", null, null);
79+
// no line numbers
80+
filter.filter(initMethod, context, output);
81+
82+
final MethodNode m = new MethodNode(
83+
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "serializer",
84+
"()Lkotlinx/serialization/KSerializer;",
85+
"()Lkotlinx/serialization/KSerializer<LExample;>;", null);
86+
final Label label0 = new Label();
87+
m.visitLabel(label0);
88+
m.visitLineNumber(1, label0);
89+
m.visitFieldInsn(Opcodes.GETSTATIC, "Example$$serializer", "INSTANCE",
90+
"LExample$$serializer;");
91+
m.visitTypeInsn(Opcodes.CHECKCAST, "kotlinx/serialization/KSerializer");
92+
m.visitInsn(Opcodes.ARETURN);
93+
94+
filter.filter(m, context, output);
95+
96+
assertMethodIgnored(m);
97+
}
98+
99+
/**
100+
* <pre>
101+
* &#064;kotlinx.serialization.Serializable
102+
* data class Example(val data: String) {
103+
* companion object // line 2
104+
* }
105+
* </pre>
106+
*/
107+
@Test
108+
public void should_filter_generated_serializer_method_in_hand_written_companion() {
109+
context.className = "Example$Companion";
110+
111+
final MethodNode initMethod = new MethodNode(Opcodes.ACC_PRIVATE,
112+
"<init>", "()V", null, null);
113+
final Label initMethodLineNumberLabel = new Label();
114+
initMethod.visitLabel(initMethodLineNumberLabel);
115+
initMethod.visitLineNumber(2, initMethodLineNumberLabel);
116+
filter.filter(initMethod, context, output);
117+
118+
final MethodNode m = new MethodNode(
119+
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "serializer",
120+
"()Lkotlinx/serialization/KSerializer;",
121+
"()Lkotlinx/serialization/KSerializer<LExample;>;", null);
122+
final Label label0 = new Label();
123+
m.visitLabel(label0);
124+
m.visitLineNumber(2, label0);
125+
m.visitFieldInsn(Opcodes.GETSTATIC, "Example$$serializer", "INSTANCE",
126+
"LExample$$serializer;");
127+
m.visitTypeInsn(Opcodes.CHECKCAST, "kotlinx/serialization/KSerializer");
128+
m.visitInsn(Opcodes.ARETURN);
129+
130+
filter.filter(m, context, output);
131+
132+
assertMethodIgnored(m);
133+
}
134+
135+
/**
136+
* <pre>
137+
* data class Example(val data: String) {
138+
* companion object { // line 2
139+
* fun serializer(): KSerializer&lt;Example&gt; = CustomSerializer
140+
* }
141+
* }
142+
* </pre>
143+
*/
144+
@Test
145+
public void should_not_filter_hand_written_serializer_method() {
146+
context.className = "Example$Companion";
147+
148+
final MethodNode initMethod = new MethodNode(Opcodes.ACC_PRIVATE,
149+
"<init>", "()V", null, null);
150+
final Label initMethodLineNumberLabel = new Label();
151+
initMethod.visitLabel(initMethodLineNumberLabel);
152+
initMethod.visitLineNumber(2, initMethodLineNumberLabel);
153+
filter.filter(initMethod, context, output);
154+
155+
final MethodNode m = new MethodNode(
156+
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "serializer",
157+
"()Lkotlinx/serialization/KSerializer;",
158+
"()Lkotlinx/serialization/KSerializer<LExample;>;", null);
159+
final Label label0 = new Label();
160+
m.visitLabel(label0);
161+
m.visitLineNumber(3, label0);
162+
m.visitFieldInsn(Opcodes.GETSTATIC, "CustomSerializer", "INSTANCE",
163+
"LCustomSerializer;");
164+
m.visitTypeInsn(Opcodes.CHECKCAST, "kotlinx/serialization/KSerializer");
165+
m.visitInsn(Opcodes.ARETURN);
166+
167+
filter.filter(m, context, output);
168+
169+
assertIgnored(m);
170+
}
171+
172+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2025 Mountainminds GmbH & Co. KG and Contributors
3+
* This program and the accompanying materials are made available under
4+
* the terms of the Eclipse Public License 2.0 which is available at
5+
* http://www.eclipse.org/legal/epl-2.0
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Evgeny Mandrikov - initial API and implementation
11+
*
12+
*******************************************************************************/
13+
package org.jacoco.core.internal.analysis.filter;
14+
15+
import org.objectweb.asm.tree.MethodNode;
16+
17+
/**
18+
* Filters methods generated by <a href=
19+
* "https://github.com/JetBrains/kotlin/tree/v2.1.20/plugins/kotlinx-serialization">Kotlin
20+
* serialization compiler plugin</a>.
21+
*/
22+
final class KotlinSerializableFilter implements IFilter {
23+
24+
public void filter(final MethodNode methodNode,
25+
final IFilterContext context, final IFilterOutput output) {
26+
// TODO
27+
}
28+
29+
}

0 commit comments

Comments
 (0)