Esempio n. 1
0
            public void ContentAndMetadataReturnsCorrectDocuments()
            {
                // Given
                List<string> content = new List<string>();
                List<object> values = new List<object>();
                Engine engine = new Engine();
                Core.Modules.Control.Documents documents = new Core.Modules.Control.Documents(
                    Tuple.Create("A", new Dictionary<string, object> {{"Foo", "a"}}.AsEnumerable()),
                    Tuple.Create("B", new Dictionary<string, object> {{"Foo", "b"}}.AsEnumerable()),
                    Tuple.Create("C", new Dictionary<string, object> {{"Foo", "c"}}.AsEnumerable()));
                Execute gatherData = new Execute((d, c) =>
                {
                    content.Add(d.Content);
                    values.Add(d["Foo"]);
                    return null;
                });
                engine.Pipelines.Add(documents, gatherData);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(3, content.Count);
                Assert.AreEqual(3, values.Count);
                CollectionAssert.AreEqual(new[] {"A", "B", "C"}, content);
                CollectionAssert.AreEqual(new[] {"a", "b", "c"}, values);
            }
Esempio n. 2
0
            public void ResultsInCorrectCounts()
            {
                // Given
                Engine engine = new Engine();
                CountModule a = new CountModule("A")
                {
                    AdditionalOutputs = 1
                };
                CountModule b = new CountModule("B")
                {
                    AdditionalOutputs = 2
                };
                CountModule c = new CountModule("C")
                {
                    AdditionalOutputs = 3
                };
                engine.Pipelines.Add(a, new ConcatBranch(b), c);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(1, a.ExecuteCount);
                Assert.AreEqual(1, b.ExecuteCount);
                Assert.AreEqual(1, c.ExecuteCount);
                Assert.AreEqual(1, a.InputCount);
                Assert.AreEqual(2, b.InputCount);
                Assert.AreEqual(8, c.InputCount);
                Assert.AreEqual(2, a.OutputCount);
                Assert.AreEqual(6, b.OutputCount);
                Assert.AreEqual(32, c.OutputCount);
            }
Esempio n. 3
0
            public void PaginateSetsCorrectMetadata()
            {
                // Given
                List<int> currentPage = new List<int>();
                List<int> totalPages = new List<int>();
                List<bool> hasNextPage = new List<bool>();
                List<bool> hasPreviousPage = new List<bool>();
                Engine engine = new Engine();
                CountModule count = new CountModule("A")
                {
                    AdditionalOutputs = 7
                };
                Paginate paginate = new Paginate(3, count);
                Execute gatherData = new Execute((d, c) =>
                {
                    currentPage.Add(d.Get<int>(Keys.CurrentPage));
                    totalPages.Add(d.Get<int>(Keys.TotalPages));
                    hasNextPage.Add(d.Get<bool>(Keys.HasNextPage));
                    hasPreviousPage.Add(d.Get<bool>(Keys.HasPreviousPage));
                    return null;
                });
                engine.Pipelines.Add(paginate, gatherData);

                // When
                engine.Execute();

                // Then
                CollectionAssert.AreEqual(new[] {1, 2, 3}, currentPage);
                CollectionAssert.AreEqual(new[] {3, 3, 3}, totalPages);
                CollectionAssert.AreEqual(new[] {true, true, false}, hasNextPage);
                CollectionAssert.AreEqual(new[] {false, true, true}, hasPreviousPage);
            }
Esempio n. 4
0
            public void PaginateSetsDocumentsInMetadata()
            {
                // Given
                List<IList<string>> content = new List<IList<string>>();
                Engine engine = new Engine();
                CountModule count = new CountModule("A")
                {
                    AdditionalOutputs = 7
                };
                Paginate paginate = new Paginate(3, count);
                Execute gatherData = new Execute((d, c) =>
                {
                    content.Add(d.Get<IList<IDocument>>(Keys.PageDocuments).Select(x => x.Content).ToList());
                    return null;
                });
                engine.Pipelines.Add(paginate, gatherData);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(3, content.Count);
                CollectionAssert.AreEqual(new[] {"1", "2", "3"}, content[0]);
                CollectionAssert.AreEqual(new[] {"4", "5", "6"}, content[1]);
                CollectionAssert.AreEqual(new[] {"7", "8"}, content[2]);
            }
