mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-07 10:19:51 +00:00
add a packer and unpacker for Enum types
This commit is contained in:
parent
0bd4150a80
commit
cdd60e5f9c
9 changed files with 332 additions and 57 deletions
|
|
@ -21,6 +21,7 @@ import org.msgpack.Packer;
|
|||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
|
||||
public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
|
||||
|
|
@ -372,9 +373,9 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
|||
public void testFinalClassAndAbstractClass01() throws Exception {
|
||||
try {
|
||||
DynamicCodeGenPacker.create(FinalModifierClass.class);
|
||||
fail();
|
||||
} catch (DynamicCodeGenException e) {
|
||||
assertTrue(true);
|
||||
} catch (DynamicCodeGenException e) {
|
||||
fail();
|
||||
}
|
||||
assertTrue(true);
|
||||
try {
|
||||
|
|
@ -390,9 +391,9 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
|||
public void testFinalClassAndAbstractClass02() throws Exception {
|
||||
try {
|
||||
DynamicCodeGenTemplate.create(FinalModifierClass.class);
|
||||
fail();
|
||||
} catch (DynamicCodeGenException e) {
|
||||
assertTrue(true);
|
||||
} catch (DynamicCodeGenException e) {
|
||||
fail();
|
||||
}
|
||||
assertTrue(true);
|
||||
try {
|
||||
|
|
@ -429,7 +430,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testInterfaceAndEnumType02() throws Exception {
|
||||
public void testInterfaceType() throws Exception {
|
||||
try {
|
||||
DynamicCodeGenTemplate.create(SampleInterface.class);
|
||||
fail();
|
||||
|
|
@ -437,19 +438,44 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
|||
assertTrue(true);
|
||||
}
|
||||
assertTrue(true);
|
||||
try {
|
||||
DynamicCodeGenTemplate.create(SampleEnum.class);
|
||||
fail();
|
||||
} catch (DynamicCodeGenException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
public interface SampleInterface {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumTypeForOrdinal() throws Exception {
|
||||
SampleEnumFieldClass src = new SampleEnumFieldClass();
|
||||
src.f0 = 0;
|
||||
src.f1 = SampleEnum.ONE;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicCodeGenPacker
|
||||
.create(SampleEnumFieldClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicCodeGenTemplate
|
||||
.create(SampleEnumFieldClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl.convert(mpo);
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
assertTrue(src.f1 == dst.f1);
|
||||
}
|
||||
|
||||
public static class SampleEnumFieldClass {
|
||||
public int f0;
|
||||
|
||||
public SampleEnum f1;
|
||||
|
||||
public SampleEnumFieldClass() {
|
||||
}
|
||||
}
|
||||
|
||||
@MessagePackOrdinalEnum
|
||||
public enum SampleEnum {
|
||||
ONE, TWO, THREE;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import org.msgpack.Packer;
|
|||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
|
@ -351,9 +352,9 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||
public void testFinalClassAndAbstractClass01() throws Exception {
|
||||
try {
|
||||
DynamicCodeGenPacker.create(FinalModifierClass.class);
|
||||
fail();
|
||||
} catch (DynamicCodeGenException e) {
|
||||
assertTrue(true);
|
||||
} catch (DynamicCodeGenException e) {
|
||||
fail();
|
||||
}
|
||||
assertTrue(true);
|
||||
try {
|
||||
|
|
@ -369,9 +370,9 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||
public void testFinalClassAndAbstractClass02() throws Exception {
|
||||
try {
|
||||
DynamicCodeGenUnpacker.create(FinalModifierClass.class);
|
||||
fail();
|
||||
} catch (DynamicCodeGenException e) {
|
||||
assertTrue(true);
|
||||
} catch (DynamicCodeGenException e) {
|
||||
fail();
|
||||
}
|
||||
assertTrue(true);
|
||||
try {
|
||||
|
|
@ -390,7 +391,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testInterfaceAndEnumType01() throws Exception {
|
||||
public void testInterfaceType01() throws Exception {
|
||||
try {
|
||||
DynamicCodeGenPacker.create(SampleInterface.class);
|
||||
fail();
|
||||
|
|
@ -398,17 +399,10 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||
assertTrue(true);
|
||||
}
|
||||
assertTrue(true);
|
||||
try {
|
||||
DynamicCodeGenPacker.create(SampleEnum.class);
|
||||
fail();
|
||||
} catch (DynamicCodeGenException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterfaceAndEnumType02() throws Exception {
|
||||
public void testInterfaceType02() throws Exception {
|
||||
try {
|
||||
DynamicCodeGenUnpacker.create(SampleInterface.class);
|
||||
fail();
|
||||
|
|
@ -416,19 +410,41 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||
assertTrue(true);
|
||||
}
|
||||
assertTrue(true);
|
||||
try {
|
||||
DynamicCodeGenUnpacker.create(SampleEnum.class);
|
||||
fail();
|
||||
} catch (DynamicCodeGenException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
public interface SampleInterface {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumTypeForOrdinal() throws Exception {
|
||||
SampleEnumFieldClass src = new SampleEnumFieldClass();
|
||||
src.f0 = 0;
|
||||
src.f1 = SampleEnum.ONE;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicCodeGenPacker
|
||||
.create(SampleEnumFieldClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicCodeGenTemplate
|
||||
.create(SampleEnumFieldClass.class);
|
||||
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
assertTrue(src.f1 == dst.f1);
|
||||
}
|
||||
|
||||
public static class SampleEnumFieldClass {
|
||||
public int f0;
|
||||
|
||||
public SampleEnum f1;
|
||||
|
||||
public SampleEnumFieldClass() {
|
||||
}
|
||||
}
|
||||
|
||||
@MessagePackOrdinalEnum
|
||||
public enum SampleEnum {
|
||||
ONE, TWO, THREE;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue