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);
        }
Пример #3
0
 public YamlStream(YamlNodeTracker tracker = null)
 {
     _streamStart = new StreamStart();
     _streamEnd   = new StreamEnd();
     _documents   = new List <YamlDocument>();
     Tracker      = tracker;
 }
Пример #4
0
        YamlSequence(SequenceStart sequenceStart, SequenceEnd sequenceEnd, List <YamlElement> contents, YamlNodeTracker tracker)
        {
            Tracker = tracker;

            this._sequenceStart = sequenceStart;

            if (Tracker != null)
            {
                Tracker.OnSequenceStartChanged(this, null, sequenceStart);
            }

            this._sequenceEnd = sequenceEnd;

            if (Tracker == null)
            {
                _contents = contents;
            }
            else
            {
                _contents = new List <YamlElement>();
                foreach (var item in contents)
                {
                    Add(item);
                }
            }
        }
Пример #5
0
        YamlDocument(DocumentStart documentStart, DocumentEnd documentEnd, YamlElement contents, YamlNodeTracker tracker)
        {
            Tracker = tracker;

            DocumentStart = documentStart;
            DocumentEnd   = documentEnd;
            Contents      = contents;
        }
Пример #6
0
        public static YamlDocument Load(EventReader eventReader, YamlNodeTracker tracker = null)
        {
            var documentStart = eventReader.Allow <DocumentStart>();

            var contents = ReadElement(eventReader, tracker);

            var documentEnd = eventReader.Allow <DocumentEnd>();

            return(new YamlDocument(documentStart, documentEnd, contents, tracker));
        }
Пример #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);
        }
Пример #8
0
        public static YamlStream Load(EventReader eventReader, YamlNodeTracker tracker = null)
        {
            var streamStart = eventReader.Allow <StreamStart>();

            var documents = new List <YamlDocument>();

            while (!eventReader.Accept <StreamEnd>())
            {
                documents.Add(YamlDocument.Load(eventReader, tracker));
            }

            var streamEnd = eventReader.Allow <StreamEnd>();

            return(new YamlStream(streamStart, streamEnd, documents, tracker));
        }
Пример #9
0
        protected static YamlElement ReadElement(EventReader eventReader, YamlNodeTracker tracker = null)
        {
            if (eventReader.Accept <MappingStart>())
            {
                return(YamlMapping.Load(eventReader, tracker));
            }

            if (eventReader.Accept <SequenceStart>())
            {
                return(YamlSequence.Load(eventReader, tracker));
            }

            if (eventReader.Accept <Scalar>())
            {
                return(YamlValue.Load(eventReader, tracker));
            }

            return(null);
        }
Пример #10
0
        YamlStream(StreamStart streamStart, StreamEnd streamEnd, List <YamlDocument> documents, YamlNodeTracker tracker = null)
        {
            Tracker = tracker;

            this._streamStart = streamStart;
            this._streamEnd   = streamEnd;

            if (Tracker == null)
            {
                _documents = documents;
            }
            else
            {
                _documents = new List <YamlDocument>();
                foreach (var document in documents)
                {
                    Add(document);
                }
            }
        }
        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]);
        }
Пример #12
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);
        }
Пример #13
0
        YamlSequence(SequenceStart sequenceStart, SequenceEnd sequenceEnd, List <YamlElement> contents, YamlNodeTracker tracker)
        {
            if (tracker == null)
            {
                _contents = contents;
            }
            else
            {
                _contents = new List <YamlElement>();

                Tracker = tracker;

                foreach (var item in contents)
                {
                    Add(item);
                }
            }

            SequenceStart = sequenceStart;

            this._sequenceEnd = sequenceEnd;
        }
Пример #14
0
        YamlMapping(MappingStart mappingStart, MappingEnd mappingEnd, List <YamlElement> keys, Dictionary <YamlElement, YamlElement> contents, YamlNodeTracker tracker)
        {
            Tracker = tracker;

            MappingStart     = mappingStart;
            this._mappingEnd = mappingEnd;

            if (Tracker == null)
            {
                _keys     = keys;
                _contents = contents;
            }
            else
            {
                _keys     = new List <YamlElement>();
                _contents = new Dictionary <YamlElement, YamlElement>();
                foreach (var key in keys)
                {
                    Add(key, contents[key]);
                }
            }
        }
Пример #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());
        }
Пример #16
0
 public static YamlStream Load(TextReader stream, YamlNodeTracker tracker = null)
 {
     return(Load(new EventReader(new Parser(stream)), tracker));
 }
Пример #17
0
        public static YamlValue Load(EventReader eventReader, YamlNodeTracker tracker = null)
        {
            var scalar = eventReader.Allow <Scalar>();

            return(new YamlValue(scalar, tracker));
        }
Пример #18
0
        YamlValue(Scalar scalar, YamlNodeTracker tracker)
        {
            Tracker = tracker;

            Scalar = scalar ?? throw new ArgumentNullException(nameof(scalar));
        }