Skip to content

test - add getRecordCtor test (#21) #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Changes from all commits
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
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);

}

}