Merge lp:~mandel/wadlsharp/refactor_code_serialization into lp:wadlsharp/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
Reviewer Review Type Date Requested Status
Manish Sinha (मनीष सिन्हा) Approve
Review via email: mp+36118@code.launchpad.net

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
=== added file 'LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs'
--- LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs 1970-01-01 00:00:00 +0000
+++ LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs 2010-09-21 10:04:56 +0000
@@ -0,0 +1,60 @@
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright 2010 Canonical Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Authors: Manuel de la Peña <manuel.delapena@canonical.com>
28 */
29using System.CodeDom;
30using System.CodeDom.Compiler;
31using System.Collections.Generic;
32
33namespace LpNet.WadlSharp.Common.Interface
34{
35 /// <summary>
36 /// Interface to be implemented by those objects that know how to serialize
37 /// the generated code
38 /// </summary>
39 public interface ICodeSerializer
40 {
41 /// <summary>
42 /// Gets the options to be used in the code generation.
43 /// </summary>
44 CodeGeneratorOptions Options { get; }
45
46 /// <summary>
47 /// Gets and sets the string that will use to state that the code was
48 /// generated by a tool.
49 /// </summary>
50 string AutoGeneratedMessage { get; set; }
51
52 /// <summary>
53 /// Serializes the given code and returns an enumerable with the names of the files
54 /// in which the code was serialized.
55 /// </summary>
56 /// <returns>The enumerable with the files in which the code was serialized.</returns>
57 IEnumerable<string> Serialize(CodeNamespace codeNamespace);
58 }
59
60}
061
=== modified file 'LpNet.WadlSharp.Common/Interface/IConverter.cs'
--- LpNet.WadlSharp.Common/Interface/IConverter.cs 2010-05-02 07:49:15 +0000
+++ LpNet.WadlSharp.Common/Interface/IConverter.cs 2010-09-21 10:04:56 +0000
@@ -27,8 +27,18 @@
2727
28namespace LpNet.WadlSharp.Common.Interface28namespace LpNet.WadlSharp.Common.Interface
29{29{
30 /// <summary>
31 /// Interface to be implemented by an object that is able to generate the C# code required to use a
32 /// wadl service.
33 /// </summary>
30 public interface IConverter34 public interface IConverter
31 {35 {
32 void Convert(string inputFileName, string outputFileName, string rootNamespace);36 /// <summary>
37 /// Gets and sets the code serializer that will be used to serialize the code to a collection
38 /// of files.
39 /// </summary>
40 ICodeSerializer CodeSerializer { get; set; }
41
42 void Convert(string inputFileName, string rootNamespace);
33 }43 }
34}44}
3545
=== modified file 'LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj'
--- LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj 2010-09-21 10:04:56 +0000
+++ LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj 2010-09-21 10:04:56 +0000
@@ -76,6 +76,7 @@
76 <Compile Include="Interface\Generator\IParamater.cs" />76 <Compile Include="Interface\Generator\IParamater.cs" />
77 <Compile Include="Interface\Generator\IProperty.cs" />77 <Compile Include="Interface\Generator\IProperty.cs" />
78 <Compile Include="Interface\Generator\IReturn.cs" />78 <Compile Include="Interface\Generator\IReturn.cs" />
79 <Compile Include="Interface\ICodeSerializer.cs" />
79 <Compile Include="Interface\IConverter.cs" />80 <Compile Include="Interface\IConverter.cs" />
80 <Compile Include="Interface\ILoader.cs" />81 <Compile Include="Interface\ILoader.cs" />
81 <Compile Include="Properties\AssemblyInfo.cs" />82 <Compile Include="Properties\AssemblyInfo.cs" />
@@ -83,6 +84,7 @@
83 <Compile Include="Resources\wadl.cs">84 <Compile Include="Resources\wadl.cs">
84 <DependentUpon>wadl.xsd</DependentUpon>85 <DependentUpon>wadl.xsd</DependentUpon>
85 </Compile>86 </Compile>
87 <Compile Include="Serialize\SingleFileCodeSerializer.cs" />
86 <Compile Include="Utils.cs" />88 <Compile Include="Utils.cs" />
87 <Compile Include="WadlConverter.cs" />89 <Compile Include="WadlConverter.cs" />
88 <Compile Include="WadlLoader.cs" />90 <Compile Include="WadlLoader.cs" />
8991
=== added directory 'LpNet.WadlSharp.Common/Serialize'
=== added file 'LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs'
--- LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs 1970-01-01 00:00:00 +0000
+++ LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs 2010-09-21 10:04:56 +0000
@@ -0,0 +1,113 @@
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright 2010 Canonical Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Authors: Manuel de la Peña <manuel.delapena@canonical.com>
28 */
29using System.CodeDom;
30using System.CodeDom.Compiler;
31using System.Collections.Generic;
32using System.IO;
33using System.Text;
34using LpNet.WadlSharp.Common.Interface;
35using Microsoft.CSharp;
36
37namespace LpNet.WadlSharp.Common.Serialize
38{
39 /// <summary>
40 /// Serializer that writes all the code that has been generated in a single file.
41 /// </summary>
42 public class SingleFileCodeSerializer : ICodeSerializer
43 {
44 #region Variables
45
46 private readonly string _outputFile;
47
48 #endregion
49
50 #region Construtors
51
52 /// <summary>
53 /// Creates a new serializer that will serialize the code in a single file.
54 /// </summary>
55 /// <param name="outputFile">The path in which the code will be serialized.</param>
56 public SingleFileCodeSerializer(string outputFile)
57 {
58 _outputFile = outputFile;
59 Options = new CodeGeneratorOptions();
60 }
61
62 #endregion
63
64 #region Implementation of ICodeSerializer
65
66 /// <summary>
67 /// Gets the options to be used in the code generation.
68 /// </summary>
69 public CodeGeneratorOptions Options { get; private set; }
70
71 /// <summary>
72 /// Gets and sets the string that will use to state that the code was
73 /// generated by a tool.
74 /// </summary>
75 public string AutoGeneratedMessage { get;set; }
76
77 /// <summary>
78 /// Serializes the given code and returns an enumerable with the names of the files
79 /// in which the code was serialized.
80 /// </summary>
81 /// <returns>The enumerable with the files in which the code was serialized.</returns>
82 public IEnumerable<string> Serialize(CodeNamespace codeNamespace)
83 {
84 var sb = new StringBuilder();
85
86 var sw = new StringWriter(sb);
87
88 var provider = new CSharpCodeProvider();
89 provider.GenerateCodeFromNamespace(codeNamespace,
90 sw, Options);
91 var codeString = sb.ToString();
92 if (!string.IsNullOrEmpty(AutoGeneratedMessage))
93 {
94 codeString = string.Format("{0}\n{1}", AutoGeneratedMessage, codeString);
95 sb.Append(AutoGeneratedMessage);
96 }
97 var code = Encoding.ASCII.GetBytes(codeString);
98 var outputFileInfo = new FileInfo(_outputFile);
99 // if the files exists, delete it
100 if (outputFileInfo.Exists)
101 {
102 outputFileInfo.Delete();
103 }
104
105 using(var fileStream = new FileStream(outputFileInfo.FullName, FileMode.OpenOrCreate, FileAccess.Write))
106 fileStream.Write(code, 0, code.Length);
107
108 return new[] {_outputFile};
109 }
110
111 #endregion
112 }
113}
0114
=== modified file 'LpNet.WadlSharp.Common/WadlConverter.cs'
--- LpNet.WadlSharp.Common/WadlConverter.cs 2010-09-17 16:07:11 +0000
+++ LpNet.WadlSharp.Common/WadlConverter.cs 2010-09-21 10:04:56 +0000
@@ -36,11 +36,8 @@
36 using System.CodeDom;36 using System.CodeDom;
37 using Generator;37 using Generator;
38 using Interface.Generator;38 using Interface.Generator;
39 using System.CodeDom.Compiler;
40 using System.Text;
41 using Microsoft.CSharp;
4239
43 public class WadlConverter :IConverter40 public class WadlConverter : IConverter
44 {41 {
45 #region Vars42 #region Vars
4643
@@ -73,12 +70,17 @@
7370
74 #region IConverter Members71 #region IConverter Members
7572
76 public void Convert(string inputFileName, string outputFileName, string rootNamespace)73 /// <summary>
74 /// Gets and sets the code serializer that will be used to serialize the code to a collection
75 /// of files.
76 /// </summary>
77 public ICodeSerializer CodeSerializer { get; set; }
78
79 public void Convert(string inputFileName, string rootNamespace)
77 {80 {
78 #region Check if the input file is correct or not81 #region Check if the input file is correct or not
7982
80 FileInfo inputFileInfo = new FileInfo(inputFileName);83 var inputFileInfo = new FileInfo(inputFileName);
81 FileInfo outputFileInfo = new FileInfo(outputFileName);
8284
83 #endregion85 #endregion
8486
@@ -96,36 +98,11 @@
9698
97 #endregion99 #endregion
98100
99 #region Get the CSharpCodeProvider101 // serialize the code using the serializer
100102 CodeSerializer.Options.BracingStyle = "C";
101 CodeGeneratorOptions options = new CodeGeneratorOptions();103 CodeSerializer.Options.IndentString = " ";
102 options.BracingStyle = "C";104 CodeSerializer.Serialize(nameSpace);
103 options.IndentString = " ";105
104
105 StringBuilder sbCode = new StringBuilder();
106 StringWriter sw = new StringWriter(sbCode);
107
108 CSharpCodeProvider provider = new CSharpCodeProvider();
109 provider.GenerateCodeFromNamespace(nameSpace,
110 sw, options);
111
112 #endregion
113
114 #region Serialize the DOM
115
116 string finalCode = sbCode.ToString();
117
118 byte[] code = Encoding.ASCII.GetBytes(finalCode);
119
120 if(outputFileInfo.Exists)
121 {
122 outputFileInfo.Delete();
123 }
124
125 FileStream fileStream = new FileStream(outputFileInfo.FullName, FileMode.OpenOrCreate, FileAccess.Write);
126 fileStream.Write(code, 0, code.Length);
127
128 #endregion
129 }106 }
130107
131 #endregion108 #endregion
132109
=== modified file 'LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj'
--- LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj 2010-09-21 10:04:56 +0000
+++ LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj 2010-09-21 10:04:56 +0000
@@ -55,6 +55,7 @@
55 <ItemGroup>55 <ItemGroup>
56 <Compile Include="Generator\LpClassFactoryFixture.cs" />56 <Compile Include="Generator\LpClassFactoryFixture.cs" />
57 <Compile Include="Generator\LpNamespaceFixture.cs" />57 <Compile Include="Generator\LpNamespaceFixture.cs" />
58 <Compile Include="Serialize\SingleFileCodeSerializerFixture.cs" />
58 <Compile Include="WadlLoaderTest.cs" />59 <Compile Include="WadlLoaderTest.cs" />
59 <Compile Include="Properties\AssemblyInfo.cs" />60 <Compile Include="Properties\AssemblyInfo.cs" />
60 </ItemGroup>61 </ItemGroup>
6162
=== added directory 'LpNet.WadlSharp.UnitTest/Serialize'
=== added file 'LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs'
--- LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs 1970-01-01 00:00:00 +0000
+++ LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs 2010-09-21 10:04:56 +0000
@@ -0,0 +1,111 @@
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright 2010 Canonical Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Authors: Manuel de la Peña <manuel.delapena@canonical.com>
28 */
29using System.CodeDom;
30using System.IO;
31using LpNet.WadlSharp.Common.Serialize;
32using NUnit.Framework;
33
34namespace LpNet.WadlSharp.UnitTest.Serialize
35{
36 [TestFixture]
37 public class SingleFileCodeSerializerFixture
38 {
39 #region Variables
40
41 private CodeNamespace _nameSpace;
42 private string _expectedOutput;
43 private string _outputFilePath;
44 private SingleFileCodeSerializer _serializer;
45
46 #endregion
47
48 #region Setup
49
50 [SetUp]
51 public void Setup()
52 {
53 _nameSpace = new CodeNamespace("TestNamespace");
54 _expectedOutput = "namespace TestNamespace\r\n{\r\n \r\n}\r\n";
55 _outputFilePath = Path.GetTempFileName();
56 _serializer = new SingleFileCodeSerializer(_outputFilePath);
57 }
58
59 #endregion
60
61 #region Tests
62
63 [Test]
64 public void SerializeAutoGeneratedCommentTest()
65 {
66 // set the auto gen comment to ensure that it is present
67 var autogenComment = "/// autogenerated by a tool\r\n";
68
69 _serializer.AutoGeneratedMessage = autogenComment;
70 _serializer.Options.BracingStyle = "C";
71 _serializer.Options.IndentString = " ";
72 _serializer.Serialize(_nameSpace);
73
74 Assert.IsTrue(File.Exists(_outputFilePath));
75 // read the contents of the file an assert they are the expected
76 var contents = File.ReadAllText(_outputFilePath);
77 var expected = string.Format("{0}\n{1}", autogenComment, _expectedOutput);
78 Assert.AreEqual(expected, contents);
79 }
80
81 [Test]
82 public void SerializeFileExistsTest()
83 {
84 // create the file to see if we do not have any issues
85 File.WriteAllText(_outputFilePath, "Test file!");
86 // serialize the code, make sure the file is created with the correct contents
87 _serializer.Options.BracingStyle = "C";
88 _serializer.Options.IndentString = " ";
89 _serializer.Serialize(_nameSpace);
90 Assert.IsTrue(File.Exists(_outputFilePath));
91 // read the contents of the file an assert they are the expected
92 var contents = File.ReadAllText(_outputFilePath);
93 Assert.AreEqual(_expectedOutput, contents);
94 }
95
96 [Test]
97 public void SerializeTest()
98 {
99 // serialize the code, make sure the file is created with the correct contents
100 _serializer.Options.BracingStyle = "C";
101 _serializer.Options.IndentString = " ";
102 _serializer.Serialize(_nameSpace);
103 Assert.IsTrue(File.Exists(_outputFilePath));
104 // read the contents of the file an assert they are the expected
105 var contents = File.ReadAllText(_outputFilePath);
106 Assert.AreEqual(_expectedOutput, contents);
107 }
108
109 #endregion
110 }
111}
0112
=== modified file 'WadlConsole/Program.cs'
--- WadlConsole/Program.cs 2010-09-20 13:59:06 +0000
+++ WadlConsole/Program.cs 2010-09-21 10:04:56 +0000
@@ -30,6 +30,7 @@
30using System.IO;30using System.IO;
31using System.Net;31using System.Net;
32using LpNet.WadlSharp.Common;32using LpNet.WadlSharp.Common;
33using LpNet.WadlSharp.Common.Serialize;
33using NDesk.Options;34using NDesk.Options;
3435
35namespace Wadl36namespace Wadl
@@ -89,7 +90,8 @@
89 {90 {
90 converter = new WadlConverter();91 converter = new WadlConverter();
91 }92 }
92 converter.Convert(inputFileName, outputFileName, rootNamespace);93 converter.CodeSerializer = new SingleFileCodeSerializer(outputFileName);
94 converter.Convert(inputFileName, rootNamespace);
93 }95 }
9496
95 #endregion97 #endregion

Subscribers

People subscribed via source and target branches