Ejemplo n.º 1
0
        public void Tracing()
        {
            // Given
            string inputFolder = Path.Combine(Environment.CurrentDirectory, @".\Input");
            if (!Directory.Exists(inputFolder))
            {
                Directory.CreateDirectory(inputFolder);
            }
            ITrace trace = Substitute.For<ITrace>();
            IExecutionContext context = Substitute.For<IExecutionContext>();
            context.RootFolder.Returns(Environment.CurrentDirectory);
            context.InputFolder.Returns(inputFolder);
            context.Trace.Returns(trace);
            Engine engine = new Engine();
            engine.Configure();
            context.Assemblies.Returns(engine.Assemblies);
            IDocument document = Substitute.For<IDocument>();
            document.GetStream().Returns(new MemoryStream(Encoding.UTF8.GetBytes(@"@{ ExecutionContext.Trace.Information(""Test""); }")));
            Razor razor = new Razor();

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

            // Then
            ITrace receivedTrace = context.Received().Trace; // Need to assign to dummy var to keep compiler happy
            trace.Received().Information("Test");
        }
Ejemplo n.º 2
0
 public void ExecuteAllExamples()
 {
     string path = Path.Combine(Assembly.GetExecutingAssembly().Location, "Examples");
     while (!Directory.Exists(path))
     {
         path = Directory.GetParent(path).Parent.FullName;
         path = Path.Combine(path, "Examples");
     }
     int count = 0;
     foreach(string example in Directory.EnumerateDirectories(path))
     {
         Console.WriteLine("Executing example " + example);
         Engine engine = new Engine
         {
             RootFolder = example
         };
         string config = Path.Combine(example, "config.wyam");
         if (File.Exists(config))
         {
             Console.WriteLine("Loading configuration file");
             engine.Configure(File.ReadAllText(config));
         }
         engine.Execute();
         count++;
     }
     Assert.AreEqual(4, count);
 }
Ejemplo n.º 3
0
        public void SimpleTemplate()
        {
            // Given
            string inputFolder = Path.Combine(Environment.CurrentDirectory, @".\Input");
            if (!Directory.Exists(inputFolder))
            {
                Directory.CreateDirectory(inputFolder);
            }
            IExecutionContext context = Substitute.For<IExecutionContext>();
            context.RootFolder.Returns(Environment.CurrentDirectory);
            context.InputFolder.Returns(inputFolder);
            Engine engine = new Engine();
            engine.Configure();
            context.Assemblies.Returns(engine.Assemblies);
            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
            document.Received(1).Clone(Arg.Any<string>());
            document.Received().Clone(" <p>0</p>  <p>1</p>  <p>2</p>  <p>3</p>  <p>4</p> ");
        }
Ejemplo n.º 4
0
        private bool Configure(Engine engine)
        {
            try
            {
                // If we have a configuration file use it, otherwise configure with defaults
                IFile configFile = engine.FileSystem.GetRootFile(_configFilePath);
                if (configFile.Exists)
                {
                    Trace.Information("Loading configuration from {0}", configFile.Path);
                    engine.Configure(configFile, _updatePackages, _outputScripts);
                }
                else
                {
                    Trace.Information("Could not find configuration file {0}, using default configuration", _configFilePath);
                    engine.Configure(GetDefaultConfigScript(), _updatePackages);
                }
            }
            catch (Exception ex)
            {
                Trace.Critical("Error while loading configuration: {0}", ex.Message);
                return false;
            }

            return true;
        }
Ejemplo n.º 5
0
 public void ExecuteAllExamples(string example)
 {
     Engine engine = new Engine();
     engine.Trace.AddListener(new TestTraceListener());
     engine.RootFolder = example;
     engine.Config.Assemblies.LoadDirectory(TestContext.CurrentContext.TestDirectory);
     string config = Path.Combine(example, "config.wyam");
     if (File.Exists(config))
     {
         engine.Configure(File.ReadAllText(config));
     }
     engine.Execute();
 }
