File: WebPageSurrogateControlBuilderTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,740 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (552 lines) | stat: -rw-r--r-- 25,462 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WebPages.Resources;
using Microsoft.WebPages.TestUtils;

namespace Microsoft.WebPages.Test {
    /// <summary>
    ///This is a test class for WebPageSurrogateControlBuilderTest and is intended
    ///to contain all WebPageSurrogateControlBuilderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class WebPageSurrogateControlBuilderTest {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for IsAspx
        ///</summary>
        [TestMethod()]
        public void IsAspxTest() {
            Dictionary<string, bool> testCases = new Dictionary<string, bool>() {
                { "test.abc", false },
                { "test", false },
                { "test.aspx", true },
                { "TEST.AspX", true },
                { "TEST.xyzabc.AspX", true},
            };
            foreach (var kvp in testCases) {
                string virtualPath = kvp.Key;
                bool expected = kvp.Value;
                bool actual;
                actual = WebPageSurrogateControlBuilder.IsAspx(virtualPath);
                Assert.AreEqual(expected, actual);
            }
        }

        /// <summary>
        ///A test for IsAspq
        ///</summary>
        [TestMethod()]
        public void IsAspqTest() {
            Dictionary<string, bool> testCases = new Dictionary<string, bool>() {
                { "test.abc", false },
                { "test", false },
                { "test.aspq", true },
                { "TEST.AspQ", true },
                { "TEST.xyzabc.AsPq", true},
            };
            foreach (var kvp in testCases) {
                string virtualPath = kvp.Key;
                bool expected = kvp.Value;
                bool actual;
                actual = WebPageSurrogateControlBuilder.IsAspq(virtualPath);
                Assert.AreEqual(expected, actual);
            }
        }


        /// <summary>
        ///A test for IsAscq
        ///</summary>
        [TestMethod()]
        public void IsAscqTest() {
            Dictionary<string, bool> testCases = new Dictionary<string, bool>() {
                { "test.abc", false },
                { "test", false },
                { "test.ascq", true },
                { "TEST.AscQ", true },
                { "TEST.xyzabc.AsCQ", true},
            };
            foreach (var kvp in testCases) {
                string virtualPath = kvp.Key;
                bool expected = kvp.Value;
                bool actual;
                actual = WebPageSurrogateControlBuilder.IsAscq(virtualPath);
                Assert.AreEqual(expected, actual);
            }
        }

        /// <summary>
        ///A test for AddImports
        ///</summary>
        [TestMethod]
        public void AddImportsTest() {
            CodeCompileUnit ccu = new CodeCompileUnit();
            ccu.Namespaces.Add(new CodeNamespace());
            WebPageSurrogateControlBuilder.AddImports(ccu);
            VerifyDefaultNameSpaces(ccu);

            // Temporarily set the static field to something else
            var originalNamespaces = new List<string>(WebPageSurrogateControlBuilder.Namespaces);
            WebPageSurrogateControlBuilder.Namespaces.Clear();
            WebPageSurrogateControlBuilder.Namespaces.Add("System.ABC");
            ccu = new CodeCompileUnit();
            ccu.Namespaces.Add(new CodeNamespace());
            WebPageSurrogateControlBuilder.AddImports(ccu);
            Assert.AreEqual(1, ccu.Namespaces[0].Imports.Count);
            Assert.AreEqual("System.ABC", ccu.Namespaces[0].Imports[0].Namespace);

            // Restore the static field
            WebPageSurrogateControlBuilder.Namespaces.Clear();
            foreach (var ns in originalNamespaces) {
                WebPageSurrogateControlBuilder.Namespaces.Add(ns);
            }
        }

        [TestMethod]
        public void ProcessGeneratedCodeTest() {
            VerifyProcessGeneratedCode(new WebPageSurrogateControlBuilder());
            VerifyProcessGeneratedCode(new WebUserControlSurrogateControlBuilder());
        }

        public void VerifyProcessGeneratedCode(ControlBuilder builder) {
            CodeCompileUnit ccu = new CodeCompileUnit();
            ccu.Namespaces.Add(new CodeNamespace());
            builder.ProcessGeneratedCode(ccu, null, null, null, null);
            VerifyDefaultNameSpaces(ccu);
        }

        public void VerifyDefaultNameSpaces(CodeCompileUnit ccu) {
            Assert.AreEqual(13, ccu.Namespaces[0].Imports.Count);
            Assert.AreEqual(WebPageSurrogateControlBuilder.Namespaces.Count, ccu.Namespaces[0].Imports.Count);
        }
                
