Example #1
0
            protected override bool OnTest()
            {
                Logger.Log("Running BinarySerializable Test...");

                BinarySerial bin1 = new(10);

                // Try saving text1 to file. `ToFile<T>` is a convenience method that opens a
                // file stream and calls `SaveToStream` on the object.
                if (!BinarySerializable.ToFile(bin1, BinarySerialPath, true))
                {
                    return(Logger.LogReturn("Failed! Unable to serialize to file.", false));
                }

                // Try loading previously saved object from file. `FromFile<T>` is a convenience
                // method that opens a file stream and calls `LoadFromStream` on the object.
                BinarySerial bin2 = BinarySerializable.FromFile <BinarySerial>(BinarySerialPath);

                if (bin2 == null)
                {
                    return(Logger.LogReturn("Failed! Unable to deserialize from file.", false));
                }

                // Comparing saved and loaded data.
                if (bin1.NumberData != bin2.NumberData ||
                    !bin1.TextData.Equals(bin2.TextData))
                {
                    return(Logger.LogReturn("Failed! Deserialized object contains different values.", false));
                }

                bin1.Dispose();
                bin2.Dispose();

                try
                {
                    File.Delete(BinarySerialPath);
                }
                catch
                {
                    Logger.Log($"Failed! Unable to delete { BinarySerialPath }.", LogType.Warning);
                }

                return(Logger.LogReturn("BinarySerializable test succeeded!", true));
            }