Example #1
0
        public void TryGetAnnotationAtIndex_WhenIndexValidAndAtLeastOneAnnotation_ReturnsAnnotationAtIndex()
        {
            var document = new SplashDocument();
            var key      = new Key <string>("href");

            // begin/end an empty block of annotations, these will get stripped out
            document.BeginAnnotation(key, "empty");
            document.BeginAnnotation(key, "empty2");
            document.EndAnnotation(key);
            document.EndAnnotation(key);

            // add some unannotated text
            document.AppendText("some ");

            // add some annotated text
            using (document.BeginAnnotation(key, "a"))
            {
                document.AppendText("text ");

                using (document.BeginAnnotation(key, "b"))
                    document.AppendText("more");
            }

            // add some unannotated text
            document.AppendText("...");

            // begin another empty annotation at the end, should have no effect on current text
            document.BeginAnnotation(key, "trailer");

            Assert.Multiple(() =>
            {
                string value;
                for (int i = 0; i < 5; i++)
                {
                    Assert.IsFalse(document.TryGetAnnotationAtIndex(key, i, out value));
                    Assert.IsNull(value);
                }
                for (int i = 5; i < 10; i++)
                {
                    Assert.IsTrue(document.TryGetAnnotationAtIndex(key, i, out value));
                    Assert.AreEqual("a", value);
                }
                for (int i = 10; i < 14; i++)
                {
                    Assert.IsTrue(document.TryGetAnnotationAtIndex(key, i, out value));
                    Assert.AreEqual("b", value);
                }
                for (int i = 14; i < 17; i++)
                {
                    Assert.IsFalse(document.TryGetAnnotationAtIndex(key, i, out value));
                    Assert.IsNull(value);
                }
            });
        }
Example #2
0
        public void EndAnnotation_WhenNoCurrentAnnotation_Throws()
        {
            var document = new SplashDocument();
            var key      = new Key <string>("href");

            // 1st case: no annotation table exists
            Assert.Throws <InvalidOperationException>(() => document.EndAnnotation(key));

            // 2nd case: annotation table exists but there is no current annotation
            document.BeginAnnotation(key, "value");
            document.EndAnnotation(key);
            Assert.Throws <InvalidOperationException>(() => document.EndAnnotation(key));
        }
Example #3
0
        public void BeginAnnotationAndEndAnnotation_PushAndPopAnnotationsOnStack()
        {
            var    document = new SplashDocument();
            var    key      = new Key <string>("href");
            string value;

            Assert.IsFalse(document.TryGetCurrentAnnotation(key, out value));
            Assert.IsNull(value);

            document.BeginAnnotation(key, "a");
            Assert.IsTrue(document.TryGetCurrentAnnotation(key, out value));
            Assert.AreEqual("a", value);

            document.EndAnnotation(key);
            Assert.IsFalse(document.TryGetCurrentAnnotation(key, out value));
            Assert.IsNull(value);

            using (document.BeginAnnotation(key, "b"))
            {
                Assert.IsTrue(document.TryGetCurrentAnnotation(key, out value));
                Assert.AreEqual("b", value);

                using (document.BeginAnnotation(key, "c"))
                {
                    Assert.IsTrue(document.TryGetCurrentAnnotation(key, out value));
                    Assert.AreEqual("c", value);
                }

                Assert.IsTrue(document.TryGetCurrentAnnotation(key, out value));
                Assert.AreEqual("b", value);
            }

            Assert.IsFalse(document.TryGetCurrentAnnotation(key, out value));
            Assert.IsNull(value);
        }
        public void TryGetAnnotationAtIndex_WhenIndexValidAndAtLeastOneAnnotation_ReturnsAnnotationAtIndex()
        {
            var document = new SplashDocument();
            var key = new Key<string>("href");

            // begin/end an empty block of annotations, these will get stripped out
            document.BeginAnnotation(key, "empty");
            document.BeginAnnotation(key, "empty2");
            document.EndAnnotation(key);
            document.EndAnnotation(key);

            // add some unannotated text
            document.AppendText("some ");

            // add some annotated text
            using (document.BeginAnnotation(key, "a"))
            {
                document.AppendText("text ");

                using (document.BeginAnnotation(key, "b"))
                    document.AppendText("more");
            }

            // add some unannotated text
            document.AppendText("...");

            // begin another empty annotation at the end, should have no effect on current text
            document.BeginAnnotation(key, "trailer");

            Assert.Multiple(() =>
            {
                string value;
                for (int i = 0; i < 5; i++)
                {
                    Assert.IsFalse(document.TryGetAnnotationAtIndex(key, i, out value));
                    Assert.IsNull(value);
                }
                for (int i = 5; i < 10; i++)
                {
                    Assert.IsTrue(document.TryGetAnnotationAtIndex(key, i, out value));
                    Assert.AreEqual("a", value);
                }
                for (int i = 10; i < 14; i++)
                {
                    Assert.IsTrue(document.TryGetAnnotationAtIndex(key, i, out value));
                    Assert.AreEqual("b", value);
                }
                for (int i = 14; i < 17; i++)
                {
                    Assert.IsFalse(document.TryGetAnnotationAtIndex(key, i, out value));
                    Assert.IsNull(value);
                }
            });
        }
        public void BeginAnnotationAndEndAnnotation_PushAndPopAnnotationsOnStack()
        {
            var document = new SplashDocument();
            var key = new Key<string>("href");
            string value;

            Assert.IsFalse(document.TryGetCurrentAnnotation(key, out value));
            Assert.IsNull(value);

            document.BeginAnnotation(key, "a");
            Assert.IsTrue(document.TryGetCurrentAnnotation(key, out value));
            Assert.AreEqual("a", value);

            document.EndAnnotation(key);
            Assert.IsFalse(document.TryGetCurrentAnnotation(key, out value));
            Assert.IsNull(value);

            using (document.BeginAnnotation(key, "b"))
            {
                Assert.IsTrue(document.TryGetCurrentAnnotation(key, out value));
                Assert.AreEqual("b", value);

                using (document.BeginAnnotation(key, "c"))
                {
                    Assert.IsTrue(document.TryGetCurrentAnnotation(key, out value));
                    Assert.AreEqual("c", value);
                }

                Assert.IsTrue(document.TryGetCurrentAnnotation(key, out value));
                Assert.AreEqual("b", value);
            }

            Assert.IsFalse(document.TryGetCurrentAnnotation(key, out value));
            Assert.IsNull(value);
        }
        public void EndAnnotation_WhenNoCurrentAnnotation_Throws()
        {
            var document = new SplashDocument();
            var key = new Key<string>("href");

            // 1st case: no annotation table exists
            Assert.Throws<InvalidOperationException>(() => document.EndAnnotation(key));

            // 2nd case: annotation table exists but there is no current annotation
            document.BeginAnnotation(key, "value");
            document.EndAnnotation(key);
            Assert.Throws<InvalidOperationException>(() => document.EndAnnotation(key));
        }