예제 #1
0
        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, "&lt;testValue");

            instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, "testValue>"));
            CheckAttributes(instance, 1, firstAlowedAttributeName, "testValue&gt;");

            instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, @"""testValue"));
            CheckAttributes(instance, 1, firstAlowedAttributeName, "&quot;testValue");

            instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, "testValue'tv"));
            CheckAttributes(instance, 1, firstAlowedAttributeName, "testValue&apos;tv");

            instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, "testValue&tv"));
            CheckAttributes(instance, 1, firstAlowedAttributeName, "testValue&amp;tv");

            instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, @"<""testValue&tv'2"">"));
            CheckAttributes(instance, 1, firstAlowedAttributeName, "&lt;&quot;testValue&amp;tv&apos;2&quot;&gt;");

            instance.AddAttribute(new Fb2Attribute(firstAlowedAttributeName, " test value with whitespace "));
            CheckAttributes(instance, 1, firstAlowedAttributeName, " test value with whitespace ");
        }
예제 #2
0
        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();
        }
예제 #3
0
        public void EmptyNodes_EqualityNullTest(Fb2Node instance)
        {
            instance.Should().NotBe(null);

            var instanceTwo = Fb2NodeFactory.GetNodeByName(instance.Name);

            instanceTwo.Should().NotBe(null);

            instance.Should().Be(instanceTwo);
        }
예제 #4
0
        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>();
        }
예제 #5
0
 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);
 }
예제 #6
0
        /// <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);
        }
예제 #7
0
        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.");
        }
예제 #8
0
        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>();
        }
예제 #9
0
        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);
        }
예제 #10
0
        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();
        }
예제 #11
0
        // 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);
        }
예제 #12
0
        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);
        }
예제 #13
0
        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();
        }
예제 #14
0
        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");
        }
예제 #15
0
        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>();
        }
예제 #16
0
        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>();
        }
예제 #17
0
 public InvalidNodeException(Fb2Node node) : this(node.Name)
 {
 }
예제 #18
0
 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);
예제 #21
0
        public void CloneNode_EqualityTest(Fb2Node instance)
        {
            var instanceTwo = Fb2NodeFactory.GetNodeByName(instance.Name);

            instance.Should().Be(instanceTwo);
        }