Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
55 changes: 55 additions & 0 deletions src/test/java/com/jsoniter/any/TestRecord.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> map = new HashMap<String, Object>();
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());
}

}
39 changes: 39 additions & 0 deletions src/test/java/com/jsoniter/output/TestRecord.java
Original file line number Diff line number Diff line change
@@ -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());
}



}
7 changes: 6 additions & 1 deletion src/test/java/com/jsoniter/suite/AllTestCases.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
}