public void WhenDocumentDoesNotContainModelPropertiesReturnDocument()
        {
            var model = new FakeModel(
                "Value of property 1",
                "Value of property 2",
                new FakeModel.FakeSubModel("Value of property 1 of sub model", "Value of property 2 of sub model"),
                new []
            {
                new FakeModel.FakeItem("Value of property 1 of fake item 1", "Value of property 2 of fake item 1"),
                new FakeModel.FakeItem("Value of property 1 of fake item 1", "Value of property 2 of fake item 1")
            });
            var result = _formatter.GetDocument(model, GetDocument());

            using (var memoryStream = new MemoryStream(result))
                using (var document = WordprocessingDocument.Open(memoryStream, true))
                    using (var streamReader = new StreamReader(document.MainDocumentPart.GetStream()))
                    {
                        var content = streamReader.ReadToEnd();
                        content.ShouldContain(model.Property1);
                        content.ShouldNotContain(model.Property2);
                        content.ShouldContain(model.SubModel.Property1);
                        content.ShouldNotContain(model.SubModel.Property2);

                        foreach (var item in model.Items)
                        {
                            content.ShouldContain(item.Property1);
                            content.ShouldNotContain(item.Property2);
                        }

                        var regex = new Regex("{model(.*?)}");
                        var placeholdersToReplace = regex.Matches(content);
                        placeholdersToReplace.Count.ShouldBe(0);
                        Regex.Matches(content, "related-content").Count.ShouldBe(model.Items.Length);
                    }
        }
        public void CreatingWordpressingDocumentAndReplacingAllModelIndicatorsReturnsCorrectDocument()
        {
            var model  = GetModel();
            var result = _formatter.GetDocument(model, GetDocument(), new ProcessingOptions("customModelPrefix"));

            using (var memoryStream = new MemoryStream(result))
                using (var document = WordprocessingDocument.Open(memoryStream, true))
                    using (var streamReader = new StreamReader(document.MainDocumentPart.GetStream()))
                    {
                        var content = streamReader.ReadToEnd();
                        content.ShouldContain(model.Property1);
                        content.ShouldContain(model.Property2);
                        content.ShouldContain(model.Property3.ToString());
                        content.ShouldContain(model.Property4.ToString("F"));
                        content.ShouldContain(model.Property5.ToString("G"));
                        content.ShouldContain(model.Property6.ToString("D"));
                        content.ShouldContain(model.Property7.ToString("hh"));
                        content.ShouldContain(HttpUtility.HtmlEncode(model.Property8.ToString()));
                        content.ShouldContain(model.Property9.ToString("N"));
                        content.ShouldContain(model.SubModel.Property1);
                        content.ShouldContain(model.SubModel.Property2);

                        foreach (var item in model.Items)
                        {
                            content.ShouldContain(item.Property1);
                            content.ShouldContain(item.Property2);
                        }

                        var regex = new Regex("{customModelPrefix(.*?)}");
                        var placeholdersToReplace = regex.Matches(content);
                        placeholdersToReplace.Count.ShouldBe(0);
                        Regex.Matches(content, "related-content").Count.ShouldBe(model.Items.Length);
                    }
        }
        public void OpeningWordpressingDocumentAndReplacingAllModelIndicatorsReturnsCorrectDocument()
        {
            var assembly = Assembly.GetExecutingAssembly();

            using (var stream = assembly.GetManifestResourceStream("DocumentGenerator.Tests.form.docx"))
                using (MemoryStream mem = new MemoryStream())
                {
                    stream.CopyTo(mem);
                    var result = _formatter.GetDocument(_model, mem.ToArray());
                    using (var memoryStream = new MemoryStream(result))
                        using (var document = WordprocessingDocument.Open(memoryStream, true))
                            using (var streamReader = new StreamReader(document.MainDocumentPart.GetStream()))
                            {
                                var content = streamReader.ReadToEnd();
                                content.ShouldContain(_model.Property1);
                                content.ShouldContain(_model.Property2);
                                content.ShouldContain(_model.Property3.ToString());
                                content.ShouldContain(_model.Property4.ToString("F"));
                                content.ShouldContain(_model.Property5.ToString("G"));
                                content.ShouldContain(_model.Property6.ToString("D"));
                                content.ShouldContain(_model.Property7.ToString("hh"));
                                content.ShouldContain(HttpUtility.HtmlEncode(_model.Property8.ToString()));
                                content.ShouldContain(_model.Property9.ToString("N"));
                                content.ShouldContain(_model.SubModel.Property1);
                                content.ShouldContain(_model.SubModel.Property2);

                                foreach (var item in _model.Items)
                                {
                                    content.ShouldContain(item.Property1);
                                    content.ShouldContain(item.Property2);
                                }

                                var regex = new Regex("{model(.*?)}");
                                var placeholdersToReplace = regex.Matches(content);
                                placeholdersToReplace.Count.ShouldBe(0);
                            }
                }
        }
Ejemplo n.º 4
0
        public void WhenDocumentContainsMappingUnknownForModelThrowException()
        {
            var model = new FakeModel(
                "Value of property 1",
                "Value of property 2",
                new FakeModel.FakeSubModel("Value of property 1 of sub model", "Value of property 2 of sub model"),
                new []
            {
                new FakeModel.FakeItem("Value of property 1 of fake item 1", "Value of property 2 of fake item 1")
            });

            var exception = Assert.Throws <NullReferenceException>(() => _formatter.GetDocument(model, GetDocument()));

            Assert.That(exception.Message, Is.EqualTo("property is null for 'NonExistingProperty'. Document template contains unknown model expression."));
        }