Skip to content

Commit 49fc195

Browse files
committed
Closing in with annotations
1 parent 301ab10 commit 49fc195

11 files changed

+298
-17
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using NUnit.Framework;
7+
using Umbraco.CodeGen.Configuration;
8+
using Umbraco.CodeGen.Definitions;
9+
using Umbraco.CodeGen.Generators;
10+
using Umbraco.CodeGen.Generators.Annotated;
11+
using Umbraco.CodeGen.Tests.TestHelpers;
12+
13+
namespace Umbraco.CodeGen.Tests
14+
{
15+
[TestFixture]
16+
public class AnnotationGeneratorAcceptanceTests : CodeGeneratorFactory
17+
{
18+
private Func<ContentTypeConfiguration, IEnumerable<DataTypeDefinition>, CodeGeneratorBase> factory;
19+
20+
[Test]
21+
public void BuildCode_GeneratesCodeForDocumentType()
22+
{
23+
var contentType = TestFactory.CreateExpectedDocumentType();
24+
factory = CreateDocTypeGenerator;
25+
26+
Generate(contentType);
27+
}
28+
29+
[Test]
30+
public void BuildCode_GeneratesCodeForMediaType()
31+
{
32+
var contentType = TestFactory.CreateExpectedMediaType();
33+
factory = CreateMediaTypeGenerator;
34+
contentType.Info.Description = "Oy, need a description to boot!";
35+
36+
Generate(contentType);
37+
}
38+
39+
private void Generate(ContentType contentType)
40+
{
41+
var configuration = new CodeGeneratorConfiguration().DocumentTypes;
42+
var dataTypeProvider = new TestDataTypeProvider();
43+
44+
var generator = new CodeGenerator(configuration, dataTypeProvider, this);
45+
46+
var stringBuilder = new StringBuilder();
47+
var writer = new StringWriter(stringBuilder);
48+
49+
configuration.Namespace = "A.Namespace";
50+
configuration.BaseClass = "ABaseClass";
51+
52+
generator.Generate(contentType, writer);
53+
54+
Console.WriteLine(stringBuilder.ToString());
55+
Assert.Inconclusive("Not finished yet");
56+
}
57+
58+
public override CodeGeneratorBase Create(ContentTypeConfiguration configuration, IEnumerable<DataTypeDefinition> dataTypes)
59+
{
60+
return factory(configuration, dataTypes);
61+
}
62+
63+
public CodeGeneratorBase CreateDocTypeGenerator(ContentTypeConfiguration configuration, IEnumerable<DataTypeDefinition> dataTypes)
64+
{
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+
)
96+
);
97+
}
98+
public CodeGeneratorBase CreateMediaTypeGenerator(ContentTypeConfiguration configuration, IEnumerable<DataTypeDefinition> dataTypes)
99+
{
100+
return new NamespaceGenerator(
101+
configuration,
102+
new ImportsGenerator(configuration),
103+
new ClassGenerator(configuration,
104+
new CompositeCodeGenerator(
105+
configuration,
106+
new EntityNameGenerator(configuration),
107+
new AttributeCodeGenerator(
108+
"MediaType",
109+
configuration,
110+
new EntityDescriptionGenerator(configuration),
111+
new CommonInfoGenerator(configuration)
112+
)
113+
),
114+
new CtorGenerator(configuration),
115+
new PropertiesGenerator(
116+
configuration,
117+
new PropertyDeclarationGenerator(
118+
configuration,
119+
dataTypes.ToList(),
120+
new EntityNameGenerator(configuration),
121+
new AttributeCodeGenerator(
122+
"GenericProperty",
123+
configuration,
124+
new EntityDescriptionGenerator(configuration)
125+
),
126+
new PropertyBodyGenerator(configuration)
127+
)
128+
)
129+
)
130+
);
131+
}
132+
}
133+
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ namespace Umbraco.CodeGen.Tests.Generators.Annotated
1313
public class CommonInfoGeneratorTests : AnnotationCodeGeneratorTestBase
1414
{
1515
private Info info;
16-
private ContentType contentType;
1716
private CodeTypeDeclaration type;
1817
private CodeAttributeDeclaration attribute;
1918

2019
[SetUp]
2120
public void SetUp()
2221
{
2322
Configuration = new CodeGeneratorConfiguration().MediaTypes;
24-
contentType = new MediaType { Info = { Alias = "anEntity" } };
2523
Candidate = type = new CodeTypeDeclaration();
26-
info = contentType.Info;
24+
info = new Info {Alias = "anEntity"};
2725

2826
attribute = new CodeAttributeDeclaration("MediaType");
2927
type.CustomAttributes.Add(attribute);
@@ -35,7 +33,7 @@ public void SetUp()
3533
[ExpectedException(typeof(Exception), ExpectedMessage = "Common info generator must be used on an attribute declaration")]
3634
public void Generate_NotCodeTypeDeclaration_Throws()
3735
{
38-
Generator.Generate(new CodeMemberProperty(), contentType);;
36+
Generator.Generate(new CodeMemberProperty(), info);;
3937
}
4038

4139
[Test]
@@ -96,7 +94,7 @@ public void Generate_AllowAtRoot_False_OmitsAllowAtRootArgument()
9694

9795
private void Generate()
9896
{
99-
Generator.Generate(attribute, contentType);
97+
Generator.Generate(attribute, info);
10098
}
10199
}
102100
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class DocumentTypeInfoGeneratorTests : AnnotationCodeGeneratorTestBase
1313
{
1414
private DocumentTypeInfoGenerator generator;
1515
private DocumentTypeInfo info;
16-
private DocumentType documentType;
1716
private CodeAttributeDeclaration attribute;
1817

1918
[SetUp]
@@ -22,8 +21,7 @@ public void SetUp()
2221
Configuration = new CodeGeneratorConfiguration().MediaTypes;
2322
attribute = new CodeAttributeDeclaration("DocumentType");
2423
generator = new DocumentTypeInfoGenerator(Configuration);
25-
documentType = new DocumentType { Info = { Alias = "aClass" } };
26-
info = (DocumentTypeInfo)documentType.Info;
24+
info = new DocumentTypeInfo{ Alias = "aClass" };
2725
}
2826

2927
[Test]
@@ -92,7 +90,7 @@ public void Generate_AllowedTemplates_AllNullOrEmptyItems_OmitsArgument()
9290

9391
private void Generate()
9492
{
95-
generator.Generate(attribute, documentType);
93+
generator.Generate(attribute, info);
9694
}
9795
}
9896
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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;
7+
8+
namespace Umbraco.CodeGen.Tests.Generators
9+
{
10+
[TestFixture]
11+
public class AttributeGeneratorTests : TypeCodeGeneratorTestBase
12+
{
13+
private SpyGenerator spy1;
14+
private SpyGenerator spy2;
15+
16+
[SetUp]
17+
public void SetUp()
18+
{
19+
Configuration = new CodeGeneratorConfiguration().DocumentTypes;
20+
spy1 = new SpyGenerator();
21+
spy2 = new SpyGenerator();
22+
}
23+
24+
[Test]
25+
public void Generate_DocumentType_OnType_AddsAndPassesAttributeToChildGenerators()
26+
{
27+
SetupDocumentType();
28+
Generate_AddsAndPassesAttributeToChildren("DocumentType", GenerateContentType);
29+
}
30+
31+
[Test]
32+
public void Generate_MediaType_OnType_AddsAndPassesAttributeToChildGenerators()
33+
{
34+
SetupDocumentType();
35+
Generate_AddsAndPassesAttributeToChildren("MediaType", GenerateContentType);
36+
}
37+
38+
[Test]
39+
public void Generate_GenericProperty_OnProperty_AddsAndPassesAttributeToChildGenerators()
40+
{
41+
SetupDocumentType();
42+
Generate_AddsAndPassesAttributeToChildren("GenericProperty", GenerateProperty);
43+
}
44+
45+
private void Generate_AddsAndPassesAttributeToChildren(string attributeName, Action generateDelegate)
46+
{
47+
Generator = new AttributeCodeGenerator(attributeName, Configuration, spy1, spy2);
48+
generateDelegate();
49+
var attribute = FindAttribute(attributeName);
50+
Assert.AreSame(attribute, spy1.CodeObjects[0]);
51+
Assert.AreSame(attribute, spy2.CodeObjects[0]);
52+
}
53+
54+
private void SetupDocumentType()
55+
{
56+
ContentType = new DocumentType();
57+
Generator = new AttributeCodeGenerator("DocumentType", Configuration);
58+
Candidate = new CodeTypeDeclaration();
59+
}
60+
61+
private void SetupMediaType()
62+
{
63+
ContentType = new MediaType();
64+
Generator = new AttributeCodeGenerator("MediaType", Configuration);
65+
Candidate = new CodeTypeDeclaration();
66+
}
67+
68+
private void SetupProperty()
69+
{
70+
ContentType = new MediaType();
71+
Generator = new AttributeCodeGenerator("GenericProperty", Configuration);
72+
Candidate = new CodeMemberProperty();
73+
}
74+
75+
private void GenerateContentType()
76+
{
77+
Generator.Generate(Candidate, ContentType);
78+
}
79+
80+
private void GenerateProperty()
81+
{
82+
Generator.Generate(Candidate, new GenericProperty());
83+
}
84+
}
85+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
</Reference>
7777
</ItemGroup>
7878
<ItemGroup>
79+
<Compile Include="AnnotationGeneratorAcceptanceTests.cs" />
7980
<Compile Include="CodeParserTests.cs" />
8081
<Compile Include="Configuration\CodeGeneratorConfigurationTests.cs" />
8182
<Compile Include="Configuration\FileCodeGeneratorConfigurationProviderTests.cs" />
@@ -84,6 +85,7 @@
8485
<Compile Include="ContentTypeSerializerTests.cs" />
8586
<Compile Include="Generators\Annotated\AnnotationCodeGeneratorTestBase.cs" />
8687
<Compile Include="Generators\Annotated\CommonInfoGeneratorTests.cs" />
88+
<Compile Include="Generators\AttributeGeneratorTests.cs" />
8789
<Compile Include="Generators\Annotated\DocumentTypeInfoGeneratorTests.cs" />
8890
<Compile Include="Generators\Annotated\EntityDescriptionGeneratorTests.cs" />
8991
<Compile Include="Generators\ClassGeneratorTests.cs" />

Umbraco.CodeGen/Generators/Annotated/CommonInfoGenerator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public override void Generate(object codeObject, Entity entity)
1919
if (attribute == null)
2020
throw new Exception("Common info generator must be used on an attribute declaration");
2121

22-
var contentType = (ContentType)entity;
23-
var info = contentType.Info;
22+
var info = (Info)entity;
2423

2524
AddAttributeArgumentIfValue(attribute, "Icon", info.Icon);
2625
AddAttributeArgumentIfValue(attribute, "Thumbnail", info.Thumbnail);

Umbraco.CodeGen/Generators/Annotated/DocumentTypeInfoGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public override void Generate(object codeObject, Entity entity)
2020
if (attribute == null)
2121
throw new Exception("Common info generator must be used on an attribute declaration");
2222

23-
var contentType = (ContentType)entity;
24-
var info = (DocumentTypeInfo)contentType.Info;
23+
//var contentType = (ContentType)entity;
24+
var info = (DocumentTypeInfo)entity;
2525

2626
AddAttributeArgumentIfValue(attribute, "DefaultTemplate", info.DefaultTemplate);
2727

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.CodeDom;
2+
using Umbraco.CodeGen.Configuration;
3+
using Umbraco.CodeGen.Definitions;
4+
5+
namespace Umbraco.CodeGen.Generators
6+
{
7+
public class AttributeCodeGenerator : CodeGeneratorBase
8+
{
9+
private readonly string attributeName;
10+
private readonly CodeGeneratorBase[] memberGenerators;
11+
12+
public AttributeCodeGenerator(
13+
string attributeName,
14+
ContentTypeConfiguration config,
15+
params CodeGeneratorBase[] memberGenerators
16+
) : base(config)
17+
{
18+
this.attributeName = attributeName;
19+
this.memberGenerators = memberGenerators;
20+
}
21+
22+
public override void Generate(object codeObject, Entity entity)
23+
{
24+
var type = (CodeTypeMember)codeObject;
25+
26+
var attribute = AddAttribute(type, attributeName);
27+
28+
if (memberGenerators != null)
29+
foreach(var generator in memberGenerators)
30+
generator.Generate(attribute, entity);
31+
}
32+
}
33+
}

Umbraco.CodeGen/Generators/CodeGeneratorBase.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ protected static void AddAttribute(CodeTypeMember type, string attributeName, st
2424
AddAttribute(type, attribute);
2525
}
2626

27-
protected void AddAttribute(CodeMemberProperty propNode, string attributeName)
27+
protected CodeAttributeDeclaration AddAttribute(CodeTypeMember codeObject, string attributeName)
2828
{
29-
AddAttribute(propNode, CreateAttribute(attributeName));
29+
var attribute = CreateAttribute(attributeName);
30+
AddAttribute(codeObject, attribute);
31+
return attribute;
3032
}
3133

32-
private static void AddAttribute(CodeTypeMember type, CodeAttributeDeclaration attribute)
34+
protected static void AddAttribute(CodeTypeMember codeObject, CodeAttributeDeclaration attribute)
3335
{
34-
type.CustomAttributes.Add(attribute);
36+
codeObject.CustomAttributes.Add(attribute);
3537
}
3638

3739
private static CodeAttributeDeclaration CreatePrimitiveAttribute<T>(string attributeName, T value)

0 commit comments

Comments
 (0)