public void DeserialiseSpriteGroupTest() { byte[] spriteGroupData = new byte[] { 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x1C, 0x40, 0x00, 0x46, 0x1C, 0x40, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x42, 0x00, 0x00, 0x3B, 0x80, 0x00, 0x00, 0x3F, 0x7F, 0x00, 0x00, 0x3E, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x1C, 0x40, 0x00, 0x46, 0x1C, 0x40, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x7F, 0x00, 0x00, 0x3E, 0x78, 0x00, 0x00, 0x3F, 0x42, 0x00, 0x00, 0x3B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x1C, 0x40, 0x00, 0x46, 0x1C, 0x40, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x42, 0x00, 0x00, 0x3E, 0x78, 0x00, 0x00, 0x3F, 0x7F, 0x00, 0x00, 0x3B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x42, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x1C, 0x40, 0x00, 0x46, 0x1C, 0x40, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x7F, 0x00, 0x00, 0x3B, 0x80, 0x00, 0x00, 0x3F, 0x42, 0x00, 0x00, 0x3E, 0x78, 0x00, 0x00, }; using MemoryStream memoryStream = new MemoryStream(spriteGroupData); var obj = Deserialiser.Deserialise(new Imas.Binary(memoryStream, true), typeof(SpriteGroup)); }
private static T TestTopLevel <T>(T value, Action <MsgPack.Packer> packAction, Action <T> customEvaluator = null) { byte[] msgPackCliBytes; using (var stream = new MemoryStream()) using (var packer = MsgPack.Packer.Create(stream, PackerCompatibilityOptions.None)) { packAction(packer); stream.Position = 0; msgPackCliBytes = stream.ToArray(); } var deserialisedValue = new Deserialiser <T>().Deserialise(msgPackCliBytes); if (customEvaluator != null) { customEvaluator(deserialisedValue); } else { Assert.Equal(value, deserialisedValue); } var dasherBytes = new Serialiser <T>().Serialise(value); Assert.Equal(msgPackCliBytes.Length, dasherBytes.Length); Assert.Equal(msgPackCliBytes, dasherBytes); return(deserialisedValue); }
static void Main(string[] args) { for (int i = 0; i < 50000000; i++) { Deserialiser <Test> .Deserialise(new object[] { "hi", 12, "test", "" }); } }
public void HandlesRecurringType() { var stream = new MemoryStream(); var packer = new Packer(stream); packer.PackMapHeader(2); packer.Pack("Num"); packer.Pack(1); packer.Pack("Inner"); { packer.PackMapHeader(2); packer.Pack("Num"); packer.Pack(2); packer.Pack("Inner"); packer.PackNull(); } packer.Flush(); stream.Position = 0; var after = new Deserialiser <Recurring>().Deserialise(stream); Assert.Equal(1, after.Num); Assert.NotNull(after.Inner); Assert.Equal(2, after.Inner.Num); Assert.Null(after.Inner.Inner); }
public void DeserialiseEmptyAsUnion() { var bytes = PackBytes(packer => packer.PackMapHeader(0)); var o = new Deserialiser <Union <UserScore, UserScoreStruct> >(UnexpectedFieldBehaviour.Throw).Deserialise(bytes); Assert.Null(o); }
public void DeserialiseEmptyAsNullableComplexStructWithAllDefaults() { var bytes = PackBytes(packer => packer.PackMapHeader(0)); var o = new Deserialiser <StructWithAllDefaults?>(UnexpectedFieldBehaviour.Throw).Deserialise(bytes); Assert.Null(o); }
public void DeserialiseEmptyAsComplexStructWithAllDefaults() { var bytes = PackBytes(packer => packer.PackMapHeader(0)); var o = new Deserialiser <StructWithAllDefaults>(UnexpectedFieldBehaviour.Throw).Deserialise(bytes); o.AssertHasDefaultValues(); }
public void HandlesNullableWithDefault() { var bytes = PackBytes(packer => packer.PackMapHeader(0)); var after = new Deserialiser <NullableWithDefaultValue>().Deserialise(bytes); Assert.True(after.B); }
public void Import(object obj) { Console.WriteLine("Import"); var importDialog = new OpenFileDialog { Title = "Import binary file", FileName = "CookBookData", Filter = "Binary files (*.bin)|*.bin", DefaultExt = ".bin" }; if (importDialog.ShowDialog() == true) { Deserialiser d = new Deserialiser(importDialog.FileName); List <Recipe> dRecipes; List <Ingredient> dIngredients; List <Measure> dMeasures; List <RecipeIngredient> dRecipeIngredients; List <RecipeStep> dRecipeSteps; if (d.Deserialise()) { dRecipes = d.DeserialisedRecipes; dIngredients = d.DeserialisedIngredients; dMeasures = d.DeserialisedMeasures; dRecipeIngredients = d.DeserialisedRecipeIngredients; dRecipeSteps = d.DeserialisedRecipeSteps; // ToDo: clear DB foreach (var measure in dMeasures) { //this.dbActions.AddMeasure(new Measure { name = measure.name }); } foreach (var ingredient in dIngredients) { //this.dbActions.AddIngredient(new Ingredient { name = ingredient.name }); } foreach (var recipe in dRecipes) { //this.dbActions.AddRecipe(new Recipe { name = recipe.name, prepTime = recipe.prepTime }); } // ToDo: recipeSteps and RecipeIngredients // ToDo: update views MessageBox.Show("Data imported successfully", "Data imported", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Could not deserialise data", "Deserialisation error", MessageBoxButton.OK, MessageBoxImage.Error); } } }
public void HandlesEnumPropertiesCorrectly() { var bytes = PackBytes(packer => packer.PackMapHeader(1) .Pack("TestEnum").Pack("Bar")); var after = new Deserialiser <WithEnumProperty>().Deserialise(bytes); Assert.Equal(TestEnum.Bar, after.TestEnum); }
public void DeserialiseControlTest() { using FileStream fileStream = new FileStream("playground/topS4U.pau", FileMode.Open); fileStream.Seek(4, SeekOrigin.Begin); var obj = Deserialiser.Deserialise(new Imas.Binary(fileStream, true), typeof(Control)); using FileStream memoryStream1 = new FileStream("playground/topS4Uout.pau", FileMode.Create); Serialiser.Serialise(new Imas.Binary(memoryStream1, true), obj); }
public void DeserialisesEnumMembersCaseInsensitively() { var bytes = PackBytes(packer => packer.PackMapHeader(1) .Pack("TestEnum").Pack("BAR")); var after = new Deserialiser <WithEnumProperty>().Deserialise(bytes); Assert.Equal(TestEnum.Bar, after.TestEnum); }
public void UsesDefaultValuesIfNotInMessage() { var bytes = PackBytes(packer => packer.PackMapHeader(0)); var deserialiser = new Deserialiser <ClassWithAllDefaults>(); var after = deserialiser.Deserialise(bytes); after.AssertHasDefaultValues(); }
public void DeserialisesEnumMembersCaseInsensitively() { var bytes = PackBytes(packer => packer.PackMapHeader(1) .Pack(nameof(ValueWrapper <TestEnum> .Value)).Pack("BAR")); var after = new Deserialiser <ValueWrapper <TestEnum> >().Deserialise(bytes); Assert.Equal(TestEnum.Bar, after.Value); }
public void HandlesBinary() { var bytes = PackBytes(packer => packer.PackMapHeader(1) .Pack("Bytes").PackBinary(new byte[] { 1, 2, 3, 4 })); var after = new Deserialiser <WithBinary>().Deserialise(bytes); Assert.Equal(new byte[] { 1, 2, 3, 4 }, after.Bytes); }
public void ThrowsOnEmptyData() { var bytes = new byte[0]; var deserialiser = new Deserialiser <UserScore>(); var ex = Assert.Throws <IOException>( () => deserialiser.Deserialise(bytes)); Assert.Equal("End of stream reached.", ex.Message); }
public void DeserialiseToStruct() { var bytes = PackBytes(packer => packer.PackMapHeader(2) .Pack("Name").Pack("Bob") .Pack("Score").Pack(123)); var after = new Deserialiser <UserScoreStruct>().Deserialise(bytes); Assert.Equal("Bob", after.Name); Assert.Equal(123, after.Score); }
public void HandlesReadOnlyListProperty() { var bytes = PackBytes(packer => packer.PackMapHeader(2) .Pack("Name").Pack("Bob") .Pack("Scores").PackArrayHeader(3).Pack(1).Pack(2).Pack(3)); var after = new Deserialiser <UserScoreList>().Deserialise(bytes); Assert.Equal("Bob", after.Name); Assert.Equal(new[] { 1, 2, 3 }, after.Scores); }
public void HandlesDecimal() { var bytes = PackBytes(packer => packer.PackMapHeader(2) .Pack("Name").Pack("Bob") .Pack("Score").Pack("123.4567")); var after = new Deserialiser <UserScoreDecimal>().Deserialise(bytes); Assert.Equal("Bob", after.Name); Assert.Equal(123.4567m, after.Score); }
public void MultiplePropertiesExactMatch() { var bytes = PackBytes(packer => packer.PackMapHeader(2) .Pack("Name").Pack("Bob") .Pack("Score").Pack(123)); var after = new Deserialiser <UserScore>().Deserialise(bytes); Assert.Equal("Bob", after.Name); Assert.Equal(123, after.Score); }
public void ThrowsOnNonMapData() { var bytes = PackBytes(packer => packer.PackArrayHeader(2) .Pack("Name").Pack(123)); var deserialiser = new Deserialiser <UserScore>(); var ex = Assert.Throws <DeserialisationException>(() => deserialiser.Deserialise(bytes)); Assert.Equal("Message must be encoded as a MsgPack map", ex.Message); Assert.Equal(typeof(UserScore), ex.TargetType); }
public void ThrowsOnEmptyData() { var bytes = new byte[0]; var deserialiser = new Deserialiser <UserScore>(); var ex = Assert.Throws <DeserialisationException>( () => deserialiser.Deserialise(bytes)); Assert.Equal(typeof(UserScore), ex.TargetType); Assert.Equal("Data stream empty", ex.Message); }
public void MixedUpCapitalisation() { var bytes = PackBytes(packer => packer.PackMapHeader(2) .Pack("NaMe").Pack("Bob") .Pack("ScorE").Pack(123)); var after = new Deserialiser <UserScore>().Deserialise(bytes); Assert.Equal("Bob", after.Name); Assert.Equal(123, after.Score); }
public void ReorderedFields() { var bytes = PackBytes(packer => packer.PackMapHeader(2) .Pack("Score").Pack(123) .Pack("Name").Pack("Bob")); var after = new Deserialiser <UserScore>().Deserialise(bytes); Assert.Equal("Bob", after.Name); Assert.Equal(123, after.Score); }
public void SpecifiedValueOverridesDefaultValue() { var bytes = PackBytes(packer => packer.PackMapHeader(2) .Pack("Name").Pack("Bob") .Pack("Score").Pack(12345)); // score has a default of 100 var deserialiser = new Deserialiser <UserScoreWithDefaultScore>(); var after = deserialiser.Deserialise(bytes); Assert.Equal("Bob", after.Name); Assert.Equal(12345, after.Score); }
public void ThrowsOnMissingField() { var bytes = PackBytes(packer => packer.PackMapHeader(1) .Pack("Name").Pack("Bob")); var deserialiser = new Deserialiser <UserScore>(); var ex = Assert.Throws <DeserialisationException>( () => deserialiser.Deserialise(bytes)); Assert.Equal(typeof(UserScore), ex.TargetType); Assert.Equal("Missing required field \"score\".", ex.Message); }
public void HandlesListOfListProperty() { var bytes = PackBytes(packer => packer.PackMapHeader(1) .Pack("Jagged").PackArrayHeader(2) .PackArrayHeader(3).Pack(1).Pack(2).Pack(3) .PackArrayHeader(3).Pack(4).Pack(5).Pack(6)); var after = new Deserialiser <ListOfList>().Deserialise(bytes); Assert.Equal(2, after.Jagged.Count); Assert.Equal(new[] { 1, 2, 3 }, after.Jagged[0]); Assert.Equal(new[] { 4, 5, 6 }, after.Jagged[1]); }
public void ThrowsOnIncorrectDataType() { var bytes = PackBytes(packer => packer.PackMapHeader(2) .Pack("Name").Pack("Bob") .Pack("Score").Pack(123.4)); // double, should be int var deserialiser = new Deserialiser <UserScore>(); var ex = Assert.Throws <DeserialisationException>( () => deserialiser.Deserialise(bytes)); Assert.Equal(typeof(UserScore), ex.TargetType); Assert.Equal("Unexpected type for \"score\". Expected Int32, got Float64.", ex.Message); }
public void IgnoresUnexpectedField() { var bytes = PackBytes(packer => packer.PackMapHeader(3) .Pack("Name").Pack("Bob") .Pack("Score").Pack(123) .Pack("SUPRISE").Pack("Unexpected")); var deserialiser = new Deserialiser <UserScore>(UnexpectedFieldBehaviour.Ignore); var after = deserialiser.Deserialise(bytes); Assert.Equal("Bob", after.Name); Assert.Equal(123, after.Score); }
public void HandlesNestedComplexTypes() { var bytes = PackBytes(packer => packer.PackMapHeader(2) .Pack("Weight").Pack(0.5d) .Pack("UserScore").PackMapHeader(2) .Pack("Name").Pack("Bob") .Pack("Score").Pack(123)); var after = new Deserialiser <UserScoreWrapper>().Deserialise(bytes); Assert.Equal(0.5d, after.Weight); Assert.Equal("Bob", after.UserScore.Name); Assert.Equal(123, after.UserScore.Score); }