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

« back to all changes in this revision

Viewing changes to src/addins/TextTemplating/Mono.TextTemplating.Tests/TemplateEnginePreprocessTemplateTests.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
// TemplateEnginePreprocessTemplateTests.cs
 
3
//  
 
4
// Author:
 
5
//       Matt Ward
 
6
// 
 
7
// Copyright (c) 2011 Matt Ward
 
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.CodeDom.Compiler;
 
29
using System.Collections.Generic;
 
30
using System.IO;
 
31
using NUnit.Framework;
 
32
using Microsoft.VisualStudio.TextTemplating;
 
33
 
 
34
namespace Mono.TextTemplating.Tests
 
35
{
 
36
        [TestFixture]
 
37
        public class TemplateEnginePreprocessTemplateTests
 
38
        {       
 
39
                [Test]
 
40
                public void Preprocess ()
 
41
                {
 
42
                        string input = 
 
43
                                "<#@ template language=\"C#\" #>\r\n" +
 
44
                                "Test\r\n";
 
45
                        
 
46
                        string expectedOutput = OutputSample1;
 
47
                        string output = Preprocess (input);
 
48
                        
 
49
                        Assert.AreEqual (expectedOutput, output);
 
50
                }
 
51
                
 
52
                [Test]
 
53
                public void Preprocess_ControlBlockAfterIncludedTemplateWithClassFeatureBlock_ReturnsValidCSharpOutput ()
 
54
                {
 
55
                        string input = InputTemplate_ControlBlockAfterIncludedTemplateWithClassFeatureBlock;
 
56
                        DummyHost host = CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest ();
 
57
                        
 
58
                        string expectedOutput = Output_ControlBlockAfterIncludedTemplateWithClassFeatureBlock;
 
59
                        string output = Preprocess (input, host);
 
60
                        
 
61
                        Assert.AreEqual (expectedOutput, output, output);
 
62
                }
 
63
                
 
64
                #region Helpers
 
65
                
 
66
                string Preprocess (string input)
 
67
                {
 
68
                        DummyHost host = new DummyHost ();
 
69
                        return Preprocess (input, host);
 
70
                }
 
71
                
 
72
                string Preprocess (string input, DummyHost host)
 
73
                {
 
74
                        string className = "PreprocessedTemplate";
 
75
                        string classNamespace = "Templating";
 
76
                        string language = null;
 
77
                        string[] references = null;
 
78
                        
 
79
                        TemplatingEngine engine = new TemplatingEngine ();
 
80
                        string output = engine.PreprocessTemplate (input, host, className, classNamespace, out language, out references);
 
81
                        ReportErrors (host.Errors);
 
82
                        if (output != null) {
 
83
                                output = output.Replace ("\r\n", "\n");
 
84
                                return TemplatingEngineHelper.StripHeader (output, "\n");
 
85
                        }
 
86
                        return null;
 
87
                }
 
88
                
 
89
                void ReportErrors(CompilerErrorCollection errors)
 
90
                {
 
91
                        foreach (CompilerError error in errors) {
 
92
                                Console.WriteLine(error.ErrorText);
 
93
                        }
 
94
                }
 
95
                
 
96
                DummyHost CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest()
 
97
                {
 
98
                        DummyHost host = new DummyHost ();
 
99
                        
 
100
                        string includeTemplateFileName = @"d:\test\IncludedFile.tt";
 
101
                        host.Locations.Add (includeTemplateFileName, includeTemplateFileName);
 
102
                        host.Contents.Add (includeTemplateFileName, IncludedTemplate_ControlBlockAfterIncludedTemplate);
 
103
                        
 
104
                        return host;
 
105
                }
 
106
                
 
107
                #endregion
 
108
 
 
109
                #region Input templates
 
110
 
 
111
                public static string InputTemplate_ControlBlockAfterIncludedTemplateWithClassFeatureBlock =
 
112
@"
 
113
<#@ template debug=""false"" language=""C#"" #>
 
114
<#@ output extension="".cs"" #>
 
115
Text Block 1
 
116
<#
 
117
    this.TemplateMethod();
 
118
#>
 
119
Text Block 2
 
120
<#@ include file=""d:\test\IncludedFile.tt"" #>
 
121
Text Block 3
 
122
<#
 
123
    this.IncludedMethod();
 
124
#>
 
125
<#+
 
126
    void TemplateMethod()
 
127
    {
 
128
    }
 
129
#>
 
130
";
 
131
                
 
132
                public static string IncludedTemplate_ControlBlockAfterIncludedTemplate =
 
133
@"
 
134
<#@ template debug=""false"" language=""C#"" #>
 
135
<#@ output extension="".cs"" #>
 
136
Included Text Block 1
 
137
<# this.WriteLine(""Included statement block""); #>
 
138
Included Text Block 2
 
139
<#+
 
140
    void IncludedMethod()
 
141
    {
 
142
#>
 
143
Included Method Body Text Block
 
144
<#+
 
145
    }
 
146
#>
 
147
";
 
148
 
 
149
                #endregion
 
150
                
 
151
                #region Expected output strings
 
152
                
 
153
                public static string OutputSample1 = 
 
154
@"
 
155
namespace Templating {
 
156
    
 
157
    
 
158
    public partial class PreprocessedTemplate : PreprocessedTemplateBase {
 
159
        
 
160
        public virtual string TransformText() {
 
161
            this.GenerationEnvironment = null;
 
162
            
 
163
            #line 2 """"
 
164
            this.Write(""Test\r\n"");
 
165
            
 
166
            #line default
 
167
            #line hidden
 
168
            return this.GenerationEnvironment.ToString();
 
169
        }
 
170
        
 
171
        protected virtual void Initialize() {
 
172
        }
 
173
    }
 
174
    
 
175
    public class PreprocessedTemplateBase {
 
176
        
 
177
        private global::System.Text.StringBuilder builder;
 
178
        
 
179
        private global::System.Collections.Generic.IDictionary<string, object> session;
 
180
        
 
181
        private global::System.CodeDom.Compiler.CompilerErrorCollection errors;
 
182
        
 
183
        private string currentIndent = string.Empty;
 
184
        
 
185
        private global::System.Collections.Generic.Stack<int> indents;
 
186
        
 
187
        private ToStringInstanceHelper _toStringHelper = new ToStringInstanceHelper();
 
188
        
 
189
        public virtual global::System.Collections.Generic.IDictionary<string, object> Session {
 
190
            get {
 
191
                return this.session;
 
192
            }
 
193
            set {
 
194
                this.session = value;
 
195
            }
 
196
        }
 
197
        
 
198
        public global::System.Text.StringBuilder GenerationEnvironment {
 
199
            get {
 
200
                if ((this.builder == null)) {
 
201
                    this.builder = new global::System.Text.StringBuilder();
 
202
                }
 
203
                return this.builder;
 
204
            }
 
205
            set {
 
206
                this.builder = value;
 
207
            }
 
208
        }
 
209
        
 
210
        protected global::System.CodeDom.Compiler.CompilerErrorCollection Errors {
 
211
            get {
 
212
                if ((this.errors == null)) {
 
213
                    this.errors = new global::System.CodeDom.Compiler.CompilerErrorCollection();
 
214
                }
 
215
                return this.errors;
 
216
            }
 
217
        }
 
218
        
 
219
        public string CurrentIndent {
 
220
            get {
 
221
                return this.currentIndent;
 
222
            }
 
223
        }
 
224
        
 
225
        private global::System.Collections.Generic.Stack<int> Indents {
 
226
            get {
 
227
                if ((this.indents == null)) {
 
228
                    this.indents = new global::System.Collections.Generic.Stack<int>();
 
229
                }
 
230
                return this.indents;
 
231
            }
 
232
        }
 
233
        
 
234
        public ToStringInstanceHelper ToStringHelper {
 
235
            get {
 
236
                return this._toStringHelper;
 
237
            }
 
238
        }
 
239
        
 
240
        public void Error(string message) {
 
241
            this.Errors.Add(new global::System.CodeDom.Compiler.CompilerError(null, -1, -1, null, message));
 
242
        }
 
243
        
 
244
        public void Warning(string message) {
 
245
            global::System.CodeDom.Compiler.CompilerError val = new global::System.CodeDom.Compiler.CompilerError(null, -1, -1, null, message);
 
246
            val.IsWarning = true;
 
247
            this.Errors.Add(val);
 
248
        }
 
249
        
 
250
        public string PopIndent() {
 
251
            if ((this.Indents.Count == 0)) {
 
252
                return string.Empty;
 
253
            }
 
254
            int lastPos = (this.currentIndent.Length - this.Indents.Pop());
 
255
            string last = this.currentIndent.Substring(lastPos);
 
256
            this.currentIndent = this.currentIndent.Substring(0, lastPos);
 
257
            return last;
 
258
        }
 
259
        
 
260
        public void PushIndent(string indent) {
 
261
            this.Indents.Push(indent.Length);
 
262
            this.currentIndent = (this.currentIndent + indent);
 
263
        }
 
264
        
 
265
        public void ClearIndent() {
 
266
            this.currentIndent = string.Empty;
 
267
            this.Indents.Clear();
 
268
        }
 
269
        
 
270
        public void Write(string textToAppend) {
 
271
            this.GenerationEnvironment.Append(textToAppend);
 
272
        }
 
273
        
 
274
        public void Write(string format, params object[] args) {
 
275
            this.GenerationEnvironment.AppendFormat(format, args);
 
276
        }
 
277
        
 
278
        public void WriteLine(string textToAppend) {
 
279
            this.GenerationEnvironment.Append(this.currentIndent);
 
280
            this.GenerationEnvironment.AppendLine(textToAppend);
 
281
        }
 
282
        
 
283
        public void WriteLine(string format, params object[] args) {
 
284
            this.GenerationEnvironment.Append(this.currentIndent);
 
285
            this.GenerationEnvironment.AppendFormat(format, args);
 
286
            this.GenerationEnvironment.AppendLine();
 
287
        }
 
288
        
 
289
        public class ToStringInstanceHelper {
 
290
            
 
291
            private global::System.IFormatProvider formatProvider = global::System.Globalization.CultureInfo.InvariantCulture;
 
292
            
 
293
            public global::System.IFormatProvider FormatProvider {
 
294
                get {
 
295
                    return this.formatProvider;
 
296
                }
 
297
                set {
 
298
                    if ((this.formatProvider == null)) {
 
299
                        throw new global::System.ArgumentNullException(""formatProvider"");
 
300
                    }
 
301
                    this.formatProvider = value;
 
302
                }
 
303
            }
 
304
            
 
305
            public string ToStringWithCulture(object objectToConvert) {
 
306
                if ((objectToConvert == null)) {
 
307
                    throw new global::System.ArgumentNullException(""objectToConvert"");
 
308
                }
 
309
                global::System.Type type = objectToConvert.GetType();
 
310
                global::System.Type iConvertibleType = typeof(global::System.IConvertible);
 
311
                if (iConvertibleType.IsAssignableFrom(type)) {
 
312
                    return ((global::System.IConvertible)(objectToConvert)).ToString(this.formatProvider);
 
313
                }
 
314
                global::System.Reflection.MethodInfo methInfo = type.GetMethod(""ToString"", new global::System.Type[] {
 
315
                            iConvertibleType});
 
316
                if ((methInfo != null)) {
 
317
                    return ((string)(methInfo.Invoke(objectToConvert, new object[] {
 
318
                                this.formatProvider})));
 
319
                }
 
320
                return objectToConvert.ToString();
 
321
            }
 
322
        }
 
323
    }
 
324
}
 
325
";
 
326
 
 
327
                public static string Output_ControlBlockAfterIncludedTemplateWithClassFeatureBlock =
 
328
@"
 
329
namespace Templating {
 
330
    
 
331
    
 
332
    public partial class PreprocessedTemplate : PreprocessedTemplateBase {
 
333
        
 
334
        
 
335
        #line 14 """"
 
336
        
 
337
    void TemplateMethod()
 
338
    {
 
339
    }
 
340
 
 
341
        #line default
 
342
        #line hidden
 
343
        
 
344
        
 
345
        #line 7 ""d:\test\IncludedFile.tt""
 
346
        
 
347
    void IncludedMethod()
 
348
    {
 
349
 
 
350
        #line default
 
351
        #line hidden
 
352
        
 
353
        
 
354
        #line 11 ""d:\test\IncludedFile.tt""
 
355
        this.Write(""Included Method Body Text Block\n"");
 
356
 
 
357
        #line default
 
358
        #line hidden
 
359
        
 
360
        
 
361
        #line 12 ""d:\test\IncludedFile.tt""
 
362
        
 
363
    }
 
364
 
 
365
        #line default
 
366
        #line hidden
 
367
        
 
368
        public virtual string TransformText() {
 
369
            this.GenerationEnvironment = null;
 
370
            
 
371
            #line 1 """"
 
372
            this.Write(""\n"");
 
373
            
 
374
            #line default
 
375
            #line hidden
 
376
            
 
377
            #line 4 """"
 
378
            this.Write(""Text Block 1\n"");
 
379
            
 
380
            #line default
 
381
            #line hidden
 
382
            
 
383
            #line 5 """"
 
384
 
 
385
    this.TemplateMethod();
 
386
 
 
387
            
 
388
            #line default
 
389
            #line hidden
 
390
            
 
391
            #line 8 """"
 
392
            this.Write(""Text Block 2\n"");
 
393
            
 
394
            #line default
 
395
            #line hidden
 
396
            
 
397
            #line 1 ""d:\test\IncludedFile.tt""
 
398
            this.Write(""\n"");
 
399
            
 
400
            #line default
 
401
            #line hidden
 
402
            
 
403
            #line 4 ""d:\test\IncludedFile.tt""
 
404
            this.Write(""Included Text Block 1\n"");
 
405
            
 
406
            #line default
 
407
            #line hidden
 
408
            
 
409
            #line 5 ""d:\test\IncludedFile.tt""
 
410
 this.WriteLine(""Included statement block""); 
 
411
            
 
412
            #line default
 
413
            #line hidden
 
414
            
 
415
            #line 6 ""d:\test\IncludedFile.tt""
 
416
            this.Write(""Included Text Block 2\n"");
 
417
            
 
418
            #line default
 
419
            #line hidden
 
420
            
 
421
            #line 10 """"
 
422
            this.Write(""Text Block 3\n"");
 
423
            
 
424
            #line default
 
425
            #line hidden
 
426
            
 
427
            #line 11 """"
 
428
 
 
429
    this.IncludedMethod();
 
430
 
 
431
            
 
432
            #line default
 
433
            #line hidden
 
434
            return this.GenerationEnvironment.ToString();
 
435
        }
 
436
        
 
437
        protected virtual void Initialize() {
 
438
        }
 
439
    }
 
440
    
 
441
    public class PreprocessedTemplateBase {
 
442
        
 
443
        private global::System.Text.StringBuilder builder;
 
444
        
 
445
        private global::System.Collections.Generic.IDictionary<string, object> session;
 
446
        
 
447
        private global::System.CodeDom.Compiler.CompilerErrorCollection errors;
 
448
        
 
449
        private string currentIndent = string.Empty;
 
450
        
 
451
        private global::System.Collections.Generic.Stack<int> indents;
 
452
        
 
453
        private ToStringInstanceHelper _toStringHelper = new ToStringInstanceHelper();
 
454
        
 
455
        public virtual global::System.Collections.Generic.IDictionary<string, object> Session {
 
456
            get {
 
457
                return this.session;
 
458
            }
 
459
            set {
 
460
                this.session = value;
 
461
            }
 
462
        }
 
463
        
 
464
        public global::System.Text.StringBuilder GenerationEnvironment {
 
465
            get {
 
466
                if ((this.builder == null)) {
 
467
                    this.builder = new global::System.Text.StringBuilder();
 
468
                }
 
469
                return this.builder;
 
470
            }
 
471
            set {
 
472
                this.builder = value;
 
473
            }
 
474
        }
 
475
        
 
476
        protected global::System.CodeDom.Compiler.CompilerErrorCollection Errors {
 
477
            get {
 
478
                if ((this.errors == null)) {
 
479
                    this.errors = new global::System.CodeDom.Compiler.CompilerErrorCollection();
 
480
                }
 
481
                return this.errors;
 
482
            }
 
483
        }
 
484
        
 
485
        public string CurrentIndent {
 
486
            get {
 
487
                return this.currentIndent;
 
488
            }
 
489
        }
 
490
        
 
491
        private global::System.Collections.Generic.Stack<int> Indents {
 
492
            get {
 
493
                if ((this.indents == null)) {
 
494
                    this.indents = new global::System.Collections.Generic.Stack<int>();
 
495
                }
 
496
                return this.indents;
 
497
            }
 
498
        }
 
499
        
 
500
        public ToStringInstanceHelper ToStringHelper {
 
501
            get {
 
502
                return this._toStringHelper;
 
503
            }
 
504
        }
 
505
        
 
506
        public void Error(string message) {
 
507
            this.Errors.Add(new global::System.CodeDom.Compiler.CompilerError(null, -1, -1, null, message));
 
508
        }
 
509
        
 
510
        public void Warning(string message) {
 
511
            global::System.CodeDom.Compiler.CompilerError val = new global::System.CodeDom.Compiler.CompilerError(null, -1, -1, null, message);
 
512
            val.IsWarning = true;
 
513
            this.Errors.Add(val);
 
514
        }
 
515
        
 
516
        public string PopIndent() {
 
517
            if ((this.Indents.Count == 0)) {
 
518
                return string.Empty;
 
519
            }
 
520
            int lastPos = (this.currentIndent.Length - this.Indents.Pop());
 
521
            string last = this.currentIndent.Substring(lastPos);
 
522
            this.currentIndent = this.currentIndent.Substring(0, lastPos);
 
523
            return last;
 
524
        }
 
525
        
 
526
        public void PushIndent(string indent) {
 
527
            this.Indents.Push(indent.Length);
 
528
            this.currentIndent = (this.currentIndent + indent);
 
529
        }
 
530
        
 
531
        public void ClearIndent() {
 
532
            this.currentIndent = string.Empty;
 
533
            this.Indents.Clear();
 
534
        }
 
535
        
 
536
        public void Write(string textToAppend) {
 
537
            this.GenerationEnvironment.Append(textToAppend);
 
538
        }
 
539
        
 
540
        public void Write(string format, params object[] args) {
 
541
            this.GenerationEnvironment.AppendFormat(format, args);
 
542
        }
 
543
        
 
544
        public void WriteLine(string textToAppend) {
 
545
            this.GenerationEnvironment.Append(this.currentIndent);
 
546
            this.GenerationEnvironment.AppendLine(textToAppend);
 
547
        }
 
548
        
 
549
        public void WriteLine(string format, params object[] args) {
 
550
            this.GenerationEnvironment.Append(this.currentIndent);
 
551
            this.GenerationEnvironment.AppendFormat(format, args);
 
552
            this.GenerationEnvironment.AppendLine();
 
553
        }
 
554
        
 
555
        public class ToStringInstanceHelper {
 
556
            
 
557
            private global::System.IFormatProvider formatProvider = global::System.Globalization.CultureInfo.InvariantCulture;
 
558
            
 
559
            public global::System.IFormatProvider FormatProvider {
 
560
                get {
 
561
                    return this.formatProvider;
 
562
                }
 
563
                set {
 
564
                    if ((this.formatProvider == null)) {
 
565
                        throw new global::System.ArgumentNullException(""formatProvider"");
 
566
                    }
 
567
                    this.formatProvider = value;
 
568
                }
 
569
            }
 
570
            
 
571
            public string ToStringWithCulture(object objectToConvert) {
 
572
                if ((objectToConvert == null)) {
 
573
                    throw new global::System.ArgumentNullException(""objectToConvert"");
 
574
                }
 
575
                global::System.Type type = objectToConvert.GetType();
 
576
                global::System.Type iConvertibleType = typeof(global::System.IConvertible);
 
577
                if (iConvertibleType.IsAssignableFrom(type)) {
 
578
                    return ((global::System.IConvertible)(objectToConvert)).ToString(this.formatProvider);
 
579
                }
 
580
                global::System.Reflection.MethodInfo methInfo = type.GetMethod(""ToString"", new global::System.Type[] {
 
581
                            iConvertibleType});
 
582
                if ((methInfo != null)) {
 
583
                    return ((string)(methInfo.Invoke(objectToConvert, new object[] {
 
584
                                this.formatProvider})));
 
585
                }
 
586
                return objectToConvert.ToString();
 
587
            }
 
588
        }
 
589
    }
 
590
}
 
591
";
 
592
                #endregion
 
593
        }
 
594
}