Exemplo n.º 1
0
        public void TestSelfSerialize()
        {
            var mapping = new YamlMappingNode();
            var reader  = YamlObjectSerializer.NewWriter(mapping);

            var field = new SelfSerializeTest {
                Value = 456
            };

            reader.DataField(ref field, "foo", default);
Exemplo n.º 2
0
        public void SerializeDictTest()
        {
            // Arrange
            var data       = SerializableDict;
            var mapping    = new YamlMappingNode();
            var serializer = YamlObjectSerializer.NewWriter(mapping);

            // Act
            serializer.DataField(ref data, "datadict", new Dictionary <string, int>(0));

            // Assert
            var result = NodeToYamlText(mapping);

            Assert.That(result, Is.EqualTo(SerializedDictYaml));
        }
Exemplo n.º 3
0
        public void SerializeListTest()
        {
            // Arrange
            var data       = SerializableList;
            var mapping    = new YamlMappingNode();
            var serializer = YamlObjectSerializer.NewWriter(mapping);

            // Act
            serializer.DataField(ref data, "datalist", new List <int>(0));

            // Assert
            var result = NodeToYamlText(mapping);

            Assert.That(result, Is.EqualTo(SerializedListYaml));
        }
Exemplo n.º 4
0
        public void SerializeListAsReadOnlyCollectionTest()
        {
            // Arrange
            IReadOnlyCollection <int> data = SerializableList;
            var mapping    = new YamlMappingNode();
            var serializer = YamlObjectSerializer.NewWriter(mapping);

            // Act
            serializer.DataField(ref data, "datalist", Array.Empty <int>());

            // Assert
            var result = NodeToYamlText(mapping);

            Assert.That(result, Is.EqualTo(SerializedListYaml));
        }
        public void SerializeZeroWithoutFlagTest()
        {
            // Arrange
            var data       = 0;
            var mapping    = new YamlMappingNode();
            var serializer = YamlObjectSerializer.NewWriter(mapping);

            // Act
            serializer.DataField(ref data, "generic_flags", 0, WithFormat.Flags <GenericFlagTag>(), alwaysWrite: true);

            // Assert
            var result = YamlObjectSerializer_Test.NodeToYamlText(mapping);

            Assert.That(result, Is.EqualTo(SerializedZeroNum));
        }
        public void SerializeFiveFlagTest()
        {
            // Arrange
            var data       = (int)GenericFlags.Five;
            var mapping    = new YamlMappingNode();
            var serializer = YamlObjectSerializer.NewWriter(mapping);

            // Act
            serializer.DataField(ref data, "generic_flags", 0, WithFormat.Flags <GenericFlagTag>());

            // Assert
            var result = YamlObjectSerializer_Test.NodeToYamlText(mapping);

            Assert.That(result, Is.EqualTo(SerializedOneFourFlag));
        }
        public void SerializeNonZeroWithZeroFlagDoesntShowZeroTest()
        {
            // Arrange
            var data       = (int)FlagsWithZero.Two;
            var mapping    = new YamlMappingNode();
            var serializer = YamlObjectSerializer.NewWriter(mapping);

            // Act
            serializer.DataField(ref data, "generic_flags_with_zero", 0, WithFormat.Flags <GenericFlagWithZeroTag>());

            // Assert
            var result = YamlObjectSerializer_Test.NodeToYamlText(mapping);

            Assert.That(result, Is.EqualTo(SerializedTwoWithZeroFlag));
        }
Exemplo n.º 8
0
        public void SerializeOneConstantTest()
        {
            // Arrange
            var data       = (int)GenericConstants.One;
            var mapping    = new YamlMappingNode();
            var serializer = YamlObjectSerializer.NewWriter(mapping);

            // Act
            serializer.DataField(ref data, "generic_constants", 0, WithFormat.Constants <GenericConstantTag>());

            // Assert
            var result = YamlObjectSerializer_Test.NodeToYamlText(mapping);

            Assert.That(result, Is.EqualTo(SerializedOneConstant));
        }
        public void SerializeTypeTest()
        {
            ITestType?type    = new TestTypeOne();
            var       mapping = new YamlMappingNode();
            var       writer  = YamlObjectSerializer.NewWriter(mapping);

            writer.DataField(ref type, "type", null !);

            Assert.IsNotEmpty(mapping.Children);
            Assert.IsInstanceOf <YamlScalarNode>(mapping.Children[0].Key);

            var scalar = (YamlScalarNode)mapping.Children[0].Key;

            Assert.That(scalar.Value, Is.EqualTo("type"));
        }
Exemplo n.º 10
0
            private void WriteEntitySection()
            {
                var entities = new YamlSequenceNode();

                RootNode.Add("entities", entities);

                foreach (var entity in Entities)
                {
                    CurrentWritingEntity = entity;
                    var mapping = new YamlMappingNode
                    {
                        { "uid", EntityUidMap[entity.Uid].ToString(CultureInfo.InvariantCulture) }
                    };

                    if (entity.Prototype != null)
                    {
                        mapping.Add("type", entity.Prototype.ID);
                    }

                    var components = new YamlSequenceNode();
                    // See engine#636 for why the Distinct() call.
                    foreach (var component in entity.GetAllComponents())
                    {
                        var compMapping = new YamlMappingNode();
                        CurrentWritingComponent = component.Name;
                        var compSerializer = YamlObjectSerializer.NewWriter(compMapping, this);

                        component.ExposeData(compSerializer);

                        // Don't need to write it if nothing was written!
                        if (compMapping.Children.Count != 0)
                        {
                            // Something actually got written!
                            compMapping.Add("type", component.Name);
                            components.Add(compMapping);
                        }
                    }

                    if (components.Children.Count != 0)
                    {
                        mapping.Add("components", components);
                    }

                    entities.Add(mapping);
                }
            }
Exemplo n.º 11
0
            void WriteEntitySection()
            {
                var entities = new YamlSequenceNode();

                RootNode.Add("entities", entities);

                foreach (var entity in Entities)
                {
                    CurrentWritingEntity = entity;
                    var mapping = new YamlMappingNode();
                    mapping.Add("type", entity.Prototype.ID);
                    if (entity.Name != entity.Prototype.Name)
                    {
                        // TODO: This shouldn't be hardcoded.
                        mapping.Add("name", entity.Prototype.Name);
                    }

                    var components = new YamlSequenceNode();
                    // See engine#636 for why the Distinct() call.
                    foreach (var component in entity.GetAllComponents().Distinct())
                    {
                        var compMapping = new YamlMappingNode();
                        CurrentWritingComponent = component.Name;
                        var compSerializer = YamlObjectSerializer.NewWriter(compMapping, this);

                        component.ExposeData(compSerializer);

                        // Don't need to write it if nothing was written!
                        if (compMapping.Children.Count != 0)
                        {
                            // Something actually got written!
                            compMapping.Add("type", component.Name);
                            components.Add(compMapping);
                        }
                    }

                    if (components.Children.Count != 0)
                    {
                        mapping.Add("components", components);
                    }

                    entities.Add(mapping);
                }
            }
Exemplo n.º 12
0
        public void SerializeExpressionTest()
        {
            var dummy = new DummyClass
            {
                Bar = "honk!",
                Baz = Color.Black,
                Foo = 5
            };

            var mapping    = new YamlMappingNode();
            var serializer = YamlObjectSerializer.NewWriter(mapping);

            serializer.DataField(dummy, d => d.Foo, "foo", 1);
            serializer.DataField(dummy, d => d.Bar, "bar", "*silence*");
            serializer.DataField(dummy, d => d.Baz, "baz", Color.Black);

            Assert.That(mapping, Is.EquivalentTo(new YamlMappingNode {
                { "bar", "honk!" }, { "foo", "5" }
            }));
        }
Exemplo n.º 13
0
        public void SerializeTypePropertiesTest()
        {
            ITestType?type = new TestTypeTwo
            {
                TestPropertyOne = "B",
                TestPropertyTwo = 10
            };
            var mapping = new YamlMappingNode();
            var writer  = YamlObjectSerializer.NewWriter(mapping);

            writer.DataField(ref type, "test", null !);

            Assert.IsNotEmpty(mapping.Children);

            var testPropertyOne = (YamlScalarNode)mapping["test"]["testPropertyOne"];
            var testPropertyTwo = (YamlScalarNode)mapping["test"]["testPropertyTwo"];

            Assert.That(testPropertyOne.Value, Is.EqualTo("B"));
            Assert.That(testPropertyTwo.Value, Is.EqualTo("10"));
        }
Exemplo n.º 14
0
        public void SaveToUserData()
        {
            var mapping = new YamlMappingNode();
            var ser     = YamlObjectSerializer.NewWriter(mapping);

            var modifiedBindings = _modifiedKeyFunctions
                                   .Select(p => _bindingsByFunction[p])
                                   .SelectMany(p => p)
                                   .Select(p => new KeyBindingRegistration
            {
                Function  = p.Function,
                BaseKey   = p.BaseKey,
                Mod1      = p.Mod1,
                Mod2      = p.Mod2,
                Mod3      = p.Mod3,
                Priority  = p.Priority,
                Type      = p.BindingType,
                CanFocus  = p.CanFocus,
                CanRepeat = p.CanRepeat
            }).ToArray();

            var leaveEmpty = _modifiedKeyFunctions
                             .Where(p => _bindingsByFunction[p].Count == 0)
                             .ToArray();

            var version = 1;

            ser.DataField(ref version, "version", 1);
            ser.DataField(ref modifiedBindings, "binds", null);
            ser.DataField(ref leaveEmpty, "leaveEmpty", null);

            var path = new ResourcePath(KeybindsPath);

            using var writer = new StreamWriter(_resourceMan.UserData.Create(path));
            var stream = new YamlStream {
                new YamlDocument(mapping)
            };

            stream.Save(new YamlMappingFix(new Emitter(writer)), false);
        }