        /// <summary>
        ///A test for GetLanguageAttributeFromText
        ///</summary>
        [TestMethod()]
        public void GetLanguageAttributeFromTextTest() {
            Assert.AreEqual("abcd", WebPageSurrogateControlBuilder.GetLanguageAttributeFromText("<%@  Page  Language=\"  abcd\" %>  csharp c# cs  "));
            Assert.AreEqual("xxx", WebPageSurrogateControlBuilder.GetLanguageAttributeFromText("<%@  Page  lanGUAGE='xxx' %>  csharp c# cs  "));
            Assert.AreEqual("", WebPageSurrogateControlBuilder.GetLanguageAttributeFromText("<%@  Page  languagE='' %>  csharp c# cs  "));
            Assert.AreEqual(null, WebPageSurrogateControlBuilder.GetLanguageAttributeFromText("<%@  Page  hello world %>  csharp c# cs  "));
        }

        /// <summary>
        ///A test for GetLanguageFromText
        ///</summary>
        [TestMethod()]
        public void GetLanguageFromTextPositiveTest() {
            Assert.AreEqual(Language.VisualBasic, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@  Page   %>  csharp c# cs  "));
            Assert.AreEqual(Language.VisualBasic, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  vB    '   %>  csharp c# cs  "));
            Assert.AreEqual(Language.VisualBasic, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language=\"  vb    \"   %>  csharp c# cs  "));
            Assert.AreEqual(Language.VisualBasic, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  Vb    '   %>  csharp c# cs  "));
            Assert.AreEqual(Language.VisualBasic, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  vB    '   %>  csharp c# cs  "));
            Assert.AreEqual(Language.VisualBasic, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  vBs    '   %>  csharp c# cs  "));
            Assert.AreEqual(Language.VisualBasic, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  vBscript    '   %>  csharp c# cs  "));
            Assert.AreEqual(Language.VisualBasic, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  vBscript    '   %>  csharp c# cs  "));
            Assert.AreEqual(Language.VisualBasic, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  vIsuaLBasic    '   %>  csharp c# cs  "));

            Assert.AreEqual(Language.CSharp, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  c#    '   %>  vb vbs visualbasic vbscript  "));
            Assert.AreEqual(Language.CSharp, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  cS    '   %>  vb vbs visualbasic vbscript  "));
            Assert.AreEqual(Language.CSharp, WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  cShaRP    '   %>  vb vbs visualbasic vbscript  "));
        }

        /// <summary>
        ///A test for GetLanguageFromText
        ///</summary>
        [TestMethod()]
        public void GetLanguageFromTextNegativeTest() {
            ExceptionAssert.Throws<HttpException>(() =>
                WebPageSurrogateControlBuilder.GetLanguageFromText("<%@   Page   Language='  zxc'   %>  vb vbs visualbasic vbscript  "),
                String.Format(WebPageResources.WebPage_InvalidLanguage, "zxc"));
        }

        /// <summary>
        ///A test for FixUpWriteSnippetStatement
        ///</summary>
        [TestMethod()]
        public void FixUpWriteSnippetStatementTest() {
            var code = " abc zyx";
            var stmt = new CodeSnippetStatement(code);
            WebPageSurrogateControlBuilder.FixUpWriteSnippetStatement(stmt);
            Assert.AreEqual(code, stmt.Value);

            code = " this.Write(\"hello\"); ";
            stmt = new CodeSnippetStatement(code);
            WebPageSurrogateControlBuilder.FixUpWriteSnippetStatement(stmt);
            Assert.AreEqual(code, stmt.Value);

            // @__w.Write case
            code = " @__w.Write(\"hello\"); ";
            stmt = new CodeSnippetStatement(code);
            WebPageSurrogateControlBuilder.FixUpWriteSnippetStatement(stmt);
            Assert.AreEqual(" WriteLiteral(\"hello\"); ", stmt.Value);

            // __w.Write case
            code = " __w.Write(\"hello\"); ";
            stmt = new CodeSnippetStatement(code);
            WebPageSurrogateControlBuilder.FixUpWriteSnippetStatement(stmt);
            Assert.AreEqual(" WriteLiteral(\"hello\"); ", stmt.Value);
        }

        /// <summary>
        ///A test for FixUpWriteCodeExpressionStatement
        ///</summary>
        [TestMethod()]
        public void FixUpWriteCodeExpressionStatementTest() {
            // Null test
            WebPageSurrogateControlBuilder.FixUpWriteCodeExpressionStatement(null);

            // Should fix up the statement
            var invoke = new CodeMethodInvokeExpression(new CodeArgumentReferenceExpression("__w"), "Write");
            CodeExpressionStatement exprStmt = new CodeExpressionStatement(invoke);
            WebPageSurrogateControlBuilder.FixUpWriteCodeExpressionStatement(exprStmt);
            Assert.AreEqual(invoke.Method.MethodName, "WriteLiteral");
            Assert.AreEqual(invoke.Method.TargetObject.GetType(), typeof(CodeThisReferenceExpression));

            // Should NOT fix up the statement
            invoke = new CodeMethodInvokeExpression(new CodeArgumentReferenceExpression("xyz"), "Write");
            exprStmt = new CodeExpressionStatement(invoke);
            WebPageSurrogateControlBuilder.FixUpWriteCodeExpressionStatement(exprStmt);
            Assert.AreEqual(invoke.Method.MethodName, "Write");
            Assert.IsInstanceOfType(invoke.Method.TargetObject, typeof(CodeArgumentReferenceExpression));
        }

        /// <summary>
        ///A test for FixUpWriteStatement
        ///</summary>
        [TestMethod()]
        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);
        }

        [TestMethod]
        public void FixUpClassNull() {
            WebPageSurrogateControlBuilder.FixUpClass(null, null);
        }

        [TestMethod]
        public void OnCodeGenerationCompleteTest() {
            Utils.RunInSeparateAppDomain(() => {
                var vpath = "/WebSite1/index.aspq";
                var contents = "<%@ Page Language=C# %>";
                Utils.SetupVirtualPathInAppDomain(vpath, contents);
                new WebPageSurrogateControlBuilderTest().FixUpClassTest(type => {
                    var ccu = new CodeCompileUnit();
                    ccu.Namespaces.Add(new CodeNamespace());

                    var builder = new MockControlBuilder() { VPath = vpath };
                    builder.ProcessGeneratedCode(ccu, null, type, null, null);
                    builder.CallOnCodeGenerationComplete();
                });
            });
        }

        [TestMethod]
        public void OnCodeGenerationCompleteControlBuilderTest() {
            Utils.RunInSeparateAppDomain(() => {
                var vpath = "/WebSite1/index.ascq";
                var contents = "<%@ Page Language=C# %>";
                Utils.SetupVirtualPathInAppDomain(vpath, contents);
                new WebPageSurrogateControlBuilderTest().FixUpClassTest(type => {
                    var ccu = new CodeCompileUnit();
                    ccu.Namespaces.Add(new CodeNamespace());

                    var builder = new MockUserControlBuilder() { VPath = vpath };
                    builder.ProcessGeneratedCode(ccu, null, type, null, null);
                    builder.CallOnCodeGenerationComplete();
                });
            });
        }

        public class MockControlBuilder : WebPageSurrogateControlBuilder {
            public string VPath { get; set; }
            public void CallOnCodeGenerationComplete() {
                base.OnCodeGenerationComplete();
            }

            internal override string GetPageVirtualPath() {
                return VPath;
            }
        }

        public class MockUserControlBuilder : WebUserControlSurrogateControlBuilder {
            public string VPath { get; set; }
            public void CallOnCodeGenerationComplete() {
                base.OnCodeGenerationComplete();
            }

            internal override string GetPageVirtualPath() {
                return VPath;
            }
        }

        [TestMethod]
        public void FixUpClassVirtualPathTest() {
            Utils.RunInSeparateAppDomain(() => {
                var vpath = "/WebSite1/index.aspq";
                var contents = "<%@ Page Language=C# %>";
                Utils.SetupVirtualPathInAppDomain(vpath, contents);

                new WebPageSurrogateControlBuilderTest().FixUpClassTest(type =>
                    WebPageSurrogateControlBuilder.FixUpClass(type, vpath));
            });
        }

        /// <summary>
        ///A test for FixUpClass
        ///</summary>
        [TestMethod()]
        public void FixUpClassLanguageTest() {
            FixUpClassTest(type =>
                WebPageSurrogateControlBuilder.FixUpClass(type, Language.CSharp));
        }

        [TestMethod]
        public void FixUpClassDefaultApplicationBaseTypeTest() {
            Utils.RunInSeparateAppDomain(() => {
                var baseTypeField = typeof(PageParser).GetField("s_defaultApplicationBaseType", BindingFlags.Static | BindingFlags.NonPublic);
                baseTypeField.SetValue(null, typeof(WebPageHttpApplication));
                var pageType = new WebPageSurrogateControlBuilderTest().FixUpClassTest(type =>
                    WebPageSurrogateControlBuilder.FixUpClass(type, Language.CSharp));
                var properties = pageType.Members.OfType<CodeMemberProperty>().ToList();
                var prop = properties[0];
                Assert.AreEqual(typeof(WebPageHttpApplication).FullName, prop.Type.BaseType);
            });
        }

        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;
        }

        [TestMethod]
        public void FixUpAspxClassUserControlTest() {
            CodeTypeDeclaration type;
            CodeCastExpression cast;
            GetAspxClass(out type, out cast, typeof(WebUserControlSurrogate));
            WebPageSurrogateControlBuilder.FixUpAspxClass(type, "/test/test.ascx");
            Assert.AreEqual(typeof(UserControl).FullName, cast.TargetType.BaseType);
        }

        [TestMethod]
        public void FixUpClassUserControlTest() {
            CodeTypeDeclaration type;
            CodeCastExpression cast;
            GetAspxClass(out type, out cast, typeof(WebUserControlSurrogate));
            WebPageSurrogateControlBuilder.FixUpClass(type, "/test/test.ascx");
            Assert.AreEqual(typeof(UserControl).FullName, cast.TargetType.BaseType);
        }

        private static void GetAspxClass(out CodeTypeDeclaration type, out CodeCastExpression cast, Type surrogateType) {
            type = new CodeTypeDeclaration();
            type.BaseTypes.Add(new CodeTypeReference(surrogateType));
            var ctor = new CodeConstructor();
            cast = new CodeCastExpression();
            cast.TargetType = new CodeTypeReference(surrogateType);
            var prop = new CodePropertyReferenceExpression();
            prop.TargetObject = cast;
            var assign = new CodeAssignStatement();
            assign.Left = prop;
            ctor.Statements.Add(assign);
            type.Members.Add(ctor);
        }

        [TestMethod]
        public void ProcessScriptBlocksCSTest() {
            var pageType = new CodeTypeDeclaration("MyControl");
            var snippet = new CodeSnippetTypeMember("public int foo;");
            pageType.Members.Add(snippet);
            var renderMethod = new CodeMemberMethod();
            WebPageSurrogateControlBuilder.ProcessScriptBlocks(pageType, renderMethod, Language.CSharp);
            var snippets = renderMethod.Statements.OfType<CodeSnippetStatement>().ToList();
            Assert.AreEqual(1, snippets.Count);
            var snip = snippets[0];
            Assert.IsTrue(snip.Value.Contains("public static class MyControlExtensions"));
            Assert.IsTrue(snip.Value.Contains("public static HelperResult MyControl(this System.Web.Mvc.HtmlHelper htmlHelper, int foo = default(int))"));
            Assert.IsTrue(snip.Value.Contains("uc.foo = foo;"));
        }

        [TestMethod]
        public void ProcessScriptBlocksVBTest() {
            var pageType = new CodeTypeDeclaration("MyControl");
            var snippet = new CodeSnippetTypeMember("public foo as int");
            pageType.Members.Add(snippet);
            var renderMethod = new CodeMemberMethod();
            WebPageSurrogateControlBuilder.ProcessScriptBlocks(pageType, renderMethod, Language.VisualBasic);
            var snippets = renderMethod.Statements.OfType<CodeSnippetStatement>().ToList();
            Assert.AreEqual(1, snippets.Count);
            var snip = snippets[0];
            Assert.IsTrue(snip.Value.Contains("Public Module MyControlExtensions"));
            Assert.IsTrue(snip.Value.Contains("Public Function MyControl(htmlHelper As System.Web.Mvc.HtmlHelper, optional foo as int = Nothing) As HelperResult"));
            Assert.IsTrue(snip.Value.Contains("uc.foo = foo"));
        }

        [TestMethod]
        public void HasAspCodeTest() {
            Assert.IsTrue(new WebPageSurrogateControlBuilder().HasAspCode);
            Assert.IsTrue(new WebUserControlSurrogateControlBuilder().HasAspCode);
        }
    }

}