/
PythonViewCompilerTests.cs
604 lines (518 loc) · 23.8 KB
/
PythonViewCompilerTests.cs
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// Copyright 2008-2009 Louis DeJardin - http://whereslou.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using Spark.Compiler;
using Spark.Parser;
using Spark.Python.Compiler;
using Spark.Tests.Models;
using Spark.Tests.Stubs;
namespace Spark.Python.Tests
{
[TestFixture]
public class PythonViewCompilerTests
{
private PythonViewCompiler _compiler;
private PythonLanguageFactory _languageFactory;
[SetUp]
public void Init()
{
_compiler = new PythonViewCompiler
{
BaseClass = typeof(StubSparkView).FullName
};
_languageFactory = new PythonLanguageFactory();
// Load up assemblies
IronPython.Hosting.Python.CreateEngine();
}
static IList<IList<Chunk>> Chunks(params Chunk[] chunks)
{
return new[] { (IList<Chunk>)chunks };
}
private string ExecuteView()
{
return ExecuteView(new StubViewData());
}
private string ExecuteView(StubViewData viewData)
{
var view = FastActivator<StubSparkView>.New(_compiler.CompiledType);
_languageFactory.InstanceCreated(_compiler, view);
view.ViewData = viewData;
var contents = new StringWriter();
view.RenderView(contents);
_languageFactory.InstanceReleased(_compiler, view);
return contents.ToString();
}
[Test]
public void CreatedViewHasScriptProperty()
{
var chunks = Chunks(new SendLiteralChunk { Text = "Hello" });
_compiler.CompileView(chunks, chunks);
var view = FastActivator<IScriptingSparkView>.New(_compiler.CompiledType);
var source = _compiler.SourceCode;
var script = view.ScriptSource;
Assert.IsNotNull(source);
Assert.IsNotEmpty(source);
Assert.IsNotNull(script);
Assert.IsNotEmpty(script);
}
[Test]
public void CodeInheritsBaseClass()
{
var chunks = Chunks();
_compiler.BaseClass = "ThisIsTheBaseClass";
_compiler.GenerateSourceCode(chunks, chunks);
Assert.That(_compiler.SourceCode.Contains(": ThisIsTheBaseClass"));
}
[Test]
public void CodeInheritsBaseClassWithTModel()
{
var chunks = Chunks(new ViewDataModelChunk { TModel = "ThisIsTheModelClass" });
_compiler.BaseClass = "ThisIsTheBaseClass";
_compiler.GenerateSourceCode(chunks, chunks);
Assert.That(_compiler.SourceCode.Contains(": ThisIsTheBaseClass<ThisIsTheModelClass>"));
}
[Test]
public void LayeredTemplates()
{
var chunks0 = Chunks(new SendLiteralChunk { Text = "2" });
var chunks1 = Chunks(
new SendLiteralChunk { Text = "4" },
new UseContentChunk { Name = "view" },
new SendLiteralChunk { Text = "0" });
var chunks = new[] { chunks0[0], chunks1[0] };
_compiler.CompileView(chunks, chunks);
var content = ExecuteView();
Assert.AreEqual("420", content);
}
[/*Test,*/ Ignore("Not really sure if namespaces play a role in a dlr based spark view")]
public void UsingNamespaces()
{
var chunks = Chunks(new UseNamespaceChunk { Namespace = "AnotherNamespace.ToBe.Used" });
_compiler.GenerateSourceCode(chunks, chunks);
Assert.That(_compiler.SourceCode.Contains("Imports AnotherNamespace.ToBe.Used"));
}
[Test]
public void TargetNamespace()
{
var chunks = Chunks(new SendLiteralChunk { Text = "blah" });
_compiler.Descriptor = new SparkViewDescriptor { TargetNamespace = "TargetNamespace.ForThe.GeneratedView" };
_compiler.GenerateSourceCode(chunks, chunks);
Assert.That(_compiler.SourceCode.Contains("namespace TargetNamespace.ForThe.GeneratedView"));
}
[/*Test,*/ Ignore("Not really sure if namespaces play a role in a dlr based spark view")]
public void TargetNamespaceWithUsingNamespaces()
{
var chunks = Chunks(new UseNamespaceChunk { Namespace = "AnotherNamespace.ToBe.Used" });
_compiler.Descriptor = new SparkViewDescriptor { TargetNamespace = "TargetNamespace.ForThe.GeneratedView" };
_compiler.GenerateSourceCode(chunks, chunks);
Assert.That(_compiler.SourceCode.Contains("Namespace TargetNamespace.ForThe.GeneratedView"));
Assert.That(_compiler.SourceCode.Contains("Imports AnotherNamespace.ToBe.Used"));
}
[Test]
public void ViewDescriptorAttribute()
{
var chunks = Chunks();
_compiler.Descriptor = new SparkViewDescriptor { TargetNamespace = "TargetNamespace.ForThe.GeneratedView", Templates = new[] { "one", "two", "three" } };
_compiler.GenerateSourceCode(chunks, chunks);
Assert.That(_compiler.SourceCode.Contains("SparkViewAttribute"));
Assert.That(_compiler.SourceCode.Contains("one"));
Assert.That(_compiler.SourceCode.Contains("two"));
Assert.That(_compiler.SourceCode.Contains("three"));
}
[Test]
public void ContainsGeneratedViewIdProperty()
{
var chunks = Chunks();
_compiler.GenerateSourceCode(chunks, chunks);
Assert.That(_compiler.SourceCode.Contains("GeneratedViewId"));
Assert.That(_compiler.SourceCode.Contains("\"" + _compiler.GeneratedViewId + "\""));
}
[Test]
public void SendingLiteralOutput()
{
var chunks = Chunks(new SendLiteralChunk { Text = "Hello World" });
_compiler.GenerateSourceCode(chunks, chunks);
Assert.That(_compiler.SourceCode.Contains("Hello World"));
}
[Test]
public void SendingIndentedExpressionOutput()
{
var chunks = Chunks(new SendExpressionChunk { Code = "5 + 3", Position = new Position(null, 100, 0, 50, 3, null) });
_compiler.GenerateSourceCode(chunks, chunks);
Assert.That(_compiler.SourceCode.Contains("5 + 3"));
}
[Test]
public void RenderingSimpleView()
{
var chunks = Chunks(new SendLiteralChunk { Text = "Hello World" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("Hello World", contents);
}
[Test]
public void CompilingSimpleView()
{
var chunks = Chunks(new SendLiteralChunk { Text = "Hello World" });
_compiler.CompileView(chunks, chunks);
Assert.That(typeof(StubSparkView).IsAssignableFrom(_compiler.CompiledType));
}
[Test]
public void SettingLocalVariable()
{
var chunks = Chunks(
new LocalVariableChunk { Name = "x", Value = "4" },
new SendExpressionChunk { Code = "x" },
new AssignVariableChunk { Name = "x", Value = "2" },
new SendExpressionChunk { Code = "x" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("42", contents);
}
[Test]
public void SettingGlobalVariable()
{
var chunks = Chunks(
new GlobalVariableChunk { Type = "int", Name = "x", Value = "4" },
new SendExpressionChunk { Code = "x" },
new AssignVariableChunk { Name = "x", Value = "2" },
new SendExpressionChunk { Code = "x" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("42", contents);
}
[Test]
public void UsingViewData()
{
var chunks = Chunks(
new SendExpressionChunk { Code = "hello" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView(new StubViewData { { "hello", 42 } });
Assert.AreEqual("42", contents);
}
[/*Test,*/ Ignore("Not really sure if namespaces play a role in a dlr based spark view")]
public void UsingViewDataDefault()
{
var chunks = Chunks(
new ViewDataChunk { Type = "int", Name = "HelloNumber", Key = "hello", Default = "55" },
new SendExpressionChunk { Code = "HelloNumber" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView(new StubViewData { { "hello", 42 } });
Assert.AreEqual("42", contents);
var contents2 = ExecuteView();
Assert.AreEqual("55", contents2);
}
[Test]
public void InlineCodeStatements()
{
//TODO: null protection. silent nulls.
var chunks = Chunks(
new CodeStatementChunk { Code = "x = 20" },
new SendExpressionChunk { Code = "x + 22" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual(contents, "42");
}
[Test]
public void ScopeTest()
{
var scope1 = new ScopeChunk();
scope1.Body.Add(new LocalVariableChunk { Name = "x", Value = "4" });
scope1.Body.Add(new SendExpressionChunk { Code = "x" });
var scope2 = new ScopeChunk();
scope2.Body.Add(new LocalVariableChunk { Name = "x", Value = "2" });
scope2.Body.Add(new SendExpressionChunk { Code = "x" });
var chunks = Chunks(scope1, scope2);
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual(contents, "42");
}
[Test]
public void ForEachLoopOverArray()
{
var loop = new ForEachChunk { Code = "number in numbers" };
loop.Body.Add(new SendExpressionChunk { Code = "number" });
var chunks = Chunks(
loop);
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView(new StubViewData { { "numbers", new[] { 1, 2, 3, 4, 5 } } });
Assert.AreEqual("12345", contents);
}
[Test]
public void MacroAddsFunction()
{
var macro = new MacroChunk
{
Name = "foo",
Parameters = new[] { new MacroParameter { Name = "x", Type = "string" } }
};
macro.Body.Add(new SendExpressionChunk { Code = "x" });
var chunks = Chunks(
new SendExpressionChunk { Code = "foo(\"hello\")" },
macro);
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("hello", contents);
}
[Test]
public void ConditionalChunkControlsExecution()
{
var condition1 = new ConditionalChunk { Condition = "x != 12" };
condition1.Body.Add(new SendLiteralChunk { Text = "fail" });
var condition2 = new ConditionalChunk { Condition = "x == 12" };
condition2.Body.Add(new SendLiteralChunk { Text = "ok1" });
var chunks = Chunks(
new LocalVariableChunk { Name = "x", Value = "12" },
new SendLiteralChunk { Text = "a" },
condition1,
condition2,
new SendLiteralChunk { Text = "b" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("aok1b", contents);
}
[Test]
public void ElseBlockFollowsIf()
{
var condition1 = new ConditionalChunk { Condition = "x != 12" };
condition1.Body.Add(new SendLiteralChunk { Text = "fail" });
var else1 = new ConditionalChunk { Type = ConditionalType.Else };
else1.Body.Add(new SendLiteralChunk { Text = "ok1" });
var condition2 = new ConditionalChunk { Condition = "x == 12" };
condition2.Body.Add(new SendLiteralChunk { Text = "ok2" });
var else2 = new ConditionalChunk { Type = ConditionalType.Else };
else2.Body.Add(new SendLiteralChunk { Text = "fail" });
var chunks = Chunks(
new LocalVariableChunk { Name = "x", Value = "12" },
new SendLiteralChunk { Text = "a" },
condition1,
else1,
condition2,
else2,
new SendLiteralChunk { Text = "b" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("aok1ok2b", contents);
}
[Test]
public void ConditionalChunkUnlessNegates()
{
var condition1 = new ConditionalChunk { Condition = "x != 12", Type = ConditionalType.Unless };
condition1.Body.Add(new SendLiteralChunk { Text = "ok1" });
var condition2 = new ConditionalChunk { Condition = "x == 12", Type = ConditionalType.Unless };
condition2.Body.Add(new SendLiteralChunk { Text = "fail" });
var chunks = Chunks(
new LocalVariableChunk { Name = "x", Value = "12" },
new SendLiteralChunk { Text = "a" },
condition1,
condition2,
new SendLiteralChunk { Text = "b" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("aok1b", contents);
}
[Test]
public void ChainingElseIfNestsProperly()
{
var condition1 = new ConditionalChunk { Type = ConditionalType.If, Condition = "x == 1" };
condition1.Body.Add(new SendLiteralChunk { Text = "a" });
var condition2 = new ConditionalChunk { Type = ConditionalType.ElseIf, Condition = "x == 2" };
condition2.Body.Add(new SendLiteralChunk { Text = "b" });
var condition3 = new ConditionalChunk { Type = ConditionalType.ElseIf, Condition = "x == 3" };
condition3.Body.Add(new SendLiteralChunk { Text = "c" });
var condition4 = new ConditionalChunk { Type = ConditionalType.Else };
condition4.Body.Add(new SendLiteralChunk { Text = "d" });
var loop = new ForEachChunk { Code = "x in numbers" };
loop.Body.Add(condition1);
loop.Body.Add(condition2);
loop.Body.Add(condition3);
loop.Body.Add(condition4);
var chunks = Chunks(
loop);
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView(new StubViewData { { "numbers", new[] { 0, 1, 2, 3, 4, 7, 2, -4 } } });
Assert.AreEqual("dabcddbd", contents);
}
[Test]
public void RenderPartial()
{
var partial = Chunks(
new SendLiteralChunk { Text = "Hello world" });
var chunks = Chunks(
new SendLiteralChunk { Text = "(" },
new RenderPartialChunk { FileContext = new FileContext { Contents = partial[0] } },
new SendLiteralChunk { Text = ")" });
_compiler.CompileView(chunks, new[] { chunks[0], partial[0] });
var contents = ExecuteView();
Assert.AreEqual("(Hello world)", contents);
}
[Test]
public void RenderPartialWithContainedContent()
{
var partial = Chunks(
new SendLiteralChunk { Text = "[" },
new RenderSectionChunk(),
new SendLiteralChunk { Text = "]" });
var renderPartial = new RenderPartialChunk { FileContext = new FileContext { Contents = partial[0] } };
var chunks = Chunks(
new SendLiteralChunk { Text = "(" },
renderPartial,
new SendLiteralChunk { Text = ")" });
renderPartial.Body.Add(new SendLiteralChunk { Text = "From inside caller" });
_compiler.CompileView(chunks, new[] { chunks[0], partial[0] });
var contents = ExecuteView();
Assert.AreEqual("([From inside caller])", contents);
}
[Test]
public void RenderPartialWithSectionContent()
{
var partial = Chunks(
new SendLiteralChunk { Text = "[" },
new RenderSectionChunk { Name = "foo" },
new SendLiteralChunk { Text = "]" });
var renderPartial = new RenderPartialChunk { FileContext = new FileContext { Contents = partial[0] } };
renderPartial.Sections.Add("foo",
new[]
{
(Chunk) new SendLiteralChunk {Text = "From inside caller"}
});
var chunks = Chunks(
new SendLiteralChunk { Text = "(" },
renderPartial,
new SendLiteralChunk { Text = ")" });
_compiler.CompileView(chunks, new[] { chunks[0], partial[0] });
var contents = ExecuteView();
Assert.AreEqual("([From inside caller])", contents);
}
[Test]
public void CaptureContentToVariable()
{
var text1 = (Chunk)new SendLiteralChunk { Text = "a" };
var text2 = (Chunk)new SendLiteralChunk { Text = "b" };
var text3 = (Chunk)new SendLiteralChunk { Text = "c" };
var chunks = Chunks(
new LocalVariableChunk { Type = "string", Name = "foo" },
new ContentSetChunk { AddType = ContentAddType.Replace, Variable = "foo", Body = new[] { text1 } },
new ContentSetChunk { AddType = ContentAddType.InsertBefore, Variable = "foo", Body = new[] { text2 } },
new ContentSetChunk { AddType = ContentAddType.AppendAfter, Variable = "foo", Body = new[] { text3 } },
new SendExpressionChunk { Code = "foo" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("bac", contents);
}
[Test]
public void CaptureContentToNamedSpool()
{
var chunks = Chunks(
new ContentChunk { Name = "foo", Body = new[] { (Chunk)new SendLiteralChunk { Text = "b" } } },
new SendLiteralChunk { Text = "a" },
new UseContentChunk { Name = "foo" },
new SendLiteralChunk { Text = "c" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("abc", contents);
}
[Test]
public void DefaultVariablesWorking()
{
var chunks = Chunks(
new DefaultVariableChunk { Name = "x1", Value = "\"a\"" },
new LocalVariableChunk { Name = "x2", Value = "\"b\"" },
new DefaultVariableChunk { Name = "x1", Value = "\"c\"" },
new DefaultVariableChunk { Name = "x2", Value = "\"d\"" },
new DefaultVariableChunk { Name = "x3", Value = "\"e\"" },
new SendExpressionChunk { Code = "x1" },
new SendExpressionChunk { Code = "x2" },
new SendExpressionChunk { Code = "x3" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("abe", contents);
}
[Test]
public void OnceTestSendsThingsOnce()
{
var chunks = Chunks(
new ConditionalChunk
{
Type = ConditionalType.Once,
Condition = "\"foo\"",
Body = new[] { (Chunk)new SendLiteralChunk { Text = "4" } }
},
new ConditionalChunk
{
Type = ConditionalType.Once,
Condition = "\"foo\"",
Body = new[] { (Chunk)new SendLiteralChunk { Text = "3" } }
},
new ConditionalChunk
{
Type = ConditionalType.Once,
Condition = "\"bar\"",
Body = new[] { (Chunk)new SendLiteralChunk { Text = "2" } }
});
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("42", contents);
}
[Test]
public void HandleNullReferences()
{
var chunks = Chunks(
new LocalVariableChunk { Name = "user", Type = typeof(UserInfo).FullName },
new SendLiteralChunk { Text = "1" },
new SendExpressionChunk { Code = "user.Name", SilentNulls = false },
new SendLiteralChunk { Text = "2" },
new SendExpressionChunk { Code = "user.Name", SilentNulls = true },
new SendLiteralChunk { Text = "3" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("1${user.Name}23", contents);
}
[Test]
public void ForLoopAutovariables()
{
var loop = Chunks(
new SendExpressionChunk { Code = "fooIndex" },
new SendExpressionChunk { Code = "fooCount" },
new SendExpressionChunk { Code = "fooIsFirst" },
new SendExpressionChunk { Code = "fooIsLast" });
var chunks = Chunks(
new ForEachChunk { Code = "foo in stuff", Body = loop[0] });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView(new StubViewData
{
{"stuff", new[] {6, 2, 7, 4}}
});
Assert.AreEqual("04TrueFalse14FalseFalse24FalseFalse34FalseTrue", contents);
}
[Test]
public void CallingMacro()
{
var macroBody = Chunks(
new SendLiteralChunk { Text = "1" });
var chunks = Chunks(
new MacroChunk { Name = "Foo", Body = macroBody[0] },
new LocalVariableChunk { Name = "x", Value = "Foo()" },
new SendLiteralChunk { Text = "2" },
new SendExpressionChunk { Code = "x" },
new SendLiteralChunk { Text = "3" });
_compiler.CompileView(chunks, chunks);
var contents = ExecuteView();
Assert.AreEqual("213", contents);
}
}
}