Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test - add tests for getRecordCtor (#21)
  • Loading branch information
xinmiao authored and yuxin-miao committed Mar 6, 2022
commit 37a67552435e8aa1c93951679d001fe265133b26
32 changes: 25 additions & 7 deletions src/test/java/com/jsoniter/TestRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jsoniter.annotation.JsonCreator;
import com.jsoniter.annotation.JsonProperty;
import com.jsoniter.any.Any;
import com.jsoniter.spi.ClassDescriptor;
import com.jsoniter.spi.ClassInfo;
import com.jsoniter.spi.JsonException;
import junit.framework.TestCase;
Expand All @@ -17,7 +18,8 @@

public class TestRecord extends TestCase {

record TestRecord1(long field1) {}
record TestRecord1(long field1) {
}

public record TestRecord0(Long id, String name) {

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

public void test_record_2_fields_withOnlyFieldDecoder() throws IOException {

record TestRecord2(long field1, String field2) {}
record TestRecord2(long field1, String field2) {
}

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

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

public void test_record_2_fields_swapFieldOrder_withOnlyFieldDecoder() throws IOException {

record TestRecord2(String field2, long field1) {}
record TestRecord2(String field2, long field1) {
}

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

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

public void test_record_recordComposition_withOnlyFieldDecoder() throws IOException {

record TestRecordA(long fieldA) {}
record TestRecordB(long fieldB, TestRecordA a) {}
record TestRecordA(long fieldA) {
}
record TestRecordB(long fieldB, TestRecordA a) {
}

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

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

public void test_record_empty_constructor_withOnlyFieldDecoder() throws IOException {

record TestRecord3() {}
record TestRecord3() {
}

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

Expand Down Expand Up @@ -216,6 +223,8 @@ public TestRecord6(@JsonProperty("valInt") int valInt) {
}
}

assertEquals(ReflectionRecordDecoder.WithCtor.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord6.class)).getClass());

JsonIterator iter = JsonIterator.parse("{ 'valInt' : 1 }".replace('\'', '"'));
TestRecord6 record = iter.read(TestRecord6.class);

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

@JsonCreator
TestRecord2 {}
TestRecord2 {
}
}
assertEquals(ReflectionRecordDecoder.WithCtor.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord2.class)).getClass());

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

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

assertEquals(1, record.field1);
}

public void test_record_constructor() throws IOException {
ClassDescriptor desc = ClassDescriptor.getDecodingClassDescriptor(new ClassInfo(TestRecord0.class), false);
assertEquals(TestRecord0.class.getConstructors()[1], desc.ctor.ctor);

}

}