Esempio n. 5
0
            public void ResultsInCorrectCountsWithPredicate()
            {
                // Given
                Engine engine = new Engine();
                CountModule a = new CountModule("A")
                {
                    AdditionalOutputs = 1
                };
                CountModule b = new CountModule("B")
                {
                    AdditionalOutputs = 2
                };
                CountModule c = new CountModule("C")
                {
                    AdditionalOutputs = 3
                };
                engine.Pipelines.Add(a, new ConcatBranch(b).Where((x, y) => x.Content == "1"), c);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(1, a.ExecuteCount);
                Assert.AreEqual(1, b.ExecuteCount);
                Assert.AreEqual(1, c.ExecuteCount);
                Assert.AreEqual(1, a.InputCount);
                Assert.AreEqual(1, b.InputCount);
                Assert.AreEqual(5, c.InputCount);
                Assert.AreEqual(2, a.OutputCount);
                Assert.AreEqual(3, b.OutputCount);
                Assert.AreEqual(20, c.OutputCount);
            }
Esempio n. 6
0
            public void DashStringDoesNotSplitAtNonmatchingDashes()
            {
                // Given
                Engine engine = new Engine();
                Pipeline pipeline = new Pipeline("Pipeline", null);
                IExecutionContext context = new ExecutionContext(engine, pipeline);
                IDocument[] inputs = 
                {
                    context.GetDocument(@"FM1
FM2
---
Content1
Content2")
                };
                bool executed = false;
                FrontMatter frontMatter = new FrontMatter("-", new Execute((x, ctx) =>
                {
                    executed = true;
                    return new[] {x};
                }));

                // When
                IEnumerable<IDocument> documents = frontMatter.Execute(inputs, context);

                // Then
                Assert.AreEqual(1, documents.Count());
                Assert.IsFalse(executed);
                Assert.AreEqual(@"FM1
FM2
---
Content1
Content2", documents.First().Content);
            }
Esempio n. 7
0
            public void OrderByOrdersInDescendingOrder()
            {
                // Given
                List<string> content = new List<string>();
                Engine engine = new Engine();
                CountModule count = new CountModule("A")
                {
                    AdditionalOutputs = 4
                };
                CountModule count2 = new CountModule("A")
                {
                    AdditionalOutputs = 2
                };
                Concat concat = new Concat(count2);
                OrderBy orderBy = new OrderBy((d, c) => d.Get<int>("A")).Descending();
                Execute gatherData = new Execute((d, c) =>
                {
                    content.Add(d.Content);
                    return null;
                });
                engine.Pipelines.Add(count, concat, orderBy, gatherData);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(8, content.Count);
                CollectionAssert.AreEqual(new[] {"5", "4", "3", "3", "2", "2", "1", "1"}, content);
            }
Esempio n. 8
0
            public void SetsDocumentsInMetadata()
            {
                // Given
                List<IList<string>> content = new List<IList<string>>();
                Engine engine = new Engine();
                CountModule count = new CountModule("A")
                {
                    AdditionalOutputs = 7
                };
                GroupBy groupBy = new GroupBy((d, c) => d.Get<int>("A")%3, count);
                OrderBy orderBy = new OrderBy((d, c) => d.Get<int>(Keys.GroupKey));
                Execute gatherData = new Execute((d, c) =>
                {
                    content.Add(d.Get<IList<IDocument>>(Keys.GroupDocuments).Select(x => x.Content).ToList());
                    return null;
                });
                engine.Pipelines.Add(groupBy, orderBy, gatherData);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(3, content.Count);
                CollectionAssert.AreEquivalent(new[] {"3", "6"}, content[0]);
                CollectionAssert.AreEquivalent(new[] {"1", "4", "7"}, content[1]);
                CollectionAssert.AreEquivalent(new[] {"2", "5", "8"}, content[2]);
            }
Esempio n. 9
0
            public void ChildModulesAreExecuted()
            {
                // Given
                Engine engine = new Engine();
                CountModule a = new CountModule("A")
                {
                    AdditionalOutputs = 1
                };
                CountModule b = new CountModule("B")
                {
                    AdditionalOutputs = 2
                };
                CountModule c = new CountModule("C")
                {
                    AdditionalOutputs = 3
                };
                engine.Pipelines.Add(a, new Core.Modules.Control.ModuleCollection(b, c));

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(1, a.ExecuteCount);
                Assert.AreEqual(1, b.ExecuteCount);
                Assert.AreEqual(1, c.ExecuteCount);
                Assert.AreEqual(1, a.InputCount);
                Assert.AreEqual(2, b.InputCount);
                Assert.AreEqual(6, c.InputCount);
                Assert.AreEqual(2, a.OutputCount);
                Assert.AreEqual(6, b.OutputCount);
                Assert.AreEqual(24, c.OutputCount);
            }
Esempio n. 10
0
            public void OrderByOrdersThenByInAscendingOrder()
            {
                // Given
                List<string> content = new List<string>();
                Engine engine = new Engine();
                CountModule count = new CountModule("A")
                {
                    AdditionalOutputs = 4
                };
                CountModule count2 = new CountModule("B")
                {
                    AdditionalOutputs = 1
                };
                OrderBy orderBy = new OrderBy((d, c) => d.Get<int>("A"))
                    .ThenBy((d, c) => d.Get<int>("B"));
                Execute gatherData = new Execute((d, c) =>
                {
                    content.Add(d.Content);
                    return null;
                });
                engine.Pipelines.Add(count, count2, orderBy, gatherData);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(10, content.Count); // (4+1) * (21+1)
                CollectionAssert.AreEqual(new[] {"11", "12", "23", "24", "35", "36", "47", "48", "59", "510"}, content);
            }
Esempio n. 11
0
            public void DefaultCtorSplitsAtDashes()
            {
                // Given
                Engine engine = new Engine();
                Pipeline pipeline = new Pipeline("Pipeline", null);
                IExecutionContext context = new ExecutionContext(engine, pipeline);
                IDocument[] inputs = 
                {
                    context.GetDocument(@"FM1
FM2
---
Content1
Content2")
                };
                string frontMatterContent = null;
                FrontMatter frontMatter = new FrontMatter(new Execute((x, ctx) =>
                {
                    frontMatterContent = x.Content;
                    return new[] {x};
                }));

                // When
                IEnumerable<IDocument> documents = frontMatter.Execute(inputs, context);

                // Then
                Assert.AreEqual(1, documents.Count());
                Assert.AreEqual(@"FM1
FM2
", frontMatterContent);
                Assert.AreEqual(@"Content1
Content2", documents.First().Content);
            }
Esempio n. 12
0
 public void SetUp()
 {
     Engine = new Engine();
     Engine.FileSystem.FileProviders.Add(NormalizedPath.DefaultFileProvider.Scheme, GetFileProvider());
     Engine.FileSystem.RootPath = "/";
     Pipeline = new Pipeline("Pipeline", null);
     Context = new ExecutionContext(Engine, Pipeline);
 }
Esempio n. 13
0
        public EngineManager(Preprocessor preprocessor, ConfigOptions configOptions)
        {
            _configOptions = configOptions;
            Engine = new Engine();
            Configurator = new Configurator(Engine, preprocessor);
            
            // Set no cache if requested
            if (_configOptions.NoCache)
            {
                Engine.Settings.UseCache = false;
            }

            // Set folders
            Engine.FileSystem.RootPath = _configOptions.RootPath;
            if (_configOptions.InputPaths != null && _configOptions.InputPaths.Count > 0)
            {
                // Clear existing default paths if new ones are set
                // and reverse the inputs so the last one is first to match the semantics of multiple occurrence single options
                Engine.FileSystem.InputPaths.Clear();
                Engine.FileSystem.InputPaths.AddRange(_configOptions.InputPaths.Reverse());
            }
            if (_configOptions.OutputPath != null)
            {
                Engine.FileSystem.OutputPath = _configOptions.OutputPath;
            }
            if (_configOptions.NoClean)
            {
                Engine.Settings.CleanOutputPath = false;
            }
            if (_configOptions.GlobalMetadata != null)
            {
                foreach (KeyValuePair<string, object> item in _configOptions.GlobalMetadata)
                {
                    Engine.GlobalMetadata.Add(item);
                }
            }

            // Set NuGet settings
            Configurator.PackageInstaller.UpdatePackages = _configOptions.UpdatePackages;
            Configurator.PackageInstaller.UseLocalPackagesFolder = _configOptions.UseLocalPackages;
            Configurator.PackageInstaller.UseGlobalPackageSources = _configOptions.UseGlobalSources;
            if (_configOptions.PackagesPath != null)
            {
                Configurator.PackageInstaller.PackagesPath = _configOptions.PackagesPath;
            }

            // Metadata
            Configurator.GlobalMetadata = configOptions.GlobalMetadata;
            Configurator.InitialMetadata = configOptions.InitialMetadata;

            // Script output
            Configurator.OutputScript = _configOptions.OutputScript;

            // Application input
            Engine.ApplicationInput = _configOptions.Stdin;
        }
Esempio n. 14
0
 public void SetUp()
 {
     Engine = new Engine();
     Engine.FileSystem.FileProviders.Add(NormalizedPath.DefaultFileProvider.Scheme, GetFileProvider());
     Engine.FileSystem.RootPath = "/";
     Engine.FileSystem.InputPaths.Clear();
     Engine.FileSystem.InputPaths.Add("/TestFiles/Input");
     Pipeline = new Pipeline("Pipeline", null);
     Context = new ExecutionContext(Engine, Pipeline);
     Inputs = new[] { Context.GetDocument() };
 }
Esempio n. 15
0
            public void TestTraceListenerThrows(TraceEventType traceEventType)
            {
                // Given
                Engine engine = new Engine();
                engine.Pipelines.Add(new Trace(traceEventType.ToString()).EventType(traceEventType));

                // When
                TestDelegate test = () => engine.Execute();

                // Then
                Assert.Throws<Exception>(test);
            }
Esempio n. 16
0
            public void ExecuteDoesNotThrowForNullResultWithContextConfig()
            {
                // Given
                Engine engine = new Engine();
                Execute execute = new Execute(c => null);
                engine.Pipelines.Add(execute);

                // When
                engine.Execute();

                // Then
            }
Esempio n. 17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Configurator"/> class. This overload
        /// allows passing in a <see cref="Preprocessor"/> that can be reused and pre-configured
        /// with directives not sourced from the script.
        /// </summary>
        /// <param name="engine">The engine to configure.</param>
        /// <param name="preprocessor">The preprocessor.</param>
        public Configurator(Engine engine, Preprocessor preprocessor)
        {
            _engine = engine;
            _preprocessor = preprocessor;
            _assemblyResolver = new AssemblyResolver(_scriptManager); 
            AssemblyLoader = new AssemblyLoader(engine.FileSystem, engine.Assemblies, _assemblyResolver);
            PackageInstaller = new PackageInstaller(engine.FileSystem, AssemblyLoader);
            ClassCatalog = new ClassCatalog();

            // Add this namespace and assembly
            engine.Namespaces.Add(typeof(ScriptBase).Namespace);
            engine.Assemblies.Add(typeof(ScriptBase).Assembly);
        }
Esempio n. 18
0
            public void CountReturnsCorrectDocuments()
            {
                // Given
                Engine engine = new Engine();
                Core.Modules.Control.Documents documents = new Core.Modules.Control.Documents(5);
                engine.Pipelines.Add(documents);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(5, engine.Documents.Count());
            }
Esempio n. 19
0
            public void ExecuteDoesNotRequireReturnValueForContextConfig()
            {
                // Given
                int a = 0;
                Engine engine = new Engine();
                Execute execute = new Execute(c => { a = a + 1; });
                engine.Pipelines.Add(execute);

                // When
                engine.Execute();

                // Then
            }
Esempio n. 20
0
            public void ExecuteReturnsDocumentForSingleResultDocumentFromContextConfig()
            {
                // Given
                Engine engine = new Engine();
                IDocument document = Substitute.For<IDocument>();
                Execute execute = new Execute(c => document);
                engine.Pipelines.Add("Test", execute);

                // When
                engine.Execute();

                // Then
                CollectionAssert.AreEquivalent(new[] { document }, engine.Documents["Test"]);
            }
Esempio n. 21
0
            public void SimpleTemplate()
            {
                // Given
                Engine engine = new Engine();
                IExecutionContext context = GetExecutionContext(engine);
                IDocument document = Substitute.For<IDocument>();
                document.GetStream().Returns(new MemoryStream(Encoding.UTF8.GetBytes(@"@for(int c = 0 ; c < 5 ; c++) { <p>@c</p> }")));
                Razor razor = new Razor();

                // When
                razor.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(1).GetDocument(Arg.Any<IDocument>(), Arg.Any<string>());
                context.Received().GetDocument(document, " <p>0</p>  <p>1</p>  <p>2</p>  <p>3</p>  <p>4</p> ");
            }
Esempio n. 22
0
            public void Tracing()
            {
                // Given
                Engine engine = new Engine();
                IExecutionContext context = GetExecutionContext(engine);
                IDocument document = Substitute.For<IDocument>();
                TraceListener traceListener = new TraceListener();
                Trace.AddListener(traceListener);
                document.GetStream().Returns(new MemoryStream(Encoding.UTF8.GetBytes(@"@{ Trace.Information(""Test""); }")));
                Razor razor = new Razor();

                // When
                razor.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                Trace.RemoveListener(traceListener);
                CollectionAssert.Contains(traceListener.Messages, "Test");
            }
Esempio n. 23
0
            public void FileNameIsConvertedCorrectly(string input, string output)
            {
                // Given
                Engine engine = new Engine();
                Pipeline pipeline = new Pipeline("Pipeline", null);
                IExecutionContext context = new ExecutionContext(engine, pipeline);
                IDocument[] inputs = { context.GetDocument(new MetadataItems
                {
                    new MetadataItem(Keys.SourceFileName, input)
                }) };
                FileName fileName = new FileName();

                // When
                IEnumerable<IDocument> documents = fileName.Execute(inputs, context);

                // Then
                Assert.AreEqual(output, documents.First().FilePath(Keys.WriteFileName).FullPath);
            }
Esempio n. 24
0
            public void ElseIfResultsInCorrectCounts()
            {
                // Given
                Engine engine = new Engine();
                CountModule a = new CountModule("A")
                {
                    AdditionalOutputs = 2
                };
                CountModule b = new CountModule("B")
                {
                    AdditionalOutputs = 2
                };
                CountModule c = new CountModule("C")
                {
                    AdditionalOutputs = 3
                };
                CountModule d = new CountModule("B")
                {
                    AdditionalOutputs = 2
                };
                engine.Pipelines.Add(
                    a,
                    new If((x, y) => x.Content == "1", b)
                        .ElseIf((x, y) => x.Content == "2", c),
                    d);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(1, a.ExecuteCount);
                Assert.AreEqual(1, b.ExecuteCount);
                Assert.AreEqual(1, c.ExecuteCount);
                Assert.AreEqual(1, d.ExecuteCount);
                Assert.AreEqual(1, a.InputCount);
                Assert.AreEqual(1, b.InputCount);
                Assert.AreEqual(1, c.InputCount);
                Assert.AreEqual(8, d.InputCount);
                Assert.AreEqual(3, a.OutputCount);
                Assert.AreEqual(3, b.OutputCount);
                Assert.AreEqual(4, c.OutputCount);
                Assert.AreEqual(24, d.OutputCount);
            }
Esempio n. 25
0
            public void SwitchResultsInCorrectCounts()
            {
                // Given
                Engine engine = new Engine();
                CountModule a = new CountModule("A") {AdditionalOutputs = 2};
                CountModule b = new CountModule("B");
                CountModule c = new CountModule("C");
                CountModule d = new CountModule("D");

                engine.Pipelines.Add(a, new Switch((x, y) => x.Content).Case("1", b).Case("2", c).Default(d));

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(1, a.ExecuteCount);
                Assert.AreEqual(1, b.ExecuteCount);
                Assert.AreEqual(1, c.ExecuteCount);
                Assert.AreEqual(1, d.ExecuteCount);
            }
Esempio n. 26
0
            public void SwitchNoCasesResultsInCorrectCounts()
            {
                // Given
                Engine engine = new Engine();
                CountModule a = new CountModule("A") {AdditionalOutputs = 2};
                CountModule b = new CountModule("B");
                CountModule c = new CountModule("C");

                engine.Pipelines.Add(a, new Switch((x, y) => x.Content).Default(b), c);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(1, a.ExecuteCount);
                Assert.AreEqual(1, b.ExecuteCount);
                Assert.AreEqual(3, b.InputCount);
                Assert.AreEqual(3, b.OutputCount);
                Assert.AreEqual(3, c.InputCount);
            }
Esempio n. 27
0
            public void ArrayInCaseResultsInCorrectCounts()
            {
                // Given
                Engine engine = new Engine();
                CountModule a = new CountModule("A") {AdditionalOutputs = 2};
                CountModule b = new CountModule("B");
                CountModule c = new CountModule("C");

                engine.Pipelines.Add(a, new Switch((x, y) => x.Content).Case(new string[] {"1", "2"}, b), c);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(1, a.ExecuteCount);
                Assert.AreEqual(1, b.ExecuteCount);
                Assert.AreEqual(2, b.InputCount);
                Assert.AreEqual(2, b.OutputCount);
                Assert.AreEqual(3, c.InputCount);
            }
Esempio n. 28
0
            public void CombinesAndOverwritesMetadata()
            {
                // Given
                Engine engine = new Engine();
                CountModule a = new CountModule("A")
                {
                    Value = 10
                };
                CountModule b = new CountModule("A")
                {
                    Value = 20
                };
                engine.Pipelines.Add("Test", a, new Merge(b));

                // When
                engine.Execute();

                // Then
                CollectionAssert.AreEqual(new[] { 21 }, engine.Documents["Test"].Select(x => x["A"]));
            }
Esempio n. 29
0
            public void ContentReturnsCorrectDocuments()
            {
                // Given
                List<string> content = new List<string>();
                Engine engine = new Engine();
                Core.Modules.Control.Documents documents = new Core.Modules.Control.Documents("A", "B", "C", "D");
                Execute gatherData = new Execute((d, c) =>
                {
                    content.Add(d.Content);
                    return null;
                });
                engine.Pipelines.Add(documents, gatherData);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(4, content.Count);
                CollectionAssert.AreEqual(new[] {"A", "B", "C", "D"}, content);
            }
Esempio n. 30
0
		    public void FileNameShouldBeLowercase()
		    {
			    // Given
			    string input = "FileName With MiXeD CapS";
			    string output = "filename-with-mixed-caps";

                Engine engine = new Engine();
			    Pipeline pipeline = new Pipeline("Pipeline", null);
			    IExecutionContext context = new ExecutionContext(engine, pipeline);
			    IDocument[] inputs = { context.GetDocument(new MetadataItems
			    {
                    new MetadataItem(Keys.SourceFileName, new FilePath(input))
			    }) };
			    FileName fileName = new FileName();

			    // When
			    IEnumerable<IDocument> documents = fileName.Execute(inputs, context);

			    // Then
			    Assert.AreEqual(output, documents.First().FilePath(Keys.WriteFileName).FullPath);
		    }
Esempio n. 31
0
 public ExecutionContext(Engine engine, Guid executionId, ExecutionPipeline pipeline)
 {
     Engine      = engine;
     ExecutionId = executionId;
     _pipeline   = pipeline;
 }