コード例 #1
0
        public void Markdown()
        {
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            var innerChunks = new Chunk[] { new SendLiteralChunk {
                                                Text = "*test*"
                                            } };

            DoCompileView(compiler, new Chunk[]
            {
                new MarkdownChunk {
                    Body = innerChunks
                }
            });

            Assert.That(compiler.SourceCode, Text.Contains("using(MarkdownOutputScope())"));
            Assert.That(compiler.SourceCode, Text.Contains("Output.Write(\"*test*\");"));

            var instance = compiler.CreateInstance();
            var contents = instance.RenderView().Trim();

            Assert.That(contents, Is.EqualTo("<p><em>test</em></p>"));
        }
コード例 #2
0
        public void IfFalseCondition()
        {
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            var trueChunks = new Chunk[] { new SendLiteralChunk {
                                               Text = "wastrue"
                                           } };

            DoCompileView(compiler, new Chunk[]
            {
                new SendLiteralChunk {
                    Text = "<p>"
                },
                new LocalVariableChunk {
                    Name = "arg", Value = "5"
                },
                new ConditionalChunk {
                    Type = ConditionalType.If, Condition = "arg==6", Body = trueChunks
                },
                new SendLiteralChunk {
                    Text = "</p>"
                }
            });
            var instance = compiler.CreateInstance();
            var contents = instance.RenderView();

            Assert.AreEqual("<p></p>", contents);
        }
コード例 #3
0
        public void GlobalVariables()
        {
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            DoCompileView(compiler, new Chunk[]
            {
                new SendExpressionChunk {
                    Code = "title"
                },
                new AssignVariableChunk {
                    Name = "item", Value = "8"
                },
                new SendLiteralChunk {
                    Text = ":"
                },
                new SendExpressionChunk {
                    Code = "item"
                },
                new GlobalVariableChunk {
                    Name = "title", Value = "\"hello world\""
                },
                new GlobalVariableChunk {
                    Name = "item", Value = "3"
                }
            });
            var instance = compiler.CreateInstance();
            var contents = instance.RenderView();

            Assert.AreEqual("hello world:8", contents);
        }
コード例 #4
0
        public void PageBaseTypeWorksWithOptionalModel()
        {
            var compiler = new CSharpViewCompiler()
            {
                BaseClass     = "Spark.Tests.Stubs.StubSparkView",
                NullBehaviour = NullBehaviour.Strict
            };

            DoCompileView(compiler, new Chunk[]
            {
                new PageBaseTypeChunk {
                    BaseClass = "Spark.Tests.Stubs.StubSparkView2"
                },
                new ViewDataModelChunk {
                    TModel = "Spark.Tests.Models.Comment"
                },
                new SendLiteralChunk {
                    Text = "Hello world"
                }
            });
            var instance = compiler.CreateInstance();

            Assert.That(instance, Is.InstanceOfType(typeof(StubSparkView2)));
            Assert.That(instance, Is.InstanceOfType(typeof(StubSparkView2 <Comment>)));
        }
コード例 #5
0
        public IList <SparkViewDescriptor> LoadBatchCompilation(Assembly assembly)
        {
            List <SparkViewDescriptor> list = new List <SparkViewDescriptor>();

            foreach (Type type in assembly.GetExportedTypes())
            {
                if (typeof(ISparkView).IsAssignableFrom(type))
                {
                    object[] customAttributes = type.GetCustomAttributes(typeof(SparkViewAttribute), false);
                    if ((customAttributes != null) && (customAttributes.Length != 0))
                    {
                        SparkViewDescriptor item   = ((SparkViewAttribute)customAttributes[0]).BuildDescriptor();
                        CompiledViewEntry   entry2 = new CompiledViewEntry {
                            Descriptor = item,
                            Loader     = new ViewLoader()
                        };
                        CSharpViewCompiler compiler = new CSharpViewCompiler {
                            CompiledType = type
                        };
                        entry2.Compiler  = compiler;
                        entry2.Activator = this.ViewActivatorFactory.Register(type);
                        CompiledViewEntry entry = entry2;
                        this.CompiledViewHolder.Store(entry);
                        list.Add(item);
                    }
                }
            }
            return(list);
        }
