summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/test/System.Web.Razor.Test/Parser/ParserContextTest.cs
blob: d6460c7d70822daece0a84ae48afc7e37ecccbb5 (plain)
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
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.IO;
using System.Web.Razor.Parser;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Resources;
using System.Web.Razor.Test.Framework;
using System.Web.Razor.Text;
using System.Web.Razor.Tokenizer.Symbols;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Web.Razor.Test.Parser
{
    public class ParserContextTest
    {
        [Fact]
        public void ConstructorRequiresNonNullSource()
        {
            var codeParser = new CSharpCodeParser();
            Assert.ThrowsArgumentNull(() => new ParserContext(null, codeParser, new HtmlMarkupParser(), codeParser), "source");
        }

        [Fact]
        public void ConstructorRequiresNonNullCodeParser()
        {
            var codeParser = new CSharpCodeParser();
            Assert.ThrowsArgumentNull(() => new ParserContext(new SeekableTextReader(TextReader.Null), null, new HtmlMarkupParser(), codeParser), "codeParser");
        }

        [Fact]
        public void ConstructorRequiresNonNullMarkupParser()
        {
            var codeParser = new CSharpCodeParser();
            Assert.ThrowsArgumentNull(() => new ParserContext(new SeekableTextReader(TextReader.Null), codeParser, null, codeParser), "markupParser");
        }

        [Fact]
        public void ConstructorRequiresNonNullActiveParser()
        {
            Assert.ThrowsArgumentNull(() => new ParserContext(new SeekableTextReader(TextReader.Null), new CSharpCodeParser(), new HtmlMarkupParser(), null), "activeParser");
        }

        [Fact]
        public void ConstructorThrowsIfActiveParserIsNotCodeOrMarkupParser()
        {
            Assert.ThrowsArgument(() => new ParserContext(new SeekableTextReader(TextReader.Null), new CSharpCodeParser(), new HtmlMarkupParser(), new CSharpCodeParser()),
                                                    "activeParser",
                                                    RazorResources.ActiveParser_Must_Be_Code_Or_Markup_Parser);
        }

        [Fact]
        public void ConstructorAcceptsActiveParserIfIsSameAsEitherCodeOrMarkupParser()
        {
            var codeParser = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();
            new ParserContext(new SeekableTextReader(TextReader.Null), codeParser, markupParser, codeParser);
            new ParserContext(new SeekableTextReader(TextReader.Null), codeParser, markupParser, markupParser);
        }

        [Fact]
        public void ConstructorInitializesProperties()
        {
            // Arrange
            SeekableTextReader expectedBuffer = new SeekableTextReader(TextReader.Null);
            CSharpCodeParser expectedCodeParser = new CSharpCodeParser();
            HtmlMarkupParser expectedMarkupParser = new HtmlMarkupParser();

            // Act
            ParserContext context = new ParserContext(expectedBuffer, expectedCodeParser, expectedMarkupParser, expectedCodeParser);

            // Assert
            Assert.NotNull(context.Source);
            Assert.Same(expectedCodeParser, context.CodeParser);
            Assert.Same(expectedMarkupParser, context.MarkupParser);
            Assert.Same(expectedCodeParser, context.ActiveParser);
        }

        [Fact]
        public void CurrentCharacterReturnsCurrentCharacterInTextBuffer()
        {
            // Arrange
            ParserContext context = SetupTestContext("bar", b => b.Read());

            // Act
            char actual = context.CurrentCharacter;

            // Assert
            Assert.Equal('a', actual);
        }

        [Fact]
        public void CurrentCharacterReturnsNulCharacterIfTextBufferAtEOF()
        {
            // Arrange
            ParserContext context = SetupTestContext("bar", b => b.ReadToEnd());

            // Act
            char actual = context.CurrentCharacter;

            // Assert
            Assert.Equal('\0', actual);
        }

        [Fact]
        public void EndOfFileReturnsFalseIfTextBufferNotAtEOF()
        {
            // Arrange
            ParserContext context = SetupTestContext("bar");

            // Act/Assert
            Assert.False(context.EndOfFile);
        }

        [Fact]
        public void EndOfFileReturnsTrueIfTextBufferAtEOF()
        {
            // Arrange
            ParserContext context = SetupTestContext("bar", b => b.ReadToEnd());

            // Act/Assert
            Assert.True(context.EndOfFile);
        }

        [Fact]
        public void StartBlockCreatesNewBlock()
        {
            // Arrange
            ParserContext context = SetupTestContext("phoo");

            // Act
            context.StartBlock(BlockType.Expression);

            // Assert
            Assert.Equal(1, context.BlockStack.Count);
            Assert.Equal(BlockType.Expression, context.BlockStack.Peek().Type);
        }

        [Fact]
        public void EndBlockAddsCurrentBlockToParentBlock()
        {
            // Arrange
            Mock<ParserVisitor> mockListener = new Mock<ParserVisitor>();
            ParserContext context = SetupTestContext("phoo");

            // Act
            context.StartBlock(BlockType.Expression);
            context.StartBlock(BlockType.Statement);
            context.EndBlock();

            // Assert
            Assert.Equal(1, context.BlockStack.Count);
            Assert.Equal(BlockType.Expression, context.BlockStack.Peek().Type);
            Assert.Equal(1, context.BlockStack.Peek().Children.Count);
            Assert.Equal(BlockType.Statement, ((Block)context.BlockStack.Peek().Children[0]).Type);
        }

        [Fact]
        public void AddSpanAddsSpanToCurrentBlockBuilder()
        {
            // Arrange
            var factory = SpanFactory.CreateCsHtml();
            Mock<ParserVisitor> mockListener = new Mock<ParserVisitor>();
            ParserContext context = SetupTestContext("phoo");

            SpanBuilder builder = new SpanBuilder()
            {
                Kind = SpanKind.Code
            };
            builder.Accept(new CSharpSymbol(1, 0, 1, "foo", CSharpSymbolType.Identifier));
            Span added = builder.Build();

            using (context.StartBlock(BlockType.Functions))
            {
                context.AddSpan(added);
            }

            BlockBuilder expected = new BlockBuilder()
            {
                Type = BlockType.Functions,
            };
            expected.Children.Add(added);

            // Assert
            ParserTestBase.EvaluateResults(context.CompleteParse(), expected.Build());
        }

        [Fact]
        public void SwitchActiveParserSetsMarkupParserAsActiveIfCodeParserCurrentlyActive()
        {
            // Arrange
            var codeParser = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();
            ParserContext context = SetupTestContext("barbazbiz", b => b.Read(), codeParser, markupParser, codeParser);
            Assert.Same(codeParser, context.ActiveParser);

            // Act
            context.SwitchActiveParser();

            // Assert
            Assert.Same(markupParser, context.ActiveParser);
        }

        [Fact]
        public void SwitchActiveParserSetsCodeParserAsActiveIfMarkupParserCurrentlyActive()
        {
            // Arrange
            var codeParser = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();
            ParserContext context = SetupTestContext("barbazbiz", b => b.Read(), codeParser, markupParser, markupParser);
            Assert.Same(markupParser, context.ActiveParser);

            // Act
            context.SwitchActiveParser();

            // Assert
            Assert.Same(codeParser, context.ActiveParser);
        }

        private ParserContext SetupTestContext(string document)
        {
            var codeParser = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();
            return SetupTestContext(document, b => { }, codeParser, markupParser, codeParser);
        }

        private ParserContext SetupTestContext(string document, Action<TextReader> positioningAction)
        {
            var codeParser = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();
            return SetupTestContext(document, positioningAction, codeParser, markupParser, codeParser);
        }

        private ParserContext SetupTestContext(string document, Action<TextReader> positioningAction, ParserBase codeParser, ParserBase markupParser, ParserBase activeParser)
        {
            ParserContext context = new ParserContext(new SeekableTextReader(new StringReader(document)), codeParser, markupParser, activeParser);
            positioningAction(context.Source);
            return context;
        }
    }
}