Skip to content

Commit bc05b32

Browse files
committed
All annotation generation done but structure. Need to refactor away the info entity. Switched to GenerateCodeFromNamespace to remove pesky codegenerator comment with runtime version. Should make simpler comment about codegen.
1 parent 49fc195 commit bc05b32

15 files changed

+227
-97
lines changed

Umbraco.CodeGen.Tests/AnnotationGeneratorAcceptanceTests.cs

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public void BuildCode_GeneratesCodeForDocumentType()
2424
factory = CreateDocTypeGenerator;
2525

2626
Generate(contentType);
27+
Assert.Inconclusive("Not finished yet");
2728
}
2829

2930
[Test]
@@ -34,6 +35,7 @@ public void BuildCode_GeneratesCodeForMediaType()
3435
contentType.Info.Description = "Oy, need a description to boot!";
3536

3637
Generate(contentType);
38+
Assert.Inconclusive("Not finished yet");
3739
}
3840

3941
private void Generate(ContentType contentType)
@@ -52,7 +54,6 @@ private void Generate(ContentType contentType)
5254
generator.Generate(contentType, writer);
5355

5456
Console.WriteLine(stringBuilder.ToString());
55-
Assert.Inconclusive("Not finished yet");
5657
}
5758

5859
public override CodeGeneratorBase Create(ContentTypeConfiguration configuration, IEnumerable<DataTypeDefinition> dataTypes)
@@ -62,40 +63,29 @@ public override CodeGeneratorBase Create(ContentTypeConfiguration configuration,
6263

6364
public CodeGeneratorBase CreateDocTypeGenerator(ContentTypeConfiguration configuration, IEnumerable<DataTypeDefinition> dataTypes)
6465
{
65-
return new NamespaceGenerator(
66-
configuration,
67-
new ImportsGenerator(configuration),
68-
new ClassGenerator(configuration,
69-
new CompositeCodeGenerator(
70-
configuration,
71-
new EntityNameGenerator(configuration),
72-
new AttributeCodeGenerator(
73-
"DocumentType",
74-
configuration,
75-
new EntityDescriptionGenerator(configuration),
76-
new CommonInfoGenerator(configuration),
77-
new DocumentTypeInfoGenerator(configuration)
78-
)
79-
),
80-
new CtorGenerator(configuration),
81-
new PropertiesGenerator(
82-
configuration,
83-
new PropertyDeclarationGenerator(
84-
configuration,
85-
dataTypes.ToList(),
86-
new EntityNameGenerator(configuration),
87-
new AttributeCodeGenerator(
88-
"GenericProperty",
89-
configuration,
90-
new EntityDescriptionGenerator(configuration)
91-
),
92-
new PropertyBodyGenerator(configuration)
93-
)
94-
)
95-
)
66+
return CreateGenerators(
67+
configuration,
68+
dataTypes,
69+
"DocumentType",
70+
new DocumentTypeInfoGenerator(configuration)
9671
);
9772
}
73+
9874
public CodeGeneratorBase CreateMediaTypeGenerator(ContentTypeConfiguration configuration, IEnumerable<DataTypeDefinition> dataTypes)
75+
{
76+
return CreateGenerators(
77+
configuration,
78+
dataTypes,
79+
"MediaType",
80+
new CommonInfoGenerator(configuration)
81+
);
82+
}
83+
84+
private static CodeGeneratorBase CreateGenerators(
85+
ContentTypeConfiguration configuration,
86+
IEnumerable<DataTypeDefinition> dataTypes,
87+
string attributeName,
88+
CodeGeneratorBase infoGenerator)
9989
{
10090
return new NamespaceGenerator(
10191
configuration,
@@ -105,12 +95,11 @@ public CodeGeneratorBase CreateMediaTypeGenerator(ContentTypeConfiguration confi
10595
configuration,
10696
new EntityNameGenerator(configuration),
10797
new AttributeCodeGenerator(
108-
"MediaType",
98+
attributeName,
10999
configuration,
110-
new EntityDescriptionGenerator(configuration),
111-
new CommonInfoGenerator(configuration)
112-
)
113-
),
100+
infoGenerator
101+
)
102+
),
114103
new CtorGenerator(configuration),
115104
new PropertiesGenerator(
116105
configuration,
@@ -121,10 +110,10 @@ public CodeGeneratorBase CreateMediaTypeGenerator(ContentTypeConfiguration confi
121110
new AttributeCodeGenerator(
122111
"GenericProperty",
123112
configuration,
124-
new EntityDescriptionGenerator(configuration)
113+
new PropertyInfoGenerator(configuration, dataTypes.ToList())
125114
),
126115
new PropertyBodyGenerator(configuration)
127-
)
116+
)
128117
)
129118
)
130119
);