コード例 #6
0
        public void ProvideFullException()
        {
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            DoCompileView(compiler, new Chunk[]
            {
                new SendExpressionChunk {
                    Code = "NoSuchVariable"
                }
            });
        }
コード例 #7
0
        public void SimpleOutput()
        {
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            DoCompileView(compiler, new[] { new SendExpressionChunk {
                                                Code = "3 + 4"
                                            } });
            var    instance = compiler.CreateInstance();
            string contents = instance.RenderView();

            Assert.AreEqual("7", contents);
        }
コード例 #8
0
        public void MakeAndCompile()
        {
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            DoCompileView(compiler, new[] { new SendLiteralChunk {
                                                Text = "hello world"
                                            } });

            var    instance = compiler.CreateInstance();
            string contents = instance.RenderView();

            Assert.That(contents.Contains("hello world"));
        }
コード例 #9
0
        public void ProvideFullException()
        {
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            Assert.That(() =>
                        DoCompileView(compiler, new Chunk[]
            {
                new SendExpressionChunk {
                    Code = "NoSuchVariable"
                }
            }),
                        Throws.TypeOf <BatchCompilerException>());
        }
コード例 #10
0
        public void TargetNamespace()
        {
            var compiler = new CSharpViewCompiler
            {
                BaseClass  = "Spark.SparkViewBase",
                Descriptor = new SparkViewDescriptor {
                    TargetNamespace = "Testing.Target.Namespace"
                }
            };

            DoCompileView(compiler, new Chunk[] { new SendLiteralChunk {
                                                      Text = "Hello"
                                                  } });
            var instance = compiler.CreateInstance();

            Assert.AreEqual("Testing.Target.Namespace", instance.GetType().Namespace);
        }
コード例 #11
0
        public virtual ViewCompiler CreateViewCompiler(ISparkViewEngine engine, SparkViewDescriptor descriptor)
        {
            var pageBaseType = engine.Settings.PageBaseType;

            if (string.IsNullOrEmpty(pageBaseType))
            {
                pageBaseType = engine.DefaultPageBaseType;
            }

            var language = descriptor.Language;

            if (language == LanguageType.Default)
            {
                language = engine.Settings.DefaultLanguage;
            }

            ViewCompiler viewCompiler;

            switch (language)
            {
            case LanguageType.Default:
            case LanguageType.CSharp:
                viewCompiler = new CSharpViewCompiler();
                break;

            case LanguageType.VisualBasic:
                viewCompiler = new VisualBasicViewCompiler();
                break;

            case LanguageType.Javascript:
                viewCompiler = new JavascriptViewCompiler();
                break;

            default:
                throw new CompilerException(string.Format("Unknown language type {0}", descriptor.Language));
            }

            viewCompiler.BaseClass     = pageBaseType;
            viewCompiler.Descriptor    = descriptor;
            viewCompiler.Debug         = engine.Settings.Debug;
            viewCompiler.NullBehaviour = engine.Settings.NullBehaviour;
            viewCompiler.UseAssemblies = engine.Settings.UseAssemblies;
            viewCompiler.UseNamespaces = engine.Settings.UseNamespaces;
            return(viewCompiler);
        }
コード例 #12
0
        public void UnsafeLiteralCharacters()
        {
            var text     = "hello\t\r\n\"world";
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            DoCompileView(compiler, new[] { new SendLiteralChunk {
                                                Text = text
                                            } });

            Assert.That(compiler.SourceCode.Contains("Write(\"hello\\t\\r\\n\\\"world\")"));

            var    instance = compiler.CreateInstance();
            string contents = instance.RenderView();

            Assert.AreEqual(text, contents);
        }
コード例 #13
0
        public void LocalVariableDecl()
        {
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            DoCompileView(compiler, new Chunk[]
            {
                new LocalVariableChunk {
                    Name = "i", Value = "5"
                },
                new SendExpressionChunk {
                    Code = "i"
                }
            });
            var    instance = compiler.CreateInstance();
            string contents = instance.RenderView();

            Assert.AreEqual("5", contents);
        }
