5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
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.Collections.Generic;
30
using NUnit.Framework;
31
using Microsoft.VisualStudio.TextTemplating;
33
namespace Mono.TextTemplating.Tests
38
public class GenerationTests
41
public void Generate ()
43
string Input = ParsingTests.ParseSample1;
44
string Output = OutputSample1;
45
Generate (Input, Output, "\n");
49
public void GenerateMacNewlines ()
51
string MacInput = ParsingTests.ParseSample1.Replace ("\n", "\r");
52
string MacOutput = OutputSample1.Replace ("\\n", "\\r").Replace ("\n", "\r");;
53
Generate (MacInput, MacOutput, "\r");
57
public void GenerateWindowsNewlines ()
59
string WinInput = ParsingTests.ParseSample1.Replace ("\n", "\r\n");
60
string WinOutput = OutputSample1.Replace ("\\n", "\\r\\n").Replace ("\n", "\r\n");
61
Generate (WinInput, WinOutput, "\r\n");
64
//NOTE: we set the newline property on the code generator so that the whole files has matching newlines,
65
// in order to match the newlines in the verbatim code blocks
66
void Generate (string input, string expectedOutput, string newline)
68
DummyHost host = new DummyHost ();
69
string className = "GeneratedTextTransformation4f504ca0";
70
string code = GenerateCode (host, input, className, newline);
71
Assert.AreEqual (0, host.Errors.Count);
72
Assert.AreEqual (expectedOutput, TemplatingEngineHelper.StripHeader (code, newline));
77
string GenerateCode (ITextTemplatingEngineHost host, string content, string name, string generatorNewline)
79
ParsedTemplate pt = ParsedTemplate.FromText (content, host);
80
if (pt.Errors.HasErrors) {
81
host.LogErrors (pt.Errors);
85
TemplateSettings settings = TemplatingEngine.GetSettings (host, pt);
88
if (pt.Errors.HasErrors) {
89
host.LogErrors (pt.Errors);
93
var ccu = TemplatingEngine.GenerateCompileUnit (host, content, pt, settings);
94
if (pt.Errors.HasErrors) {
95
host.LogErrors (pt.Errors);
99
var opts = new System.CodeDom.Compiler.CodeGeneratorOptions ();
100
using (var writer = new System.IO.StringWriter ()) {
101
writer.NewLine = generatorNewline;
102
settings.Provider.GenerateCodeFromCompileUnit (ccu, writer, opts);
103
return writer.ToString ();
109
#region Expected output strings
111
public static string OutputSample1 =
113
namespace Microsoft.VisualStudio.TextTemplating {
116
public partial class GeneratedTextTransformation4f504ca0 : global::Microsoft.VisualStudio.TextTemplating.TextTransformation {
126
public override string TransformText() {
127
this.GenerationEnvironment = null;
130
this.Write(""Line One\nLine Two\n"");
144
this.Write(""Line Three "");
150
this.Write(global::Microsoft.VisualStudio.TextTemplating.ToStringHelper.ToStringWithCulture( bar ));
156
this.Write(""\nLine Four\n"");
160
return this.GenerationEnvironment.ToString();
163
protected override void Initialize() {