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

« back to all changes in this revision

Viewing changes to src/addins/TextTemplating/Mono.TextTemplating.Tests/GenerationTests.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
// GenerationTests.cs
 
3
//  
 
4
// Author:
 
5
//       Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
 
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.IO;
 
30
using NUnit.Framework;
 
31
using Microsoft.VisualStudio.TextTemplating;
 
32
 
 
33
namespace Mono.TextTemplating.Tests
 
34
{
 
35
        
 
36
        
 
37
        [TestFixture]
 
38
        public class GenerationTests
 
39
        {       
 
40
                [Test]
 
41
                public void Generate ()
 
42
                {
 
43
                        string Input = ParsingTests.ParseSample1;
 
44
                        string Output = OutputSample1;
 
45
                        Generate (Input, Output, "\n");
 
46
                }
 
47
                
 
48
                [Test]
 
49
                public void GenerateMacNewlines ()
 
50
                {
 
51
                        string MacInput = ParsingTests.ParseSample1.Replace ("\n", "\r");
 
52
                        string MacOutput = OutputSample1.Replace ("\\n", "\\r").Replace ("\n", "\r");;
 
53
                        Generate (MacInput, MacOutput, "\r");
 
54
                }
 
55
                
 
56
                [Test]
 
57
                public void GenerateWindowsNewlines ()
 
58
                {
 
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");
 
62
                }
 
63
                
 
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)
 
67
                {
 
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));
 
73
                }
 
74
                
 
75
                #region Helpers
 
76
                
 
77
                string GenerateCode (ITextTemplatingEngineHost host, string content, string name, string generatorNewline)
 
78
                {
 
79
                        ParsedTemplate pt = ParsedTemplate.FromText (content, host);
 
80
                        if (pt.Errors.HasErrors) {
 
81
                                host.LogErrors (pt.Errors);
 
82
                                return null;
 
83
                        }
 
84
                        
 
85
                        TemplateSettings settings = TemplatingEngine.GetSettings (host, pt);
 
86
                        if (name != null)
 
87
                                settings.Name = name;
 
88
                        if (pt.Errors.HasErrors) {
 
89
                                host.LogErrors (pt.Errors);
 
90
                                return null;
 
91
                        }
 
92
                        
 
93
                        var ccu = TemplatingEngine.GenerateCompileUnit (host, content, pt, settings);
 
94
                        if (pt.Errors.HasErrors) {
 
95
                                host.LogErrors (pt.Errors);
 
96
                                return null;
 
97
                        }
 
98
                        
 
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 ();
 
104
                        }
 
105
                }
 
106
 
 
107
                #endregion
 
108
                
 
109
                #region Expected output strings
 
110
                
 
111
                public static string OutputSample1 = 
 
112
@"
 
113
namespace Microsoft.VisualStudio.TextTemplating {
 
114
    
 
115
    
 
116
    public partial class GeneratedTextTransformation4f504ca0 : global::Microsoft.VisualStudio.TextTemplating.TextTransformation {
 
117
        
 
118
        
 
119
        #line 9 """"
 
120
         
 
121
baz \#>
 
122
 
 
123
        #line default
 
124
        #line hidden
 
125
        
 
126
        public override string TransformText() {
 
127
            this.GenerationEnvironment = null;
 
128
            
 
129
            #line 2 """"
 
130
            this.Write(""Line One\nLine Two\n"");
 
131
            
 
132
            #line default
 
133
            #line hidden
 
134
            
 
135
            #line 4 """"
 
136
 
 
137
foo
 
138
 
 
139
            
 
140
            #line default
 
141
            #line hidden
 
142
            
 
143
            #line 7 """"
 
144
            this.Write(""Line Three "");
 
145
            
 
146
            #line default
 
147
            #line hidden
 
148
            
 
149
            #line 7 """"
 
150
            this.Write(global::Microsoft.VisualStudio.TextTemplating.ToStringHelper.ToStringWithCulture( bar ));
 
151
            
 
152
            #line default
 
153
            #line hidden
 
154
            
 
155
            #line 7 """"
 
156
            this.Write(""\nLine Four\n"");
 
157
            
 
158
            #line default
 
159
            #line hidden
 
160
            return this.GenerationEnvironment.ToString();
 
161
        }
 
162
        
 
163
        protected override void Initialize() {
 
164
            base.Initialize();
 
165
        }
 
166
    }
 
167
}
 
168
";
 
169
                #endregion
 
170
        }
 
171
}