Ejemplo n.º 6
0
 public void ExecuteAllExamples(string example)
 {
     Engine engine = new Engine
     {
         RootFolder = example
     };
     engine.Trace.AddListener(new TestTraceListener());
     string config = Path.Combine(example, "config.wyam");
     if (File.Exists(config))
     {
         engine.Configure(File.ReadAllText(config));
     }
     engine.Execute();
 }
Ejemplo n.º 7
0
        private bool Configure(Engine engine)
        {
            try
            {
                // If we have a configuration file use it, otherwise configure with defaults
                if (File.Exists(_configFile))
                {
                    engine.Trace.Information("Loading configuration from {0}", _configFile);
                    engine.Configure(Wyam.Common.IO.SafeIOHelper.ReadAllText(_configFile), _updatePackages, Path.GetFileName(_configFile), _outputScripts);
                }
                else
                {
                    engine.Trace.Information("Could not find configuration file {0}, using default configuration", _configFile);
                    engine.Configure(GetDefaultConfigScript(), _updatePackages, null, _outputScripts);
                }
            }
            catch (Exception ex)
            {
                engine.Trace.Critical("Error while loading configuration: {0}", ex.Message);
                return false;
            }

            return true;
        }
Ejemplo n.º 8
0
        public void ConfigureSupportsGlobalConstructorMethods()
        {
            // Given
            Engine engine = new Engine();
            string configScript = @"
                Pipelines.Add(
                    ReadFiles(""*.cshtml""),
	                WriteFiles("".html""));
            ";

            // When
            engine.Configure(configScript);

            // Then
            Assert.AreEqual(1, ((PipelineCollection)engine.Pipelines).Pipelines.Count());
            Assert.AreEqual(2, ((PipelineCollection)engine.Pipelines).Pipelines.First().Count);
        }
Ejemplo n.º 9
0
        public void ConfigureAddsPipelineAndModules()
        {
            // Given
            Engine engine = new Engine();
            string configScript = @"
                Pipelines.Add(
                    new ReadFiles(""*.cshtml""),
	                new WriteFiles("".html""));
            ";

            // When
            engine.Configure(configScript);

            // Then
            Assert.AreEqual(1, ((PipelineCollection)engine.Pipelines).Pipelines.Count());
            Assert.AreEqual(2, ((PipelineCollection)engine.Pipelines).Pipelines.First().Count);
        }
Ejemplo n.º 10
0
        public void ConfigureSetsPrimitiveMetadata()
        {
            // Given
            Engine engine = new Engine();
            string configScript = @"
                Metadata[""TestString""] = ""teststring"";
                Metadata[""TestInt""] = 1234;
                Metadata[""TestFloat""] = 1234.567;
                Metadata[""TestBool""] = true;
            ";

            // When
            engine.Configure(configScript);

            // Then
            Assert.AreEqual("teststring", engine.Metadata["TestString"]);
            Assert.AreEqual(1234, engine.Metadata["TestInt"]);
            Assert.AreEqual(1234.567, engine.Metadata["TestFloat"]);
            Assert.AreEqual(true, engine.Metadata["TestBool"]);
        }
Ejemplo n.º 11
0
            public void Document()
            {
                // Given
                string inputFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, @".\Input");
                if (!Directory.Exists(inputFolder))
                {
                    Directory.CreateDirectory(inputFolder);
                }
                IExecutionContext context = Substitute.For<IExecutionContext>();
                context.RootFolder.Returns(TestContext.CurrentContext.TestDirectory);
                context.InputFolder.Returns(inputFolder);
                Engine engine = new Engine();
                engine.Configure();
                context.Assemblies.Returns(engine.Assemblies);
                IDocument document = Substitute.For<IDocument>();
                document.Source.Returns(@"C:\Temp\temp.txt");
                document.GetStream().Returns(new MemoryStream(Encoding.UTF8.GetBytes(@"<p>@Document.Source</p>")));
                Razor razor = new Razor();

                // When
                razor.Execute(new[] { document }, context).ToList();

                // Then
                context.Received(1).GetDocument(Arg.Any<IDocument>(), Arg.Any<string>());
                context.Received().GetDocument(document, @"<p>C:\Temp\temp.txt</p>");
            }
