2
// TemplateEnginePreprocessTemplateTests.cs
7
// Copyright (c) 2011 Matt Ward
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using System.CodeDom.Compiler;
29
using System.Collections.Generic;
31
using NUnit.Framework;
32
using Microsoft.VisualStudio.TextTemplating;
34
namespace Mono.TextTemplating.Tests
37
public class TemplateEnginePreprocessTemplateTests
40
public void Preprocess ()
43
"<#@ template language=\"C#\" #>\r\n" +
46
string expectedOutput = OutputSample1;
47
string output = Preprocess (input);
49
Assert.AreEqual (expectedOutput, output);
53
public void Preprocess_ControlBlockAfterIncludedTemplateWithClassFeatureBlock_ReturnsValidCSharpOutput ()
55
string input = InputTemplate_ControlBlockAfterIncludedTemplateWithClassFeatureBlock;
56
DummyHost host = CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest ();
58
string expectedOutput = Output_ControlBlockAfterIncludedTemplateWithClassFeatureBlock;
59
string output = Preprocess (input, host);
61
Assert.AreEqual (expectedOutput, output, output);
66
string Preprocess (string input)
68
DummyHost host = new DummyHost ();
69
return Preprocess (input, host);
72
string Preprocess (string input, DummyHost host)
74
string className = "PreprocessedTemplate";
75
string classNamespace = "Templating";
76
string language = null;
77
string[] references = null;
79
TemplatingEngine engine = new TemplatingEngine ();
80
string output = engine.PreprocessTemplate (input, host, className, classNamespace, out language, out references);
81
ReportErrors (host.Errors);
83
output = output.Replace ("\r\n", "\n");
84
return TemplatingEngineHelper.StripHeader (output, "\n");
89
void ReportErrors(CompilerErrorCollection errors)
91
foreach (CompilerError error in errors) {
92
Console.WriteLine(error.ErrorText);
96
DummyHost CreateDummyHostForControlBlockAfterIncludedTemplateWithClassFeatureBlockTest()
98
DummyHost host = new DummyHost ();
100
string includeTemplateFileName = @"d:\test\IncludedFile.tt";
101
host.Locations.Add (includeTemplateFileName, includeTemplateFileName);
102
host.Contents.Add (includeTemplateFileName, IncludedTemplate_ControlBlockAfterIncludedTemplate);
109
#region Input templates
111
public static string InputTemplate_ControlBlockAfterIncludedTemplateWithClassFeatureBlock =
113
<#@ template debug=""false"" language=""C#"" #>
114
<#@ output extension="".cs"" #>
117
this.TemplateMethod();
120
<#@ include file=""d:\test\IncludedFile.tt"" #>
123
this.IncludedMethod();
126
void TemplateMethod()
132
public static string IncludedTemplate_ControlBlockAfterIncludedTemplate =
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
140
void IncludedMethod()
143
Included Method Body Text Block
151
#region Expected output strings
153
public static string OutputSample1 =
155
namespace Templating {
158
public partial class PreprocessedTemplate : PreprocessedTemplateBase {
160
public virtual string TransformText() {
161
this.GenerationEnvironment = null;
164
this.Write(""Test\r\n"");
168
return this.GenerationEnvironment.ToString();
171
protected virtual void Initialize() {
175
public class PreprocessedTemplateBase {
177
private global::System.Text.StringBuilder builder;
179
private global::System.Collections.Generic.IDictionary<string, object> session;
181
private global::System.CodeDom.Compiler.CompilerErrorCollection errors;
183
private string currentIndent = string.Empty;
185
private global::System.Collections.Generic.Stack<int> indents;
187
private ToStringInstanceHelper _toStringHelper = new ToStringInstanceHelper();
189
public virtual global::System.Collections.Generic.IDictionary<string, object> Session {
194
this.session = value;
198
public global::System.Text.StringBuilder GenerationEnvironment {
200
if ((this.builder == null)) {
201
this.builder = new global::System.Text.StringBuilder();
206
this.builder = value;
210
protected global::System.CodeDom.Compiler.CompilerErrorCollection Errors {
212
if ((this.errors == null)) {
213
this.errors = new global::System.CodeDom.Compiler.CompilerErrorCollection();
219
public string CurrentIndent {
221
return this.currentIndent;
225
private global::System.Collections.Generic.Stack<int> Indents {
227
if ((this.indents == null)) {
228
this.indents = new global::System.Collections.Generic.Stack<int>();
234
public ToStringInstanceHelper ToStringHelper {
236
return this._toStringHelper;
240
public void Error(string message) {
241
this.Errors.Add(new global::System.CodeDom.Compiler.CompilerError(null, -1, -1, null, message));
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);
250
public string PopIndent() {
251
if ((this.Indents.Count == 0)) {
254
int lastPos = (this.currentIndent.Length - this.Indents.Pop());
255
string last = this.currentIndent.Substring(lastPos);
256
this.currentIndent = this.currentIndent.Substring(0, lastPos);
260
public void PushIndent(string indent) {
261
this.Indents.Push(indent.Length);
262
this.currentIndent = (this.currentIndent + indent);
265
public void ClearIndent() {
266
this.currentIndent = string.Empty;
267
this.Indents.Clear();
270
public void Write(string textToAppend) {
271
this.GenerationEnvironment.Append(textToAppend);
274
public void Write(string format, params object[] args) {
275
this.GenerationEnvironment.AppendFormat(format, args);
278
public void WriteLine(string textToAppend) {
279
this.GenerationEnvironment.Append(this.currentIndent);
280
this.GenerationEnvironment.AppendLine(textToAppend);
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();
289
public class ToStringInstanceHelper {
291
private global::System.IFormatProvider formatProvider = global::System.Globalization.CultureInfo.InvariantCulture;
293
public global::System.IFormatProvider FormatProvider {
295
return this.formatProvider;
298
if ((this.formatProvider == null)) {
299
throw new global::System.ArgumentNullException(""formatProvider"");
301
this.formatProvider = value;
305
public string ToStringWithCulture(object objectToConvert) {
306
if ((objectToConvert == null)) {
307
throw new global::System.ArgumentNullException(""objectToConvert"");
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);
314
global::System.Reflection.MethodInfo methInfo = type.GetMethod(""ToString"", new global::System.Type[] {
316
if ((methInfo != null)) {
317
return ((string)(methInfo.Invoke(objectToConvert, new object[] {
318
this.formatProvider})));
320
return objectToConvert.ToString();
327
public static string Output_ControlBlockAfterIncludedTemplateWithClassFeatureBlock =
329
namespace Templating {
332
public partial class PreprocessedTemplate : PreprocessedTemplateBase {
337
void TemplateMethod()
345
#line 7 ""d:\test\IncludedFile.tt""
347
void IncludedMethod()
354
#line 11 ""d:\test\IncludedFile.tt""
355
this.Write(""Included Method Body Text Block\n"");
361
#line 12 ""d:\test\IncludedFile.tt""
368
public virtual string TransformText() {
369
this.GenerationEnvironment = null;
378
this.Write(""Text Block 1\n"");
385
this.TemplateMethod();
392
this.Write(""Text Block 2\n"");
397
#line 1 ""d:\test\IncludedFile.tt""
403
#line 4 ""d:\test\IncludedFile.tt""
404
this.Write(""Included Text Block 1\n"");
409
#line 5 ""d:\test\IncludedFile.tt""
410
this.WriteLine(""Included statement block"");
415
#line 6 ""d:\test\IncludedFile.tt""
416
this.Write(""Included Text Block 2\n"");
422
this.Write(""Text Block 3\n"");
429
this.IncludedMethod();
434
return this.GenerationEnvironment.ToString();
437
protected virtual void Initialize() {
441
public class PreprocessedTemplateBase {
443
private global::System.Text.StringBuilder builder;
445
private global::System.Collections.Generic.IDictionary<string, object> session;
447
private global::System.CodeDom.Compiler.CompilerErrorCollection errors;
449
private string currentIndent = string.Empty;
451
private global::System.Collections.Generic.Stack<int> indents;
453
private ToStringInstanceHelper _toStringHelper = new ToStringInstanceHelper();
455
public virtual global::System.Collections.Generic.IDictionary<string, object> Session {
460
this.session = value;
464
public global::System.Text.StringBuilder GenerationEnvironment {
466
if ((this.builder == null)) {
467
this.builder = new global::System.Text.StringBuilder();
472
this.builder = value;
476
protected global::System.CodeDom.Compiler.CompilerErrorCollection Errors {
478
if ((this.errors == null)) {
479
this.errors = new global::System.CodeDom.Compiler.CompilerErrorCollection();
485
public string CurrentIndent {
487
return this.currentIndent;
491
private global::System.Collections.Generic.Stack<int> Indents {
493
if ((this.indents == null)) {
494
this.indents = new global::System.Collections.Generic.Stack<int>();
500
public ToStringInstanceHelper ToStringHelper {
502
return this._toStringHelper;
506
public void Error(string message) {
507
this.Errors.Add(new global::System.CodeDom.Compiler.CompilerError(null, -1, -1, null, message));
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);
516
public string PopIndent() {
517
if ((this.Indents.Count == 0)) {
520
int lastPos = (this.currentIndent.Length - this.Indents.Pop());
521
string last = this.currentIndent.Substring(lastPos);
522
this.currentIndent = this.currentIndent.Substring(0, lastPos);
526
public void PushIndent(string indent) {
527
this.Indents.Push(indent.Length);
528
this.currentIndent = (this.currentIndent + indent);
531
public void ClearIndent() {
532
this.currentIndent = string.Empty;
533
this.Indents.Clear();
536
public void Write(string textToAppend) {
537
this.GenerationEnvironment.Append(textToAppend);
540
public void Write(string format, params object[] args) {
541
this.GenerationEnvironment.AppendFormat(format, args);
544
public void WriteLine(string textToAppend) {
545
this.GenerationEnvironment.Append(this.currentIndent);
546
this.GenerationEnvironment.AppendLine(textToAppend);
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();
555
public class ToStringInstanceHelper {
557
private global::System.IFormatProvider formatProvider = global::System.Globalization.CultureInfo.InvariantCulture;
559
public global::System.IFormatProvider FormatProvider {
561
return this.formatProvider;
564
if ((this.formatProvider == null)) {
565
throw new global::System.ArgumentNullException(""formatProvider"");
567
this.formatProvider = value;
571
public string ToStringWithCulture(object objectToConvert) {
572
if ((objectToConvert == null)) {
573
throw new global::System.ArgumentNullException(""objectToConvert"");
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);
580
global::System.Reflection.MethodInfo methInfo = type.GetMethod(""ToString"", new global::System.Type[] {
582
if ((methInfo != null)) {
583
return ((string)(methInfo.Invoke(objectToConvert, new object[] {
584
this.formatProvider})));
586
return objectToConvert.ToString();