Umbraco.CodeGen.Tests/CodeGeneratorAcceptanceTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using System;
2-
using System.CodeDom;
3-
using System.CodeDom.Compiler;
4-
using System.Collections.Generic;
52
using System.IO;
63
using System.Text;
7-
using Microsoft.CSharp;
84
using NUnit.Framework;
95
using Umbraco.CodeGen.Configuration;
106
using Umbraco.CodeGen.Definitions;

Umbraco.CodeGen.Tests/Generators/Annotated/CommonInfoGeneratorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public void SetUp()
3030
}
3131

3232
[Test]
33-
[ExpectedException(typeof(Exception), ExpectedMessage = "Common info generator must be used on an attribute declaration")]
34-
public void Generate_NotCodeTypeDeclaration_Throws()
33+
[ExpectedException(typeof(InvalidCastException), ExpectedMessage = "Unable to cast object of type 'System.CodeDom.CodeMemberProperty' to type 'System.CodeDom.CodeAttributeDeclaration'.")]
34+
public void Generate_NotCodeAttributeDeclaration_Throws()
3535
{
3636
Generator.Generate(new CodeMemberProperty(), info);;
3737
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System;
2+
using System.CodeDom;
3+
using NUnit.Framework;
4+
using Umbraco.CodeGen.Configuration;
5+
using Umbraco.CodeGen.Definitions;
6+
using Umbraco.CodeGen.Generators.Annotated;
7+
using Umbraco.CodeGen.Tests.TestHelpers;
8+
9+
namespace Umbraco.CodeGen.Tests.Generators.Annotated
10+
{
11+
public class PropertyInfoCodeGeneratorTests : AnnotationCodeGeneratorTestBase
12+
{
13+
private CodeGeneratorConfiguration codeGenConfig;
14+
private GenericProperty property;
15+
private CodeAttributeDeclaration attribute;
16+
17+
[SetUp]
18+
public void SetUp()
19+
{
20+
codeGenConfig = new CodeGeneratorConfiguration();
21+
Configuration = codeGenConfig.MediaTypes;
22+
Generator = new PropertyInfoGenerator(
23+
Configuration,
24+
TestDataTypeProvider.All
25+
);
26+
attribute = new CodeAttributeDeclaration();
27+
property = new GenericProperty{Alias="anEntity"};
28+
}
29+
30+
[Test]
31+
public void Generate_Definition_WhenDefinitionExists_IsDefinitionName()
32+
{
33+
property.Definition = TestDataTypeProvider.Numeric.DefinitionId;
34+
Generate();
35+
Assert.AreEqual("Numeric", FindAttributeArgumentValue(attribute, "Definition"));
36+
}
37+
38+
[Test]
39+
public void Generate_Definition_WhenDefinitionDoesNotExist_IsDefaultDefinition()
40+
{
41+
Generate();
42+
Assert.AreEqual("Textstring", FindAttributeArgumentValue(attribute, "Definition"));
43+
}
44+
45+
[Test]
46+
[ExpectedException(typeof(Exception), ExpectedMessage = "TypeMappings/DefaultDefinitionId not set. Cannot guess default definition.")]
47+
public void Generate_Definition_WhenDefinitionDoesNotExist_DefaultNotConfigured_Throws()
48+
{
49+
codeGenConfig.DefaultDefinitionId = null;
50+
Generate();
51+
}
52+
53+
[Test]
54+
public void Generate_Tab_WhenSet_TabArgumentHasValue()
55+
{
56+
property.Tab = "A tab";
57+
Generate();
58+
Assert.AreEqual("A tab", FindAttributeArgumentValue(attribute, "Tab"));
59+
}
60+
61+
[Test]
62+
[TestCase(null)]
63+
[TestCase("")]
64+
[TestCase(" ")]
65+
public void Generate_Tab_WhenEmpty_OmitsTabArgument(string value)
66+
{
67+
property.Tab = value;
68+
Generate();
69+
Assert.IsNull(FindAttributeArgument(attribute, "Tab"));
70+
}
71+
72+
[Test]
73+
public void Generate_Mandatory_WhenTrue_MandatoryArgumentIsSet()
74+
{
75+
property.Mandatory = true;
76+
Generate();
77+
Assert.IsTrue((bool)FindAttributeArgumentValue(attribute, "Mandatory"));
78+
}
79+
80+
[Test]
81+
public void Generate_Mandatory_WhenFalse_OmitsMandatoryArgument()
82+
{
83+
Generate();
84+
Assert.IsNull(FindAttributeArgument(attribute, "Mandatory"));
85+
}
86+
87+
[Test]
88+
public void Generate_Validation_HasValue_ValidationArgumentHasValue()
89+
{
90+
property.Validation = "[a-z]";
91+
Generate();
92+
Assert.AreEqual("[a-z]", FindAttributeArgumentValue(attribute, "Validation"));
93+
}
94+
95+
[Test]
96+
[TestCase(null)]
97+
[TestCase("")]
98+
[TestCase(" ")]
99+
public void Generate_Validation_IsNullOrEmpty_OmitsValidationArgument(string value)
100+
{
101+
property.Validation = value;
102+
Generate();
103+
Assert.IsNull(FindAttributeArgument(attribute, "Validation"));
104+
}
105+
106+
protected virtual void Generate()
107+
{
108+
Generator.Generate(attribute, property);
109+
}
110+
}
111+
}

Umbraco.CodeGen.Tests/Generators/CodeGenerationHelper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ public static CodeNamespace CreateNamespaceWithType(CodeTypeDeclaration type)
2222

2323
public static StringBuilder GenerateCode(CodeNamespace ns)
2424
{
25-
var compileUnit = new CodeCompileUnit();
2625
var codeProvider = new CSharpCodeProvider();
2726
var builder = new StringBuilder();
2827
var writer = new StringWriter(builder);
2928

30-
compileUnit.Namespaces.Add(ns);
31-
codeProvider.GenerateCodeFromCompileUnit(compileUnit, writer, new CodeGeneratorOptions());
29+
codeProvider.GenerateCodeFromNamespace(ns, writer, new CodeGeneratorOptions());
3230
writer.Flush();
3331
return builder;
3432
}

Umbraco.CodeGen.Tests/Generators/CtorGeneratorTests.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@ public void Generate_AddsConstructorWithBaseCall_PassingIPublishedContentParamet
2020
var code = CodeGenerationHelper.GenerateCode(ns);
2121

2222
Assert.AreEqual(
23-
@"//------------------------------------------------------------------------------
24-
// <auto-generated>
25-
// This code was generated by a tool.
26-
// Runtime Version:4.0.30319.18408
27-
//
28-
// Changes to this file may cause incorrect behavior and will be lost if
29-
// the code is regenerated.
30-
// </auto-generated>
31-
//------------------------------------------------------------------------------
32-
33-
namespace ANamespace {
23+
@"namespace ANamespace {
3424
3525
3626
public class AName : ABaseType {

Umbraco.CodeGen.Tests/TestFiles/SomeDocumentType.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
//------------------------------------------------------------------------------
2-
// <auto-generated>
3-
// This code was generated by a tool.
4-
// Runtime Version:4.0.30319.18408
5-
//
6-
// Changes to this file may cause incorrect behavior and will be lost if
7-
// the code is regenerated.
8-
// </auto-generated>
9-
//------------------------------------------------------------------------------
10-
11-
namespace Umbraco.CodeGen.Models
1+
namespace Umbraco.CodeGen.Models
122
{
133
using System;
144
using System.ComponentModel;

Umbraco.CodeGen.Tests/TestFiles/SomeMediaType.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
//------------------------------------------------------------------------------
2-
// <auto-generated>
3-
// This code was generated by a tool.
4-
// Runtime Version:4.0.30319.18408
5-
//
6-
// Changes to this file may cause incorrect behavior and will be lost if
7-
// the code is regenerated.
8-
// </auto-generated>
9-
//------------------------------------------------------------------------------
10-
11-
namespace Umbraco.CodeGen.Models
1+
namespace Umbraco.CodeGen.Models
122
{
133
using System;
144
using System.ComponentModel;

Umbraco.CodeGen.Tests/Umbraco.CodeGen.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Compile Include="ContentTypeSerializerTests.cs" />
8686
<Compile Include="Generators\Annotated\AnnotationCodeGeneratorTestBase.cs" />
8787
<Compile Include="Generators\Annotated\CommonInfoGeneratorTests.cs" />
88+
<Compile Include="Generators\Annotated\PropertyInfoCodeGeneratorTests.cs" />
8889
<Compile Include="Generators\AttributeGeneratorTests.cs" />
8990
<Compile Include="Generators\Annotated\DocumentTypeInfoGeneratorTests.cs" />
9091
<Compile Include="Generators\Annotated\EntityDescriptionGeneratorTests.cs" />

Umbraco.CodeGen/CodeGenerator.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ public void Generate(ContentType contentType, TextWriter writer)
4343
{
4444
EnsureGenerator();
4545

46-
var compileUnit = new CodeCompileUnit();
47-
generator.Generate(compileUnit, contentType);
48-
CodeProvider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
46+
//var compileUnit = new CodeCompileUnit();
47+
//generator.Generate(compileUnit, contentType);
48+
//CodeProvider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
49+
var ns = new CodeNamespace();
50+
generator.Generate(ns, contentType);
51+
CodeProvider.GenerateCodeFromNamespace(ns, writer, options);
52+
4953
writer.Flush();
5054
}
5155

0 commit comments

Comments
 (0)