public void DisposeTest()
        {
            // In .NET versions higher than 3.5, the parents dictionary is replaced with
            // ConditionalWeakTable, allowing tracked YAML nodes to be freed properly.
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test4.yaml");

            var childrenAdded = 0;

            var tracker = new YamlNodeTracker();

            tracker.TrackerEvent += (sender, args) =>
            {
                if (args.EventType == TrackerEventType.MappingPairAdded ||
                    args.EventType == TrackerEventType.SequenceElementAdded ||
                    args.EventType == TrackerEventType.StreamDocumentAdded)
                {
                    childrenAdded++;
                }
            };

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream, tracker);

            var weakRef = new System.WeakReference(stream);

            stream = null;

            System.GC.Collect();
            System.GC.WaitForFullGCComplete();
            System.GC.WaitForPendingFinalizers();

            Assert.AreEqual(null, stream);
            Assert.IsFalse(weakRef.IsAlive);
        }
        public void DeserializeTest()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test11.yaml");

            var childrenAdded = 0;

            var tracker = new YamlNodeTracker();

            tracker.TrackerEvent += (sender, args) =>
            {
                if (args.EventType == TrackerEventType.MappingPairAdded ||
                    args.EventType == TrackerEventType.SequenceElementAdded ||
                    args.EventType == TrackerEventType.StreamDocumentAdded)
                {
                    childrenAdded++;
                }
            };

            var fileStream = new StreamReader(file);

            YamlStream.Load(fileStream, tracker);

            Assert.AreEqual(9, childrenAdded);
        }
Example #3
0
        public void ReadYamlReference()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.YamlReferenceCard.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            var collectionIndicators = ((YamlMapping)((YamlMapping)stream[0].Contents)["Collection indicators"]);

            var firstCollectionIndicator = collectionIndicators.Keys.First();

            Assert.AreEqual("? ", firstCollectionIndicator.ToObject <string>());

            var firstCollectionIndicatorValue = collectionIndicators[firstCollectionIndicator];

            collectionIndicators[0] = new KeyValuePair <YamlElement, YamlElement>(
                new YamlValue(":-)"),
                firstCollectionIndicatorValue
                );

            var serialized = new StringBuilder();

            stream.WriteTo(new StringWriter(serialized));

            stream = YamlStream.Load(new StringReader(serialized.ToString()));

            collectionIndicators = ((YamlMapping)((YamlMapping)stream[0].Contents)["Collection indicators"]);

            firstCollectionIndicator = collectionIndicators.Keys.First();

            Assert.AreEqual(":-)", firstCollectionIndicator.ToObject <string>());
        }
Example #4
0
        public void DeepClone()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test11.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            var serialized = new StringBuilder();

            stream.WriteTo(new StringWriter(serialized), true);

            var clone = (YamlStream)stream.DeepClone();

            ((YamlMapping)((YamlMapping)clone[0].Contents)[2].Value)[new YamlValue("key 2")] = new YamlValue("value 3");

            var serialized2 = new StringBuilder();

            stream.WriteTo(new StringWriter(serialized2), true);

            Assert.AreEqual(serialized.ToString(), serialized2.ToString());

            var serialized3 = new StringBuilder();

            clone.WriteTo(new StringWriter(serialized3), true);

            Assert.AreNotEqual(serialized.ToString(), serialized3.ToString());
        }
Example #5
0
        public void AllowMissingKeyLookup()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test11.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            Assert.IsNull(((YamlMapping)stream[0].Contents)["Bla"]);
        }
Example #6
0
        public void ToStringTest()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test8.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            Assert.AreEqual("[item 1, item 2, item 3]", stream.ToString());
            Assert.AreEqual("[item 1, item 2, item 3]", stream[0].Contents.ToString());
            Assert.AreEqual("item 1", ((YamlSequence)stream[0].Contents)[0].ToString());
        }
Example #7
0
        public void TrackerAssignmentTest()
        {
            var tracker = new YamlNodeTracker();
            var stream  = new YamlStream(tracker);

            var document = new YamlDocument();
            var sequence = new YamlSequence();

            document.Contents = sequence;

            var mapping = new YamlMapping();

            sequence.Add(mapping);

            var key   = new YamlValue("key");
            var value = new YamlValue("value");

            var eventList = new List <TrackerEventArgs>();

            tracker.TrackerEvent += (sender, args) => eventList.Add(args);

            mapping[key] = value;

            Assert.IsNull(document.Tracker);
            Assert.IsNull(sequence.Tracker);
            Assert.IsNull(mapping.Tracker);
            Assert.IsNull(key.Tracker);
            Assert.IsNull(value.Tracker);

            stream.Add(document);

            Assert.AreEqual(tracker, document.Tracker);
            Assert.AreEqual(tracker, sequence.Tracker);
            Assert.AreEqual(tracker, mapping.Tracker);
            Assert.AreEqual(tracker, key.Tracker);
            Assert.AreEqual(tracker, value.Tracker);

            Assert.AreEqual(4, eventList.Count);
            Assert.IsTrue(eventList[0] is MappingPairAdded);
            Assert.IsTrue(eventList[1] is SequenceElementAdded);
            Assert.IsTrue(eventList[2] is DocumentContentsChanged);
            Assert.IsTrue(eventList[3] is StreamDocumentAdded);

            eventList.Clear();

            var key2   = new YamlValue("key2");
            var value2 = new YamlValue("value2");

            mapping[key2] = value2;

            Assert.AreEqual(1, eventList.Count);
            Assert.IsTrue(eventList[0] is MappingPairAdded);
        }
