diff --git a/src/test/java/com/jsoniter/any/TestRecord.java b/src/test/java/com/jsoniter/any/TestRecord.java new file mode 100644 index 00000000..4631832a --- /dev/null +++ b/src/test/java/com/jsoniter/any/TestRecord.java @@ -0,0 +1,55 @@ +package com.jsoniter.any; +import junit.framework.TestCase; + +import java.util.*; + +public class TestRecord extends TestCase { + + record TestRecord1(int field1) { + + } + + public void test_wrap_int(){ + Any any = Any.wrap(new TestRecord1(3)); + assertEquals(3, any.get("field1").toInt()); + } + + record TestRecord2(int field1, String field2) { + + } + + public void test_iterator(){ + Any any = Any.wrap(new TestRecord2(3,"hej")); + Any.EntryIterator iter = any.entries(); + HashMap map = new HashMap(); + while (iter.next()) { + if(iter.key() == "field1"){ + assertEquals(3,iter.value().toInt()); + } + if(iter.key() == "field2"){ + assertEquals("hej",iter.value().toString()); + } + } + } + + public void test_size() { + Any any = Any.wrap(new TestRecord2(7,"ho")); + assertEquals(2, any.size()); + } + + public void test_to_string() { + assertEquals("{\"field1\":7,\"field2\":\"hej\"}", Any.wrap(new TestRecord2(7,"hej")).toString()); + } + + record TestRecord3(){ + + } + + public void test_to_boolean() { + Any any = Any.wrap(new TestRecord3()); + assertFalse(any.toBoolean()); + any = Any.wrap(new TestRecord2(1,"hallo")); + assertTrue(any.toBoolean()); + } + +} diff --git a/src/test/java/com/jsoniter/output/TestRecord.java b/src/test/java/com/jsoniter/output/TestRecord.java new file mode 100644 index 00000000..8a63edde --- /dev/null +++ b/src/test/java/com/jsoniter/output/TestRecord.java @@ -0,0 +1,39 @@ +package com.jsoniter.output; + +import junit.framework.TestCase; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.*; + +public class TestRecord extends TestCase { + private ByteArrayOutputStream baos; + private JsonStream stream; + + public void setUp() { + baos = new ByteArrayOutputStream(); + stream = new JsonStream(baos, 4096); + } + + record TestRecord1(float field1){ + + } + + public void test_gen_record() throws IOException { + stream.writeVal(new TestRecord1(2.5f)); + stream.close(); + assertEquals("{'field1':2.5}".replace('\'', '"'), baos.toString()); + } + + record TestRecord2(){ + + } + + public void test_empty_record() throws IOException { + stream.writeVal(new TestRecord2()); + stream.close(); + assertEquals("{}".replace('\'', '"'), baos.toString()); + } + + + +} diff --git a/src/test/java/com/jsoniter/suite/AllTestCases.java b/src/test/java/com/jsoniter/suite/AllTestCases.java index d3196ed4..b5054ecf 100644 --- a/src/test/java/com/jsoniter/suite/AllTestCases.java +++ b/src/test/java/com/jsoniter/suite/AllTestCases.java @@ -6,6 +6,7 @@ import com.jsoniter.TestGson; import com.jsoniter.TestNested; import com.jsoniter.TestObject; +import com.jsoniter.TestRecord; import com.jsoniter.TestString; import com.jsoniter.any.TestList; import com.jsoniter.any.TestLong; @@ -58,6 +59,10 @@ TestList.class, TestAnnotationJsonObject.class, TestLong.class, - TestOmitValue.class}) + TestOmitValue.class, + TestRecord.class, + com.jsoniter.output.TestRecord.class, + com.jsoniter.any.TestRecord.class +}) public abstract class AllTestCases { }