Skip to content

Commit 37a6755

Browse files
xinmiaoyuxin-miao
authored andcommitted
test - add tests for getRecordCtor (#21)
1 parent 2d2ff04 commit 37a6755

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/test/java/com/jsoniter/TestRecord.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jsoniter.annotation.JsonCreator;
55
import com.jsoniter.annotation.JsonProperty;
66
import com.jsoniter.any.Any;
7+
import com.jsoniter.spi.ClassDescriptor;
78
import com.jsoniter.spi.ClassInfo;
89
import com.jsoniter.spi.JsonException;
910
import junit.framework.TestCase;
@@ -17,7 +18,8 @@
1718

1819
public class TestRecord extends TestCase {
1920

20-
record TestRecord1(long field1) {}
21+
record TestRecord1(long field1) {
22+
}
2123

2224
public record TestRecord0(Long id, String name) {
2325

@@ -114,7 +116,8 @@ public void test_record_withOnlyFieldDecoder() throws IOException {
114116

115117
public void test_record_2_fields_withOnlyFieldDecoder() throws IOException {
116118

117-
record TestRecord2(long field1, String field2) {}
119+
record TestRecord2(long field1, String field2) {
120+
}
118121

119122
assertEquals(ReflectionRecordDecoder.OnlyFieldRecord.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord2.class)).getClass());
120123

@@ -127,7 +130,8 @@ record TestRecord2(long field1, String field2) {}
127130

128131
public void test_record_2_fields_swapFieldOrder_withOnlyFieldDecoder() throws IOException {
129132

130-
record TestRecord2(String field2, long field1) {}
133+
record TestRecord2(String field2, long field1) {
134+
}
131135

132136
assertEquals(ReflectionRecordDecoder.OnlyFieldRecord.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord2.class)).getClass());
133137

@@ -140,8 +144,10 @@ record TestRecord2(String field2, long field1) {}
140144

141145
public void test_record_recordComposition_withOnlyFieldDecoder() throws IOException {
142146

143-
record TestRecordA(long fieldA) {}
144-
record TestRecordB(long fieldB, TestRecordA a) {}
147+
record TestRecordA(long fieldA) {
148+
}
149+
record TestRecordB(long fieldB, TestRecordA a) {
150+
}
145151

146152
assertEquals(ReflectionRecordDecoder.OnlyFieldRecord.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecordB.class)).getClass());
147153

@@ -154,7 +160,8 @@ record TestRecordB(long fieldB, TestRecordA a) {}
154160

155161
public void test_record_empty_constructor_withOnlyFieldDecoder() throws IOException {
156162

157-
record TestRecord3() {}
163+
record TestRecord3() {
164+
}
158165

159166
assertEquals(ReflectionRecordDecoder.OnlyFieldRecord.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord3.class)).getClass());
160167

@@ -216,6 +223,8 @@ public TestRecord6(@JsonProperty("valInt") int valInt) {
216223
}
217224
}
218225

226+
assertEquals(ReflectionRecordDecoder.WithCtor.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord6.class)).getClass());
227+
219228
JsonIterator iter = JsonIterator.parse("{ 'valInt' : 1 }".replace('\'', '"'));
220229
TestRecord6 record = iter.read(TestRecord6.class);
221230

@@ -227,8 +236,10 @@ public void test_record_withCtorDecoder() throws IOException {
227236
record TestRecord2(@JsonProperty long field1) {
228237

229238
@JsonCreator
230-
TestRecord2 {}
239+
TestRecord2 {
240+
}
231241
}
242+
assertEquals(ReflectionRecordDecoder.WithCtor.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord2.class)).getClass());
232243

233244
assertEquals(ReflectionDecoderFactory.create(new ClassInfo(TestRecord2.class)).getClass(), ReflectionObjectDecoder.WithCtor.class);
234245

@@ -237,4 +248,11 @@ record TestRecord2(@JsonProperty long field1) {
237248

238249
assertEquals(1, record.field1);
239250
}
251+
252+
public void test_record_constructor() throws IOException {
253+
ClassDescriptor desc = ClassDescriptor.getDecodingClassDescriptor(new ClassInfo(TestRecord0.class), false);
254+
assertEquals(TestRecord0.class.getConstructors()[1], desc.ctor.ctor);
255+
256+
}
257+
240258
}

0 commit comments

Comments
 (0)