Example #8
0
        public void MappingStringKey()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test11.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            Assert.AreEqual("value 2", ((YamlMapping)((YamlMapping)stream[0].Contents)[2].Value)["key 2"].ToObject <string>());

            ((YamlMapping)((YamlMapping)stream[0].Contents)[2].Value)["key 3"] = new YamlValue("value 3");

            Assert.AreEqual("key 3", ((YamlMapping)((YamlMapping)stream[0].Contents)[2].Value)[2].Key.ToObject <string>());
            Assert.AreEqual("value 3", ((YamlMapping)((YamlMapping)stream[0].Contents)[2].Value)[2].Value.ToObject <string>());
        }
Example #9
0
        public void ScalarStyleTest()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test6.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            var value = ((YamlValue)stream[0].Contents);

            Assert.AreEqual(ScalarStyle.DoubleQuoted, value.Style);

            value.Style = ScalarStyle.Plain;

            var serialized = new StringBuilder();

            stream.WriteTo(new StringWriter(serialized), true);
            Assert.IsTrue(serialized.ToString().StartsWith("!!float 3.14"));
        }
Example #10
0
        public void TagTest()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.dictionaryExplicit.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            var dict = stream[0].Contents.ToObject <object>();

            Assert.AreEqual(typeof(Dictionary <string, int>), dict.GetType());
            Assert.AreEqual("!System.Collections.Generic.Dictionary`2[System.String,System.Int32],mscorlib", stream[0].Contents.Tag);

            stream[0].Contents.Tag = "!System.Collections.Generic.Dictionary`2[System.String,System.Double],mscorlib";

            var dict2 = stream[0].Contents.ToObject <object>();

            Assert.AreEqual(typeof(Dictionary <string, double>), dict2.GetType());
        }
Example #11
0
        public void IsCanonicalTest()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("SharpYaml.Tests.files.test6.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            var value = ((YamlValue)stream[0].Contents);

            Assert.IsTrue(value.IsCanonical);

            value.IsPlainImplicit = true;
            value.Style           = ScalarStyle.Plain;

            var serialized = new StringBuilder();

            stream.WriteTo(new StringWriter(serialized), true);
            Assert.IsTrue(serialized.ToString().StartsWith("3.14"));
        }
        public void ValueSetTest()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test4.yaml");

            var childrenAdded = 0;

            var tracker = new YamlNodeTracker();

            tracker.TrackerEvent += (sender, args) =>
            {
                if (args.EventType == TrackerEventType.MappingPairAdded ||
                    args.EventType == TrackerEventType.SequenceElementAdded ||
                    args.EventType == TrackerEventType.StreamDocumentAdded)
                {
                    childrenAdded++;
                }
            };

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream, tracker);

            Assert.AreEqual(3, childrenAdded);

            ScalarValueChanged valueChanged = null;

            tracker.TrackerEvent += (sender, args) =>
            {
                if (args is ScalarValueChanged)
                {
                    valueChanged = (ScalarValueChanged)args;
                }
            };
            ((YamlValue)stream[0].Contents).Value = "a silly scalar";

            Assert.AreEqual("a scalar", valueChanged.OldValue);
            Assert.AreEqual("a silly scalar", valueChanged.NewValue);
            Assert.AreEqual(stream[0].Contents, valueChanged.Node);
            Assert.AreEqual(1, valueChanged.ParentPaths.Count);
            Assert.AreEqual(new SharpYaml.Model.Path(stream, new[] { new ChildIndex(0, false), new ChildIndex(-1, false) }), valueChanged.ParentPaths[0]);
        }
