1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
2 
3 using System.Web.Razor.Generator;
4 using System.Web.Razor.Parser;
5 using System.Web.Razor.Parser.SyntaxTree;
6 using System.Web.Razor.Test.Framework;
7 using System.Web.Razor.Text;
8 using Xunit;
9 
10 namespace System.Web.Razor.Test.Parser.VB
11 {
12     public class VBDirectiveTest : VBHtmlCodeParserTestBase
13     {
14         [Fact]
VB_Code_Directive()15         public void VB_Code_Directive()
16         {
17             ParseBlockTest(@"@Code
18     foo()
19 End Code
20 ' Not part of the block",
21                 new StatementBlock(
22                     Factory.CodeTransition(SyntaxConstants.TransitionString)
23                            .Accepts(AcceptedCharacters.None),
24                     Factory.MetaCode("Code")
25                            .Accepts(AcceptedCharacters.None),
26                     Factory.Code("\r\n    foo()\r\n")
27                            .AsStatement()
28                            .With(new AutoCompleteEditHandler(VBLanguageCharacteristics.Instance.TokenizeString)),
29                     Factory.MetaCode("End Code")
30                            .Accepts(AcceptedCharacters.None)));
31         }
32 
33         [Fact]
VB_Functions_Directive()34         public void VB_Functions_Directive()
35         {
36             ParseBlockTest(@"@Functions
37     Public Function Foo() As String
38         Return ""Foo""
39     End Function
40 
41     Public Sub Bar()
42     End Sub
43 End Functions
44 ' Not part of the block",
45                 new FunctionsBlock(
46                     Factory.CodeTransition(SyntaxConstants.TransitionString)
47                            .Accepts(AcceptedCharacters.None),
48                     Factory.MetaCode("Functions")
49                            .Accepts(AcceptedCharacters.None),
50                     Factory.Code("\r\n    Public Function Foo() As String\r\n        Return \"Foo\"\r\n    End Function\r\n\r\n    Public Sub Bar()\r\n    End Sub\r\n")
51                            .AsFunctionsBody(),
52                     Factory.MetaCode("End Functions")
53                            .Accepts(AcceptedCharacters.None)));
54         }
55 
56         [Fact]
VB_Section_Directive()57         public void VB_Section_Directive()
58         {
59             ParseBlockTest(@"@Section Header
60     <p>Foo</p>
61 End Section",
62                 new SectionBlock(new SectionCodeGenerator("Header"),
63                     Factory.CodeTransition(SyntaxConstants.TransitionString),
64                     Factory.MetaCode(@"Section Header"),
65                     new MarkupBlock(
66                         Factory.Markup("\r\n    <p>Foo</p>\r\n")),
67                     Factory.MetaCode("End Section")
68                            .Accepts(AcceptedCharacters.None)));
69         }
70 
71         [Fact]
SessionStateDirectiveWorks()72         public void SessionStateDirectiveWorks()
73         {
74             ParseBlockTest(@"@SessionState InProc
75 ",
76                 new DirectiveBlock(
77                     Factory.CodeTransition(),
78                     Factory.MetaCode("SessionState ")
79                         .Accepts(AcceptedCharacters.None),
80                     Factory.MetaCode("InProc\r\n")
81                         .Accepts(AcceptedCharacters.None)
82                         .With(new RazorDirectiveAttributeCodeGenerator("SessionState", "InProc"))
83                 )
84             );
85         }
86 
87         [Fact]
SessionStateDirectiveIsCaseInsensitive()88         public void SessionStateDirectiveIsCaseInsensitive()
89         {
90             ParseBlockTest(@"@sessionstate disabled
91 ",
92                 new DirectiveBlock(
93                     Factory.CodeTransition(),
94                     Factory.MetaCode("sessionstate ")
95                         .Accepts(AcceptedCharacters.None),
96                     Factory.MetaCode("disabled\r\n")
97                         .Accepts(AcceptedCharacters.None)
98                         .With(new RazorDirectiveAttributeCodeGenerator("SessionState", "disabled"))
99                 )
100             );
101         }
102 
103         [Fact]
VB_Helper_Directive()104         public void VB_Helper_Directive()
105         {
106             ParseBlockTest(@"@Helper Strong(s as String)
107     s = s.ToUpperCase()
108     @<strong>s</strong>
109 End Helper",
110                 new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Strong(s as String)", 8, 0, 8), headerComplete: true),
111                     Factory.CodeTransition(SyntaxConstants.TransitionString),
112                     Factory.MetaCode("Helper ")
113                            .Accepts(AcceptedCharacters.None),
114                     Factory.Code("Strong(s as String)").Hidden(),
115                     new StatementBlock(
116                         Factory.Code("\r\n    s = s.ToUpperCase()\r\n")
117                                .AsStatement(),
118                         new MarkupBlock(
119                             Factory.Markup("    "),
120                             Factory.MarkupTransition(SyntaxConstants.TransitionString),
121                             Factory.Markup("<strong>s</strong>\r\n")
122                                    .Accepts(AcceptedCharacters.None)),
123                         Factory.EmptyVB()
124                                .AsStatement(),
125                         Factory.MetaCode("End Helper")
126                                .Accepts(AcceptedCharacters.None))));
127         }
128     }
129 }
130