Skip to content

Commit

Permalink
test - add tests for getRecordCtor (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinmiao committed Mar 6, 2022
1 parent 2d2ff04 commit 7ea9072
Showing 1 changed file with 25 additions and 7 deletions.
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);

}

}

0 comments on commit 7ea9072

Please sign in to comment.