コード例 #1
0
 private void VerifyPerson(YamlMapping mapping, string name, string reason)
 {
     Assert.That(mapping, Is.Not.Null);
     Assert.That(mapping.ContainsKey("name"), Is.True);
     Assert.That(mapping["name"].ToString(), Is.EqualTo(name));
     Assert.That(mapping.ContainsKey("reason"), Is.True);
     Assert.That(mapping["reason"].ToString(), Is.EqualTo(reason));
 }
コード例 #2
0
        public void TestChangingMappingKey()
        {
            var key = (YamlScalar)"def";
            var map = new YamlMapping(key, 3);

            key.Value = "ghi";
            Assert.IsTrue(map.ContainsKey(key));
            Assert.IsTrue(map.Remove(key));

            map[map] = 4;
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);

            var map2 = new YamlMapping(key, 3);

            map2[map2] = 2;
            map2[map]  = 4;
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            map[map2] = 2;
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);

            map.Tag = YamlNode.DefaultTagPrefix + "map";
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);
            Assert.IsTrue(map.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map[map2]);

            var seq = new YamlSequence(map);

            map.Add(seq, 4);
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);
            Assert.IsTrue(map.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map[map2]);
            seq.Add(map);
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);
            Assert.IsTrue(map.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map[map2]);
        }
コード例 #3
0
ファイル: YamlParserTests.cs プロジェクト: jagregory/yayaml
        private void ValidatePersonYaml(YamlMapping map, string name, string age, string[] phones)
        {
            Assert.That(map, Is.Not.Null);
            Assert.That(map.ContainsKey("name"), Is.True);
            Assert.That(map.ContainsKey("age"), Is.True);
            Assert.That(map.ContainsKey("phones"), Is.True);

            Assert.That(map["name"].ToString(), Is.EqualTo(name));
            Assert.That(map["age"].ToString(), Is.EqualTo(age));

            var phoneSeq = map["phones"] as YamlSequence;

            Assert.That(phoneSeq, Is.Not.Null);
            Assert.That(phoneSeq.Count, Is.EqualTo(phones.Length));

            for (int i = 0; i < phones.Length; i++)
            {
                Assert.That(phoneSeq[i].ToString(), Is.EqualTo(phones[i]));
            }
        }
コード例 #4
0
ファイル: YamlExtensions.cs プロジェクト: dekkerb115/Bam.Net
        public static object GetValue(this YamlMapping mapping, string key, object ifKeyNotPresent = null)
        {
            if (!mapping.ContainsKey(key))
            {
                return(ifKeyNotPresent);
            }
            object value = mapping[key];

            if (value is YamlScalar scalar)
            {
                return(scalar.Value);
            }
            return(value);
        }
コード例 #5
0
ファイル: YamlExtensions.cs プロジェクト: dekkerb115/Bam.Net
        public static object Convert(this YamlMapping mapping, Type type)
        {
            object result = type.Construct();

            foreach (PropertyInfo property in type.GetProperties())
            {
                if (mapping.ContainsKey(property.Name))
                {
                    YamlNode node = mapping[property.Name];
                    if (node is YamlScalar value)
                    {
                        result.Property(property.Name, System.Convert.ChangeType(value.Value, property.PropertyType));
                    }
                    else if (node is YamlMapping childMapping)
                    {
                        result.Property(property.Name, childMapping.Convert(property.PropertyType));
                    }
                }
            }
            return(result);
        }
コード例 #6
0
        public void MergeKey()
        {
            var map      = new YamlMapping("existing", "value");
            var mergeKey = new YamlScalar("!!merge", "<<");

            map.Add(mergeKey, new YamlMapping("existing", "new value"));
            map.OnLoaded();
            Assert.AreEqual(1, map.Count);
            Assert.IsTrue(map.ContainsKey("existing"));
            Assert.AreEqual((YamlNode)"value", map["existing"]);

            map.Add(mergeKey, new YamlMapping("not existing", "new value"));
            map.OnLoaded();
            Assert.AreEqual(2, map.Count);
            Assert.IsTrue(map.ContainsKey("existing"));
            Assert.AreEqual((YamlNode)"value", map["existing"]);
            Assert.IsTrue(map.ContainsKey("not existing"));
            Assert.AreEqual((YamlNode)"new value", map["not existing"]);

            map.Add(mergeKey, new YamlMapping("key1", "value1", 2, 2, 3.0, 3.0));
            map.OnLoaded();
            Assert.AreEqual(5, map.Count);
            Assert.IsTrue(map.ContainsKey("existing"));
            Assert.AreEqual((YamlNode)"value", map["existing"]);
            Assert.IsTrue(map.ContainsKey("not existing"));
            Assert.AreEqual((YamlNode)"new value", map["not existing"]);
            Assert.IsTrue(map.ContainsKey(2));
            Assert.AreEqual((YamlNode)2, map[2]);
            Assert.IsTrue(map.ContainsKey(3.0));
            Assert.AreEqual((YamlNode)3.0, map[3.0]);

            map = new YamlMapping(
                "existing", "value",
                mergeKey, new YamlMapping("not existing", "new value"));
            map.OnLoaded();
            Assert.AreEqual(2, map.Count);
            Assert.IsTrue(map.ContainsKey("existing"));
            Assert.AreEqual((YamlNode)"value", map["existing"]);
            Assert.IsTrue(map.ContainsKey("not existing"));
            Assert.AreEqual((YamlNode)"new value", map["not existing"]);

            map = (YamlMapping)YamlNode.FromYaml(
                "key1: value1\r\n" +
                "key2: value2\r\n" +
                "<<: \r\n" +
                "  key2: value2 modified\r\n" +
                "  key3: value3\r\n" +
                "  <<: \r\n" +
                "    key4: value4\r\n" +
                "    <<: value5\r\n" +
                "key6: <<\r\n")[0];
            Assert.AreEqual(
                "%YAML 1.2\r\n" +
                "---\r\n" +
                "<<: value5\r\n" +
                "key6: <<\r\n" +
                "key3: value3\r\n" +
                "key2: value2\r\n" +
                "key4: value4\r\n" +
                "key1: value1\r\n" +
                "...\r\n",
                map.ToYaml()
                );
            Assert.IsTrue(map.ContainsKey(mergeKey));
            Assert.AreEqual(mergeKey, map["key6"]);

            map.Remove(mergeKey);
            map.Add(mergeKey, map); // recursive
            map.OnLoaded();
            Assert.AreEqual(        // nothing has been changed
                "%YAML 1.2\r\n" +
                "---\r\n" +
                "key6: <<\r\n" +
                "key3: value3\r\n" +
                "key2: value2\r\n" +
                "key4: value4\r\n" +
                "key1: value1\r\n" +
                "...\r\n",
                map.ToYaml()
                );
        }
