Datei: UnitTestGenerator/CodeGenerator/Class.cs

Last Commit (0bec001)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Text;
25 using System.Collections;
26 using System.Collections.Generic;
27 using System.IO;
28
29 namespace CodeGenerator
30 {
31 ····/// <summary>
32 ····/// Renders classes.
33 ····/// </summary>
34 ····public class Class
35 ····{
36 ········string name;
37 ········Class baseClass;
38 ········bool isAbstract;
39 private readonly string nameSpace;
40
41 ········List<string> statements = new List<string>();
42 ········List<string> attributes = new List<string>();
43
44 ········public Class(bool isAbstract, string nameSpace, string name, Class baseClass)
45 ········{
46 ············this.name = name;
47 ············this.baseClass = baseClass;
48 ············this.isAbstract = isAbstract;
49 ············this.nameSpace = nameSpace;
50 ········}
51
52 ········public Class(string nameSpace, string name) : this(false, nameSpace, name, null)
53 ········{
54 ········}
55
56 ········public List<string> Attributes
57 ········{
58 ············get { return attributes; }
59 ········}
60
61
62 ········List<Property> properties = new List<Property>();
63 ········public List<Property> Properties
64 ········{
65 ············get { return properties; }
66 ········}
67 ········List<Function> functions = new List<Function>();
68 ········public List<Function> Functions
69 ········{
70 ············get { return functions; }
71 ········}
72 ········public List<string> Statements
73 ········{
74 ············get { return statements; }
75 ········}
76
77 ········ArrayList fields = new ArrayList();
78 ········public ArrayList Fields
79 ········{
80 ············get { return fields; }
81 ········}
82
83 ········List<string> genericParameters = new List<string>();
84 ········public List<string> GenericParameters
85 ········{
86 ············get { return genericParameters; }
87 ········}
88
89
90 ········protected virtual void AddStringCollection(StringBuilder sb, List<string> sc)
91 ········{
92 ············foreach(string s in sc)
93 ············{
94 ················sb.Append('\t');
95 ················sb.Append(s);
96 ················sb.Append('\n');
97 ············}
98 ········}
99
100 ········protected virtual void GenerateStatements(StringBuilder sb)
101 ········{
102 ············AddStringCollection(sb, statements);
103 ········}
104
105 ········protected virtual void GenerateProperties(StringBuilder sb)
106 ········{
107 ············foreach(Property p in properties)
108 ················AddStringCollection(sb, p.Text);
109 ········}
110
111 ········protected virtual void GenerateFunctions(StringBuilder sb)
112 ········{
113 ············foreach (Function f in functions)
114 ············{
115 ················if (f.Statements.Count > 0)
116 ················AddStringCollection( sb, f.Text );
117 ············}
118 ········}
119
120 ········protected virtual void GenerateFields(StringBuilder sb)
121 ········{
122 ············foreach (Field f in fields)
123 ················AddStringCollection(sb, f.Text);
124 ········}
125
126 ········protected virtual void GenerateFooter(StringBuilder sb)
127 ········{
128 ············sb.Append("}\n\n");
129 ········}
130
131 ········public override string··ToString()
132 ········{
133 ············StringBuilder sb = new StringBuilder();
134 ············if (attributes.Count > 0)
135 ············{
136 ················sb.Append('[');
137 ················for (int i = 0; i < attributes.Count; i++)
138 ················{
139 ····················sb.Append(attributes[i]);
140 ····················if (i < attributes.Count - 1)
141 ························sb.Append(", ");
142 ················}
143 ················sb.Append("]\n");
144 ············}
145 ············sb.Append(this.GenerateHeader(this.isAbstract, this.name, this.baseClass));
146 ············GenerateStatements(sb);
147 ············GenerateFields(sb);
148 ············GenerateProperties(sb);
149 ············GenerateFunctions(sb);
150 ············GenerateFooter(sb);
151 ············return sb.ToString();
152 ········}
153
154 ········public string Name => name;
155
156 ········public string FullName => nameSpace + '.' + name;
157
158
159 ········public bool IsAbstract
160 ········{
161 ············get { return isAbstract; }
162 ········}
163 ········protected virtual string GenerateGenericArgs()
164 ········{
165 ············int genParCount = this.genericParameters.Count;
166 ············if (genParCount == 0)
167 ················return string.Empty;
168
169 ············StringBuilder sb = new StringBuilder();
170 ············if (genParCount > 0)
171 ············{
172 ················sb.Append('<');
173 ················for (int i = 0; i < genParCount; i++)
174 ················{
175 ····················sb.Append(this.genericParameters[i]);
176 ····················if (i < genParCount - 1)
177 ························sb.Append(',');
178 ················}
179 ················sb.Append('>');
180 ············}
181 ············return sb.ToString();
182 ········}
183
184 ········protected virtual string GenerateHeader(bool isAbstract, string name, Class baseClass)
185 ········{
186 ············StringBuilder sb = new StringBuilder("public ");
187 ············if (isAbstract)
188 ················sb.Append("abstract ");
189 ············sb.Append("class ");
190 ············sb.Append(name);
191 ············sb.Append(GenerateGenericArgs());
192 ············if (baseClass != null)
193 ············{
194 ················sb.Append(" : ");
195 ················sb.Append(baseClass.name + baseClass.GenerateGenericArgs());
196 ············}
197 ············sb.Append("\n{\n");
198 ············return sb.ToString();
199 ········}
200
201 ········public Function NewFunction(string resultType, string name)
202 ········{
203 ············Function f = new Function(resultType, name);
204 ············this.functions.Add(f);
205 ············return f;
206 ········}
207
208 ········public Function NewFunction(string resultType, string name, string[] paramTypes, string[] paramNames)
209 ········{
210 ············Function f = new Function(resultType, name, paramTypes, paramNames);
211 ············this.functions.Add(f);
212 ············return f;
213 ········}
214
215
216 ········public void AddVarAndProperty(string type, string varName)
217 ········{
218 ············this.fields.Add(new Field(type, varName));
219 ············this.properties.Add(new Property(type, varName, true, true));
220 ········}
221
222 ········public void AddField(string type, string varName)
223 ········{
224 ············this.fields.Add( new Field( type, varName ) );
225 ········}
226
227 ····}
228 }
229
New Commit (6d63e12)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Text;
25 using System.Collections;
26 using System.Collections.Generic;
27 using System.IO;
28
29 namespace CodeGenerator
30 {
31 ····/// <summary>
32 ····/// Renders classes.
33 ····/// </summary>
34 ····public class Class
35 ····{
36 ········string name;
37 ········Class baseClass;
38 ········bool isAbstract;
39 protected readonly string nameSpace;
40
41 ········List<string> statements = new List<string>();
42 ········List<string> attributes = new List<string>();
43
44 ········public Class(bool isAbstract, string nameSpace, string name, Class baseClass)
45 ········{
46 ············this.name = name;
47 ············this.baseClass = baseClass;
48 ············this.isAbstract = isAbstract;
49 ············this.nameSpace = nameSpace;
50 ········}
51
52 ········public Class(string nameSpace, string name) : this(false, nameSpace, name, null)
53 ········{
54 ········}
55
56 ········public List<string> Attributes
57 ········{
58 ············get { return attributes; }
59 ········}
60
61
62 ········List<Property> properties = new List<Property>();
63 ········public List<Property> Properties
64 ········{
65 ············get { return properties; }
66 ········}
67 ········List<Function> functions = new List<Function>();
68 ········public List<Function> Functions
69 ········{
70 ············get { return functions; }
71 ········}
72 ········public List<string> Statements
73 ········{
74 ············get { return statements; }
75 ········}
76
77 ········ArrayList fields = new ArrayList();
78 ········public ArrayList Fields
79 ········{
80 ············get { return fields; }
81 ········}
82
83 ········List<string> genericParameters = new List<string>();
84 ········public List<string> GenericParameters
85 ········{
86 ············get { return genericParameters; }
87 ········}
88
89
90 ········protected virtual void AddStringCollection(StringBuilder sb, List<string> sc)
91 ········{
92 ············foreach(string s in sc)
93 ············{
94 ················sb.Append('\t');
95 ················sb.Append(s);
96 ················sb.Append('\n');
97 ············}
98 ········}
99
100 ········protected virtual void GenerateStatements(StringBuilder sb)
101 ········{
102 ············AddStringCollection(sb, statements);
103 ········}
104
105 ········protected virtual void GenerateProperties(StringBuilder sb)
106 ········{
107 ············foreach(Property p in properties)
108 ················AddStringCollection(sb, p.Text);
109 ········}
110
111 ········protected virtual void GenerateFunctions(StringBuilder sb)
112 ········{
113 ············foreach (Function f in functions)
114 ············{
115 ················if (f.Statements.Count > 0)
116 ················AddStringCollection( sb, f.Text );
117 ············}
118 ········}
119
120 ········protected virtual void GenerateFields(StringBuilder sb)
121 ········{
122 ············foreach (Field f in fields)
123 ················AddStringCollection(sb, f.Text);
124 ········}
125
126 ········protected virtual void GenerateFooter(StringBuilder sb)
127 ········{
128 ············sb.Append("}\n\n");
129 ········}
130
131 ········public override string··ToString()
132 ········{
133 ············StringBuilder sb = new StringBuilder();
134 ············if (attributes.Count > 0)
135 ············{
136 ················sb.Append('[');
137 ················for (int i = 0; i < attributes.Count; i++)
138 ················{
139 ····················sb.Append(attributes[i]);
140 ····················if (i < attributes.Count - 1)
141 ························sb.Append(", ");
142 ················}
143 ················sb.Append("]\n");
144 ············}
145 ············sb.Append(this.GenerateHeader(this.isAbstract, this.name, this.baseClass));
146 ············GenerateStatements(sb);
147 ············GenerateFields(sb);
148 ············GenerateProperties(sb);
149 ············GenerateFunctions(sb);
150 ············GenerateFooter(sb);
151 ············return sb.ToString();
152 ········}
153
154 ········public string Name => name;
155
156 ········public string FullName => nameSpace + '.' + name;
157
158
159 ········public bool IsAbstract
160 ········{
161 ············get { return isAbstract; }
162 ········}
163 ········protected virtual string GenerateGenericArgs()
164 ········{
165 ············int genParCount = this.genericParameters.Count;
166 ············if (genParCount == 0)
167 ················return string.Empty;
168
169 ············StringBuilder sb = new StringBuilder();
170 ············if (genParCount > 0)
171 ············{
172 ················sb.Append('<');
173 ················for (int i = 0; i < genParCount; i++)
174 ················{
175 ····················sb.Append(this.genericParameters[i]);
176 ····················if (i < genParCount - 1)
177 ························sb.Append(',');
178 ················}
179 ················sb.Append('>');
180 ············}
181 ············return sb.ToString();
182 ········}
183
184 ········protected virtual string GenerateHeader(bool isAbstract, string name, Class baseClass)
185 ········{
186 ············StringBuilder sb = new StringBuilder("public ");
187 ············if (isAbstract)
188 ················sb.Append("abstract ");
189 ············sb.Append("class ");
190 ············sb.Append(name);
191 ············sb.Append(GenerateGenericArgs());
192 ············if (baseClass != null)
193 ············{
194 ················sb.Append(" : ");
195 ················sb.Append(baseClass.name + baseClass.GenerateGenericArgs());
196 ············}
197 ············sb.Append("\n{\n");
198 ············return sb.ToString();
199 ········}
200
201 ········public Function NewFunction(string resultType, string name)
202 ········{
203 ············Function f = new Function(resultType, name);
204 ············this.functions.Add(f);
205 ············return f;
206 ········}
207
208 ········public Function NewFunction(string resultType, string name, string[] paramTypes, string[] paramNames)
209 ········{
210 ············Function f = new Function(resultType, name, paramTypes, paramNames);
211 ············this.functions.Add(f);
212 ············return f;
213 ········}
214
215
216 ········public void AddVarAndProperty(string type, string varName)
217 ········{
218 ············this.fields.Add(new Field(type, varName));
219 ············this.properties.Add(new Property(type, varName, true, true));
220 ········}
221
222 ········public void AddField(string type, string varName)
223 ········{
224 ············this.fields.Add( new Field( type, varName ) );
225 ········}
226
227 ····}
228 }
229