Пример #1
0
        public void FixUpWriteStatementTest()
        {
            var invoke = new CodeMethodInvokeExpression(new CodeArgumentReferenceExpression("__w"), "Write");
            CodeExpressionStatement exprStmt = new CodeExpressionStatement(invoke);

            WebPageSurrogateControlBuilder.FixUpWriteStatement(exprStmt);
            Assert.AreEqual(invoke.Method.MethodName, "WriteLiteral");
            Assert.IsInstanceOfType(invoke.Method.TargetObject, typeof(CodeThisReferenceExpression));

            // @__w.Write case
            var code = " @__w.Write(\"hello\"); ";
            var stmt = new CodeSnippetStatement(code);

            WebPageSurrogateControlBuilder.FixUpWriteStatement(stmt);
            Assert.AreEqual(" WriteLiteral(\"hello\"); ", stmt.Value);

            // __w.Write case
            code = " __w.Write(\"hello\"); ";
            stmt = new CodeSnippetStatement(code);
            WebPageSurrogateControlBuilder.FixUpWriteStatement(stmt);
            Assert.AreEqual(" WriteLiteral(\"hello\"); ", stmt.Value);
        }
Пример #2
0
        public CodeTypeDeclaration FixUpClassTest(Action <CodeTypeDeclaration> fixUpClassMethod)
        {
            var type = new CodeTypeDeclaration();

            // Add some dummy base types which should get removed
            type.BaseTypes.Add("basetype1");
            type.BaseTypes.Add("basetype2");
            type.BaseTypes.Add("basetype3");

            // Add a property which should get retained
            var appInstance = new CodeMemberProperty()
            {
                Name = "ApplicationInstance"
            };
            var returnStatement = new CodeMethodReturnStatement(new CodeCastExpression(typeof(HttpApplication), null));

            appInstance.GetStatements.Add(returnStatement);
            type.Members.Add(appInstance);

            // Add a render method which should get retained but modified
            var renderMethod = new CodeMemberMethod();

            renderMethod.Name = "__Render__control1";

            // Add a code snippet statement that should not be modified
            var stmt1 = new CodeSnippetStatement("MyCode.DoSomething");

            renderMethod.Statements.Add(stmt1);

            // Add a code snippet statement that should be modified
            var code2 = " @__w.Write(\"hello\"); ";
            var stmt2 = new CodeSnippetStatement(code2);

            renderMethod.Statements.Add(stmt2);

            // Add a method invoke statement that should be modified
            var invoke3 = new CodeMethodInvokeExpression(new CodeArgumentReferenceExpression("__w"), "Write");
            CodeExpressionStatement stmt3 = new CodeExpressionStatement(invoke3);

            WebPageSurrogateControlBuilder.FixUpWriteStatement(stmt3);
            renderMethod.Statements.Add(stmt3);

            type.Members.Add(renderMethod);

            // Snippets should get retained
            var snippet1 = "public void Test1() { }";
            var snippet2 = "public void Test2() { }";

            type.Members.Add(new CodeSnippetTypeMember(snippet1));
            type.Members.Add(new CodeSnippetTypeMember(snippet2));

            // Add dummy members which should get removed
            type.Members.Add(new CodeMemberProperty()
            {
                Name = "DummyProperty1"
            });
            type.Members.Add(new CodeMemberProperty()
            {
                Name = "DummyProperty2"
            });
            type.Members.Add(new CodeMemberMethod()
            {
                Name = "DummyMethod1"
            });
            type.Members.Add(new CodeMemberMethod()
            {
                Name = "DummyMethod2"
            });

            // Run the method we are testing
            fixUpClassMethod(type);

            // Basic verification
            Assert.AreEqual(1, type.BaseTypes.Count);
            Assert.AreEqual(4, type.Members.Count);

            // Verify properties
            var properties = type.Members.OfType <CodeMemberProperty>().ToList();

            Assert.AreEqual(1, properties.Count);
            Assert.AreEqual("ApplicationInstance", properties[0].Name);

            // Verify snippets
            var snippets = type.Members.OfType <CodeSnippetTypeMember>().ToList();

            Assert.AreEqual(2, snippets.Count);
            Assert.IsNotNull(snippets.Find(s => s.Text == snippet1));
            Assert.IsNotNull(snippets.Find(s => s.Text == snippet2));

            // Verify methods
            var methods = type.Members.OfType <CodeMemberMethod>().ToList();

            Assert.AreEqual(1, methods.Count);
            Assert.AreEqual("Execute", methods[0].Name);

            // Verify statements in the method
            var statements = methods[0].Statements;

            Assert.AreEqual(4, statements.Count); // The fourth statement is a snippet generated for use as helper.

            // First statement should be unchanged
            Assert.AreEqual(stmt1, statements[0]);

            // Second statement should be fixed to use WriteLiteral
            Assert.IsInstanceOfType(statements[1], typeof(CodeSnippetStatement));
            Assert.AreEqual(" WriteLiteral(\"hello\"); ", ((CodeSnippetStatement)statements[1]).Value);

            // Third statement should be fixed to use WriteLiteral
            Assert.IsInstanceOfType(statements[2], typeof(CodeExpressionStatement));
            var invokeExpr = ((CodeExpressionStatement)statements[2]).Expression;

            Assert.IsInstanceOfType(invokeExpr, typeof(CodeMethodInvokeExpression));
            var invoke = invokeExpr as CodeMethodInvokeExpression;

            Assert.AreEqual(invoke.Method.MethodName, "WriteLiteral");
            Assert.IsInstanceOfType(invoke.Method.TargetObject, typeof(CodeThisReferenceExpression));

            // Fourth statement should be a generated code snippet
            Assert.IsInstanceOfType(statements[3], typeof(CodeSnippetStatement));
            return(type);
        }