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); }
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); }
/// <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()); } }
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"); }
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"); }
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 }
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(); }
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"); }