Skip to content

Commit ac59b48

Browse files
authored
Merge pull request noahra#17 from noahra/issue/noahra#15
test - add tests for any wrap and stream (noahra#15) closes noahra#15
2 parents 149b796 + 4b7c1f0 commit ac59b48

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.jsoniter.any;
2+
import junit.framework.TestCase;
3+
4+
import java.util.*;
5+
6+
public class TestRecord extends TestCase {
7+
8+
record TestRecord1(int field1) {
9+
10+
}
11+
12+
public void test_wrap_int(){
13+
Any any = Any.wrap(new TestRecord1(3));
14+
assertEquals(3, any.get("field1").toInt());
15+
}
16+
17+
record TestRecord2(int field1, String field2) {
18+
19+
}
20+
21+
public void test_iterator(){
22+
Any any = Any.wrap(new TestRecord2(3,"hej"));
23+
Any.EntryIterator iter = any.entries();
24+
HashMap<String, Object> map = new HashMap<String, Object>();
25+
while (iter.next()) {
26+
if(iter.key() == "field1"){
27+
assertEquals(3,iter.value().toInt());
28+
}
29+
if(iter.key() == "field2"){
30+
assertEquals("hej",iter.value().toString());
31+
}
32+
}
33+
}
34+
35+
public void test_size() {
36+
Any any = Any.wrap(new TestRecord2(7,"ho"));
37+
assertEquals(2, any.size());
38+
}
39+
40+
public void test_to_string() {
41+
assertEquals("{\"field1\":7,\"field2\":\"hej\"}", Any.wrap(new TestRecord2(7,"hej")).toString());
42+
}
43+
44+
record TestRecord3(){
45+
46+
}
47+
48+
public void test_to_boolean() {
49+
Any any = Any.wrap(new TestRecord3());
50+
assertFalse(any.toBoolean());
51+
any = Any.wrap(new TestRecord2(1,"hallo"));
52+
assertTrue(any.toBoolean());
53+
}
54+
55+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.jsoniter.output;
2+
3+
import junit.framework.TestCase;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.util.*;
7+
8+
public class TestRecord extends TestCase {
9+
private ByteArrayOutputStream baos;
10+
private JsonStream stream;
11+
12+
public void setUp() {
13+
baos = new ByteArrayOutputStream();
14+
stream = new JsonStream(baos, 4096);
15+
}
16+
17+
record TestRecord1(float field1){
18+
19+
}
20+
21+
public void test_gen_record() throws IOException {
22+
stream.writeVal(new TestRecord1(2.5f));
23+
stream.close();
24+
assertEquals("{'field1':2.5}".replace('\'', '"'), baos.toString());
25+
}
26+
27+
record TestRecord2(){
28+
29+
}
30+
31+
public void test_empty_record() throws IOException {
32+
stream.writeVal(new TestRecord2());
33+
stream.close();
34+
assertEquals("{}".replace('\'', '"'), baos.toString());
35+
}
36+
37+
38+
39+
}

src/test/java/com/jsoniter/suite/AllTestCases.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.jsoniter.TestGson;
77
import com.jsoniter.TestNested;
88
import com.jsoniter.TestObject;
9+
import com.jsoniter.TestRecord;
910
import com.jsoniter.TestString;
1011
import com.jsoniter.any.TestList;
1112
import com.jsoniter.any.TestLong;
@@ -58,6 +59,10 @@
5859
TestList.class,
5960
TestAnnotationJsonObject.class,
6061
TestLong.class,
61-
TestOmitValue.class})
62+
TestOmitValue.class,
63+
TestRecord.class,
64+
com.jsoniter.output.TestRecord.class,
65+
com.jsoniter.any.TestRecord.class
66+
})
6267
public abstract class AllTestCases {
6368
}

0 commit comments

Comments
 (0)