Merge lp:~mandel/wadlsharp/refactor_code_serialization into lp:wadlsharp/trunk
- refactor_code_serialization
- Merge into trunk
Proposed by
Manuel de la Peña
Status: | Merged |
---|---|
Merged at revision: | 32 |
Proposed branch: | lp:~mandel/wadlsharp/refactor_code_serialization |
Merge into: | lp:wadlsharp/trunk |
Prerequisite: | lp:~mandel/wadlsharp/increase_lpnamespace_coverage |
Diff against target: |
458 lines (+315/-39) 8 files modified
LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs (+60/-0) LpNet.WadlSharp.Common/Interface/IConverter.cs (+11/-1) LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj (+2/-0) LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs (+113/-0) LpNet.WadlSharp.Common/WadlConverter.cs (+14/-37) LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj (+1/-0) LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs (+111/-0) WadlConsole/Program.cs (+3/-1) |
To merge this branch: | bzr merge lp:~mandel/wadlsharp/refactor_code_serialization |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Manish Sinha (मनीष सिन्हा) | Approve | ||
Review via email: mp+36118@code.launchpad.net |
Commit message
Description of the change
Refactored the serialization code to allow more options. Increased the code coverage.
To post a comment you must log in.
Revision history for this message
Manish Sinha (मनीष सिन्हा) (manishsinha) : | # |
review:
Approve
Revision history for this message
Manish Sinha (मनीष सिन्हा) (manishsinha) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added file 'LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs' | |||
2 | --- LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs 1970-01-01 00:00:00 +0000 | |||
3 | +++ LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs 2010-09-21 10:04:56 +0000 | |||
4 | @@ -0,0 +1,60 @@ | |||
5 | 1 | /* | ||
6 | 2 | * The MIT License (MIT) | ||
7 | 3 | * | ||
8 | 4 | * Copyright 2010 Canonical Ltd. | ||
9 | 5 | * | ||
10 | 6 | * Permission is hereby granted, free of charge, to any person | ||
11 | 7 | * obtaining a copy of this software and associated documentation | ||
12 | 8 | * files (the "Software"), to deal in the Software without | ||
13 | 9 | * restriction, including without limitation the rights to use, | ||
14 | 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
15 | 11 | * copies of the Software, and to permit persons to whom the | ||
16 | 12 | * Software is furnished to do so, subject to the following | ||
17 | 13 | * conditions: | ||
18 | 14 | |||
19 | 15 | * The above copyright notice and this permission notice shall be | ||
20 | 16 | * included in all copies or substantial portions of the Software. | ||
21 | 17 | * | ||
22 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
23 | 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
24 | 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
25 | 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
26 | 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
27 | 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
28 | 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
29 | 25 | * OTHER DEALINGS IN THE SOFTWARE. | ||
30 | 26 | * | ||
31 | 27 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
32 | 28 | */ | ||
33 | 29 | using System.CodeDom; | ||
34 | 30 | using System.CodeDom.Compiler; | ||
35 | 31 | using System.Collections.Generic; | ||
36 | 32 | |||
37 | 33 | namespace LpNet.WadlSharp.Common.Interface | ||
38 | 34 | { | ||
39 | 35 | /// <summary> | ||
40 | 36 | /// Interface to be implemented by those objects that know how to serialize | ||
41 | 37 | /// the generated code | ||
42 | 38 | /// </summary> | ||
43 | 39 | public interface ICodeSerializer | ||
44 | 40 | { | ||
45 | 41 | /// <summary> | ||
46 | 42 | /// Gets the options to be used in the code generation. | ||
47 | 43 | /// </summary> | ||
48 | 44 | CodeGeneratorOptions Options { get; } | ||
49 | 45 | |||
50 | 46 | /// <summary> | ||
51 | 47 | /// Gets and sets the string that will use to state that the code was | ||
52 | 48 | /// generated by a tool. | ||
53 | 49 | /// </summary> | ||
54 | 50 | string AutoGeneratedMessage { get; set; } | ||
55 | 51 | |||
56 | 52 | /// <summary> | ||
57 | 53 | /// Serializes the given code and returns an enumerable with the names of the files | ||
58 | 54 | /// in which the code was serialized. | ||
59 | 55 | /// </summary> | ||
60 | 56 | /// <returns>The enumerable with the files in which the code was serialized.</returns> | ||
61 | 57 | IEnumerable<string> Serialize(CodeNamespace codeNamespace); | ||
62 | 58 | } | ||
63 | 59 | |||
64 | 60 | } | ||
65 | 0 | 61 | ||
66 | === modified file 'LpNet.WadlSharp.Common/Interface/IConverter.cs' | |||
67 | --- LpNet.WadlSharp.Common/Interface/IConverter.cs 2010-05-02 07:49:15 +0000 | |||
68 | +++ LpNet.WadlSharp.Common/Interface/IConverter.cs 2010-09-21 10:04:56 +0000 | |||
69 | @@ -27,8 +27,18 @@ | |||
70 | 27 | 27 | ||
71 | 28 | namespace LpNet.WadlSharp.Common.Interface | 28 | namespace LpNet.WadlSharp.Common.Interface |
72 | 29 | { | 29 | { |
73 | 30 | /// <summary> | ||
74 | 31 | /// Interface to be implemented by an object that is able to generate the C# code required to use a | ||
75 | 32 | /// wadl service. | ||
76 | 33 | /// </summary> | ||
77 | 30 | public interface IConverter | 34 | public interface IConverter |
78 | 31 | { | 35 | { |
80 | 32 | void Convert(string inputFileName, string outputFileName, string rootNamespace); | 36 | /// <summary> |
81 | 37 | /// Gets and sets the code serializer that will be used to serialize the code to a collection | ||
82 | 38 | /// of files. | ||
83 | 39 | /// </summary> | ||
84 | 40 | ICodeSerializer CodeSerializer { get; set; } | ||
85 | 41 | |||
86 | 42 | void Convert(string inputFileName, string rootNamespace); | ||
87 | 33 | } | 43 | } |
88 | 34 | } | 44 | } |
89 | 35 | 45 | ||
90 | === modified file 'LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj' | |||
91 | --- LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj 2010-09-21 10:04:56 +0000 | |||
92 | +++ LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj 2010-09-21 10:04:56 +0000 | |||
93 | @@ -76,6 +76,7 @@ | |||
94 | 76 | <Compile Include="Interface\Generator\IParamater.cs" /> | 76 | <Compile Include="Interface\Generator\IParamater.cs" /> |
95 | 77 | <Compile Include="Interface\Generator\IProperty.cs" /> | 77 | <Compile Include="Interface\Generator\IProperty.cs" /> |
96 | 78 | <Compile Include="Interface\Generator\IReturn.cs" /> | 78 | <Compile Include="Interface\Generator\IReturn.cs" /> |
97 | 79 | <Compile Include="Interface\ICodeSerializer.cs" /> | ||
98 | 79 | <Compile Include="Interface\IConverter.cs" /> | 80 | <Compile Include="Interface\IConverter.cs" /> |
99 | 80 | <Compile Include="Interface\ILoader.cs" /> | 81 | <Compile Include="Interface\ILoader.cs" /> |
100 | 81 | <Compile Include="Properties\AssemblyInfo.cs" /> | 82 | <Compile Include="Properties\AssemblyInfo.cs" /> |
101 | @@ -83,6 +84,7 @@ | |||
102 | 83 | <Compile Include="Resources\wadl.cs"> | 84 | <Compile Include="Resources\wadl.cs"> |
103 | 84 | <DependentUpon>wadl.xsd</DependentUpon> | 85 | <DependentUpon>wadl.xsd</DependentUpon> |
104 | 85 | </Compile> | 86 | </Compile> |
105 | 87 | <Compile Include="Serialize\SingleFileCodeSerializer.cs" /> | ||
106 | 86 | <Compile Include="Utils.cs" /> | 88 | <Compile Include="Utils.cs" /> |
107 | 87 | <Compile Include="WadlConverter.cs" /> | 89 | <Compile Include="WadlConverter.cs" /> |
108 | 88 | <Compile Include="WadlLoader.cs" /> | 90 | <Compile Include="WadlLoader.cs" /> |
109 | 89 | 91 | ||
110 | === added directory 'LpNet.WadlSharp.Common/Serialize' | |||
111 | === added file 'LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs' | |||
112 | --- LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs 1970-01-01 00:00:00 +0000 | |||
113 | +++ LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs 2010-09-21 10:04:56 +0000 | |||
114 | @@ -0,0 +1,113 @@ | |||
115 | 1 | /* | ||
116 | 2 | * The MIT License (MIT) | ||
117 | 3 | * | ||
118 | 4 | * Copyright 2010 Canonical Ltd. | ||
119 | 5 | * | ||
120 | 6 | * Permission is hereby granted, free of charge, to any person | ||
121 | 7 | * obtaining a copy of this software and associated documentation | ||
122 | 8 | * files (the "Software"), to deal in the Software without | ||
123 | 9 | * restriction, including without limitation the rights to use, | ||
124 | 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
125 | 11 | * copies of the Software, and to permit persons to whom the | ||
126 | 12 | * Software is furnished to do so, subject to the following | ||
127 | 13 | * conditions: | ||
128 | 14 | |||
129 | 15 | * The above copyright notice and this permission notice shall be | ||
130 | 16 | * included in all copies or substantial portions of the Software. | ||
131 | 17 | * | ||
132 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
133 | 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
134 | 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
135 | 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
136 | 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
137 | 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
138 | 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
139 | 25 | * OTHER DEALINGS IN THE SOFTWARE. | ||
140 | 26 | * | ||
141 | 27 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
142 | 28 | */ | ||
143 | 29 | using System.CodeDom; | ||
144 | 30 | using System.CodeDom.Compiler; | ||
145 | 31 | using System.Collections.Generic; | ||
146 | 32 | using System.IO; | ||
147 | 33 | using System.Text; | ||
148 | 34 | using LpNet.WadlSharp.Common.Interface; | ||
149 | 35 | using Microsoft.CSharp; | ||
150 | 36 | |||
151 | 37 | namespace LpNet.WadlSharp.Common.Serialize | ||
152 | 38 | { | ||
153 | 39 | /// <summary> | ||
154 | 40 | /// Serializer that writes all the code that has been generated in a single file. | ||
155 | 41 | /// </summary> | ||
156 | 42 | public class SingleFileCodeSerializer : ICodeSerializer | ||
157 | 43 | { | ||
158 | 44 | #region Variables | ||
159 | 45 | |||
160 | 46 | private readonly string _outputFile; | ||
161 | 47 | |||
162 | 48 | #endregion | ||
163 | 49 | |||
164 | 50 | #region Construtors | ||
165 | 51 | |||
166 | 52 | /// <summary> | ||
167 | 53 | /// Creates a new serializer that will serialize the code in a single file. | ||
168 | 54 | /// </summary> | ||
169 | 55 | /// <param name="outputFile">The path in which the code will be serialized.</param> | ||
170 | 56 | public SingleFileCodeSerializer(string outputFile) | ||
171 | 57 | { | ||
172 | 58 | _outputFile = outputFile; | ||
173 | 59 | Options = new CodeGeneratorOptions(); | ||
174 | 60 | } | ||
175 | 61 | |||
176 | 62 | #endregion | ||
177 | 63 | |||
178 | 64 | #region Implementation of ICodeSerializer | ||
179 | 65 | |||
180 | 66 | /// <summary> | ||
181 | 67 | /// Gets the options to be used in the code generation. | ||
182 | 68 | /// </summary> | ||
183 | 69 | public CodeGeneratorOptions Options { get; private set; } | ||
184 | 70 | |||
185 | 71 | /// <summary> | ||
186 | 72 | /// Gets and sets the string that will use to state that the code was | ||
187 | 73 | /// generated by a tool. | ||
188 | 74 | /// </summary> | ||
189 | 75 | public string AutoGeneratedMessage { get;set; } | ||
190 | 76 | |||
191 | 77 | /// <summary> | ||
192 | 78 | /// Serializes the given code and returns an enumerable with the names of the files | ||
193 | 79 | /// in which the code was serialized. | ||
194 | 80 | /// </summary> | ||
195 | 81 | /// <returns>The enumerable with the files in which the code was serialized.</returns> | ||
196 | 82 | public IEnumerable<string> Serialize(CodeNamespace codeNamespace) | ||
197 | 83 | { | ||
198 | 84 | var sb = new StringBuilder(); | ||
199 | 85 | |||
200 | 86 | var sw = new StringWriter(sb); | ||
201 | 87 | |||
202 | 88 | var provider = new CSharpCodeProvider(); | ||
203 | 89 | provider.GenerateCodeFromNamespace(codeNamespace, | ||
204 | 90 | sw, Options); | ||
205 | 91 | var codeString = sb.ToString(); | ||
206 | 92 | if (!string.IsNullOrEmpty(AutoGeneratedMessage)) | ||
207 | 93 | { | ||
208 | 94 | codeString = string.Format("{0}\n{1}", AutoGeneratedMessage, codeString); | ||
209 | 95 | sb.Append(AutoGeneratedMessage); | ||
210 | 96 | } | ||
211 | 97 | var code = Encoding.ASCII.GetBytes(codeString); | ||
212 | 98 | var outputFileInfo = new FileInfo(_outputFile); | ||
213 | 99 | // if the files exists, delete it | ||
214 | 100 | if (outputFileInfo.Exists) | ||
215 | 101 | { | ||
216 | 102 | outputFileInfo.Delete(); | ||
217 | 103 | } | ||
218 | 104 | |||
219 | 105 | using(var fileStream = new FileStream(outputFileInfo.FullName, FileMode.OpenOrCreate, FileAccess.Write)) | ||
220 | 106 | fileStream.Write(code, 0, code.Length); | ||
221 | 107 | |||
222 | 108 | return new[] {_outputFile}; | ||
223 | 109 | } | ||
224 | 110 | |||
225 | 111 | #endregion | ||
226 | 112 | } | ||
227 | 113 | } | ||
228 | 0 | 114 | ||
229 | === modified file 'LpNet.WadlSharp.Common/WadlConverter.cs' | |||
230 | --- LpNet.WadlSharp.Common/WadlConverter.cs 2010-09-17 16:07:11 +0000 | |||
231 | +++ LpNet.WadlSharp.Common/WadlConverter.cs 2010-09-21 10:04:56 +0000 | |||
232 | @@ -36,11 +36,8 @@ | |||
233 | 36 | using System.CodeDom; | 36 | using System.CodeDom; |
234 | 37 | using Generator; | 37 | using Generator; |
235 | 38 | using Interface.Generator; | 38 | using Interface.Generator; |
236 | 39 | using System.CodeDom.Compiler; | ||
237 | 40 | using System.Text; | ||
238 | 41 | using Microsoft.CSharp; | ||
239 | 42 | 39 | ||
241 | 43 | public class WadlConverter :IConverter | 40 | public class WadlConverter : IConverter |
242 | 44 | { | 41 | { |
243 | 45 | #region Vars | 42 | #region Vars |
244 | 46 | 43 | ||
245 | @@ -73,12 +70,17 @@ | |||
246 | 73 | 70 | ||
247 | 74 | #region IConverter Members | 71 | #region IConverter Members |
248 | 75 | 72 | ||
250 | 76 | public void Convert(string inputFileName, string outputFileName, string rootNamespace) | 73 | /// <summary> |
251 | 74 | /// Gets and sets the code serializer that will be used to serialize the code to a collection | ||
252 | 75 | /// of files. | ||
253 | 76 | /// </summary> | ||
254 | 77 | public ICodeSerializer CodeSerializer { get; set; } | ||
255 | 78 | |||
256 | 79 | public void Convert(string inputFileName, string rootNamespace) | ||
257 | 77 | { | 80 | { |
258 | 78 | #region Check if the input file is correct or not | 81 | #region Check if the input file is correct or not |
259 | 79 | 82 | ||
262 | 80 | FileInfo inputFileInfo = new FileInfo(inputFileName); | 83 | var inputFileInfo = new FileInfo(inputFileName); |
261 | 81 | FileInfo outputFileInfo = new FileInfo(outputFileName); | ||
263 | 82 | 84 | ||
264 | 83 | #endregion | 85 | #endregion |
265 | 84 | 86 | ||
266 | @@ -96,36 +98,11 @@ | |||
267 | 96 | 98 | ||
268 | 97 | #endregion | 99 | #endregion |
269 | 98 | 100 | ||
300 | 99 | #region Get the CSharpCodeProvider | 101 | // serialize the code using the serializer |
301 | 100 | 102 | CodeSerializer.Options.BracingStyle = "C"; | |
302 | 101 | CodeGeneratorOptions options = new CodeGeneratorOptions(); | 103 | CodeSerializer.Options.IndentString = " "; |
303 | 102 | options.BracingStyle = "C"; | 104 | CodeSerializer.Serialize(nameSpace); |
304 | 103 | options.IndentString = " "; | 105 | |
275 | 104 | |||
276 | 105 | StringBuilder sbCode = new StringBuilder(); | ||
277 | 106 | StringWriter sw = new StringWriter(sbCode); | ||
278 | 107 | |||
279 | 108 | CSharpCodeProvider provider = new CSharpCodeProvider(); | ||
280 | 109 | provider.GenerateCodeFromNamespace(nameSpace, | ||
281 | 110 | sw, options); | ||
282 | 111 | |||
283 | 112 | #endregion | ||
284 | 113 | |||
285 | 114 | #region Serialize the DOM | ||
286 | 115 | |||
287 | 116 | string finalCode = sbCode.ToString(); | ||
288 | 117 | |||
289 | 118 | byte[] code = Encoding.ASCII.GetBytes(finalCode); | ||
290 | 119 | |||
291 | 120 | if(outputFileInfo.Exists) | ||
292 | 121 | { | ||
293 | 122 | outputFileInfo.Delete(); | ||
294 | 123 | } | ||
295 | 124 | |||
296 | 125 | FileStream fileStream = new FileStream(outputFileInfo.FullName, FileMode.OpenOrCreate, FileAccess.Write); | ||
297 | 126 | fileStream.Write(code, 0, code.Length); | ||
298 | 127 | |||
299 | 128 | #endregion | ||
305 | 129 | } | 106 | } |
306 | 130 | 107 | ||
307 | 131 | #endregion | 108 | #endregion |
308 | 132 | 109 | ||
309 | === modified file 'LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj' | |||
310 | --- LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj 2010-09-21 10:04:56 +0000 | |||
311 | +++ LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj 2010-09-21 10:04:56 +0000 | |||
312 | @@ -55,6 +55,7 @@ | |||
313 | 55 | <ItemGroup> | 55 | <ItemGroup> |
314 | 56 | <Compile Include="Generator\LpClassFactoryFixture.cs" /> | 56 | <Compile Include="Generator\LpClassFactoryFixture.cs" /> |
315 | 57 | <Compile Include="Generator\LpNamespaceFixture.cs" /> | 57 | <Compile Include="Generator\LpNamespaceFixture.cs" /> |
316 | 58 | <Compile Include="Serialize\SingleFileCodeSerializerFixture.cs" /> | ||
317 | 58 | <Compile Include="WadlLoaderTest.cs" /> | 59 | <Compile Include="WadlLoaderTest.cs" /> |
318 | 59 | <Compile Include="Properties\AssemblyInfo.cs" /> | 60 | <Compile Include="Properties\AssemblyInfo.cs" /> |
319 | 60 | </ItemGroup> | 61 | </ItemGroup> |
320 | 61 | 62 | ||
321 | === added directory 'LpNet.WadlSharp.UnitTest/Serialize' | |||
322 | === added file 'LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs' | |||
323 | --- LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs 1970-01-01 00:00:00 +0000 | |||
324 | +++ LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs 2010-09-21 10:04:56 +0000 | |||
325 | @@ -0,0 +1,111 @@ | |||
326 | 1 | /* | ||
327 | 2 | * The MIT License (MIT) | ||
328 | 3 | * | ||
329 | 4 | * Copyright 2010 Canonical Ltd. | ||
330 | 5 | * | ||
331 | 6 | * Permission is hereby granted, free of charge, to any person | ||
332 | 7 | * obtaining a copy of this software and associated documentation | ||
333 | 8 | * files (the "Software"), to deal in the Software without | ||
334 | 9 | * restriction, including without limitation the rights to use, | ||
335 | 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
336 | 11 | * copies of the Software, and to permit persons to whom the | ||
337 | 12 | * Software is furnished to do so, subject to the following | ||
338 | 13 | * conditions: | ||
339 | 14 | |||
340 | 15 | * The above copyright notice and this permission notice shall be | ||
341 | 16 | * included in all copies or substantial portions of the Software. | ||
342 | 17 | * | ||
343 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
344 | 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
345 | 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
346 | 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
347 | 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
348 | 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
349 | 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
350 | 25 | * OTHER DEALINGS IN THE SOFTWARE. | ||
351 | 26 | * | ||
352 | 27 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
353 | 28 | */ | ||
354 | 29 | using System.CodeDom; | ||
355 | 30 | using System.IO; | ||
356 | 31 | using LpNet.WadlSharp.Common.Serialize; | ||
357 | 32 | using NUnit.Framework; | ||
358 | 33 | |||
359 | 34 | namespace LpNet.WadlSharp.UnitTest.Serialize | ||
360 | 35 | { | ||
361 | 36 | [TestFixture] | ||
362 | 37 | public class SingleFileCodeSerializerFixture | ||
363 | 38 | { | ||
364 | 39 | #region Variables | ||
365 | 40 | |||
366 | 41 | private CodeNamespace _nameSpace; | ||
367 | 42 | private string _expectedOutput; | ||
368 | 43 | private string _outputFilePath; | ||
369 | 44 | private SingleFileCodeSerializer _serializer; | ||
370 | 45 | |||
371 | 46 | #endregion | ||
372 | 47 | |||
373 | 48 | #region Setup | ||
374 | 49 | |||
375 | 50 | [SetUp] | ||
376 | 51 | public void Setup() | ||
377 | 52 | { | ||
378 | 53 | _nameSpace = new CodeNamespace("TestNamespace"); | ||
379 | 54 | _expectedOutput = "namespace TestNamespace\r\n{\r\n \r\n}\r\n"; | ||
380 | 55 | _outputFilePath = Path.GetTempFileName(); | ||
381 | 56 | _serializer = new SingleFileCodeSerializer(_outputFilePath); | ||
382 | 57 | } | ||
383 | 58 | |||
384 | 59 | #endregion | ||
385 | 60 | |||
386 | 61 | #region Tests | ||
387 | 62 | |||
388 | 63 | [Test] | ||
389 | 64 | public void SerializeAutoGeneratedCommentTest() | ||
390 | 65 | { | ||
391 | 66 | // set the auto gen comment to ensure that it is present | ||
392 | 67 | var autogenComment = "/// autogenerated by a tool\r\n"; | ||
393 | 68 | |||
394 | 69 | _serializer.AutoGeneratedMessage = autogenComment; | ||
395 | 70 | _serializer.Options.BracingStyle = "C"; | ||
396 | 71 | _serializer.Options.IndentString = " "; | ||
397 | 72 | _serializer.Serialize(_nameSpace); | ||
398 | 73 | |||
399 | 74 | Assert.IsTrue(File.Exists(_outputFilePath)); | ||
400 | 75 | // read the contents of the file an assert they are the expected | ||
401 | 76 | var contents = File.ReadAllText(_outputFilePath); | ||
402 | 77 | var expected = string.Format("{0}\n{1}", autogenComment, _expectedOutput); | ||
403 | 78 | Assert.AreEqual(expected, contents); | ||
404 | 79 | } | ||
405 | 80 | |||
406 | 81 | [Test] | ||
407 | 82 | public void SerializeFileExistsTest() | ||
408 | 83 | { | ||
409 | 84 | // create the file to see if we do not have any issues | ||
410 | 85 | File.WriteAllText(_outputFilePath, "Test file!"); | ||
411 | 86 | // serialize the code, make sure the file is created with the correct contents | ||
412 | 87 | _serializer.Options.BracingStyle = "C"; | ||
413 | 88 | _serializer.Options.IndentString = " "; | ||
414 | 89 | _serializer.Serialize(_nameSpace); | ||
415 | 90 | Assert.IsTrue(File.Exists(_outputFilePath)); | ||
416 | 91 | // read the contents of the file an assert they are the expected | ||
417 | 92 | var contents = File.ReadAllText(_outputFilePath); | ||
418 | 93 | Assert.AreEqual(_expectedOutput, contents); | ||
419 | 94 | } | ||
420 | 95 | |||
421 | 96 | [Test] | ||
422 | 97 | public void SerializeTest() | ||
423 | 98 | { | ||
424 | 99 | // serialize the code, make sure the file is created with the correct contents | ||
425 | 100 | _serializer.Options.BracingStyle = "C"; | ||
426 | 101 | _serializer.Options.IndentString = " "; | ||
427 | 102 | _serializer.Serialize(_nameSpace); | ||
428 | 103 | Assert.IsTrue(File.Exists(_outputFilePath)); | ||
429 | 104 | // read the contents of the file an assert they are the expected | ||
430 | 105 | var contents = File.ReadAllText(_outputFilePath); | ||
431 | 106 | Assert.AreEqual(_expectedOutput, contents); | ||
432 | 107 | } | ||
433 | 108 | |||
434 | 109 | #endregion | ||
435 | 110 | } | ||
436 | 111 | } | ||
437 | 0 | 112 | ||
438 | === modified file 'WadlConsole/Program.cs' | |||
439 | --- WadlConsole/Program.cs 2010-09-20 13:59:06 +0000 | |||
440 | +++ WadlConsole/Program.cs 2010-09-21 10:04:56 +0000 | |||
441 | @@ -30,6 +30,7 @@ | |||
442 | 30 | using System.IO; | 30 | using System.IO; |
443 | 31 | using System.Net; | 31 | using System.Net; |
444 | 32 | using LpNet.WadlSharp.Common; | 32 | using LpNet.WadlSharp.Common; |
445 | 33 | using LpNet.WadlSharp.Common.Serialize; | ||
446 | 33 | using NDesk.Options; | 34 | using NDesk.Options; |
447 | 34 | 35 | ||
448 | 35 | namespace Wadl | 36 | namespace Wadl |
449 | @@ -89,7 +90,8 @@ | |||
450 | 89 | { | 90 | { |
451 | 90 | converter = new WadlConverter(); | 91 | converter = new WadlConverter(); |
452 | 91 | } | 92 | } |
454 | 92 | converter.Convert(inputFileName, outputFileName, rootNamespace); | 93 | converter.CodeSerializer = new SingleFileCodeSerializer(outputFileName); |
455 | 94 | converter.Convert(inputFileName, rootNamespace); | ||
456 | 93 | } | 95 | } |
457 | 94 | 96 | ||
458 | 95 | #endregion | 97 | #endregion |