Datei: UnitTestGenerator/TestGenerator/ClassGenerator.cs

Last Commit (72f6226)
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.Collections;
25 using System.Text;
26 using System.IO;
27 using CodeGenerator;
28 using System.Collections.Generic;
29
30 namespace TestGenerator
31 {
32 ····/// <summary>
33 ····/// Summary for ClassGenerator.
34 ····/// </summary>
35 ····public class ClassGenerator
36 ····{
37 ········IEnumerable<RelInfo> relInfos;
38 ········string fileName;
39 ········StreamWriter sw;
40 ········int count;
41 ········readonly string nameSpace = "RelationTestClasses";
42
43 ········public ClassGenerator( IEnumerable<RelInfo> relInfos )
44 ········{
45 ············this.relInfos = relInfos;
46 fileName = Path. Combine( AppDomain. CurrentDomain. BaseDirectory, @". . \. . \PersistentClasses\PersistentClasses. cs") ;
47 ········}
48
49 ········void AddRelationAccessor(Class ownBaseClass, Class otherBaseClass, RelInfo ri)
50 ········{
51 ············Function func = ownBaseClass.NewFunction("void", "AssignRelation", new string[]{otherBaseClass.Name}, new string[]{"relObj"});
52 ············func.AccessModifier = "public";
53 ············if (ri.IsList)
54 ················func.Statements.Add("relField.Add(relObj);");
55 ············else
56 ················func.Statements.Add("relField = relObj;");
57 ········}
58
59 ········void AddRelationRemover(Class ownBaseClass, RelInfo ri)
60 ········{
61 ············if (!ri.IsList)
62 ················return;
63 ············Function func = ownBaseClass.NewFunction("void", "RemoveRelatedObject");
64 ············func.AccessModifier = "public";
65 ············func.Statements.Add("relField.RemoveAt(0);");
66 ········}
67
68
69 ········void GenerateClassGroup(RelInfo ri)
70 ········{
71 ············Test test = new Test(ri, this.nameSpace);
72
73 ············PersistentClass ownBaseClass = test.OwnClass;
74 ············PersistentClass otherBaseClass = test.OtherClass;
75 ············ownBaseClass.AddVarAndProperty("int", "dummy");
76 ············PersistentClass ownDerivedClass;
77 ············ownBaseClass.NewRelation(ri, otherBaseClass.Name);
78 ············AddRelationAccessor(ownBaseClass, otherBaseClass, ri);
79 ············AddRelationRemover(ownBaseClass, ri);
80
81 ············sw.WriteLine(ownBaseClass.ToString());
82 ············if (ri.OwnPoly)
83 ············{
84 ················ownDerivedClass = test.OwnDerivedClass;
85 ················sw.WriteLine(ownDerivedClass.ToString());
86 ············}
87
88 ············
89 ············// Right class
90 ············otherBaseClass.AddVarAndProperty("int", "dummy");
91 ············if (ri.IsBi)
92 ················otherBaseClass.NewForeignRelation(ri, ownBaseClass.Name);
93 ············sw.WriteLine(otherBaseClass.ToString());
94 ············if (ri.OtherPoly)
95 ············{
96 ················Class otherDerivedClass = test.OtherDerivedClass;
97 ················sw.WriteLine(otherDerivedClass.ToString());
98 ············}
99 ············count += 2;
100 ············if (ri.OwnPoly)
101 ················count++;
102 ············if (ri.OtherPoly)
103 ················count++;
104 ········}
105
106
107 ········public void Generate()
108 ········{
109 ············sw = new StreamWriter(fileName, false, Encoding.UTF8);
110 ············sw.WriteLine( @"//
111 // Copyright (c) 2002-2016 Mirko Matytschak
112 // (www.netdataobjects.de)
113 //
114 // Author: Mirko Matytschak
115 //
116 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
117 // documentation files (the ""Software""), to deal in the Software without restriction, including without limitation
118 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
119 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
120 // conditions:
121
122 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
123 // of the Software.
124 //
125 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
126 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
127 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
128 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
129 // DEALINGS IN THE SOFTWARE.
130
131 " );
132 ············sw.WriteLine("using System;");
133 ············sw.WriteLine("using System.Collections.Generic;");
134 ············sw.WriteLine("using NDO;\n");
135 ············sw.WriteLine( "using NDO.Mapping.Attributes;\n" );
136 ············sw.WriteLine( $"namespace··{this.nameSpace}" );
137 ············sw.WriteLine("{\n");
138 ············foreach(RelInfo ri in relInfos)
139 ················GenerateClassGroup(ri);
140 ············Console.WriteLine("Number of Classes: " + count);
141 ············sw.WriteLine("}");
142 ············sw.Close();
143 ········}
144 ····}
145 }
146
New 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.Collections;
25 using System.Text;
26 using System.IO;
27 using CodeGenerator;
28 using System.Collections.Generic;
29
30 namespace TestGenerator
31 {
32 ····/// <summary>
33 ····/// Summary for ClassGenerator.
34 ····/// </summary>
35 ····public class ClassGenerator
36 ····{
37 ········IEnumerable<RelInfo> relInfos;
38 ········string fileName;
39 ········StreamWriter sw;
40 ········int count;
41 ········readonly string nameSpace = "RelationTestClasses";
42
43 ········public ClassGenerator( IEnumerable<RelInfo> relInfos )
44 ········{
45 ············this.relInfos = relInfos;
46 fileName = Path. Combine( AppDomain. CurrentDomain. BaseDirectory, @". . \. . \. . \PersistentClasses\PersistentClasses. cs") ;
47 ········}
48
49 ········void AddRelationAccessor(Class ownBaseClass, Class otherBaseClass, RelInfo ri)
50 ········{
51 ············Function func = ownBaseClass.NewFunction("void", "AssignRelation", new string[]{otherBaseClass.Name}, new string[]{"relObj"});
52 ············func.AccessModifier = "public";
53 ············if (ri.IsList)
54 ················func.Statements.Add("relField.Add(relObj);");
55 ············else
56 ················func.Statements.Add("relField = relObj;");
57 ········}
58
59 ········void AddRelationRemover(Class ownBaseClass, RelInfo ri)
60 ········{
61 ············if (!ri.IsList)
62 ················return;
63 ············Function func = ownBaseClass.NewFunction("void", "RemoveRelatedObject");
64 ············func.AccessModifier = "public";
65 ············func.Statements.Add("relField.RemoveAt(0);");
66 ········}
67
68
69 ········void GenerateClassGroup(RelInfo ri)
70 ········{
71 ············Test test = new Test(ri, this.nameSpace);
72
73 ············PersistentClass ownBaseClass = test.OwnClass;
74 ············PersistentClass otherBaseClass = test.OtherClass;
75 ············ownBaseClass.AddVarAndProperty("int", "dummy");
76 ············PersistentClass ownDerivedClass;
77 ············ownBaseClass.NewRelation(ri, otherBaseClass.Name);
78 ············AddRelationAccessor(ownBaseClass, otherBaseClass, ri);
79 ············AddRelationRemover(ownBaseClass, ri);
80
81 ············sw.WriteLine(ownBaseClass.ToString());
82 ············if (ri.OwnPoly)
83 ············{
84 ················ownDerivedClass = test.OwnDerivedClass;
85 ················sw.WriteLine(ownDerivedClass.ToString());
86 ············}
87
88 ············
89 ············// Right class
90 ············otherBaseClass.AddVarAndProperty("int", "dummy");
91 ············if (ri.IsBi)
92 ················otherBaseClass.NewForeignRelation(ri, ownBaseClass.Name);
93 ············sw.WriteLine(otherBaseClass.ToString());
94 ············if (ri.OtherPoly)
95 ············{
96 ················Class otherDerivedClass = test.OtherDerivedClass;
97 ················sw.WriteLine(otherDerivedClass.ToString());
98 ············}
99 ············count += 2;
100 ············if (ri.OwnPoly)
101 ················count++;
102 ············if (ri.OtherPoly)
103 ················count++;
104 ········}
105
106
107 ········public void Generate()
108 ········{
109 ············sw = new StreamWriter(fileName, false, Encoding.UTF8);
110 ············sw.WriteLine( @"//
111 // Copyright (c) 2002-2016 Mirko Matytschak
112 // (www.netdataobjects.de)
113 //
114 // Author: Mirko Matytschak
115 //
116 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
117 // documentation files (the ""Software""), to deal in the Software without restriction, including without limitation
118 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
119 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
120 // conditions:
121
122 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
123 // of the Software.
124 //
125 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
126 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
127 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
128 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
129 // DEALINGS IN THE SOFTWARE.
130
131 " );
132 ············sw.WriteLine("using System;");
133 ············sw.WriteLine("using System.Collections.Generic;");
134 ············sw.WriteLine("using NDO;\n");
135 ············sw.WriteLine( "using NDO.Mapping.Attributes;\n" );
136 ············sw.WriteLine( $"namespace··{this.nameSpace}" );
137 ············sw.WriteLine("{\n");
138 ············foreach(RelInfo ri in relInfos)
139 ················GenerateClassGroup(ri);
140 ············Console.WriteLine("Number of Classes: " + count);
141 ············sw.WriteLine("}");
142 ············sw.Close();
143 ········}
144 ····}
145 }
146