Example #13
0
        public void FromObject()
        {
            var stream   = new YamlStream();
            var document = new SharpYaml.Model.YamlDocument();

            stream.Add(document);

            var sequence = (YamlSequence)SharpYaml.Model.YamlNode.FromObject(new[] { "item 4", "item 5", "item 6" }, new SerializerSettings {
                EmitAlias = false
            }, typeof(string[]));

            sequence.SequenceStart = new SequenceStart(sequence.SequenceStart.Anchor, sequence.SequenceStart.Tag, true, YamlStyle.Flow);

            stream[0].Contents = sequence;

            var serialized = new StringBuilder();

            stream.WriteTo(new StringWriter(serialized), true);

            Assert.AreEqual("[item 4, item 5, item 6]", serialized.ToString().Trim());
        }
Example #14
0
        public void SubscriberTest()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("SharpYaml.Tests.files.test12.yaml");

            var childrenAdded = 0;

            var tracker = new YamlNodeTracker();

            var fileStream = new StreamReader(file);
            var yaml       = YamlStream.Load(fileStream, tracker);

            var mapping1 = (YamlMapping)((YamlSequence)yaml[0].Contents)[1];
            var mapping2 = (YamlMapping)((YamlSequence)yaml[0].Contents)[2];

            var handler = new SubscriberHandler();

            tracker.Subscribe(handler, null, "A");
            tracker.Subscribe(handler, tracker.GetPaths(yaml[0].Contents)[0], "B");
            tracker.Subscribe(handler, tracker.GetPaths(mapping1)[0], "C");

            mapping1["key 1"] = new YamlValue("Bla");

            Assert.AreEqual(1, handler.ACalls);
            Assert.AreEqual(1, handler.BCalls);
            Assert.AreEqual(1, handler.CCalls);

            mapping2[0] = new KeyValuePair <YamlElement, YamlElement>(new YamlValue("K"), new YamlValue("V"));

            Assert.AreEqual(2, handler.ACalls);
            Assert.AreEqual(2, handler.BCalls);
            Assert.AreEqual(1, handler.CCalls);

            ((YamlSequence)yaml[0].Contents).Add(new YamlValue("5"));

            Assert.AreEqual(3, handler.ACalls);
            Assert.AreEqual(3, handler.BCalls);
            Assert.AreEqual(1, handler.CCalls);
        }
Example #15
0
        public void AddPairTest()
        {
            var tracker = new YamlNodeTracker();
            var stream  = new YamlStream(tracker);

            stream.Add(new YamlDocument());
            stream[0].Contents = new YamlMapping();

            TrackerEventArgs receivedArgs = null;

            tracker.TrackerEvent += (sender, args) => {
                receivedArgs = args;
            };

            ((YamlMapping)stream[0].Contents)["A"] = new YamlValue(5);

            Assert.IsTrue(receivedArgs is MappingPairAdded);
            Assert.AreEqual(TrackerEventType.MappingPairAdded, ((MappingPairAdded)receivedArgs).EventType);
            Assert.AreEqual(0, ((MappingPairAdded)receivedArgs).Index);
            Assert.AreEqual(new Model.Path(stream, new [] { new ChildIndex(0, false), new ChildIndex(-1, false) }), ((MappingPairAdded)receivedArgs).ParentPaths[0]);
            Assert.AreEqual("A", ((MappingPairAdded)receivedArgs).Child.Key.ToString());
            Assert.AreEqual("5", ((MappingPairAdded)receivedArgs).Child.Value.ToString());
        }
Example #16
0
        public void StyleTest()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test10.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            var seq = (YamlSequence)(YamlContainer)stream[0].Contents;

            Assert.AreEqual(YamlStyle.Block, seq.Style);
            Assert.AreEqual(YamlStyle.Block, ((YamlContainer)seq[2]).Style);
            Assert.AreEqual(YamlStyle.Block, ((YamlContainer)seq[3]).Style);

            seq.Style = YamlStyle.Flow;
            ((YamlContainer)seq[2]).Style = YamlStyle.Flow;
            ((YamlContainer)seq[3]).Style = YamlStyle.Flow;

            var serialized = new StringBuilder();

            stream.WriteTo(new StringWriter(serialized), true);
            Assert.AreEqual(1, serialized.ToString().Split(new[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries).Length);
        }
Example #17
0
        public void YamlValue()
        {
            var file = System.Reflection.Assembly.GetExecutingAssembly()
                       .GetManifestResourceStream("Evergine.Yaml.Tests.files.test6.yaml");

            var fileStream = new StreamReader(file);
            var stream     = YamlStream.Load(fileStream);

            var value = ((YamlValue)stream[0].Contents);

            Assert.AreEqual(3.14f, value.ToObject <float>());

            stream[0].Contents = new YamlValue(double.PositiveInfinity);

            var serialized = new StringBuilder();

            stream.WriteTo(new StringWriter(serialized));

            stream = YamlStream.Load(new StringReader(serialized.ToString()));

            value = ((YamlValue)stream[0].Contents);

            Assert.AreEqual(float.PositiveInfinity, value.ToObject <float>());
        }