コード例 #14
0
        public void LenientOutputNullDoesNotCauseWarningCS0168()
        {
            var compiler = new CSharpViewCompiler()
            {
                BaseClass     = "Spark.Tests.Stubs.StubSparkView",
                NullBehaviour = NullBehaviour.Lenient
            };
            var chunks = new Chunk[]
            {
                new ViewDataChunk {
                    Name = "comment", Type = "Spark.Tests.Models.Comment"
                },
                new SendExpressionChunk {
                    Code = "comment.Text", SilentNulls = false
                }
            };

            compiler.CompileView(new[] { chunks }, new[] { chunks });
            Assert.That(compiler.SourceCode.Contains("catch(System.NullReferenceException)"));
        }
コード例 #15
0
        public void PageBaseTypeOverridesBaseClass()
        {
            var compiler = new CSharpViewCompiler()
            {
                BaseClass     = "Spark.Tests.Stubs.StubSparkView",
                NullBehaviour = NullBehaviour.Strict
            };

            DoCompileView(compiler, new Chunk[]
            {
                new PageBaseTypeChunk {
                    BaseClass = "Spark.Tests.Stubs.StubSparkView2"
                },
                new SendLiteralChunk {
                    Text = "Hello world"
                }
            });
            var instance = compiler.CreateInstance();

            Assert.That(instance, Is.InstanceOfType(typeof(StubSparkView2)));
        }
コード例 #16
0
        public void ForEachLoop()
        {
            var compiler = new CSharpViewCompiler {
                BaseClass = "Spark.SparkViewBase"
            };

            DoCompileView(compiler, new Chunk[]
            {
                new LocalVariableChunk {
                    Name = "data", Value = "new[]{3,4,5}"
                },
                new SendLiteralChunk {
                    Text = "<ul>"
                },
                new ForEachChunk
                {
                    Code = "var item in data",
                    Body = new Chunk[]
                    {
                        new SendLiteralChunk {
                            Text = "<li>"
                        },
                        new SendExpressionChunk {
                            Code = "item"
                        },
                        new SendLiteralChunk {
                            Text = "</li>"
                        }
                    }
                },
                new SendLiteralChunk {
                    Text = "</ul>"
                }
            });
            var instance = compiler.CreateInstance();
            var contents = instance.RenderView();

            Assert.AreEqual("<ul><li>3</li><li>4</li><li>5</li></ul>", contents);
        }
コード例 #17
0
        public void StrictNullUsesException()
        {
            var compiler = new CSharpViewCompiler()
            {
                BaseClass     = "Spark.Tests.Stubs.StubSparkView",
                NullBehaviour = NullBehaviour.Strict
            };
            var chunks = new Chunk[]
            {
                new ViewDataChunk {
                    Name = "comment", Type = "Spark.Tests.Models.Comment"
                },
                new SendExpressionChunk {
                    Code = "comment.Text", SilentNulls = false
                }
            };

            compiler.CompileView(new[] { chunks }, new[] { chunks });
            Assert.That(compiler.SourceCode.Contains("catch(System.NullReferenceException ex)"));
            Assert.That(compiler.SourceCode.Contains("ArgumentNullException("));
            Assert.That(compiler.SourceCode.Contains(", ex);"));
        }
コード例 #18
0
        public void PageBaseTypeWorksWithGenericParametersIncluded()
        {
            var compiler = new CSharpViewCompiler()
            {
                BaseClass     = "Spark.Tests.Stubs.StubSparkView",
                NullBehaviour = NullBehaviour.Strict
            };

            DoCompileView(compiler, new Chunk[]
            {
                new PageBaseTypeChunk {
                    BaseClass = "Spark.Tests.Stubs.StubSparkView3<Spark.Tests.Models.Comment, string>"
                },
                new SendLiteralChunk {
                    Text = "Hello world"
                }
            });
            var instance = compiler.CreateInstance();

            Assert.That(instance, Is.InstanceOfType(typeof(StubSparkView2)));
            Assert.That(instance, Is.InstanceOfType(typeof(StubSparkView2 <Comment>)));
            Assert.That(instance, Is.InstanceOfType(typeof(StubSparkView3 <Comment, string>)));
        }