/// <summary>
 /// Write a single starting node along with its attributes
 /// </summary>
 /// <param name="nodeToWrite">Definition of the node to write</param>
 private void WriteStartNode(XmlNodeDef nodeToWrite)
 {
     _xmlWriter.WriteStartElement(nodeToWrite.Name);
     foreach (var attr in nodeToWrite.GetAttributes())
     {
         _xmlWriter.WriteAttributeString(attr.Item1, attr.Item2?.ToString());
     }
 }
        /// <summary>
        /// Asynchronously write a single starting node along with its attributes
        /// </summary>
        /// <param name="nodeToWrite">Definition of the node to write</param>
        /// <returns></returns>
        private async Task WriteStartNodeAsync(XmlNodeDef nodeToWrite)
        {
            await _xmlWriter.WriteStartElementAsync(null, nodeToWrite.Name, null);

            foreach (var attr in nodeToWrite.GetAttributes())
            {
                await _xmlWriter.WriteAttributeStringAsync(null, attr.Item1, null, attr.Item2?.ToString());
            }
        }
Exemplo n.º 3
0
        public void ctor_emptySpecs_undetermined()
        {
            //arrange
            var actual = new XmlNodeDef(string.Empty);

            //act

            //assert
            actual.Name.Should().Be("__undetermined__");
            actual.GetAttributes().Should().BeOfType <List <Tuple <string, object> > >();
            actual.GetAttributes().Count.Should().Be(0);
        }
