summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/test/System.Web.Razor.Test/Text/LookaheadTextReaderTestBase.cs
blob: f3f934e81534648a9044afea543cf2a4ba0bc4fd (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
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Text;
using System.Web.Razor.Resources;
using System.Web.Razor.Text;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Web.Razor.Test.Text
{
    public abstract class LookaheadTextReaderTestBase
    {
        protected abstract LookaheadTextReader CreateReader(string testString);

        protected void RunPeekTest(string input, int peekAt = 0)
        {
            RunPeekOrReadTest(input, peekAt, false);
        }

        protected void RunReadTest(string input, int readAt = 0)
        {
            RunPeekOrReadTest(input, readAt, true);
        }

        protected void RunSourceLocationTest(string input, SourceLocation expected, int checkAt = 0)
        {
            RunSourceLocationTest(input, expected, r => AdvanceReader(checkAt, r));
        }

        protected void RunSourceLocationTest(string input, SourceLocation expected, Action<LookaheadTextReader> readerAction)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader(input);
            readerAction(reader);

            // Act
            SourceLocation actual = reader.CurrentLocation;

            // Assert
            Assert.Equal(expected, actual);
        }

        protected void RunEndLookaheadUpdatesSourceLocationTest()
        {
            SourceLocation? expectedLocation = null;
            SourceLocation? actualLocation = null;

            RunLookaheadTest("abc\r\ndef\r\nghi", null,
                             Read(6),
                             CaptureSourceLocation(s => expectedLocation = s),
                             Lookahead(Read(6)),
                             CaptureSourceLocation(s => actualLocation = s));
            // Assert
            Assert.Equal(expectedLocation.Value.AbsoluteIndex, actualLocation.Value.AbsoluteIndex);
            Assert.Equal(expectedLocation.Value.CharacterIndex, actualLocation.Value.CharacterIndex);
            Assert.Equal(expectedLocation.Value.LineIndex, actualLocation.Value.LineIndex);
        }

        protected void RunReadToEndTest()
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            // Act
            string str = reader.ReadToEnd();

            // Assert
            Assert.Equal("abcdefg", str);
        }

        protected void RunCancelBacktrackOutsideLookaheadTest()
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            // Act and Assert
            Assert.Throws<InvalidOperationException>(() => reader.CancelBacktrack(), RazorResources.CancelBacktrack_Must_Be_Called_Within_Lookahead);
        }

        protected Action<StringBuilder, LookaheadTextReader> CaptureSourceLocation(Action<SourceLocation> capture)
        {
            return (_, reader) => { capture(reader.CurrentLocation); };
        }

        protected Action<StringBuilder, LookaheadTextReader> Read(int count)
        {
            return (builder, reader) =>
            {
                for (int i = 0; i < count; i++)
                {
                    Read(builder, reader);
                }
            };
        }

        protected void Read(StringBuilder builder, LookaheadTextReader reader)
        {
            builder.Append((char)reader.Read());
        }

        protected void ReadToEnd(StringBuilder builder, LookaheadTextReader reader)
        {
            builder.Append(reader.ReadToEnd());
        }

        protected void CancelBacktrack(StringBuilder builder, LookaheadTextReader reader)
        {
            reader.CancelBacktrack();
        }

        protected Action<StringBuilder, LookaheadTextReader> Lookahead(params Action<StringBuilder, LookaheadTextReader>[] readerCommands)
        {
            return (builder, reader) =>
            {
                using (reader.BeginLookahead())
                {
                    RunAll(readerCommands, builder, reader);
                }
            };
        }

        protected void RunLookaheadTest(string input, string expected, params Action<StringBuilder, LookaheadTextReader>[] readerCommands)
        {
            // Arrange
            StringBuilder builder = new StringBuilder();
            using (LookaheadTextReader reader = CreateReader(input))
            {
                RunAll(readerCommands, builder, reader);
            }

            if (expected != null)
            {
                Assert.Equal(expected, builder.ToString());
            }
        }

        protected void RunReadUntilTest(Func<LookaheadTextReader, string> readMethod, int expectedRaw, int expectedChar, int expectedLine)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("a\r\nbcd\r\nefg");

            reader.Read(); // Reader: "\r\nbcd\r\nefg"
            reader.Read(); // Reader: "\nbcd\r\nefg"
            reader.Read(); // Reader: "bcd\r\nefg"

            // Act
            string read = null;
            SourceLocation actualLocation;
            using (reader.BeginLookahead())
            {
                read = readMethod(reader);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.Equal(3, reader.CurrentLocation.AbsoluteIndex);
            Assert.Equal(0, reader.CurrentLocation.CharacterIndex);
            Assert.Equal(1, reader.CurrentLocation.LineIndex);
            Assert.Equal(expectedRaw, actualLocation.AbsoluteIndex);
            Assert.Equal(expectedChar, actualLocation.CharacterIndex);
            Assert.Equal(expectedLine, actualLocation.LineIndex);
            Assert.Equal('b', reader.Peek());
            Assert.Equal(read, readMethod(reader));
        }

        protected void RunBufferReadTest(Func<LookaheadTextReader, char[], int, int, int> readMethod)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            reader.Read(); // Reader: "bcdefg"

            // Act
            char[] buffer = new char[4];
            int read = -1;
            SourceLocation actualLocation;
            using (reader.BeginLookahead())
            {
                read = readMethod(reader, buffer, 0, 4);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.Equal("bcde", new String(buffer));
            Assert.Equal(4, read);
            Assert.Equal(5, actualLocation.AbsoluteIndex);
            Assert.Equal(5, actualLocation.CharacterIndex);
            Assert.Equal(0, actualLocation.LineIndex);
            Assert.Equal(1, reader.CurrentLocation.CharacterIndex);
            Assert.Equal(0, reader.CurrentLocation.LineIndex);
            Assert.Equal('b', reader.Peek());
        }

        private static void RunAll(Action<StringBuilder, LookaheadTextReader>[] readerCommands, StringBuilder builder, LookaheadTextReader reader)
        {
            foreach (Action<StringBuilder, LookaheadTextReader> readerCommand in readerCommands)
            {
                readerCommand(builder, reader);
            }
        }

        private void RunPeekOrReadTest(string input, int offset, bool isRead)
        {
            using (LookaheadTextReader reader = CreateReader(input))
            {
                AdvanceReader(offset, reader);

                // Act
                int? actual = null;
                if (isRead)
                {
                    actual = reader.Read();
                }
                else
                {
                    actual = reader.Peek();
                }

                Assert.NotNull(actual);

                // Asserts
                AssertReaderValueCorrect(actual.Value, input, offset, "Peek");

                if (isRead)
                {
                    AssertReaderValueCorrect(reader.Peek(), input, offset + 1, "Read");
                }
                else
                {
                    Assert.Equal(actual, reader.Peek());
                }
            }
        }

        private static void AdvanceReader(int offset, LookaheadTextReader reader)
        {
            for (int i = 0; i < offset; i++)
            {
                reader.Read();
            }
        }

        private void AssertReaderValueCorrect(int actual, string input, int expectedOffset, string methodName)
        {
            if (expectedOffset < input.Length)
            {
                Assert.Equal(input[expectedOffset], actual);
            }
            else
            {
                Assert.Equal(-1, actual);
            }
        }
    }
}