~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/UnitTests/MonoDevelop.AspNet.Mvc.StateEngine/RazorStatementsParsingTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ//
 
2
// RazorStatementsParsingTests.cs
 
3
//
 
4
// Author:
 
5
//              Piotr Dowgiallo <sparekd@gmail.com>
 
6
//
 
7
// Copyright (c) 2012 Piotr Dowgiallo
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Linq;
 
30
using System.Text;
 
31
using NUnit.Framework;
 
32
using MonoDevelop.AspNet.Mvc.StateEngine;
 
33
using MonoDevelop.Xml.StateEngine;
 
34
 
 
35
namespace UnitTests.MonoDevelop.AspNet.Mvc.StateEngine
 
36
{
 
37
        public class RazorStatementsParsingTests : RazorParsingTests
 
38
        {
 
39
                [Test ()]
 
40
                public void ForStatement ()
 
41
                {
 
42
                        parser.Parse ("@for(int i = 0; i++; i < length) { foo($); }$", () => {
 
43
                                parser.AssertStateIs<RazorStatementState> ();
 
44
                                parser.AssertPath ("//@for");
 
45
                                parser.AssertNodeIs<RazorStatement> ();
 
46
                        }, () => {
 
47
                                parser.AssertStateIs<RazorFreeState> ();
 
48
                                parser.AssertPath ("/");
 
49
                        });
 
50
                }
 
51
 
 
52
                [Test ()]
 
53
                public void ForeachStatement ()
 
54
                {
 
55
                        parser.Parse ("@foreach (var item in Model) { foo($); }$", () => {
 
56
                                parser.AssertStateIs<RazorStatementState> ();
 
57
                                parser.AssertPath ("//@foreach");
 
58
                                parser.AssertNodeIs<RazorStatement> ();
 
59
                        }, () => {
 
60
                                parser.AssertStateIs<RazorFreeState> ();
 
61
                                parser.AssertPath ("/");
 
62
                        });
 
63
                }
 
64
 
 
65
                [Test ()]
 
66
                public void SwitchStatement ()
 
67
                {
 
68
                        parser.Parse ("@switch (foo) { case bar: $break; }$", () => {
 
69
                                parser.AssertStateIs<RazorStatementState> ();
 
70
                                parser.AssertPath ("//@switch");
 
71
                                parser.AssertNodeIs<RazorStatement> ();
 
72
                        }, () => {
 
73
                                parser.AssertStateIs<RazorFreeState> ();
 
74
                                parser.AssertPath ("/");
 
75
                        });
 
76
                }
 
77
 
 
78
                [Test ()]
 
79
                public void WhileStatement ()
 
80
                {
 
81
                        parser.Parse ("@while (foo !=$ bar) { foo (); }$", () => {
 
82
                                parser.AssertStateIs<RazorStatementState> ();
 
83
                                parser.AssertPath ("//@while");
 
84
                                parser.AssertNodeIs<RazorStatement> ();
 
85
                        }, () => {
 
86
                                parser.AssertStateIs<RazorFreeState> ();
 
87
                                parser.AssertPath ("/");
 
88
                        });
 
89
                }
 
90
 
 
91
                [Test ()]
 
92
                public void LockStatement ()
 
93
                {
 
94
                        parser.Parse ("@lock (foo) { bar()$; }$", () => {
 
95
                                parser.AssertStateIs<RazorStatementState> ();
 
96
                                parser.AssertPath ("//@lock");
 
97
                                parser.AssertNodeIs<RazorStatement> ();
 
98
                        }, () => {
 
99
                                parser.AssertStateIs<RazorFreeState> ();
 
100
                                parser.AssertPath ("/");
 
101
                        });
 
102
                }
 
103
 
 
104
                [Test ()]
 
105
                public void DoStatement ()
 
106
                {
 
107
                        parser.Parse ("@do { bar()$; } while (true)$", () => {
 
108
                                parser.AssertStateIs<RazorStatementState> ();
 
109
                                parser.AssertPath ("//@do");
 
110
                                parser.AssertNodeIs<RazorStatement> ();
 
111
                        }, () => {
 
112
                                parser.AssertStateIs<RazorFreeState> ();
 
113
                                parser.AssertPath ("/");
 
114
                        });
 
115
                }
 
116
 
 
117
                [Test ()]
 
118
                public void UsingStatement ()
 
119
                {
 
120
                        parser.Parse ("@using (resource) { foo ($); }$", () => {
 
121
                                parser.AssertStateIs<RazorStatementState> ();
 
122
                                parser.AssertPath ("//@using");
 
123
                                parser.AssertNodeIs<RazorStatement> ();
 
124
                        }, () => {
 
125
                                parser.AssertStateIs<RazorFreeState> ();
 
126
                                parser.AssertPath ("/");
 
127
                        });
 
128
                }
 
129
 
 
130
                [Test ()]
 
131
                public void IfStatementRemainsInStatementStateIfCanBeContinued ()
 
132
                {
 
133
                        parser.Parse ("@if(true) { $foo(); }$", () => {
 
134
                                parser.AssertStateIs<RazorStatementState> ();
 
135
                                parser.AssertPath ("//@if");
 
136
                                parser.AssertNodeIs<RazorStatement> ();
 
137
                        }, () => {
 
138
                                parser.AssertStateIs<RazorStatementState> ();
 
139
                                parser.AssertPath ("/");
 
140
                                parser.AssertNodeIs<XDocument> (); // if statement is ended
 
141
                        });
 
142
                }
 
143
 
 
144
                [Test ()]
 
145
                public void IfStatementReturnsToParentStateWhenNotContinued ()
 
146
                {
 
147
                        parser.Parse ("@if(true) { $foo(); } bar$", () => {
 
148
                                parser.AssertStateIs<RazorStatementState> ();
 
149
                                parser.AssertPath ("//@if");
 
150
                                parser.AssertNodeIs<RazorStatement> ();
 
151
                        }, () => {
 
152
                                parser.AssertStateIs<RazorFreeState> ();
 
153
                                parser.AssertPath ("/");
 
154
                        });
 
155
                }
 
156
 
 
157
                [Test ()]
 
158
                public void IfStatementCanBeContinuedWithElseIf ()
 
159
                {
 
160
                        parser.Parse ("@if (true) { $foo(); } else if (false) { $foo(); }", () => {
 
161
                                parser.AssertStateIs<RazorStatementState> ();
 
162
                                parser.AssertPath ("//@if");
 
163
                                parser.AssertNodeIs<RazorStatement> ();
 
164
                        }, () => {
 
165
                                parser.AssertStateIs<RazorStatementState> ();
 
166
                                parser.AssertPath ("//@else if");
 
167
                                parser.AssertNodeIs<RazorStatement> ();
 
168
                        });
 
169
                }
 
170
 
 
171
                [Test ()]
 
172
                public void IfStatementCanBeContinuedWithElse ()
 
173
                {
 
174
                        parser.Parse ("@if (true) { $foo(); } else { $foo(); }", () => {
 
175
                                parser.AssertStateIs<RazorStatementState> ();
 
176
                                parser.AssertPath ("//@if");
 
177
                                parser.AssertNodeIs<RazorStatement> ();
 
178
                        }, () => {
 
179
                                parser.AssertStateIs<RazorStatementState> ();
 
180
                                parser.AssertPath ("//@else");
 
181
                                parser.AssertNodeIs<RazorStatement> ();
 
182
                        });
 
183
                }
 
184
 
 
185
                [Test ()]
 
186
                public void ElseIfStatementCanBeContinuedWithElse ()
 
187
                {
 
188
                        parser.Parse ("@if (true) { foo(); } else if (false) { $foo(); } else { bar $(); }", () => {
 
189
                                parser.AssertStateIs<RazorStatementState> ();
 
190
                                parser.AssertPath ("//@else if");
 
191
                                parser.AssertNodeIs<RazorStatement> ();
 
192
                        }, () => {
 
193
                                parser.AssertStateIs<RazorStatementState> ();
 
194
                                parser.AssertPath ("//@else");
 
195
                                parser.AssertNodeIs<RazorStatement> ();
 
196
                        });
 
197
                }
 
198
 
 
199
                [Test ()]
 
200
                public void ElseIfStatementCanBeContinuedWithElseIf ()
 
201
                {
 
202
                        parser.Parse ("@if (true) { foo(); } else if (true) { $foo(); } else if (false) { bar $(); }", () => {
 
203
                                parser.AssertStateIs<RazorStatementState> ();
 
204
                                parser.AssertPath ("//@else if");
 
205
                                parser.AssertNodeIs<RazorStatement> ();
 
206
                        }, () => {
 
207
                                parser.AssertStateIs<RazorStatementState> ();
 
208
                                parser.AssertPath ("//@else if");
 
209
                                parser.AssertNodeIs<RazorStatement> ();
 
210
                        });
 
211
                }
 
212
 
 
213
                [Test ()]
 
214
                public void TryStatementRemainsInStatementStateIfCanBeContinued ()
 
215
                {
 
216
                        parser.Parse ("@try { $foo(); }$", () => {
 
217
                                parser.AssertStateIs<RazorStatementState> ();
 
218
                                parser.AssertPath ("//@try");
 
219
                                parser.AssertNodeIs<RazorStatement> ();
 
220
                        }, () => {
 
221
                                parser.AssertStateIs<RazorStatementState> ();
 
222
                                parser.AssertPath ("/");
 
223
                                parser.AssertNodeIs<XDocument> (); // try statement is ended
 
224
                        });
 
225
                }
 
226
 
 
227
                [Test ()]
 
228
                public void TryStatementReturnsToParentStateWhenNotContinued ()
 
229
                {
 
230
                        parser.Parse ("@try { $foo(); } bar$", () => {
 
231
                                parser.AssertStateIs<RazorStatementState> ();
 
232
                                parser.AssertPath ("//@try");
 
233
                                parser.AssertNodeIs<RazorStatement> ();
 
234
                        }, () => {
 
235
                                parser.AssertStateIs<RazorFreeState> ();
 
236
                                parser.AssertPath ("/");
 
237
                        });
 
238
                }
 
239
 
 
240
                [Test ()]
 
241
                public void TryStatementCanBeContinuedWithCatch ()
 
242
                {
 
243
                        parser.Parse ("@try { $foo(); } catch(Exception e) { $Foo(); }", () => {
 
244
                                parser.AssertStateIs<RazorStatementState> ();
 
245
                                parser.AssertPath ("//@try");
 
246
                                parser.AssertNodeIs<RazorStatement> ();
 
247
                        }, () => {
 
248
                                parser.AssertStateIs<RazorStatementState> ();
 
249
                                parser.AssertPath ("//@catch");
 
250
                                parser.AssertNodeIs<RazorStatement> ();
 
251
                        });
 
252
                }
 
253
 
 
254
                [Test ()]
 
255
                public void TryStatementCanBeContinuedWithFinally ()
 
256
                {
 
257
                        parser.Parse ("@try { $foo(); } finally { $Foo(); }", () => {
 
258
                                parser.AssertStateIs<RazorStatementState> ();
 
259
                                parser.AssertPath ("//@try");
 
260
                                parser.AssertNodeIs<RazorStatement> ();
 
261
                        }, () => {
 
262
                                parser.AssertStateIs<RazorStatementState> ();
 
263
                                parser.AssertPath ("//@finally");
 
264
                                parser.AssertNodeIs<RazorStatement> ();
 
265
                        });
 
266
                }
 
267
 
 
268
                [Test ()]
 
269
                public void CatchStatementCanBeContinuedWithCatchOrFinally ()
 
270
                {
 
271
                        parser.Parse ("@try { foo(); } catch { $Foo(); } catch (Exception e) { $Foo(); } finally { $Foo(); }", () => {
 
272
                                parser.AssertStateIs<RazorStatementState> ();
 
273
                                parser.AssertPath ("//@catch");
 
274
                                parser.AssertNodeIs<RazorStatement> ();
 
275
                        }, () => {
 
276
                                parser.AssertStateIs<RazorStatementState> ();
 
277
                                parser.AssertPath ("//@catch");
 
278
                                parser.AssertNodeIs<RazorStatement> ();
 
279
                        }, () => {
 
280
                                parser.AssertStateIs<RazorStatementState> ();
 
281
                                parser.AssertPath ("//@finally");
 
282
                                parser.AssertNodeIs<RazorStatement> ();
 
283
                        });
 
284
                }
 
285
 
 
286
                [Test ()]
 
287
                public void StatementWithNestedBrackets ()
 
288
                {
 
289
                        parser.Parse ("@foreach (var item in Foo) { { { $ } } $}$", () => {
 
290
                                parser.AssertStateIs<RazorStatementState> ();
 
291
                                parser.AssertPath ("//@foreach");
 
292
                        }, () => {
 
293
                                parser.AssertStateIs<RazorStatementState> ();
 
294
                                parser.AssertPath ("//@foreach");
 
295
                        }, () => {
 
296
                                parser.AssertStateIs<RazorFreeState> ();
 
297
                                parser.AssertPath ("/");
 
298
                        });
 
299
                }
 
300
 
 
301
                [Test ()]
 
302
                public void StatementInHtml ()
 
303
                {
 
304
                        parser.Parse ("<foo><bar>@if (true) { Foo $ bar }</bar></foo>", () => {
 
305
                                parser.AssertStateIs<RazorStatementState> ();
 
306
                                parser.AssertPath ("//foo/bar/@if");
 
307
                        });
 
308
                }
 
309
 
 
310
                [Test ()]
 
311
                public void StatementSupportsHtml ()
 
312
                {
 
313
                        parser.Parse ("@while (true) {<foo><bar>$</bar></foo> }", () => {
 
314
                                parser.AssertStateIs<RazorStatementState> ();
 
315
                                parser.AssertPath ("//@while/foo/bar");
 
316
                        });
 
317
                }
 
318
        }
 
319
}