Пример #1
0
        public void TestSimple()
        {
            var yamlMapping = new DynamicYaml(
                @"name: This is a property
key: second key");

            Assert.Equal(2, yamlMapping.RootNode.Children.Count);

            // Test basic accessor
            dynamic obj = yamlMapping.DynamicRootNode;

            Assert.Equal("This is a property", (string)obj.name);
            Assert.Equal("second key", (string)obj.key);
            Assert.Null((string)obj.invalid);

            // Test remove a key
            var dyn = (DynamicYamlMapping)obj;

            dyn.RemoveChild("name");
            Assert.Null((string)obj.name);

            // Test serialization back to a string
            var text = yamlMapping.ToString().TrimEnd();

            Assert.Equal("key: second key", text);
        }
Пример #2
0
        public void TestWithOverrides()
        {
            var yamlMapping = new DynamicYaml(
                @"name*: This is a property
key!: second key
override1!*: combine override 1
override2*!: combine override 2
nooverrides: no overrides here!
");

            Assert.Equal(5, yamlMapping.RootNode.Children.Count);

            // Test basic accessor
            dynamic obj = yamlMapping.DynamicRootNode;

            Assert.Equal("This is a property", (string)obj.name);
            Assert.Equal("second key", (string)obj.key);
            Assert.Equal("no overrides here!", (string)obj.nooverrides);
            Assert.Null((string)obj.invalid);

            // Check overrides
            var dyn = (DynamicYamlMapping)obj;

            Assert.Equal(OverrideType.New, dyn.GetOverride("name"));
            Assert.Equal(OverrideType.Sealed, dyn.GetOverride("key"));
            Assert.Equal(OverrideType.New | OverrideType.Sealed, dyn.GetOverride("override1"));
            Assert.Equal(OverrideType.New | OverrideType.Sealed, dyn.GetOverride("override2"));
            Assert.Equal(OverrideType.Base, dyn.GetOverride("nooverrides"));

            // Check that removing a child will remove overrides information
            dyn.RemoveChild("override2");
            Assert.Equal(OverrideType.Base, dyn.GetOverride("override2"));

            // Modify overrides, check that we can still access the values
            dyn.SetOverride("name", OverrideType.Sealed);
            dyn.SetOverride("key", OverrideType.New);
            Assert.Equal("This is a property", (string)obj.name);
            Assert.Equal("second key", (string)obj.key);

            // Test serialization back to a string
            var text = yamlMapping.ToString().TrimEnd().Replace("\r\n", "\n");

            Assert.Equal(@"name!: This is a property
key*: second key
override1!*: combine override 1
nooverrides: no overrides here!
".TrimEnd().Replace("\r\n", "\n"), text);
        }