コード例 #7
0
        public void TestChangingMappingKey()
        {
            var key = (YamlScalar)"def";
            var map = new YamlMapping(key, 3);
            key.Value = "ghi";
            Assert.IsTrue(map.ContainsKey(key));
            Assert.IsTrue(map.Remove(key));

            map[map] = 4;
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);

            var map2 = new YamlMapping(key, 3);
            map2[map2] = 2;
            map2[map] = 4;
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            map[map2] = 2;
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);

            map.Tag = YamlNode.DefaultTagPrefix + "map";
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);
            Assert.IsTrue(map.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map[map2]);

            var seq = new YamlSequence(map);
            map.Add(seq, 4);
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);
            Assert.IsTrue(map.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map[map2]);
            seq.Add(map);
            Assert.IsTrue(map2.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map2[map2]);
            Assert.AreEqual((YamlNode)4, map2[map]);
            Assert.IsTrue(map.ContainsKey(map));
            Assert.AreEqual((YamlNode)4, map[map]);
            Assert.IsTrue(map.ContainsKey(map2));
            Assert.AreEqual((YamlNode)2, map[map2]);
        }
コード例 #8
0
        public void MergeKey()
        {
            var map = new YamlMapping("existing", "value");
            var mergeKey = new YamlScalar("!!merge", "<<");

            map.Add(mergeKey, new YamlMapping("existing", "new value"));
            map.OnLoaded();
            Assert.AreEqual(1, map.Count);
            Assert.IsTrue(map.ContainsKey("existing"));
            Assert.AreEqual((YamlNode) "value", map["existing"]);

            map.Add(mergeKey, new YamlMapping("not existing", "new value"));
            map.OnLoaded();
            Assert.AreEqual(2, map.Count);
            Assert.IsTrue(map.ContainsKey("existing"));
            Assert.AreEqual((YamlNode) "value", map["existing"]);
            Assert.IsTrue(map.ContainsKey("not existing"));
            Assert.AreEqual((YamlNode) "new value", map["not existing"]);

            map.Add(mergeKey, new YamlMapping("key1", "value1", 2, 2, 3.0, 3.0));
            map.OnLoaded();
            Assert.AreEqual(5, map.Count);
            Assert.IsTrue(map.ContainsKey("existing"));
            Assert.AreEqual((YamlNode) "value", map["existing"]);
            Assert.IsTrue(map.ContainsKey("not existing"));
            Assert.AreEqual((YamlNode) "new value", map["not existing"]);
            Assert.IsTrue(map.ContainsKey(2));
            Assert.AreEqual((YamlNode) 2, map[2]);
            Assert.IsTrue(map.ContainsKey(3.0));
            Assert.AreEqual((YamlNode) 3.0, map[3.0]);

            map = new YamlMapping(
                "existing", "value",
                mergeKey, new YamlMapping("not existing", "new value"));
            map.OnLoaded();
            Assert.AreEqual(2, map.Count);
            Assert.IsTrue(map.ContainsKey("existing"));
            Assert.AreEqual((YamlNode) "value", map["existing"]);
            Assert.IsTrue(map.ContainsKey("not existing"));
            Assert.AreEqual((YamlNode) "new value", map["not existing"]);

            map = (YamlMapping) YamlNode.FromYaml(
                "key1: value1\r\n" +
                "key2: value2\r\n" +
                "<<: \r\n" +
                "  key2: value2 modified\r\n" +
                "  key3: value3\r\n" +
                "  <<: \r\n" +
                "    key4: value4\r\n" +
                "    <<: value5\r\n" +
                "key6: <<\r\n")[0];

            var mapBack = map.ToYaml();

            Assert.AreEqual(@"%YAML 1.2
---
<<: value5
key1: value1
key2: value2
key3: value3
key4: value4
key6: <<
...
", mapBack);
            Assert.IsTrue(map.ContainsKey(mergeKey));
            Assert.AreEqual(mergeKey, map["key6"]);

            map.Remove(mergeKey);
            map.Add(mergeKey, map); // recursive
            map.OnLoaded();

            // nothing has been changed
            mapBack = map.ToYaml();
            Assert.AreEqual(@"%YAML 1.2
---
key1: value1
key2: value2
key3: value3
key4: value4
key6: <<
...
", mapBack);
        }