public void AddAttribute_AllowedAttribute_EscapesAttributeValue_Works(Fb2Node instance) { instance.Should().NotBe(null); if (!instance.AllowedAttributes.Any()) { return; } var firstAlowedAttributeName = instance.AllowedAttributes.First(); instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, "<testValue")); CheckAttributes(instance, 1, firstAlowedAttributeName, "<testValue"); instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, "testValue>")); CheckAttributes(instance, 1, firstAlowedAttributeName, "testValue>"); instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, @"""testValue")); CheckAttributes(instance, 1, firstAlowedAttributeName, ""testValue"); instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, "testValue'tv")); CheckAttributes(instance, 1, firstAlowedAttributeName, "testValue'tv"); instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, "testValue&tv")); CheckAttributes(instance, 1, firstAlowedAttributeName, "testValue&tv"); instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, @"<""testValue&tv'2"">")); CheckAttributes(instance, 1, firstAlowedAttributeName, "<"testValue&tv'2">"); instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, " test value with whitespace ")); CheckAttributes(instance, 1, firstAlowedAttributeName, " test value with whitespace "); }
public void EmptyNode_TryGetAttribute_ReturnsFalse(Fb2Node instance) { if (instance.AllowedAttributes.Count == 0) { return; } instance.Attributes.Should().BeEmpty(); instance.TryGetAttribute(instance.AllowedAttributes.First(), out var result) .Should() .BeFalse(); //result.Key.Should().BeNullOrEmpty(); //result.Value.Should().BeNullOrEmpty(); result.Should().BeNull(); //result.Value.Should().BeNullOrEmpty(); instance.TryGetAttribute(instance.AllowedAttributes.First(), true, out var resultIgnoreCase) .Should() .BeFalse(); resultIgnoreCase.Should().BeNull(); //resultIgnoreCase.Key.Should().BeNullOrEmpty(); //resultIgnoreCase.Value.Should().BeNullOrEmpty(); }
public void EmptyNodes_EqualityNullTest(Fb2Node instance) { instance.Should().NotBe(null); var instanceTwo = Fb2NodeFactory.GetNodeByName(instance.Name); instanceTwo.Should().NotBe(null); instance.Should().Be(instanceTwo); }
public void AddAttribute_InvalidAttribute_Throws(Fb2Node instance) { instance.Should().NotBe(null); if (!instance.AllowedAttributes.Any()) { return; } //instance // .Invoking(i => i.AddAttribute(() => new Fb2Attribute("testK", ""))) // .Should() // .ThrowExactly<InvalidAttributeException>(); instance .Invoking(i => i.AddAttribute(() => new Fb2Attribute("", "testV"))) .Should() .ThrowExactly <InvalidAttributeException>(); //instance // .Invoking(i => i.AddAttribute(new Fb2Attribute("testK", ""))) // .Should() // .ThrowExactly<InvalidAttributeException>(); instance .Invoking(i => i.AddAttribute(new Fb2Attribute("", "testV"))) .Should() .ThrowExactly <InvalidAttributeException>(); //instance // .Invoking(i => i.AddAttribute(new Fb2Attribute("testK", ""))) // .Should() // .ThrowExactly<InvalidAttributeException>(); instance .Invoking(i => i.AddAttribute(new Fb2Attribute("", "testV"))) .Should() .ThrowExactly <InvalidAttributeException>(); instance .Invoking(i => i.AddAttributes(new Fb2Attribute("", ""), new Fb2Attribute("", ""))) .Should() .ThrowExactly <InvalidAttributeException>(); instance .Invoking(i => i.AddAttributes( new List <Fb2Attribute> { new Fb2Attribute("testKey", ""), new Fb2Attribute("", "testValue") } )) .Should() .ThrowExactly <InvalidAttributeException>(); }
private static void CheckAttributes( Fb2Node instance, int expectedCount, string expectedName, string expectedValue) { instance.Should().NotBeNull(); instance.Attributes.Should().HaveCount(expectedCount); instance.Attributes.Should().Contain((attr) => attr.Key == expectedName); //instance.Attributes[expectedName].Should().Be(expectedValue); instance.GetAttribute(expectedName).Value.Should().Be(expectedValue); }
/// <summary> /// Determines whether given <paramref name="node"/> is valid. /// </summary> /// <param name="node">Node to perform check on.</param> /// <returns><see langword="true"/> if node is valid; otherwise, <see langword="false"/>.</returns> /// <exception cref="ArgumentNullException"></exception> public static bool IsKnownNode([In] Fb2Node node) { if (node == null) { throw new ArgumentNullException(nameof(node)); } var hasKnownName = IsKnownNodeName(node.Name); var hasKnownType = KnownNodes.ContainsValue(node.GetType()); return(hasKnownName && hasKnownType); }
public void AddAttribute_NoAttributesAllowed_Throws(Fb2Node instance) { instance.Should().NotBe(null); if (instance.AllowedAttributes.Any()) { return; } instance .Invoking(i => i.AddAttribute(new Fb2Attribute("testKey", "testValue"))) .Should() .ThrowExactly <NoAttributesAllowedException>() .WithMessage($"Node '{instance.Name}' has no allowed attributes."); instance .Invoking(i => i.AddAttribute(() => new Fb2Attribute("testKey", "testValue"))) .Should() .ThrowExactly <NoAttributesAllowedException>() .WithMessage($"Node '{instance.Name}' has no allowed attributes."); instance .Invoking(i => i.AddAttribute(new Fb2Attribute("testKey", "testValue"))) .Should() .ThrowExactly <NoAttributesAllowedException>() .WithMessage($"Node '{instance.Name}' has no allowed attributes."); instance .Invoking(i => i.AddAttributes( new Fb2Attribute("testKey", "testValue"), new Fb2Attribute("testKey2", "testValue2"))) .Should() .ThrowExactly <NoAttributesAllowedException>() .WithMessage($"Node '{instance.Name}' has no allowed attributes."); instance .Invoking(i => i.AddAttributes( new List <Fb2Attribute> { new Fb2Attribute("testKey", "testValue"), new Fb2Attribute("testKey2", "testValue2") })) .Should() .ThrowExactly <NoAttributesAllowedException>() .WithMessage($"Node '{instance.Name}' has no allowed attributes."); instance .Invoking(async i => await i.AddAttributeAsync(() => Task.FromResult(new Fb2Attribute("testKey", "testValue")))) .Should() .ThrowExactlyAsync <NoAttributesAllowedException>() .WithMessage($"Node '{instance.Name}' has no allowed attributes."); }
public void AddAttribute_NotAllowedAttribute_Throws(Fb2Node instance) { instance.Should().NotBe(null); if (!instance.AllowedAttributes.Any()) { return; } instance .Invoking(i => i.AddAttribute(new Fb2Attribute("NotExistingKey", "NotExistingValue"))) .Should() .ThrowExactly <UnexpectedAtrributeException>(); }
private static string GetCellAlignment(Fb2Node cell, string alignAttributeName) { if (cell == null || string.IsNullOrWhiteSpace(alignAttributeName)) { throw new ArgumentNullException(); } if (cell.TryGetAttribute(alignAttributeName, true, out var align)) { return(align !.Value); } return(string.Empty); }
public void EmptyNode_GetAttribute_ReturnsDefaultKeyValuePair(Fb2Node instance) { if (instance.AllowedAttributes.Count == 0) { return; } instance.Attributes.Should().BeEmpty(); //instance.GetAttribute(instance.AllowedAttributes.First()).Should().Be(default(KeyValuePair<string, string>)); //instance.GetAttribute(instance.AllowedAttributes.First(), true).Should().Be(default(KeyValuePair<string, string>)); instance.GetAttribute(instance.AllowedAttributes.First()).Should().BeNull(); instance.GetAttribute(instance.AllowedAttributes.First(), true).Should().BeNull(); }
// if no span found return 1 as minimal cell unit width/height private static int GetCellSpan(Fb2Node cell, string spanAttrName) { if (cell == null || string.IsNullOrWhiteSpace(spanAttrName)) { throw new ArgumentNullException(); } if (cell.TryGetAttribute(spanAttrName, true, out var span) && int.TryParse(span !.Value, out int spanNumber) && spanNumber > 1) { return(spanNumber); } return(1); }
public void Clone_AddAttribute_NotEquals(Fb2Node instance) { if (instance.AllowedAttributes == null || !instance.AllowedAttributes.Any()) { return; } var instanceTwo = instance.Clone() as Fb2Node; instanceTwo.AddAttribute(new Fb2Attribute(instance.AllowedAttributes.First(), "testValue")); instance.Should().NotBe(instanceTwo); instance.Attributes.Should().BeEmpty(); instanceTwo.Attributes.Should().NotBeEmpty().And.HaveCount(1); }
public void EmptyNode_HasAttribute_ReturnsFalse(Fb2Node instance) { instance.IsEmpty.Should().BeTrue(); if (instance.AllowedAttributes.Count == 0) { return; } instance.Attributes.Should().BeEmpty(); instance.HasAttribute(instance.AllowedAttributes.First()).Should().BeFalse(); instance.HasAttribute(new Fb2Attribute(instance.AllowedAttributes.First(), "blah")).Should().BeFalse(); instance.HasAttribute(instance.AllowedAttributes.First(), true).Should().BeFalse(); instance.HasAttribute(new Fb2Attribute(instance.AllowedAttributes.First(), "blah", "http://www.w3.org//xlink")).Should().BeFalse(); }
public void AddAttribute_AllowedAttribute_Works(Fb2Node instance) { instance.Should().NotBe(null); if (!instance.AllowedAttributes.Any()) { return; } var firstAlowedAttributeName = instance.AllowedAttributes.First(); instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, "testValue")); var attributes = instance.Attributes; //attributes.Should().HaveCount(1).And.ContainKey(firstAlowedAttributeName); //attributes[firstAlowedAttributeName].Should().Be("testValue"); attributes.Should().HaveCount(1).And.Contain((attr) => attr.Key == firstAlowedAttributeName); attributes[0].Value.Should().Be("testValue"); }
public void AddAttribute_Whitespaces_Throws(Fb2Node instance) { instance.Should().NotBe(null); if (!instance.AllowedAttributes.Any()) { return; } instance // whitespace .Invoking(i => i.AddAttribute(new Fb2Attribute(" NotExistingKey", "NotExistingValue"))) .Should() .ThrowExactly <UnexpectedAtrributeException>(); instance // whitespace .Invoking(i => i.AddAttribute(new Fb2Attribute(" NotExistingKey", "NotExistingValue"))) .Should() .ThrowExactly <UnexpectedAtrributeException>(); instance .Invoking(i => i.AddAttribute(new Fb2Attribute('\t' + "NotExistingKey", "NotExistingValue"))) .Should() .ThrowExactly <UnexpectedAtrributeException>(); instance .Invoking(i => i.AddAttribute(new Fb2Attribute(Environment.NewLine + "NotExistingKey", "NotExistingValue"))) .Should() .ThrowExactly <UnexpectedAtrributeException>(); instance .Invoking(i => i.AddAttribute(new Fb2Attribute('\n' + "NotExistingKey", "NotExistingValue"))) .Should() .ThrowExactly <UnexpectedAtrributeException>(); instance .Invoking(i => i.AddAttribute(new Fb2Attribute('\r' + "NotExistingKey", "NotExistingValue"))) .Should() .ThrowExactly <UnexpectedAtrributeException>(); }
public void AddAttribute_EmptyOrNull_Throws(Fb2Node instance) { instance.Should().NotBe(null); if (!instance.AllowedAttributes.Any()) { return; } instance .Invoking(i => i.AddAttribute((Fb2Attribute)null)) .Should() .ThrowExactly <ArgumentNullException>(); instance .Invoking(i => i.AddAttributeAsync(null)) .Should() .ThrowExactlyAsync <ArgumentNullException>(); instance .Invoking(i => i.AddAttributes()) .Should() .ThrowExactly <ArgumentNullException>(); instance .Invoking(i => i.AddAttributes(Array.Empty <Fb2Attribute>())) .Should() .ThrowExactly <ArgumentNullException>(); instance .Invoking(i => i.AddAttributes((List <Fb2Attribute>)null)) .Should() .ThrowExactly <ArgumentNullException>(); //instance // .Invoking(i => i.AddAttributes(new Dictionary<string, string>())) // .Should() // .ThrowExactly<ArgumentNullException>(); }
public InvalidNodeException(Fb2Node node) : this(node.Name) { }
public void IsKnownNode_ValidNode_ReturnsTrue(Fb2Node node) { Fb2NodeFactory.IsKnownNode(node).Should().BeTrue(); }
/// <summary> /// "Type-accurate" wrapper for <see cref="Fb2Container.AddContent(Fb2Node)"/> method. /// <para> Adds <paramref name="node"/> to given <paramref name="fb2Container"/>.</para> /// </summary> /// <typeparam name="T">Type of node, inferred from usage implicitly.</typeparam> /// <param name="fb2Container">Fb2Container node to use extension on.</param> /// <param name="node">Node to add to given <paramref name="fb2Container"/>.</param> /// <returns><paramref name="fb2Container"/> with it's original type.</returns> public static T AppendContent <T>(this T fb2Container, Fb2Node node) where T : Fb2Container => (T)fb2Container.AddContent(node);
/// <summary> /// "Type-accurate" wrapper for <see cref="Fb2Container.RemoveContent(Fb2Node)"/> method. /// <para> Removes particular <paramref name="node"/> from given <paramref name="fb2Container"/>.</para> /// </summary> /// <typeparam name="T">Type of node, inferred from usage implicitly.</typeparam> /// <param name="fb2Container">Fb2Container node to use extension on.</param> /// <param name="node"></param> /// <returns><paramref name="fb2Container"/> with it's original type.</returns> public static T DeleteContent <T>(this T fb2Container, Fb2Node node) where T : Fb2Container => (T)fb2Container.RemoveContent(node);
public void CloneNode_EqualityTest(Fb2Node instance) { var instanceTwo = Fb2NodeFactory.GetNodeByName(instance.Name); instance.Should().Be(instanceTwo); }