SerializationTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MineCase.Engine;
  7. using MineCase.Engine.Serialization;
  8. using MineCase.Graphics;
  9. using MineCase.Serialization.Serializers;
  10. using MongoDB.Bson;
  11. using MongoDB.Bson.IO;
  12. using MongoDB.Bson.Serialization;
  13. using Xunit;
  14. namespace MineCase.UnitTest
  15. {
  16. public class SerializationTest
  17. {
  18. public DependencyProperty<Slot> SlotProperty = DependencyProperty.Register<Slot>("Slot", typeof(SerializationTest), new PropertyMetadata<Slot>(Slot.Empty));
  19. public DependencyProperty<Shape> ShapeProperty = DependencyProperty.Register<Shape>("Shape", typeof(SerializationTest));
  20. public DependencyProperty<Cuboid> CuboidProperty = DependencyProperty.Register<Cuboid>("Cuboid", typeof(SerializationTest));
  21. public DependencyProperty<StateHolder> StateProperty = DependencyProperty.Register<StateHolder>("State", typeof(SerializationTest));
  22. public class Pair
  23. {
  24. public int Int { get; set; }
  25. public Shape Collider { get; set; }
  26. }
  27. public class StateHolder
  28. {
  29. public List<Pair> Shape { get; set; }
  30. }
  31. public SerializationTest()
  32. {
  33. Serializers.RegisterAll();
  34. }
  35. [Fact]
  36. public async Task Test1()
  37. {
  38. var slot = new Slot { BlockId = 1, ItemDamage = 2, ItemCount = 3 };
  39. var shape = new Cuboid(new Point3d(0, 1, 2), new Size(10, 20, 30));
  40. var state = new StateHolder { Shape = new List<Pair> { new Pair { Collider = shape } } };
  41. var entity = new TestEntity();
  42. await entity.ReadStateAsync();
  43. entity.SetCurrentValue(SlotProperty, slot);
  44. entity.SetCurrentValue(ShapeProperty, shape);
  45. entity.SetCurrentValue(CuboidProperty, shape);
  46. entity.SetCurrentValue(StateProperty, state);
  47. var doc = Serialize(entity);
  48. Assert.Equal(2, doc.ElementCount);
  49. var vs = doc.GetElement(1);
  50. Assert.Equal("ValueStorage", vs.Name);
  51. var vsv = (BsonDocument)vs.Value;
  52. Assert.Equal(4, vsv.ElementCount);
  53. entity = new TestEntity();
  54. entity.BsonDocument = doc;
  55. await entity.ReadStateAsync();
  56. Assert.Equal(4, entity.ValueStorage.Keys.Count());
  57. Assert.Equal(slot, entity.GetValue(SlotProperty));
  58. Assert.Equal(shape, entity.GetValue(ShapeProperty));
  59. Assert.Equal(shape, entity.GetValue(CuboidProperty));
  60. Assert.Equal(shape, entity.GetValue(StateProperty).Shape[0].Collider);
  61. }
  62. private BsonDocument Serialize(TestEntity entity)
  63. {
  64. var doc = new BsonDocument();
  65. var writer = new BsonDocumentWriter(doc);
  66. var context = BsonSerializationContext.CreateRoot(writer);
  67. var serializer = new DependencyObjectStateSerializer();
  68. serializer.Serialize(context, new DependencyObjectState
  69. {
  70. GrainKeyString = "test",
  71. ValueStorage = entity.ValueStorage
  72. });
  73. return doc;
  74. }
  75. internal class TestEntity : DependencyObject
  76. {
  77. public BsonDocument BsonDocument;
  78. protected override Task<DependencyObjectState> DeserializeStateAsync()
  79. {
  80. if (BsonDocument == null) return base.DeserializeStateAsync();
  81. var reader = new BsonDocumentReader(BsonDocument);
  82. var serializer = new DependencyObjectStateSerializer();
  83. var context = BsonDeserializationContext.CreateRoot(reader);
  84. return Task.FromResult(serializer.Deserialize(context));
  85. }
  86. }
  87. }
  88. }