Exemplo n.º 1
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;
        }
Exemplo n.º 2
0
 public void ForEachAutoVariables()
 {
     var compiler = new VisualBasicViewCompiler { BaseClass = "Spark.AbstractSparkView" };
     DoCompileView(compiler, new Chunk[]
                             {
                                 new LocalVariableChunk {Name = "data", Value = "new Integer(){3,4,5}"},
                                 new SendLiteralChunk {Text = "<ul>"},
                                 new ForEachChunk
                                 {
                                     Code = "item As Integer in data",
                                     Body = new Chunk[]
                                            {
                                                new SendLiteralChunk {Text = "<li>"},
                                                new SendExpressionChunk {Code = "item"},
                                                new SendExpressionChunk {Code = "itemIsFirst"},
                                                new SendExpressionChunk {Code = "itemIsLast"},
                                                new SendExpressionChunk {Code = "itemIndex"},
                                                new SendExpressionChunk {Code = "itemCount"},
                                                new SendLiteralChunk {Text = "</li>"}
                                            }
                                 },
                                 new SendLiteralChunk {Text = "</ul>"}
                             });
     var instance = compiler.CreateInstance();
     var contents = instance.RenderView();
     Assert.AreEqual("<ul><li>3TrueFalse03</li><li>4FalseFalse13</li><li>5FalseTrue23</li></ul>", contents);
 }
 public void ForEachLoop()
 {
     var compiler = new VisualBasicViewCompiler { BaseClass = "Spark.AbstractSparkView" };
     DoCompileView(compiler, new Chunk[]
                             {
                                 new LocalVariableChunk {Name = "data", Value = "new Integer(){3,4,5}"},
                                 new SendLiteralChunk {Text = "<ul>"},
                                 new ForEachChunk
                                 {
                                     Code = "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);
 }
Exemplo n.º 4
0
 public void GlobalVariables()
 {
     var compiler = new VisualBasicViewCompiler { BaseClass = "Spark.AbstractSparkView" };
     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);
 }
Exemplo n.º 5
0
        public void TargetNamespace()
        {
            var compiler = new VisualBasicViewCompiler
            {
                BaseClass = "Spark.AbstractSparkView",
                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);
        }
Exemplo n.º 6
0
        public void StronglyTypedBase()
        {
            var compiler = new VisualBasicViewCompiler { BaseClass = "Spark.Tests.Stubs.StubSparkView" };

            DoCompileView(compiler, new Chunk[]
            {
                new SendLiteralChunk { Text = "hello world" },
                new ViewDataModelChunk { TModel="Global.System.String"}
            });

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

            Assert.That(contents.Contains("hello world"));
        }
Exemplo n.º 7
0
 public void StrictNullUsesException()
 {
     var compiler = new VisualBasicViewCompiler()
                    {
                        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 ex As Global.System.NullReferenceException"));
     Assert.That(compiler.SourceCode.Contains("ArgumentNullException("));
     Assert.That(compiler.SourceCode.Contains(", ex)"));
 }
Exemplo n.º 8
0
 public void SimpleOutput()
 {
     var compiler = new VisualBasicViewCompiler { BaseClass = "Spark.AbstractSparkView" };
     DoCompileView(compiler, new[] { new SendExpressionChunk { Code = "3 + 4" } });
     var instance = compiler.CreateInstance();
     string contents = instance.RenderView();
     Assert.AreEqual("7", contents);
 }
Exemplo n.º 9
0
 public void ProvideFullException()
 {
     var compiler = new VisualBasicViewCompiler { BaseClass = "Spark.AbstractSparkView" };
     Assert.That(() =>
                 DoCompileView(compiler, new Chunk[]
                                             {
                                                 new SendExpressionChunk {Code = "NoSuchVariable"}
                                             }),
                 Throws.TypeOf<BatchCompilerException>());
 }
Exemplo n.º 10
0
 public void PageBaseTypeWorksWithOptionalModel()
 {
     var compiler = new VisualBasicViewCompiler()
                    {
                        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.InstanceOf(typeof(StubSparkView2)));
     Assert.That(instance, Is.InstanceOf(typeof(StubSparkView2<Comment>)));
 }
Exemplo n.º 11
0
 public void PageBaseTypeWorksWithGenericParametersIncluded()
 {
     var compiler = new VisualBasicViewCompiler()
                    {
                        BaseClass = "Spark.Tests.Stubs.StubSparkView",
                        NullBehaviour = NullBehaviour.Strict
                    };
     DoCompileView(compiler, new Chunk[]
                             {
                                 new PageBaseTypeChunk {BaseClass = "Spark.Tests.Stubs.StubSparkView3(Of Spark.Tests.Models.Comment, string)"},
                                 new SendLiteralChunk {Text = "Hello world"}
                             });
     var instance = compiler.CreateInstance();
     Assert.That(instance, Is.InstanceOf(typeof(StubSparkView2)));
     Assert.That(instance, Is.InstanceOf(typeof(StubSparkView2<Comment>)));
     Assert.That(instance, Is.InstanceOf(typeof(StubSparkView3<Comment, string>)));
 }
Exemplo n.º 12
0
 public void PageBaseTypeOverridesBaseClass()
 {
     var compiler = new VisualBasicViewCompiler()
                    {
                        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.InstanceOf(typeof(StubSparkView2)));
 }
Exemplo n.º 13
0
        public void LocalVariableDecl()
        {
            var compiler = new VisualBasicViewCompiler { BaseClass = "Spark.AbstractSparkView" };
            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);
        }
Exemplo n.º 14
0
        public void IfTrueCondition()
        {
            var compiler = new VisualBasicViewCompiler { BaseClass = "Spark.AbstractSparkView" };

            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=5", Body=trueChunks},
                                        new SendLiteralChunk {Text = "</p>"}
                                    });
            var instance = compiler.CreateInstance();
            var contents = instance.RenderView();
            Assert.AreEqual("<p>wastrue</p>", contents);
        }
 public void ProvideFullException()
 {
     var compiler = new VisualBasicViewCompiler { BaseClass = "Spark.AbstractSparkView" };
     DoCompileView(compiler, new Chunk[]
                             {
                                 new SendExpressionChunk {Code = "NoSuchVariable"}
                             });
 }
Exemplo n.º 16
0
        public void TargetNamespace()
        {
            // TODO: Fix on Mono/Linux - ahjohannessen
            var platform = Environment.OSVersion.Platform;
            if(platform == PlatformID.Unix || platform == PlatformID.MacOSX)
            {
                Assert.Ignore();
            }

            var compiler = new VisualBasicViewCompiler
            {
                BaseClass = "Spark.AbstractSparkView",
                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);
        }