Exemplo n.º 4
0
        public void ctor_OnlyNameInSpecs_CorrectData()
        {
            //arrange
            var name = "MySpecialName";

            //act
            var actual = new XmlNodeDef(name);

            //assert
            actual.Name.Should().Be(name);
            actual.GetAttributes().Should().BeOfType <List <Tuple <string, object> > >();
            actual.GetAttributes().Count.Should().Be(0);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Determine if current reader position matches given node pattern
        /// </summary>
        /// <param name="pattern">Definition of a single node</param>
        /// <returns>True if current node matches pattern; false if not.</returns>
        private bool MatchFound(XmlNodeDef pattern)
        {
            if (_xmlReader.NodeType != XmlNodeType.Element)
            {
                return(false);
            }

            if (_xmlReader.Name != pattern.Name)
            {
                return(false);
            }

            //Name matches, let's look for attributes:
            var attribsToMatch = pattern.GetAttributeDict();

            if (!attribsToMatch.Any())
            {
                return(true);
            }

            var retVal = false;

            if (_xmlReader.HasAttributes)
            { //locate attributes matching name and value
                while (_xmlReader.MoveToNextAttribute())
                {
                    if (attribsToMatch.ContainsKey(_xmlReader.Name))
                    { //attribute name match, remove from the list to match, but only if value matches (or no value in pattern)
                        var valueToMatch = attribsToMatch[_xmlReader.Name];
                        if (valueToMatch == null || valueToMatch.ToString() == _xmlReader.Value)
                        {
                            attribsToMatch.Remove(_xmlReader.Name);
                            if (!attribsToMatch.Any())
                            { //success! we matched all attributes
                                retVal = true;
                                break;
                            }
                        }
                        else
                        {
                            break; //match failed; no chance to run into the same attribute again
                        }
                    }
                }
            }
            return(retVal);
        }
Exemplo n.º 6
0
        public void GetAttributeDict_ComplexSpecs_CorrectData()
        {
            //arrange
            var specs = "MyName[@id][@key1=x][@key2=\"\"]";

            //act
            var actual     = new XmlNodeDef(specs);
            var attribDict = actual.GetAttributeDict();

            //assert
            actual.Name.Should().Be("MyName");
            attribDict.Should().BeOfType <Dictionary <string, object> >();
            attribDict.Count.Should().Be(3);
            attribDict["id"].Should().BeNull();
            attribDict["key1"].Should().Be("x");
            attribDict["key2"].Should().Be(string.Empty);
        }
Exemplo n.º 7
0
        public void ctor_OnlyAttribInSpecs_CorrectData()
        {
            //arrange
            var specs = "[@id][@key=\"x\"]";

            //act
            var actual  = new XmlNodeDef(specs);
            var attribs = actual.GetAttributes();

            //assert
            actual.Name.Should().Be("__undetermined__");
            attribs.Should().BeOfType <List <Tuple <string, object> > >();
            attribs.Count.Should().Be(2);
            attribs[0].Item1.Should().Be("id");
            attribs[0].Item2.Should().BeNull();
            attribs[1].Item1.Should().Be("key");
            attribs[1].Item2.Should().Be("x");
        }
Exemplo n.º 8
0
        public void AddAttribute_DupKey_AttributeIgnored()
        {
            //arrange
            var specs  = "[@id][@key=\"x\"]";
            var actual = new XmlNodeDef(specs);

            //act
            actual.AddAttribute("id", "dummy");
            var attribs = actual.GetAttributes();

            //assert
            actual.Name.Should().Be("__undetermined__");
            attribs.Should().BeOfType <List <Tuple <string, object> > >();
            attribs.Count.Should().Be(2);
            attribs[0].Item1.Should().Be("id");
            attribs[0].Item2.Should().BeNull();
            attribs[1].Item1.Should().Be("key");
            attribs[1].Item2.Should().Be("x");
        }
Exemplo n.º 9
0
        public void ctor_ComplexSpecs_CorrectData()
        {
            //arrange
            var specs = "MyName[@id][@key=\"x\"][@key2=x][@key=\"y\"]";

            //act
            var actual  = new XmlNodeDef(specs);
            var attribs = actual.GetAttributes();

            //assert
            actual.Name.Should().Be("MyName");
            attribs.Should().BeOfType <List <Tuple <string, object> > >();
            attribs.Count.Should().Be(3);
            attribs[0].Item1.Should().Be("id");
            attribs[0].Item2.Should().BeNull();
            attribs[1].Item1.Should().Be("key");
            attribs[1].Item2.Should().Be("x");
            attribs[2].Item1.Should().Be("key2");
            attribs[2].Item2.Should().Be("x"); //attrib value can be surrounded in quotes or not
            //note that the 2nd key attribute was a dup and as such was rejected
        }
Exemplo n.º 10
0
        public void GetAttributeValue_NoVsEmptyValue_CorrectDataReturned()
        {
            //arrange
            var specs  = "[@NoVal][@EmptyVal1=][@EmptyVal2=\"\"][@SomeVal=blah]";
            var actual = new XmlNodeDef(specs);

            //act
            var attribs = actual.GetAttributes();

            //assert
            actual.Name.Should().Be("__undetermined__");
            attribs.Should().BeOfType <List <Tuple <string, object> > >();
            attribs.Count.Should().Be(4);
            attribs[0].Item1.Should().Be("NoVal");
            attribs[0].Item2.Should().BeNull();
            attribs[1].Item1.Should().Be("EmptyVal1");
            attribs[1].Item2.Should().Be(string.Empty);
            attribs[2].Item1.Should().Be("EmptyVal2");
            attribs[2].Item2.Should().Be(string.Empty);
            attribs[3].Item1.Should().Be("SomeVal");
            attribs[3].Item2.Should().Be("blah");

            actual.GetAttributeValue("BadAttr").Should().BeNull(); //non-existing attribute
            actual.AttributeExists("BadAttr").Should().BeFalse();

            actual.GetAttributeValue("NoVal").Should().BeNull();             //attribute with no value
            actual.AttributeExists("NoVal").Should().BeTrue();               //GetAttributeValue does not distinguish between non=existing attribute and attribute with no value, but AttributeExists does

            actual.GetAttributeValue("EmptyVal1").Should().Be(string.Empty); //attribute with empty value
            actual.AttributeExists("EmptyVal1").Should().BeTrue();

            actual.GetAttributeValue("EmptyVal2").Should().Be(string.Empty); //attribute with empty value
            actual.AttributeExists("EmptyVal2").Should().BeTrue();

            actual.GetAttributeValue("SomeVal").Should().Be("blah"); //attribute with non-empty value
            actual.AttributeExists("SomeVal").Should().BeTrue();
        }
Exemplo n.º 11
0
        public void AddAttribute_UniqueKey_AttributeAdded()
        {
            //arrange
            var specs  = "[@id][@key=\"x\"]";
            var actual = new XmlNodeDef(specs);

            //act
            actual.AddAttribute("new1", null);
            actual.AddAttribute("new2", "val2");
            var attribs = actual.GetAttributes();

            //assert
            actual.Name.Should().Be("__undetermined__");
            attribs.Should().BeOfType <List <Tuple <string, object> > >();
            attribs.Count.Should().Be(4);
            attribs[0].Item1.Should().Be("id");
            attribs[0].Item2.Should().BeNull();
            attribs[1].Item1.Should().Be("key");
            attribs[1].Item2.Should().Be("x");
            attribs[2].Item1.Should().Be("new1");
            attribs[2].Item2.Should().BeNull();
            attribs[3].Item1.Should().Be("new2");
            attribs[3].Item2.Should().Be("val2");
        }