Ejemplo n.º 12
0
            public void Tracing()
            {
                // Given
                string inputFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, @".\Input");
                if (!Directory.Exists(inputFolder))
                {
                    Directory.CreateDirectory(inputFolder);
                }
                IExecutionContext context = Substitute.For<IExecutionContext>();
                context.RootFolder.Returns(TestContext.CurrentContext.TestDirectory);
                context.InputFolder.Returns(inputFolder);
                Engine engine = new Engine();
                engine.Configure();
                context.Assemblies.Returns(engine.Assemblies);
                context.Namespaces.Returns(engine.Namespaces);
                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");
            }
Ejemplo n.º 13
0
        public void Metadata()
        {
            // Given
            string inputFolder = Path.Combine(Environment.CurrentDirectory, @".\Input");
            if (!Directory.Exists(inputFolder))
            {
                Directory.CreateDirectory(inputFolder);
            }
            IExecutionContext context = Substitute.For<IExecutionContext>();
            context.RootFolder.Returns(Environment.CurrentDirectory);
            context.InputFolder.Returns(inputFolder);
            Engine engine = new Engine();
            engine.Configure();
            context.Assemblies.Returns(engine.Assemblies);
            IDocument document = Substitute.For<IDocument>();
            document.Metadata["MyKey"].Returns("MyValue");
            document.GetStream().Returns(new MemoryStream(Encoding.UTF8.GetBytes(@"<p>@Metadata[""MyKey""]</p>")));
            Razor razor = new Razor();

            // When
            razor.Execute(new[] { document }, context).ToList();

            // Then
            document.Received(1).Clone(Arg.Any<string>());
            document.Received().Clone("<p>MyValue</p>");
        }
Ejemplo n.º 14
0
        public void SimpleTemplate()
        {
            // Given
            string inputFolder = Path.Combine(Environment.CurrentDirectory, @".\Input");
            if (!Directory.Exists(inputFolder))
            {
                Directory.CreateDirectory(inputFolder);
            }
            IExecutionContext context = Substitute.For<IExecutionContext>();
            context.RootFolder.Returns(Environment.CurrentDirectory);
            context.InputFolder.Returns(inputFolder);
            Engine engine = new Engine();
            engine.Configure();
            context.Assemblies.Returns(engine.Assemblies);
            IDocument document = Substitute.For<IDocument>();
            document.Metadata.Get("SourceFileBase", "/").Returns("/");
            List<string> items = new List<string>();
            document
                .When(x => x.Clone(Arg.Any<string>()))
                .Do(x => items.Add(x.Arg<string>()));
            document.Content.Returns(@"@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
            document.Received().Clone(Arg.Any<string>());
            Assert.AreEqual(1, items.Count());
            Assert.AreEqual(" <p>0</p>  <p>1</p>  <p>2</p>  <p>3</p>  <p>4</p> ", items.First());
        }
Ejemplo n.º 15
0
        public void Metadata()
        {
            // Given
            string inputFolder = Path.Combine(Environment.CurrentDirectory, @".\Input");
            if (!Directory.Exists(inputFolder))
            {
                Directory.CreateDirectory(inputFolder);
            }
            IExecutionContext context = Substitute.For<IExecutionContext>();
            context.RootFolder.Returns(Environment.CurrentDirectory);
            context.InputFolder.Returns(inputFolder);
            Engine engine = new Engine();
            engine.Configure();
            context.Assemblies.Returns(engine.Assemblies);
            IDocument document = Substitute.For<IDocument>();
            document.Metadata.Get("SourceFileBase", "/").Returns("/");
            document.Metadata["MyKey"].Returns("MyValue");
            List<string> items = new List<string>();
            document
                .When(x => x.Clone(Arg.Any<string>()))
                .Do(x => items.Add(x.Arg<string>()));
            document.Content.Returns(@"<p>@Metadata[""MyKey""]</p>");
            Razor razor = new Razor();

            // When
            razor.Execute(new[] { document }, context).ToList();

            // Then
            document.Received().Clone(Arg.Any<string>());
            Assert.AreEqual(1, items.Count());
            Assert.AreEqual("<p>MyValue</p>", items.First());
        }